问题描述
我有一个需要调查的第 3 方源代码.我想看看函数的调用顺序,但我不想浪费时间打字:
I have a 3rd party source code that I have to investigate. I want to see in what order the functions are called but I don't want to waste my time typing:
printf("Entered into %s", __FUNCTION__)
和
printf("Exited from %s", __FUNCTION__)
对于每个函数,我也不想接触任何源文件.
for each function, nor do I want to touch any source file.
你有什么建议吗?是否有编译器标志可以自动为我执行此操作?
Do you have any suggestions? Is there a compiler flag that automagically does this for me?
对评论的澄清:
- 我将交叉编译源代码以在 ARM 上运行它.
- 我会用 gcc 编译它.
- 我不想分析静态代码.我想跟踪运行时.所以 doxygen 不会让我的生活更轻松.
- 我有源代码,我可以编译它.
- 我不想使用面向方面的编程.
我发现 gdb 提示符中的 'frame' 命令会在那个时间点打印当前帧(或者,您可以说是函数名称).也许,每次调用函数时都可以(使用 gdb 脚本)调用frame"命令.你怎么看?
I found that 'frame' command in the gdb prompt prints the current frame (or, function name, you could say) at that point in time. Perhaps, it is possible (using gdb scripts) to call 'frame' command everytime a function is called. What do you think?
推荐答案
除了通常的调试器和面向方面的编程技术之外,您还可以使用 gcc 的 -finstrument-functions
命令行选项.您必须实现自己的 __cyg_profile_func_enter()
和 __cyg_profile_func_exit()
函数(在 C++ 中将它们声明为 extern "C"
).
Besides the usual debugger and aspect-oriented programming techniques, you can also inject your own instrumentation functions using gcc's -finstrument-functions
command line options. You'll have to implement your own __cyg_profile_func_enter()
and __cyg_profile_func_exit()
functions (declare these as extern "C"
in C++).
它们提供了一种方法来跟踪从何处调用了什么函数.但是,该接口有点难以使用,因为例如传递的是被调用函数的地址及其调用点,而不是函数名称.您可以记录地址,然后使用类似 objdump 的东西从符号表中提取相应的名称--syms
或 nm
,当然前提是这些符号没有从相关二进制文件中删除.
They provide a means to track what function was called from where. However, the interface is a bit difficult to use since the address of the function being called and its call site are passed instead of a function name, for example. You could log the addresses, and then pull the corresponding names from the symbol table using something like objdump --syms
or nm
, assuming of course the symbols haven't been stripped from the binaries in question.
使用 gdb
可能更容易.天啊.:)
It may just be easier to use gdb
. YMMV. :)
这篇关于自动将进入/退出功能日志添加到项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!