使用CodeViz生成C/C++函数调用关系图

时间:2023-02-03 12:12:29

     运行环境:虚拟机下的Ubuntu 11.04

 

    结合Graphviz工具,使用CodeViz可以生成直观和漂亮的C/C++程序函数之间的调用关系图。

    1、安装graphviz

    在安装CodeViz之前,必须先安装它所依赖的工具dot,否则将无法完成./configure操作并提示以下错误信息:

checking for dot...not found
FATAL: The program dot was not in your path. This is probably available for your distribution
with the graphviz package. Install this before running configure again
    既可以从http://www.graphviz.org/Download_linux_ubuntu.php 上下载最新版本的graphviz安装程序手动安装,也可以使用以下命令自动安装:

$ sudo apt-get install graphviz graphviz-dev graphviz-doc

    程序简要说明:

graphviz - rich set of graph drawing tools
graphviz-dev - transitional package for graphviz-dev rename
graphviz-doc - additional documentation for graphviz

    安装相关库:

$ sudo apt-get install libgv-*

    相关库简要说明: 

libgv-guile - Guile bindings for graphviz
libgv-lua - Lua bindings for graphviz
libgv-ocaml - OCaml bindings for graphviz
libgv-perl - Perl bindings for graphviz
libgv-php5 - Php5 bindings for graphviz
libgv-python - Python bindings for graphviz
libgv-ruby - Ruby bindings for graphviz
libgv-tcl - Tcl bindings for graphviz

    2、安装CodeViz

    从http://www.skynet.ie/~mel/projects/codeviz/ 上下载CodeViz安装包codeviz-1.0.10.tar.gz以及从ftp://ftp.gnu.org/pub/gnu/gcc/gcc-3.4.6 上下载GCC源码包gcc-3.4.6.tar.gz,并把它们拷贝到Ubuntu下的同一目录下。

    然后解压CodeViz安装包,并把gcc-3.4.6.tar.gz拷贝到codeviz-1.0.10/compiler目录下:

$ tar zvxf codeviz-1.0.10.tar.gz
$ cd codeviz-1.0.10/
$ cp ../gcc-3.4.6.tar.gz compilers/ //也可以略过这一步,让ncftp(Ubuntu默认未安装)在make执行过程中自动下载gcc-3.4.6.tar.gz
$ ./configure

    Patched GCC默认安装在/usr/local/gccgraph目录下。

    在执行make之前,先通过以下命令创建一个链接文件asm:

$ sudo ln -sf /usr/include/asm-generic/ /usr/include/asm

    否则,在make执行过程中会出现在/usr/include/linux/errno.h文件中找不到asm/errno.h文件的错误信息并中止编译。

    接下来执行make和make install命令即可在/usr/local/gccgraph目录下创建打过补丁的GCC编译器以及在/usr/local/bin目录下创建两个perl脚本程序genfull和gengraph。

$ make
$ sudo make install

    3、CodeViz的使用举例

    例子源代码如下: 

/* test.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void func(char *p)
{
p = (char *)malloc(20);
}

int main(void)
{
char *str = NULL;

func(str);

strcpy(str, "hello world");

printf("string is %s\n", str);

return 0;
}

    使用刚生成的编译器编译test.c,会相应地生成一个test.c.cdepn文件。 

$ /usr/local/gccgraph/bin/gcc test.c

    test.c.cdepn文件信息: 

F {func} {test.c:6}
C {func} {test.c:7} {malloc}
F {main} {test.c:11}
C {main} {test.c:18} {printf}
C {main} {test.c:16} {strcpy}
C {main} {test.c:14} {func}

    然后执行genfull脚本生成full.graph。 

$ genfull

    full.graph文件信息: 

digraph fullgraph {
node [ fontname=Helvetica, fontsize=12 ];
"func" [label="func\ntest.c:5:"];
"main" [label="main\ntest.c:10:"];
"func" -> "malloc" [label="test.c:7"];
"main" -> "func" [label="test.c:14"];
"main" -> "strcpy" [label="test.c:16"];
"main" -> "printf" [label="test.c:18"];
}

    最后执行gengraph生成函数关系调用图。 

$ gengraph -f main --output-type "png"


 使用CodeViz生成C/C++函数调用关系图

    -f指定最顶层的函数,--output-type指定图片的输出格式。

    至于genfull和gengraph的详细使用说明请参考man手册,使用以下命令查看:

$ sudo apt-get install perl-doc //须先安装perl-doc
$ genfull --man
$ gengraph --man