2009年9月10日 星期四

gcc's -finstrument-functions option

gcc's -finstrument-functions option讓我可你取得callee 的retrun address和start address, 一但知道callee 的retrun address就可以透過excutable file中的symbol table去取得caller 的start address,
進而知道caller name

qustion@jeff-debian:~/Samsung/hello/printf$ gcc -finstrument-functions -o hello hello.c
qustion@jeff-debian:~/Samsung/hello/printf$ ./hello
__cyg_profile_func_enter: func = 0x804846a, called by = 0x80484ec
func: hello world
__cyg_profile_func_exit: func = 0x804846a, called by = 0x80484ec
qustion@jeff-debian:~/Samsung/hello/printf$ objdump -t ./hello | awk '{print $1" "$3" "$6}'|grep "F" | sort -k 1
080482e0 F _init
08048350 F _start
08048380 F __do_global_dtors_aux
080483e0 F frame_dummy
08048404 F __cyg_profile_func_enter
08048437 F __cyg_profile_func_exit
0804846a F func
080484b8 F main
08048520 F __libc_csu_fini
08048530 F __libc_csu_init
0804858a F .hidden
08048590 F __do_global_ctors_aux
080485bc F _fini
080485e0 O __FUNCTION__.1594
0804861b O __FUNCTION__.1602
08048633 O __FUNCTION__.1608
0804864c O __FRAME_END__

qustion@jeff-debian:~/Samsung/hello/printf$ cat -b ./hello.c
1 #include
2 #define DUMP(func, call) \
3 printf("%s: func = %p, called by = %p\n", __FUNCTION__, func, call)
4 void __cyg_profile_func_enter (void *this_fn, void *call_site)
5 __attribute__ ((no_instrument_function));
6 void __cyg_profile_func_exit (void *this_fn, void *call_site)
7 __attribute__ ((no_instrument_function));
8 int func();
9 static int cyg_profile_enabled=0;

10 void
11 __cyg_profile_func_enter (void *this_fn, void *call_site)
12 {
13 if(cyg_profile_enabled)
14 DUMP(this_fn, call_site);
15 }

16 void
17 __cyg_profile_func_exit (void *this_fn, void *call_site)
18 {
19 if(cyg_profile_enabled)
20 DUMP(this_fn, call_site);
21 }


22 int func(){

23 printf("%s: hello world\n",__FUNCTION__);
24 return 0;

25 }
26 int main(int argc, char *argv[]) {
27 cyg_profile_enabled=1;
28 func();
29 cyg_profile_enabled=0;
30 return 0;
31 }

reference:
http://blog.linux.org.tw/~jserv/archives/001870.html
http://blog.linux.org.tw/~jserv/archives/002054.html
http://www.logix.cz/michal/devel/CygProfiler/

沒有留言: