本文介绍了 - 仪器函数不适用于动态加载的g ++共享对象(.so)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我现在在Ubuntu上使用g ++共享对象(.so)文件来测试仪表功能。我发现一个奇怪的行为,仪器功能似乎只有当图书馆是静态链接工作。如果我使用dlopen / dlsym等链接到库,代码的功能仍然有效,但它不会调用__cyg_profile *函数。 快速重现问题的代码: MyLib.h 在 。一种方法是将其链接到主可执行文件。或将其链接到直接链接到您的可执行文件的共享库(即 dlopen() 。 一旦你这样做,你的实现将是列表中的第一个,它将赢得 libc.so中的一个。 6 ,您将看到它生成的输出。 I am testing -finstrument-functions with g++ shared object (.so) files on Ubuntu these days. I found a strange behavior that -finstrument-functions seems to work only if the library is statically linked. If I link to the library with dlopen/dlsym etc., the functionality of the code still works, but it won't call the __cyg_profile* functions.Here are some codes to quickly reproduce the problem:MyLib.h#ifndef __MYLIB_H__#define __MYLIB_H__class MyLib{public: void sayHello();};#endifMyLib.cpp#include "MyLib.h"#include <iostream>using namespace std;void MyLib::sayHello(){ cout<<"Hello"<<endl;}MyLibStub.cpp (C interface to the .so)#include "MyLib.h"extern "C" void LoadMyLib (){ MyLib().sayHello();}Trace.cpp#include <stdio.h>#ifdef __cplusplusextern "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));}#endifvoid __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;}and compile with the following commands (under Ubuntu 11.10):when called with ./MainStaticit gives something like:however, when called with ./MainDynamicit only gives a "Hello".Does anybody here know why there is such difference between statically and dynamically linked libraries? Is there any solution to make it work even when dynamically loaded? Thanks in advance. 解决方案 This behavior is expected.In order to understand it, you first need to know that the dynamic loader searches for symbols using a linked list, in the order that different ELF images were loaded. At the head of that list is the main executable itself, followed by all libraries directly linked to it. When you dlopen() some library, it gets appended to the tail of the list.So when the code in the library you just loaded calls __cyg_profile_func_enter, the loader searches the list for the first definition of that function. That first definition happens to be the default one, provided by libc.so.6, which is near the end of the list, but is before your dlopen()ed library.You can observe all of this by running:LD_DEBUG=symbols,bindings ./MainDynamicand looking for __cyg_profile_func_enter in the output.So, what do you have to do in order to see your instrumentation? You have to get your own __cyg_profile_func_enter somewhere before the one from libc.so.6. One way to do that is to link it into your main executable. Or link it into a shared library that is directly linked to your executable (i.e. not dlopen()d one).Once you do that, your implementation will be the first one on the list, and it will win over the one in libc.so.6, and you will see the output it generates. 这篇关于 - 仪器函数不适用于动态加载的g ++共享对象(.so)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 06:05