問題描述
我有一個需要調查的第 3 方源代碼.我想看看函數(shù)的調用順序,但我不想浪費時間打字:
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__)
對于每個函數(shù),我也不想接觸任何源文件.
for each function, nor do I want to touch any source file.
你有什么建議嗎?是否有編譯器標志可以自動為我執(zhí)行此操作?
Do you have any suggestions? Is there a compiler flag that automagically does this for me?
對評論的澄清:
- 我將交叉編譯源代碼以在 ARM 上運行它.
- 我會用 gcc 編譯它.
- 我不想分析靜態(tài)代碼.我想跟蹤運行時.所以 doxygen 不會讓我的生活更輕松.
- 我有源代碼,我可以編譯它.
- 我不想使用面向方面的編程.
我發(fā)現(xiàn) gdb 提示符中的 'frame' 命令會在那個時間點打印當前幀(或者,您可以說是函數(shù)名稱).也許,每次調用函數(shù)時都可以(使用 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
命令行選項.您必須實現(xiàn)自己的 __cyg_profile_func_enter()
和 __cyg_profile_func_exit()
函數(shù)(在 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++).
它們提供了一種方法來跟蹤從何處調用了什么函數(shù).但是,該接口有點難以使用,因為例如傳遞的是被調用函數(shù)的地址及其調用點,而不是函數(shù)名稱.您可以記錄地址,然后使用類似 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. :)
這篇關于自動將進入/退出功能日志添加到項目的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!