我希望使用valgrind获得C++程序调用的函数的时间顺序日志,最好是在文本文件中。
对于下面的示例C++程序(simple.cpp):
void baz(){
}
void bar(){
for(int i = 0; i < 3; i++)
baz();
}
void foo(){
bar();
}
int main(){
foo();
return 0;
}我希望获得main() -> foo()-> bar->baz()*3
我尝试过的:
编译为g++ -g simple.cpp -o simple.out并运行valgrind --tool=callgrind ./simple.out以获取callgrind.out.3519
运行callgrind_annotate --tree=both callgrind.out.3519 | grep baz不会返回任何内容。
输入kcachegrind callgrind.out.3519,然后在函数main()的源代码视图中导航,就可以按时间顺序查看调用。
有没有办法将这些信息写到日志中?
发布于 2013-06-05 01:19:19
看起来callgrind_annotate就是要用callgrind生成的数据来做这件事。给出这个简单的例子,它没有显示"baz“的原因很简单,因为您的测试代码执行得太快了,与开销代码(例如,动态库加载代码)中花费的时间相比,在其中花费的执行时间就相形见绌了。
您可以通过使用threshold参数让callgrind_annotate包含您的baz:
callgrind_annotate --threshold=100 --tree=both callgrind.out.3519 | grep baz或者通过更改示例:
int main(){
for(int i=0;i<1000000;i++9 {
foo();
}
return 0;
}发布于 2013-06-04 15:06:14
只需在每个函数中编写一个阻塞函数名“<<”;。会毁了你的性能,但这就是日志的作用。
如果你想跟踪调用,我宁愿推荐注释和构建doxygen doku,只要没有函数指针,它就可以生成调用者图。
如果这不适合您,我们应该知道您需要日志记录的确切目的。
https://stackoverflow.com/questions/16910631
复制相似问题