Reference
Use the finstrument-functions _ attribute _ + graphviz visual function to call the GCC function.
IBM graphviz visualized function call
Gcc/g ++ uses-finstrument-functions to check the call of each function of the Code
Http://www.logix.cz/michal/devel/CygProfiler/cyg-profile.c.xp
Http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/Code-Gen-Options.html#Code-Gen-Options official parameter description
Index
- Introduction
- C
- C ++
- Finstrument-functions-exclude-function-list = test exclusion Function
Because the project is too large, too many threads, and GPROF is not reliableFinstrument-Functions
-Finstrument-FunctionsCall the two hook functions "_ cyg_profile_func_enter" and "_ cyg_profile_func_exit" before each incoming and exiting function. What are the solutions for "inbound" and "exit? C programming language is the most typical practice. Although there is no way to define the actual statement, function call actually exists in the form of a stack frame, last year, I mentioned "dig into Hello World" Part II. Therefore, if we do not pass through the built-in GCC function "_ builtin_return_address" to obtain the Dynamic Address of caller and callee, that can still be passed through-Finstrument-FunctionsLet GCC synthesize related processing commands so that we can catch up with them. When we see the _ Cyg's starting function, we know it's from cygnus's kernel. We can see a lot of it in GCC 2.x.
C
Hello. c
# 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);} Main () {puts ( " Hello world! " ); Return 0 ;}
Makefile
ALL: Hello. O gcc-Finstrument-functions hello. O-O hellohello. O: gcc-Finstrument-functions-C hello. Cclean: Rm*. O hello-RF
Run:
./Hello _ cyg_profile_func_enter: func=Zero X 8048438, Called by =0x658dec/Nhello world!_ Cyg_profile_func_exit: func=Zero X 8048438, Called by =0x658dec/N
End
C ++
Hello. CC
# Include <cstdlib> # Include <Cstdlib> # Include <Fstream> # Include <Iomanip> # Include <Iostream> Using Namespace STD; # Define Dump (func, call) printf ("% s: func = % P, called by = % P \ n", _ FUNCTION __, func, call) # Ifdef _ cplusplus Extern " C " { # Endif /* Static functions. */ Static File * openlogfile ( Const Char * Filename) _ attribute _ (no_instrument_function )); Static Void Closelogfile ( Void ) _ Attribute _ (no_instrument_function )); /* Note that these are linked internally by the compiler. * don't call them directly! */ Void _ Cyg_profile_func_enter ( Void * This_fn, Void * Call_site) _ attribute _ (no_instrument_function )); Void _ Cyg_profile_func_exit ( Void * This_fn, Void * Call_site) _ attribute _ (no_instrument_function); # ifdef _ cplusplus }; # Endif Void _ Cyg_profile_func_enter ( Void * This_fn, Void * Call_site) {dump (this_fn, call_site );} Void _ Cyg_profile_func_exit ( Void * This_fn, Void * Call_site) {dump (this_fn, call_site );} Int Main ( Int Argc,Char * Argv []) {cout < " \ Nprogram " <Argv [ 0 ] <Endl < Endl; Return Exit_success ;} // ---------- End of function main ----------
Makefile:
ALL: G++-Finstrument-functions hello.CC
Run:
./ A. Out _ cyg_profile_func_enter: func = 0x801_ AE , Called by = 0x8048a3b _ Cyg_profile_func_enter: func = Zero X 8048714 , Called by = 0x801_d7 _ Cyg_profile_func_exit: func = Zero X 8048714 , Called by = 0x801_d7 _ Cyg_profile_func_exit: func = 0x801_ AE , Called by =0x8048a3b _ Cyg_profile_func_enter: func = Zero X 8048882 , Called by = 0x658dec Program. / A. Out _ cyg_profile_func_exit: func = Zero X 8048882 , Called by = 0x658dec _ Cyg_profile_func_enter: func = 0x804881a , Called by = 0x66e7f9 _ Cyg_profile_func_exit: func =0x804881a , Called by = 0x66e7f9
Hello. c
# Include <stdio. h> # Include " Test. 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 );} Void Test () {printf ( " Nihao \ n " );} Main () {test (); puts ( " Hello world! " ); Return 0 ;}
Makefile
Cc =Gcc44all: Hello. o $ (cc)-G-finstrument-functions hello. O-O hellohello. O: $ (cc)-G-finstrument-functions-exclude-function-list = test-C hello. Cclean: Rm*. O hello-RF
Run:
./Hello _ cyg_profile_func_enter: func=0x804847e, Called by =0xb7e6adecNihaohello world!_ Cyg_profile_func_exit: func=0x804847e, Called by =0xb7e6adec
End