How to compile a cpp and then link it to a shared library

北战南征 提交于 2019-11-29 17:34:48
user4786271

As @Flexo restates what @EmployedRussian said in the linked question, the main point is to get your implementation of __cyg_profile_func_*** before the one provided by libc.so.6.

One method to do this, is to use the LD_PRELOAD environment variable. Here you can read what LD_PRELOAD does and how it works.

To use the LD_PRELOAD trick you will need to compile your implementation of the above-mentioned functions as a shared library.

You can do this by doing:

g++ -shared -fPIC myImp.cc -o myImp.so -ldl

Once you get the .so file, navigate to the directory where your executable is located and do:

LD_PRELOAD=<path/to/myImp.so>- ./<myExecutable>

For shared libraries, dynamic linking is used. Meaning:

resolving of some undefined symbols (is postponed) until a program is run.

By using LD_PRELOAD you resolve the symbols of your interest before letting the linked do that.


Here you have an implementation of myImp.cc, which I took from: https://groups.google.com/forum/#!topic/gnu.gcc.help/a-hvguqe10I

The current version lacks proper implementation for __cyg_profile_func_exit, and I have not been able to demangle the function names.

#ifdef __cplusplus
extern "C"
{

        #include <stdio.h>
        #include <stdlib.h>
        #include <sys/types.h>
        #include <sys/stat.h>
        #include <unistd.h>
    #include <dlfcn.h>


        void __cyg_profile_func_enter(void *this_fn, void *call_site)__attribute__((no_instrument_function));
        void __cyg_profile_func_exit(void *this_fn, void *call_site)__attribute__((no_instrument_function));

}

#endif
static FILE *fp;
int call_level=0;
void * last_fn;
void __cyg_profile_func_enter(void *this_fn, void *call_site)
{

    Dl_info di; 

        if (fp == NULL) fp = fopen( "trace.txt", "w" );
        if (fp == NULL) exit(-1);

        if ( this_fn!=last_fn) ++call_level;
        for (int i=0;i<=call_level;i++) 
    {
        fprintf(fp,"\t");
    }
        //fprintf(fp,  "entering %p\n", (int *)this_fn);
        fprintf(fp,  "entering %p", (int *)this_fn);
        if (dladdr(this_fn, &di)) {
          fprintf(fp,  " %s (%s)", di.dli_sname ? di.dli_sname : "<unknown>", di.dli_fname);
        }
        fputs("\n", fp);
        (void)call_site;
        last_fn = this_fn;
}

void __cyg_profile_func_exit(void *this_fn, void *call_site)
{
        --call_level;
        for (int i=0;i<=call_level;i++) fprintf(fp,"\t");
        fprintf(fp, "exiting %p\n", (int *)this_fn);
        (void)call_site;

}

Another option for function tracing which uses LD_PRELOAD is used by LTTng, in the section Function Tracing, but I have never used it...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!