实例
1 两个c文件
/***d.c***/
void fun()
{
int i;
i = 0;
}
int main()
{
int i;
i = 0;
while(i++ < 10)
fun();
return 0;
}
/***test.c***/
#include <stdio.h>
#include <stdlib.h>
/* Constructor and Destructor Prototypes */
void main_constructor( void ) __attribute__ ((no_instrument_function, constructor));
void main_destructor( void ) __attribute__ ((no_instrument_function, destructor));
void __cyg_profile_func_enter( void *this, void *callsite )__attribute__ ((no_instrument_function, destructor));
void __cyg_profile_func_exit( void *this, void *callsite )__attribute__ ((no_instrument_function, destructor));
/* Output trace file pointer */
static FILE *fp;
void main_constructor( void )
{
fp = fopen( "trace.txt ", "w" );
if (fp == NULL) exit(-1);
}
void main_deconstructor( void )
{
fclose( fp );
}
void __cyg_profile_func_enter( void *this, void *callsite )
{
/* Function Entry Address */
fprintf(fp, "E%p/n", (int *)this);
}
void __cyg_profile_func_exit( void *this, void *callsite )
{
/* Function Exit Address */
fprintf(fp, "X%p/n", (int *)this);
}
*****************华*丽*的*分*割*线 ****************
编译:gcc -o foo d.c test.c -g -finstrument-functions
执行:./foo ,生成了trace.txt文件
E0x80484f9
E0x80484c4
X0x80484c4
E0x80484c4
X0x80484c4
E0x80484c4
X0x80484c4
E0x80484c4
X0x80484c4
E0x80484c4
X0x80484c4
E0x80484c4
X0x80484c4
E0x80484c4
X0x80484c4
E0x80484c4
X0x80484c4
E0x80484c4
X0x80484c4
E0x80484c4
X0x80484c4
X0x80484f9
E(nil)
X(nil)
执行:./pvtrace foo,生成graph.dot
digraph foo {
main [shape=rectangle]
fun [shape=ellipse]
main -> fun [label="10 calls" fontsize="10"]
}
dot -Tjpg graph.dot -o graph.jpg 生成图片
详情参考