1. 程式人生 > >如何利用Callgraph生成函式呼叫圖?

如何利用Callgraph生成函式呼叫圖?

Ubuntu版本:ubuntu-gnome-16.04-desktop-amd64,gnome版-----------------------------------------------------------------------1. 安裝 CallgraphCallgraph 實際由三個工具組合而成。
  • 一個是用於生成 C 函式呼叫樹的 cflow 或者 calltree,下文主要介紹 cflow。
  • 一個處理 dot 文字圖形語言的工具,由 graphviz 提升。
  • 一個用於把 C 函式呼叫樹轉換為 dot 格式的指令碼:tree2dotx
以 Ubuntu 為例,分別安裝它們:
 sudo apt-get install cflow graphviz
接下來安裝 tree2dotx 和 Callgraph,這裡都預設安裝到 /usr/local/bin。
$ wget -c https://github.com/tinyclub/linux-0.11-lab/raw/master/tools/tree2dotx
$ wget -c https://github.com/tinyclub/linux-0.11-lab/raw/master/tools/callgraph
$ sudo cp tree2dotx callgraph /usr/local/bin
$ sudo chmod +x /usr/local/bin/{tree2dotx,callgraph}
2. 使用2.1 生成呼叫圖 callgraph -f main -d ./file.c -b firefox
指定分析file.c檔案中的main函式,並使用firefox顯示圖片,也可以使用其他瀏覽器。生成的函式呼叫關係圖預設儲存為main.file_c.svg。2.2 其他用法1)類似 main 函式,實際也可渲染其他函式,例如: callgraph -f test1 -d ./file.c -b firefox2)指定函式所在檔案(或者指定函式搜尋路徑)使用 -d 選項3)砍掉不感興趣的函式分支callgraph -f main -d file.c -F printf -b firefox同時指定多個函式分支:callgraph -f main -d file.c -F "printf test3 test2" -b firefox
4)指定函式呼叫深度:用 -D 命令可以指定:callgraph -f main -d file.c -D 2 -b firefox
3. 程式碼
#include<stdio.h>

void test1();
void test2();
void test3();


void test1()
{
	printf("hello");
}

void test2()
{
	test3();
}

void test3()
{

}

void main()
{
	test1();
	test2();
	test3();
	printf("hello.\n");
}