相关疑难解决方法(0)

如何分析在Linux上运行的C++代码?

我有一个在Linux上运行的C++应用程序,我正在优化它.如何确定代码的哪些区域运行缓慢?

c++ unix profiling

1732
推荐指数
12
解决办法
49万
查看次数

如何在每个函数的入口处添加代码?

我想在每个函数调用之前添加一些代码来进行一些检查.我知道的唯一方法是:

#define SOME_CODE printf("doing something...");

class testObject
{
void function1()
{
 SOME_CODE
 ...
}
void function2()
{
 SOME_CODE
 ...
}
}
Run Code Online (Sandbox Code Playgroud)

有没有更清洁的方法来实现这一目标?我正在寻找一种方法,所以我没有手动为每个函数添加"SOME_CODE".

c++

12
推荐指数
4
解决办法
4758
查看次数

-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++ ×3

profiling ×2

g++ ×1

linux ×1

shared-libraries ×1

unix ×1