[轉] 使用CodeViz產生C/C++函數呼叫歷程圖

來源:互聯網
上載者:User

標籤:

運行環境:虛擬機器下的Ubuntu 11.04

 

    結合Graphviz工具,使用CodeViz可以產生直觀和漂亮的C/C++程式函數之間的呼叫歷程圖。

    1、安裝graphviz

    在安裝CodeViz之前,必須先安裝它所依賴的工具dot,否則將無法完成./configure操作並提示以下錯誤資訊:

checking for dot...not foundFATAL: 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 toolsgraphviz-dev - transitional package for graphviz-dev renamegraphviz-doc - additional documentation for graphviz

    安裝相關庫:

$ sudo apt-get install libgv-*

    相關庫簡要說明: 

libgv-guile - Guile bindings for graphvizlibgv-lua - Lua bindings for graphvizlibgv-ocaml - OCaml bindings for graphvizlibgv-perl - Perl bindings for graphvizlibgv-php5 - Php5 bindings for graphvizlibgv-python - Python bindings for graphvizlibgv-ruby - Ruby bindings for graphvizlibgv-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目錄下。

  安裝GCC時,需要安裝下面的庫

  apt-get install libgmp10-dev libmpfr-dev libmpc-dev

    在執行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"


 

    -f指定最頂層的函數,--output-type指定圖片的輸出格式。

    至於genfull和gengraph的詳細使用說明請參考man手冊,使用以下命令查看:

$ sudo apt-get install perl-doc //須先安裝perl-doc$ genfull --man$ gengraph --man

另外參考:
http://blog.csdn.net/solstice/article/details/486788

CodeViz是《Understanding The Linux Virtual Memory Manager》(at Amazon,在頁尾)的作者 Mel Gorman 寫的一款分析C/C++原始碼中函數調用關係的open source工具(類似的open source軟體有 egypt、ncc)。其基本原理是給 GCC 打個補丁,讓它在編譯時間每個源檔案時 dump 出其中函數的 call graph,然後用 Perl 指令碼收集並整理調用關係,轉交給Graphviz繪製圖形。

CodeViz 原本是作者用來分析 Linux virtual memory 的源碼時寫的一個小工具,現在已經基本支援 C++ 語言,最新的 1.0.9 版能在 Windows + Cygwin 下順利地編譯使用:)。需要注意的是:1) 下載 GCC 3.4.1 的源碼 gcc-3.4.1.tar.gz 放到 codeviz-1.0.9/compilers,2) 安裝 patch 程式(屬於Utils類),3) 從 http://www.graphviz.org 下載並安裝 Graphviz 2.6。

我用 CodeViz 分析《嵌入式即時作業系統 uC/OS-II (第二版)》中的第一個範常式序,步驟如下:

1. 想辦法讓 gcc 能編譯uC/OS 2.52和範常式序的源碼,每個C源檔案產生對於的.c.cdepn檔案。只要編譯(參數 -c)就行,無需串連。

2. 調用genfull產生full.graph,這個檔案記錄了所有函數在源碼中的位置和它們之間的調用關係。

3. 使用gengraph產生我關心的函數的調用關係。

首先分析main():

1. gengraph --output-type gif -f main
分析main()的call graph,得到的圖如下,看不出要領:

2. gengraph --output-type gif -f main -s OSInit
暫時不關心OSInit()的內部實現細節(參數 -s),讓它顯示為一個節點。得到的圖如下,有點亂,不過好多了:

3. gengraph --output-type gif -f main -s OSInit -i "OSCPUSaveSR;OSCPURestoreSR"
基本上每個函數都會有進入/退出臨界區的代碼,忽略之(參數 -i)。得到的圖如下,基本清楚了:

4. gengraph --output-type gif -f main -s "OSInit;OSSemCreate" -i "OSCPUSaveSR;OSCPURestoreSR" -k
OSSemCreate()的內部細節似乎也不用關心,不過保留中間檔案sub.graph(參數 -k),得到的圖如下,

5. dot -Tgif -o main.gif sub.graph
修改sub.graph,使圖形符合函數調用順序,最後得到的圖如下,有了這個都不用看代碼了:)

接著分析OSTimeDly()的被調用關係:

gengraph --output-type gif -r -f OSTimeDly

看看哪些函數調用了OSTimeDly(),參數 -r ,Task()和TaskStart()都是使用者編寫的函數:

最後看看Task()直接調用了哪些函數:

gengraph --output-type gif -d 1 -f Task

只看從Task出發的第一層調用(參數 -d 1):

在分析源碼的時候,把這些圖形列印在手邊,在上面做筆記,實在方便得很。

[轉] 使用CodeViz產生C/C++函數呼叫歷程圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.