1 #include <stdio.h> 2 #include <stdlib.h> 3 4 /* Function prototypes with attributes */ 5 void main_constructor( void ) 6 __attribute__ ((no_instrument_function, constructor)); 7 8 void main_destructor( void ) 9 __attribute__ ((no_instrument_function, destructor)); 10 11 void __cyg_profile_func_enter( void *, void * ) 12 __attribute__ ((no_instrument_function)); 13 14 void __cyg_profile_func_exit( void *, void * ) 15 __attribute__ ((no_instrument_function)); 16 17 18 static FILE *fp; 19 20 21 void main_constructor( void ) 22 { 23 fp = fopen( "trace.txt", "w" ); 24 if (fp == NULL) exit(-1); 25 } 26 27 28 void main_destructor( void ) 29 { 30 fclose( fp ); 31 } 32 33 34 void __cyg_profile_func_enter( void *this, void *callsite ) 35 { 36 fprintf(fp, "E%p\n", (int *)this); 37 } 38 39 40 void __cyg_profile_func_exit( void *this, void *callsite ) 41 { 42 fprintf(fp, "X%p\n", (int *)this); 43 }
constructor、destructor作用于main方法,即程序开始与退出时。
enter、exit作用于每个方法,在每个方法执行与退出时被调用。
上述代码为instrument.c文件代码。
2、将instrument加入程序源码,产生执行路径
a、将instrument.c拷入程序源码的src文件夹中
b、修改makefile文件(之前可以执行./configure --prefix=。。。修改安装目录)
--将CFLAGS 加入-g -finstrument-functions
--将instrument.c加入 程序名_SOURCE变量
--将instrument.$(OBJEXT) 加入am_程序名_OBJECTS变量
--src、外层文件夹中的makefile都要改,一般外层文件夹中的makefile只改CFLAGS
c、make
d、make install
3、有可能会出现问题,,此时将src文件夹中的CFLAGS选项的 -O2去掉即可(segmentation fault错误)