• 用 Graphviz+pvtrace 可视化函数调用


    最近在想怎么把一个程序的函数调用关系快速的用流程图的方式画出来,之后看到了这个一篇文章“用 Graphviz 可视化函数调用”(http://www.ibm.com/developerworks/cn/linux/l-graphvis/)感觉不错,详细的原理请看原文章,这里只把我的实验过程记录下,以备自己以后的不时之需。

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

    #cat test.c

    #include <stdio.h>
    #include <string.h>
    
    void haha();
    void hehe()
    {
        printf("int hehe
    ");
        haha();
    }
    
    void test()
    {
        printf("hehe test
    ");
        hehe();
        haha();
    }
    
    void haha()
    {
        printf("in haha
    ");
    }
    
    int main()
    {
        test();
        printf("hello world !
    ");
    
        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.ibm.com/developerworks/cn/linux/l-graphvis/

    http://blog.sina.com.cn/s/blog_67fcf49e0101m9r0.html

    http://download.csdn.net/download/u011843461/8389925

  • 相关阅读:
    二分多重匹配(HDU5093)
    2-sat(and,or,xor)poj3678
    某个点到其他点的曼哈顿距离之和最小(HDU4311)
    第k最短路A*启发式搜索
    求树的直径和中心(ZOJ3820)
    并查集hdu4424
    map容器结构体离散化
    二维坐标系极角排序的应用(POJ1696)
    【进阶3-3期】深度广度解析 call 和 apply 原理、使用场景及实现(转)
    判断js数据类型的四种方法,以及各自的优缺点(转)
  • 原文地址:https://www.cnblogs.com/fengbohello/p/4904182.html
Copyright © 2020-2023  润新知