Gcc: how to track only calls to certain functions

The -pg , -mfentry and -finstrument-functions parameters affect all functions in the .c file. How do I insert a trace call only in certain functions, but not all?

I checked the attributes of the gcc function , but there seems to be no analogues -pg , -mfentry and -finstrument-functions , which can be used to decorate only certain functions,

no_instrument_function excludes functions, but what I want is the opposite, i.e. includes functions.

+6
source share
2 answers

You can do this with Backtraces in C But with this method, you have to add code to the function you want to track.

Here is a simple example:

  #include <execinfo.h> #include <stdio.h> #include <stdlib.h> /* Obtain a backtrace and print it to stdout. */ void print_trace (void) { void *array[10]; size_t size; char **strings; size_t i; size = backtrace (array, 10); strings = backtrace_symbols (array, size); printf ("Obtained %zd stack frames.\n", size); for (i = 0; i < size; i++) printf ("%s\n", strings[i]); free (strings); } /* A dummy function to make the backtrace more interesting. */ void dummy_function (void) { print_trace (); } int main (void) { dummy_function (); return 0; } 

At compilation, add the -g -rdynamic flag to the linker:

  gcc -g -rdynamic example.c -o example 
0
source

With -finstrument-functions you can filter the address of the function in __cyg_profile_func_enter and __cyg_profile_func_exit to continue only the functions that you want to track.

To be more friendly and filter by function names instead of your addresses, you can build a hash table from character table data.

0
source

Source: https://habr.com/ru/post/973962/


All Articles