问题描述
我正在寻找一种聪明的方法来跟踪函数调用和返回.我知道我可以使用调试器,但我想要一种方法,让它在调用函数而不是单步执行代码时将某些内容打印到终端.
我在想我也许可以使用预处理器,但我不确定最好的方法是什么.
或者有没有办法使用 gdb 打印出有用的信息,而不必单步执行代码.
I am looking for a clever way to track function calls and returns.
I know I can use the debugger, but I would like a way to just have it print something out to the terminal when calling a function vs having to step through code.
I am thinking that I might be able to use the preprocessor, but I am not sure what would be the best way to go about this.
Or is there a way to use gdb to print out the information that would be useful, while not having to step through the code.
推荐答案
大多数编译器允许您在函数调用之前和之后注入检测函数.
Most compilers allow you to inject an instrumentation function before and after the function call.
在 MSVC 中,它们是 _penter
和 _pexit
.一篇不错的文章:http://www.drdobbs.com/184403601.
In MSVC they are _penter
and _pexit
. A nice article: http://www.drdobbs.com/184403601.
在 GCC 中,您将使用 -finstrument-functions
选项,请参阅 文档.
In GCC you would use the -finstrument-functions
option, see the docs.
您可以使用调试库或映射文件来获取更多信息.
You can use debug libaries or map files to get more info.
这篇关于C/C++ 需要一种聪明的方法来跟踪函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!