如何打印c中函数的调用顺序?
问题描述:
我正在阅读Apache httpd源代码,我想知道什么时候发出请求,哪个函数首先被调用,然后是哪个函数,等等,是否有一些简单的方法可以这样做?如何打印c中函数的调用顺序?
喜欢的东西
Waiting client connection... # a client send a HTTP request
client.c:accept_request() is called
client.c:handle_request() is called
asdf.c:func1() is called
fdsa.c:func2() is called
response.c:send_response() is called
Waiting client connection...
答
不是一个容易没办法,但也有多种可能性:
- ,如果你可以运行代码,然后通过它去与调试器或分析器可以帮助你看看是怎么回事
- 再次,如果你可以运行的代码,你可以添加跟踪了解的功能流
printf(">>> entering %s\n", __func__); printf("<<< leaving %s\n", __func__);
- 如果你不能运行代码,那么也许ctags这样或cscope的工具可以帮助您查看哪些函数被调用时(或类似Eclipse或的IntelliJ一个IDE)
EHH?不是C代码吗? (你是这样标记的),AFAIK,C是面向流程的... –
请澄清一下:你想在运行时打印调用堆栈吗?这在标准C中是不可能的。 –
对于Linux,有['backtrace()'](http://man7.org/linux/man-pages/man3/backtrace.3.html)。有意义输出的符号。 –