1

I'm reading Apache httpd source code, I would like to know when a request come, which function get called first, followed by which function, and so on, is there some easy way to do so?

Something like

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...
Sato
  • 8,192
  • 17
  • 60
  • 115
  • ehh? Isn't is C code? (you tagged so), and AFAIK, C is process oriented... – Sourav Ghosh Aug 01 '17 at 08:14
  • Please clarify: Do you want to print the call stack at runtime? This isn't possible in standard C. –  Aug 01 '17 at 08:15
  • For Linux, there is [`backtrace()`](http://man7.org/linux/man-pages/man3/backtrace.3.html) .. compile with debugging symbols for meaningful output. –  Aug 01 '17 at 08:22
  • 1
    Possible duplicate of [How can one grab a stack trace in C?](https://stackoverflow.com/questions/105659/how-can-one-grab-a-stack-trace-in-c) –  Aug 01 '17 at 08:22
  • Can you give an example of the output you want to see? This can probably be done using `systemtap`. – Mark Plotnick Aug 01 '17 at 15:00
  • @MarkPlotnick updated – Sato Aug 02 '17 at 01:13

2 Answers2

1

Put a printf statement at the beginning of each function

printf("Called function: %s\n", __func__);

This will print the name of the function when that function is called and this way you will able to know the function call sequence.

haccks
  • 104,019
  • 25
  • 176
  • 264
1

Not an easy way no, but there are multiple possibilities:

  • if you can run the code then going through it with a debugger or a profiler can help you see what is going on
  • again if you can run the code you can add traces to understand the flow of functions
printf(">>> entering %s\n", __func__);
printf("<<< leaving %s\n", __func__);
  • if you cannot run the code then maybe tools like ctags or cscope can help your see which functions are called when (or an IDE like eclipse or intellij)
Alex Garcia
  • 773
  • 7
  • 21