5

I have an almost identical question as How to add code at the entry of every function? but for C:

As I'm maintaining someone else's large undocumented project, I wish 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 on entry of every function, so that I

  1. have a log of what calls what, and
  2. can tell, on which nth call to a function it breaks.

The existing code comprises hundreds of source files, so it is unfeasible to put a macro e.g.

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

in every function. I am also not using DevStudio, Visual Studio or other compiler providing __cyg_profile_func_enter or such extensions.

Optionally, I'd like to printf the return value of each function on exit in a similar style. Can I do that too?

Community
  • 1
  • 1
Gnubie
  • 2,587
  • 4
  • 25
  • 38

1 Answers1

10

Since you have tagged with gcc it has the -finstrument-functions option:

Generate instrumentation calls for entry and exit to functions. ...

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177