IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    CodeViz学习笔记

    一根稻草发表于 2015-05-28 00:00:00
    love 0

    CodeViz provides the ability to generate call graphs to aid the task of understanding code.
    其原理是通过修改gcc,在编译过程中生成函数调用关系,然后用grapviz工具绘图。
    生成简单的函数调用图可以用gprof实现:gcc -pg + gprof 查看函数调用关系图的实例见《gprof学习笔记》

    安装环境

    • ubuntu 12.04 LTS 32bit
    • codeviz-1.0.12
    • gcc-4.6.2

    安装过程

    1.安装gcc依赖库

        sudo apt-get install libgmp3-dev libmpfr-dev libmpc-dev
    

    2.安装图形绘制库

        sudo apt-get install graphviz
    

    3.下载codeviz和gcc-4.6.2

        download codeviz-1.0.12.tar.gz
        download gcc-4.6.2.tar.gz
    

    4.安装codeviz

        $tar xvf codeviz-1.0.12.tar.gz
        $cd codeviz-1.0.12
        $mv  ../gcc-4.6.2.tar.gz codeviz-1.0.12/compilers
        $vi compilers/install_gcc-4.6.2.sh
            35行添加 `--disable-multilib`选项
        $echo "export C_INCLUDE_PATH=/usr/include/i386-linux-gnu" >> ~/.bashrc
        $echo "export CPLUS_INCLUDE_PATH=/usr/include/i386-linux-gnu" >> ~/.bashrc
        $echo "export LIBRARY_PATH=/usr/lib/i386-linux-gnu" >> ~/.bashrc
        $source ~/.bashrc
        $make
        $sudo make install
    

    5.安装成功能看到如下信息(gcc-4.6.2的位置及genfull的用法)

        Patched gcc is installed to /usr/local/gccgraph. To compile a project
        for use with CodeViz, genearlly the following will work
    
        make CC=/usr/local/gccgraph/bin/gcc or g++
    
        To generate a full.graph file for C, use
    
        genfull
    
        For C++, make sure you use the cppdepn method with
    
        genfull -g cppdepn
    
        or the results will not be what you expect.
    

    hack example

    利用github上一个实例来看看codeviz的用法

        $git clone https://github.com/onestraw/traffic-analysis
        $cd traffic-analysis/dnseye
        $vi Makefile
            edit CC = /usr/local/gccgraph/bin/gcc
        $make clean
        $make
        $genfull 
        $gengraph -f DecodeEthPkt -s "DecodeICMP;DecodeARP;DecodeTCP;DecodeUDP" -i "fprintf;GetTime;bzero;inet_ntoa;__builtin_strncpy;__builtin_puts;__builtin_memcpy;__builtin_constant_p" --output-type=png
        
    

    上述过程执行完毕会在dnseye/目录下生成一个png图片,如下所示:

    DecodeEthPkt-of-dnseye

    hack kernel

        $tar xvf linux-3.13.tar.gz
        $cd linux-3.13
        $make menuconfig
        $make CC=/usr/local/gccgraph/bin/gcc bzImage
        $make CC=/usr/local/gccgraph/bin/gcc modules
    
        正在编译过程中....
        $genfull -s "mm include/linux drivers/block arch/i386 fs init lib net kernel ipc"
        $gengraph --output-type=png --output-layout=LR -f vfs_read -d 2 -s "fsnotify_access" -j "__builtin*" -y "security*"
    

    执行成功会生成一个vfs_read.png的图片,如下所示 vfs_read

    由于干扰函数过滤,gengraph的正则表达式选项就显得格外重要

        Regular Expression Options:
              -z, --func-re     Regular expressions of top-level functions to graph
              -j, --ignore-re   Regular expressions of functions to ignore
              -y, --show-re     Regular expressions of functions to show but not traverse
    

    输出图片的布局是TB,即root函数在最上面,向下扩散,上面vfs_read实例中用的布局是LR,即从左向右。

    --output-layout   Layout direction: LR|RL|BT|TB (default TB)
    

    Reference

    • codeviz 官网
    • 用Graphviz + CodeViz生成C/C++函数调用图


沪ICP备19023445号-2号
友情链接