问题
I try to build profiler to some c project. I want that gcc plant some assembly instruction in all the function entries and function exit points in compile time. I try to search some guides in the web but without success. where can I learn how to do that?
thank in advance.
回答1:
Apparently you can use the -finstrument-functions
flag to get gcc to generate instrumentation calls
void __cyg_profile_func_enter(void *func, void *callsite);
void __cyg_profile_func_exit(void *func, void *callsite);
at function entry and exit. I've never used this, but a quick search brings up information and examples here, here, here and here.
回答2:
Unless you want to modify gcc
(which is non-trivial!), I would think that there are two fairly obvious approaches.
- Pre-process the C code itself - it's not easy, but not terribly hard either. Find the beginning and end of a function, and add your code to it, then let the compiler proper do the job of making the code... There are quite a few tools on the market that does this in one way or another, for a variety of purposes [code flow analysis, profiling, etc].
- Take the assembler output of gcc and process it to to add code to functions there. This is in some ways easier, and in some ways harder. Identifiying functions is probably no more difficult, but "not breaking" the assembler code may be harder unless your inserted assembler code is completely "safe".
Obviously, the option of modifying gcc
is also a possibility, but the compiler code is fairly complex, and unless you basically take all the existing hooks for gprof
, I don't think it's a school project - unless you are on your way to a PhD or some such.
来源:https://stackoverflow.com/questions/14850548/how-can-i-plant-assembly-instructions-in-the-prologue-and-epilogue-of-function-v