How to use callgrafh to generate function call graph?

Ubuntu version book: ubuntu-gnome-16.04-desktop-amd64, gnome version
-----------------------------------------------------------------------

1. Install Callgraph
Callgraph is actually a combination of three tools.
  • One is cflow or calltree used to generate C function call tree, and the following mainly introduces cflow.
  • A tool for dealing with the dot text graphics language, enhanced by graphviz.
  • A script for converting C function call trees to dot format: tree2dotx
Using Ubuntu as an example, install them separately:
$ sudo apt-get install cflow graphviz
Next install tree2dotx and Callgraph, which are installed to /usr/local/bin by default .
$ 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. Use
2.1 Generate call graph
callgraph -f main -d ./file.c -b firefox
Specify to analyze the main function in the file.c file, and use firefox to display the picture, or you can use other browsers.
The generated function call diagram is saved as main.file_c.svg by default.

2.2 Other usage
1)类似 main 函数,实际也可渲染其他函数,例如:
callgraph -f test1 -d ./file.c -b firefox
2)指定函数所在文件(或者指定函数搜索路径)
使用 -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");
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325429422&siteId=291194637