9

I have some very large C files, having lots of functions. I need to trace the execution path at run time. There is no way I can trace it through debugging as its a hypervisor code currently running over qemu and doing a lot of binary translations.

Can anyone point me to some script in Perl or Python which can add a printf at the starting of all functions and the text could be something like "I am in < function name >"?

Zaid
  • 36,680
  • 16
  • 86
  • 155
g__k
  • 91
  • 2

2 Answers2

24

Just pass -finstrument-functions to gcc when compiling. See the gcc(1) man page for details.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    Perfect answer to the spirit of the question. – Noufal Ibrahim Jun 20 '10 at 09:12
  • 2
    +1. Just to add, then entry code might look like: printf("Function details: %s\n", ____PRETTY_FUNCTION____); ____PRETTY_FUNCTION____ is predefined macro that provides a string containing function name + signature. – Fanatic23 Jun 20 '10 at 10:52
  • Note: you need to implement each trace function manually, or use a tool like `etrace` that parses the ELF and generates those calls for you: https://stackoverflow.com/questions/6176284/why-doesnt-finstrument-functions-work-for-me – Ciro Santilli OurBigBook.com Nov 05 '17 at 11:06
2

Here is a nice example of what you want.

Yousf
  • 3,957
  • 3
  • 27
  • 37