How can I set build instructions in prologue and function epilogue via gcc

I am trying to create a profiler for some c project. I want gcc to set some build instruction in all function entries and function exit points at compile time. I have been trying to find several tutorials on the internet with no success. where can i find out how to do this?

Thanks in advance.

+3


source to share


2 answers


Apparently you can use a flag -finstrument-functions

to get gcc to make control calls

void __cyg_profile_func_enter(void *func, void *callsite); 
void __cyg_profile_func_exit(void *func, void *callsite); 

      



when entering and exiting the function. I've never used this, but a quick search brings up information and examples here , here , here and here .

+5


source


If you don't want to change gcc

(which is non-trivial!), I think there are two fairly obvious approaches.

  • Pre-processing the C code itself is not easy, but not very difficult. Find the beginning and end of the function and add your code to it, and then let the compiler do the work of generating the code ... There are quite a few tools on the market that do this one way or another, for various purposes [code flow analysis, profiling, etc. .d.].
  • Take the gcc assembler output and process it to add code to functions. This is in some way easier, and in some sense more difficult. Identity functions are probably not more difficult, but "not breaking" the assembly code can be more difficult if your pasted assembly code is not completely "safe".


Obviously a modification option is gcc

also possible, but the compiler code is quite complex and unless you are basically using all the existing hooks for gprof

, I don't think this is a school project, unless you are on the path to a candidate or some of them.

+2


source







All Articles