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.
- int main ()
- void foo1 ()
- void foo2 ()
- And so on by the call stack ...
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 #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.
Source: https://ru.stackoverflow.com/questions/589214/
All Articles