These suggestions apply to the GCC. You can use the gcov tool to get a detailed report on which parts of the program were executed and how often. You must pass some special options to GCC to create the correct hardware and output for gcov to handle.
--coverage This option is used to compile and link code intended for coverage analysis. This parameter is synonymous with -fprofile-arcs -ftest-coverage (at compilation) and -lgcov (at linking). See the documentation for these parameters for details.
Then, when you run your program, some profiling and coverage data is generated. Then you can call gcov to analyze this output. The following is an example of the output taken from the link above:
-: 0:Source:tmp.c -: 0:Graph:tmp.gcno -: 0:Data:tmp.gcda -: 0:Runs:1 -: 0:Programs:1 -: 1:
If you want to implement your own tool for recording call history of a program, you can use -finstrument-functions and its associated parameters on GCC.
-finstrument-functions
Generate instrumental calls to enter and exit functions. Immediately after entering the function and immediately before the exit of the function, the following profiling functions are called with the address of the current function and its call site. (On some platforms, __builtin_return_address does not work outside the current function, so call site information may not be available for profiling functions otherwise.)
void __cyg_profile_func_enter (void *this_fn, void *call_site); void __cyg_profile_func_exit (void *this_fn, void *call_site);
The first argument is the address of the beginning of the current function, which can be searched for exactly in the symbol table.
In C ++, your implementation of these hooks must be declared extern "C" . You can implement hooks to register every time a function is called. You do not get function names, but after that you can then process pointers with objdump or addr2line .
source share