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
  • 35,890
  • 14
  • 83
  • 151
g__k
  • 91
  • 1

2 Answers2

24

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

Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
  • 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 Путлер Капут 六四事 Nov 05 '17 at 11:06
2

Here is a nice example of what you want.

Yousf
  • 3,901
  • 2
  • 26
  • 37