自动将输入/退出功能日志添加到项目 [英] Automatically adding Enter/Exit Function Logs to a Project

查看:148
本文介绍了自动将输入/退出功能日志添加到项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第三方源代码,我必须调查。我想看看功能的调用顺序,但是我不想浪费我的时间输入:

  printf(输入%s,__FUNCTION__)

  printf(从%s退出,__FUNCTION__)

为每个功能,也不想触摸任何源文件。



你有什么建议吗?有没有一个自动为我做的编译器标志?



澄清意见:




  • 我将交叉编译源代码以在ARM上运行。

  • 我将使用gcc编译它。

  • 我不想分析静态代码。我想跟踪运行时。所以doxygen不会使我的生活更轻松。

  • 我有源代码,可以编译它。

  • 我不想使用面向方面编程。



编辑:
我发现gdb提示符中的'frame'命令打印当前帧名字,你可以说)在那个时间点。也许,每当调用一个函数时,都可以(使用gdb脚本)调用frame命令。你认为如何?

解决方案

除了通常的调试器和面向方面的编程技术,您还可以使用gcc的 - 仪器功能 命令行选项。您必须实现自己的 __ cyg_profile_func_enter() __ cyg_profile_func_exit()函数(声明为 externC在C ++中)



它们提供了一种方法来跟踪从哪里调用哪个函数。但是,由于被调用的函数的地址和其调用站点被传递而不是函数名称,因此接口有点难以使用。您可以记录地址,然后从符号表中使用类似 objdump --syms nm ,假设符号没有被从相关的二进制文件中删除。



它可能更容易使用 gdb 。 YMMV。 :)


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__)

and

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?

Clarifications to the comments:

  • I will cross-compile the source to run it on ARM.
  • I will compile it with gcc.
  • I don't want to analyze the static code. I want to trace the runtime. So doxygen will not make my life easier.
  • I have the source and I can compile it.
  • I don't want to use Aspect Oriented Programming.

EDIT: 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?

解决方案

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++).

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.

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

这篇关于自动将输入/退出功能日志添加到项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆