// dump function enter and exit/* https://blog.csdn.net/jasonchen_gbd/article/details/44044899$ gcc -L/usr/lib/x86_64-linux-gnu -finstrument-functions main.c -o main$ ./main*/#include<stdio.h>#define DUMP(func, call) \ printf("%s: func = %p, called by = %p\n", __FUNCTION__, func, call)void__attribute__((no_instrument_function))__cyg_profile_func_enter(void*this_func,void*call_site){DUMP(this_func,call_site);}void__attribute__((no_instrument_function))__cyg_profile_func_exit(void*this_func,void*call_site){DUMP(this_func,call_site);}intdo_multi(inta,intb){returna*b;}intdo_calc(inta,intb){returndo_multi(a,b);}intmain(void){inta=4,b=5;printf("result: %d\n",do_calc(a,b));// TIP: print function point in the code (both main and &main are OK) if no add2line from toolchainprintf("The address of main: %p %p\n",main,&main);printf("The address of do_calc function: %p %p\n",do_calc,&do_calc);printf("The address of do_multi function: %p %p\n",do_multi,&do_multi);return0;}
Result:
$ ./main
__cyg_profile_func_enter: func= 0x56206e5437b2, called by= 0x7fa8fc459bf7
__cyg_profile_func_enter: func= 0x56206e543763, called by= 0x56206e5437eb
__cyg_profile_func_enter: func= 0x56206e54371c, called by= 0x56206e543794
__cyg_profile_func_exit: func= 0x56206e54371c, called by= 0x56206e543794
__cyg_profile_func_exit: func= 0x56206e543763, called by= 0x56206e5437eb
result: 20
The address of main: 0x56206e5437b2 0x56206e5437b2
The address of do_calc function: 0x56206e543763 0x56206e543763
The address of do_multi function: 0x56206e54371c 0x56206e54371c
__cyg_profile_func_exit: func= 0x56206e5437b2, called by= 0x7fa8fc459bf7
// https://www.codenong.com/cs106497648/// https://stackoverflow.com/questions/1644868/define-macro-for-debug-printing-in-c// an example of using debug MACRO#include<stdio.h>#include<time.h>#define DEBUG /* comment this to disable Debug Message */// #line 206 /* Uncomment this to redeinfe LINE number */#ifdef DEBUG#define debug_print(fmt, ...) \ do { fprintf(stderr, "[%lu | %s:%d:%s()] " fmt, time(0), __FILE__, __LINE__, __func__, __VA_ARGS__); } while (0)#else#define debug_print(fmt, ...)#endifvoidsilly_function(void){debug_print("%s\n","Here I am");}intmain(void){debug_print("%s\n","Here I am");silly_function();return0;}
Result:
[1627893524| main.c:27:main()] Here I am
[1627893524| main.c:22:silly_function()] Here I am