为 C/C++ 编写检测探查器的最简单方法是什么?
What's the easiest way to write an instrumenting profiler for C/C++?
我见过一些像Pin和DynInt这样的工具,它们可以进行动态代码操作,以便在不需要重新编译的情况下插入代码。这些似乎是一个简单问题的重量级解决方案:从程序中检索准确的函数调用数据。
我想写一些东西,这样在我的代码中,我可以写
void SomeFunction() {
StartProfiler();
...
StopProfiler();
}
执行后,检索关于在StartProfiler()
和StopProfiler()
(整个调用树)之间调用了什么函数以及每个函数花费了多长时间的数据。
最好我也能读出调试符号,以获得函数名而不是地址。
这里有一个关于我发现的解决方案的有趣提示。
gcc(和llvm>=3.0)在编译时有一个-pg
选项,这在传统上是为了支持gprof。使用此标志编译代码时,编译器会在每个函数定义的开头添加对函数mcount
的调用。您可以重写此函数,但需要在程序集中执行,否则您定义的mcount
函数将被插入对mcount
的调用,并且在main
被调用之前,您将很快耗尽堆栈空间。
这里有一个概念的小证明:
foo.c:
int total_calls = 0;
void foo(int c) {
if (c > 0)
foo(c-1);
}
int main() {
foo(4);
printf("%dn", total_calls);
}
foo.s:
.globl mcount
mcount:
movl _total_calls(%rip), %eax
addl $1, %eax
movl %eax, _total_calls(%rip)
ret
用CCD_ 8编译。结果:
$ ./foo
6
main
为1,foo
为4,printf
为1。
以下是clang为foo
:发出的asm
_foo:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl %edi, -8(%rbp) ## 4-byte Spill
callq mcount
movl -8(%rbp), %edi ## 4-byte Reload
...
相关文章:
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 在OSX上使用CMake将Adobe的XMP工具包构建为共享库的最简单方法是什么?
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
- 将时间戳打印到流的最简单方法
- 在C++中创建文件夹选取器对话框的最简单方法是什么?
- boost::variant - 对变体应用算术的最简单方法
- 创建可以遍历 std::map 值的通用模板迭代器的最简单方法是什么?
- 在C++中将算术类型转换为 std::array 的最简单方法<uint8_t>
- C++-将ASCII字符数组中可能的数值转换为字符的最简单方法
- 从 txt 文件中读取数据的最简单方法
- 根据浮点符号对浮点进行舍入的最简单方法是什么
- 对于我扩展此程序来计算最高10x10矩阵的倒数的最简单方法是什么
- C++98 中获取并继续调用当前类不知道的类方法的最简单方法是什么?
- 编写要在 Python 中使用的并行 C/C++ 模块的最简单方法
- 开放式颜色多边形表面的最简单方法
- 确定代码中当前程序集是否为 32/64 位的最简单方法
- 将unique_ptr存储在堆上的最简单方法是什么
- 使用 C# 获取 OpenGL 版本的最简单方法
- 在自定义 C/C++ 程序中获取 PPP0 接口 Tx/Rx 字节的最简单方法是什么?
- 为depth_first_search定义颜色图的最简单方法