有没有办法找出C函数的顶级调用者?

veh*_*zzz 5 c c++ optimization code-analysis

假设我有来自许多不同地方的称为LOT的功能.所以我想找出谁最能称这个功能.例如,前5个呼叫者或曾经调用此功能超过N次.

我使用的是AS3 Linux,gcc 3.4.

现在我只是放了一个断点,然后每隔300次停在那里,这样就强行了......

有谁知道可以帮助我的工具?

谢谢

dan*_*dam 18

使用-pg选项编译,运行程序一段时间,然后使用gprof.运行使用-pg选项编译的程序将生成带有执行配置文件的gmon.out文件.gprof可以读取此文件并以可读的形式呈现.

  • gprof2dot(http://code.google.com/p/jrfonseca/wiki/Gprof2Dot)可以将输出变成一个漂亮的图形;-) (3认同)

sam*_*wry 2

我写通话记录示例只是为了好玩。宏将函数调用更改为已检测的函数调用。

include <stdio.h>. 

int funcA( int a, int b ){ return a+b; }

// instrumentation

void call_log(const char*file,const char*function,const int line,const char*args){
  printf("file:%s line: %i function: %s args: %s\n",file,line,function,args);
}

#define funcA(...) \ 
  (call_log(__FILE__, __FUNCTION__, __LINE__, "" #__VA_ARGS__), funcA(__VA_ARGS__)). 

// testing

void funcB(void){
  funcA(7,8);
}


int main(void){
  int x = funcA(1,2)+

          funcA(3,4);

  printf( "x: %i (==10)\n", x );

  funcA(5,6);

  funcB();
}
Run Code Online (Sandbox Code Playgroud)

输出:

file:main.c line: 22 function: main args: 1,2
file:main.c line: 24 function: main args: 3,4
x: 10 (==10)
file:main.c line: 28 function: main args: 5,6
file:main.c line: 17 function: funcB args: 7,8
Run Code Online (Sandbox Code Playgroud)