相关疑难解决方法(0)

-finstrument-functions不适用于动态加载的g ++共享对象(.so)

这些天我在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
Run Code Online (Sandbox Code Playgroud)

MyLib.cpp

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

void MyLib::sayHello()
{
    cout<<"Hello"<<endl;
}
Run Code Online (Sandbox Code Playgroud)

MyLibStub.cpp(.so的C接口)

#include "MyLib.h"

extern "C" void LoadMyLib ()
{
    MyLib().sayHello();
}
Run Code Online (Sandbox Code Playgroud)

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)
{ …
Run Code Online (Sandbox Code Playgroud)

c++ linux profiling g++ shared-libraries

7
推荐指数
1
解决办法
2048
查看次数

标签 统计

c++ ×1

g++ ×1

linux ×1

profiling ×1

shared-libraries ×1