自动将进入/退出功能日志添加到项目中

我有一个必须调查的第 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 提示符中的帧"命令会在该时间点打印当前帧(或者,你可以说是函数名).也许,每次调用函数时都可以(使用 gdb 脚本)调用框架"命令.你怎么看?

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 从符号表中提取相应的名称--symsnm,当然假设没有从相关二进制文件中删除符号.

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 可能更容易.YMMV.:)

It may just be easier to use gdb. YMMV. :)

相关文章