Porting the project there was a question how to execute a trace call if I have access to the source code and to the Makefile (to gnuMake).

You need a trace call by type.

  1. int main ()
  2. void foo1 ()
  3. void foo2 ()
  4. And so on by the call stack ...

    3 answers 3

    In gcc there is an option -finstrument-functions, which allows you to call a callback on a call to each function.

    A good example: https://github.com/elcritch/etrace

    main | Crumble_make_apple_crumble | | Crumble_buy_stuff | | | Crumble_buy | | | Crumble_buy (total: 5 times) | | Crumble_prepare_apples | | | Crumble_skin_and_dice | | Crumble_mix | | Crumble_finalize | | | Crumble_put | | | Crumble_put (total: 2 times) | | Crumble_cook | | | Crumble_put | | | Crumble_bake 
    • It seems that this is what I was looking for, thanks! - hays
     #include <execinfo.h> int backtrace(void **buffer, int size); char **backtrace_symbols(void *const *buffer, int size); void backtrace_symbols_fd(void *const *buffer, int size, int fd); 

    DESCRIPTION

    backtrace () returns a backtrace for the calling program. A backtrace is currently active function calls for the program.

      In addition to backtrace* you can write your own wrapper, which would add arbitrary information, allow you to format and filter it in your own way, and could use your own loggers. Here is an example for C++ : a source article . Compatibility with C is done in a similar way, albeit with minor differences. I will not give my sources here, but I can say that there are quite a few macros with a variable number of arguments, and one problem has not been solved. It looks something like this:

       static f_start( int, ab_sum, (int a, int b) ) st_dump(NULL, NULL, " ", 2 ); _return( a + b); } _main() int rc = ab_sum( 1, -1 ); _return( rc ); } 

      What the debug mode turns into:

       static int ab_sum( int a, int b ) { /* какая-Ρ‚ΠΎ магия для сохранСния ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠΈ */ /* Π²Ρ‹Π²ΠΎΠ΄ трСйса */ return a + b; } int main( int argv, char **argv ) { /* инициализация трСйса */ /* какая-Ρ‚ΠΎ магия для сохранСния ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠΈ */ int rc = ab_sum( 1, -1 ); return rc; } 

      When compiling with NDEBUG all these macros are expanded into β€œdummies” and the code is not superfluous.

      In general, all this can be considered as a starting point, if the capabilities of standard libraries are not enough, or you need to write cross-platform code.