2009年5月16日 星期六

gcc/g++使用-finstrument-functions來觀察code每個function的呼叫

man gcc可以找到-finstrument-functions的一些相關說明,簡單看到下面的敘述 Generate instrumentation calls for entry and exit to functions. 所以重要的是可以將每一個function call的呼叫攔截,加上我們自己的codes,有點LD_PRELOAD的意味存在。 以下介紹兩個例子,gcc和g++的寫法有點不同。 yslinnote@linux1 ~> gcc -finstrument-functions hello.c -o hello /*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); } int main() { puts("Hello World!"); return 0; }

yslinnote@linux1 ~> gcc -finstrument-functions hello.cc -o hello
/*hello.cc*/ #include <stdio.h> #define DUMP(func, call) \ printf("%s: func = %p, called by = %p\n", __FUNCTION__, func, call) #ifdef __cplusplus extern "C" { 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)); } #endif int main() { puts("Hello World!"); return 0; } /* 每個函式進入時會呼叫此函式 */ void __cyg_profile_func_enter(void *this_func, void *call_site) { DUMP(this_func, call_site); } /* 每個函式離開時會呼叫此函式 */ void __cyg_profile_func_exit(void *this_func, void *call_site) { DUMP(this_func, call_site); } error: can't set 'no_instrument_function' attribute after definition 要是出現上面的錯誤,表示你在c++的code中多加了__attribute__((no_instrument_function)), 只要在開頭宣告有加就好了。畢竟這是c的code。