Automatically run code at the beginning of each C function

I have almost the same question, how to add code when entering each function? but for C :

Since I am supporting some other large undocumented project, I want to have code similar to

static C0UNT_identifier_not_used_anywhere_else = 0; printf("%s%s:%d#%d", __func__, strrchr(__FILE__,'/'), __LINE__, ++C0UNT_identifier_not_used_anywhere_else); 

to run when you enter each function, so I

  • there is a journal of what causes that, and
  • may indicate by which n th calls the function that it breaks.

Existing code contains hundreds of source files, so it is impossible to put a macro, for example.

 #define ENTRY_CODE ... ... int function() { ENTRY_CODE ... } 

in every function. I also do not use DevStudio, Visual Studio or another compiler providing __cyg_profile_func_enter or such extensions.

Optionally, I would like to print the return value of each function when exiting in a similar style. Can I do it?

+4
source share
1 answer

Since you -finstrument-functions gcc, it has the -finstrument-functions option:

Generate toolkit calls to enter and exit functions ....

+9
source

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


All Articles