【问题标题】:-finstrument-functions doesn't work with dynamically loaded g++ shared objects (.so)-finstrument-functions 不适用于动态加载的 g++ 共享对象 (.so)
【发布时间】:2012-04-09 10:53:39
【问题描述】:

这些天我在 Ubuntu 上使用 g++ 共享对象 (.so) 文件测试 -finstrument-functions。我发现了一个奇怪的行为,即 -finstrument-functions 似乎只有在库是静态链接的情况下才有效。如果我使用 dlopen/dlsym 等链接到库,代码的功能仍然有效,但不会调用 __cyg_profile* 函数。

这里有一些代码可以快速重现问题:

MyLib.h

#ifndef __MYLIB_H__
#define __MYLIB_H__
class MyLib
{
public:
    void sayHello();
};
#endif

MyLib.cpp

#include "MyLib.h"
#include <iostream>
using namespace std;

void MyLib::sayHello()
{
    cout<<"Hello"<<endl;
}

MyLibStub.cpp(.so 的 C 接口)

#include "MyLib.h"

extern "C" void LoadMyLib ()
{
    MyLib().sayHello();
}

Trace.cpp

#include <stdio.h>
#ifdef __cplusplus
extern "C"
{
    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

void __cyg_profile_func_enter(void* this_fn, void* call_site)
{
    printf("entering %p\n", (int*)this_fn);
}

void __cyg_profile_func_exit(void* this_fn, void* call_site)
{
    printf("exiting %p\n", (int*)this_fn);
}

MainStatic.cpp

#include <iostream>
using namespace std;

extern "C" void LoadMyLib ();

int main()
{
    LoadMyLib();
    return 0;
}

MainDynamic.cpp

#include <iostream>
#include <dlfcn.h>

const char* pszLibName  = "libMyLib.so.0.0";
const char* pszFuncName  = "LoadMyLib";

int main()
{
    void* pLibHandle = dlopen(pszLibName, RTLD_NOW);
    if(!pLibHandle) {
        return 1;
    }
    void (*pFuncLoad)() = 0;
    //Resolve the function in MyLibStub.cpp
    pFuncLoad = (void (*)())dlsym(pLibHandle, pszFuncName);
    if(!pFuncLoad) {
        return 1;
    }
    pFuncLoad();
    dlclose(pLibHandle);
    return 0;
}

并使用以下命令编译(在 Ubuntu 11.10 下):

g++ -g -finstrument-functions -Wall -Wl,-soname,libMyLib.so.0 -shared -fPIC -rdynamic MyLib.cpp MyLibStub.cpp Trace.cpp -o libMyLib.so.0.0  
ln -s libMyLib.so.0.0 libMyLib.so.0  
ln -s libMyLib.so.0.0 libMyLib.so  
g++ MainStatic.cpp -g -Wall -lMyLib -L./ -o MainStatic   
g++ MainDynamic.cpp -g -Wall -ldl -o MainDynamic

当使用./MainStatic 调用时

它给出了类似的东西:

entering 0xb777693f
entering 0xb777689b
exiting 0xb777689b
exiting 0xb777693f
entering 0xb7776998
entering 0xb777680c
Hello
exiting 0xb777680c
exiting 0xb7776998

但是,当使用./MainDynamic 调用时

它只给出一个“你好”。

Hello

这里有人知道为什么静态链接库和动态链接库之间存在这种差异吗?即使在动态加载的情况下,是否有任何解决方案可以使其工作?提前致谢。

【问题讨论】:

标签: c++ linux g++ profiling shared-libraries


【解决方案1】:

你可以

dlopen(pszLibName, RTLD_NOW | RTLD_DEEPBIND);

那么库中定义的符号将优先于全局符号

【讨论】:

    【解决方案2】:

    这种行为是意料之中的。

    为了理解它,您首先需要知道动态加载器使用链表搜索符号,按照加载不同ELF 图像的顺序。该列表的顶部是主要可执行文件本身,然后是直接链接到它的所有库。当你 dlopen() 某个库时,它会被附加到列表的 tail 中。

    因此,当您刚刚加载的库中的代码调用__cyg_profile_func_enter 时,加载程序会在列表中搜索该函数的第一个定义。第一个定义恰好是由 libc.so.6 提供的默认定义,它位于列表末尾附近,但在您的dlopen()ed 库之前

    你可以通过运行观察所有这些:

    LD_DEBUG=symbols,bindings ./MainDynamic
    

    并在输出中寻找__cyg_profile_func_enter

    那么,您需要做什么才能看到您的仪器?您必须libc.so.6 的某个位置之前获得自己的__cyg_profile_func_enter。一种方法是将其链接到您的主可执行文件中。或者将其链接到一个共享库,该库直接链接到您的可执行文件(即不是dlopen()d one)。

    一旦你这样做了,你的实现将是列表中的第一个,它将胜过libc.so.6 中的那个,你会看到它生成的输出。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2018-12-17
    • 2011-09-04
    相关资源
    最近更新 更多