用 Graphviz+pvtrace 可视化函数调用

1、要有代码,随便写个小程序,如下所示。

#cat test.c

复制代码
#include <stdio.h>
#include <string.h>

void haha();
void hehe()
{
    printf("int hehe\n");
    haha();
}

void test()
{
    printf("hehe test\n");
    hehe();
    haha();
}

void haha()
{
    printf("in haha\n");
}

int main()
{
    test();
    printf("hello world !\n");

    return 0;
}
复制代码

2、要下载一个叫pvtrace的解析工具,我把它放到了百度网盘上:http://pan.baidu.com/s/1sj23YPJ

这个工具解压后的文件如下

复制代码
# unzip pvtrace.zip
[root@localhost pvtrace]# ls
instrument.c  Makefile  stack.c  stack.h  symbols.c  symbols.h  trace.c
复制代码

之后编译、安装

复制代码
[root@localhost pvtrace]# make
gcc  -Wall -c trace.c
gcc  -Wall -c symbols.c
gcc  -Wall -c stack.c
gcc -o pvtrace trace.o symbols.o stack.o
[root@localhost pvtrace]# make install
cp pvtrace /usr/local/bin
复制代码

3、把pvtrace目录下的instrument.c文件放到你的项目下

# ls
instrument.c  Makefile  test.c

修改Makefile,编译test.c的时候把这个源文件加上。还有就是在编译选项中加上这个参数 -finstrument-functions

编译自己的项目:

复制代码
# make
gcc -g -c -o test.o test.c -I../../include -g -finstrument-functions
gcc -g -c -o instrument.o instrument.c -I../../include -g -finstrument-functions
gcc -g -o test test.o instrument.o -L./
复制代码

4、现在开始生成函数调用的流程图了

运行自己的项目test

# ./test

:这里有个问题是你的程序运行多少,它记录多少函数之间的关系。你运行的少,它就记录的少,运行的多久记录的多。也就是说,那些暂时没有运行的代码,是不能被记录的。这个在原文章中有说明,因为人家记录的时候就是在运行的时候记录的嘛。

运行完成后会生成一个叫做trace.txt的文件

# ls
instrument.c  instrument.o  Makefile  test  test.c  test.o  trace.txt

接下来使用pvtrace解析这个文件,但是pvtrace后面跟的参数不是trace.txt,而是我们的程序test

复制代码
[root@localhost drawFuncPic]# pvtrace test
[root@localhost drawFuncPic]# ls
graph.dot  instrument.c  instrument.o  Makefile  test  test.c  test.o  trace.txt
复制代码

生成了gaph.dot文件,接下来就是使用Graphviz的dot工具,把这个graph.dot变成一个图片了。

复制代码
[root@localhost drawFuncPic]# dot -Tjpg graph.dot -o graph.jpg
[root@localhost drawFuncPic]# ls
graph.dot  graph.jpg  instrument.c  instrument.o  Makefile  test  test.c  test.o  trace.txt
复制代码

注:没有Graphviz的话可以去官网下载或者使用命令“#yum install graphviz”进行安装:

复制代码
# yum install graphviz
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * extras: mirrors.yun-idc.com
 * updates: mirrors.opencas.cn
Setting up Install Process
Package graphviz-2.26.0-10.el6.x86_64 already installed and latest version
Nothing to do
复制代码

我这里有一个安装graphviz的笔记:http://www.cnblogs.com/fengbohello/p/4689131.html

生成的流程图如下:

 



本文转自郝峰波博客园博客,原文链接:http://www.cnblogs.com/fengbohello/p/4904182.html,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值