pvtrace和Graphviz实现对linux下C程序的函数调用跟踪

来源:互联网 发布:联通3g网络制式 编辑:程序博客网 时间:2024/04/29 12:43
函数调用跟踪是在C语言开发来,在IDE里面有很多工具实现这个功能,但是,在我们使用vim+gcc+gdb开发程序而言,有一种比较好的方法,下面主要找这个方法:
请参看:http://hi.baidu.com/caosicong/blog/item/ef86426dc2c9c1f8431694fb.html


第一,安装pvtrace:

下载地址: http://www.mtjones.com/developerworks/pvtrace.zip,如果你的linux不支持解压*.zip最好在windows下解压后copy到linux下。解压后有以下几个文件:

instrument.c   Makefile   stack.c   stack.h   symbols.c   symbols.h    trace.c

在linux上安装pvtrace,需要root权限:

$ make

$ make install

2.2 Graphviz安装:

Graphviz也是一个开源项目,可以将pvtrace生成的*.dot文件转成图片格式便于查看。Graphviz的下载地址http://www.graphviz.org/Download..php

如果是使用基于debian的发行版本,可以:

$ sudo apt-get install graphviz

首先:先写一个小的test.c


void aa(int i)
{
printf("%d\n",i);
}
int main()
{
printf("hellow world\n");
aa(10);
printf("test\n");
return;
}


然后将instrument.c考到与test.c相同的目录
$ ls
instrument.c       test.c

执行:
$ gcc -g -finstrument-functions test.c instrument.c -o test
再:
$ ./test  //可以看到多了一个trace.txt
$ ls
instrument.c        test.c
test                trace.txt

然后再:
$ pvtrace test  //得到 graph.dot
$ ls
graph.dot   test   trace.txt    instrument.c  test.c

接着,我们使用Graphviz对graph.dot生成图像:

$ dot graph.dot -Tpng -o out.png

这样,我们就可以查看到图片out.png了,如下:


0 0