1、编译安装
下载地址: https://pan.baidu.com/s/15LMzyjoEstpi4rgwTZCoxw(提取码3exq)
安装步骤:
# 如果系统是x86架构,则需要进行以下规则的配置
cp RULES/i686-linux-cc.rul RULES/x86_64-linux-cc.rul
# 由于项目中的configure已经弃用,推荐直接使用make,但是make之前先配置项目中函数名与gcc函数名冲突问题
find . -name "*.[c|h]" |xargs sed -i -e "s/fexecve/fexecve_calltree/"
find . -name "*.[c|h]" |xargs sed -i -e "s/getline/getline_calltree/"
make
# 这里系统是x86架构,所以拷贝的是x86_64-linux-cc目录下的程序(或建立链接文件也可以)
sudo cp calltree/OBJ/x86_64-linux-cc/calltree /usr/bin/calltree
2、使用方法
通过calltree --help
命令查看使用帮助信息,主要有以下几个常用选项:
- -b:在每个制表位处打印垂直条;
- -g:输出函数所在文件的目录;
- -m:只分析main函数调用关系;
- -p:使用c预处理(默认),缺点就是容易产生多余的信息;
- -np:不使用c预处理;
- -xvcg:导出供xvcg使用的格式;
- -dot:导出供graphviz使用的格式;
- depth=#:设置最大打印深度;
- list=name:仅为函数name生成调用图;
- listfile=file:只列出在file中找到的函数;
- igorefile=file:不列出在file中找到的函数。
示例
/* file: test.c */
#include <stdio.h>
void func_5(void){
printf("hello!\n");
}
void func_4(void){
func_5();
}
void func_3(void){
func_4();
}
void func_2(void){
func_3();
}
void func_1(void){
func_2();
}
int main()
{
func_1();
func_4();
return 0;
}
使用情景1:直接查看函数调用(当然也可以重定位到文件)
book@book-VirtualBox:~$ calltree -np -g -b depth=10 list=main test.c
main [test.c:26]:
| func_1 [test.c:21]
| | func_2 [test.c:17]
| | | func_3 [test.c:13]
| | | | func_4 [test.c:9]
| | | | | func_5 [test.c:5]
| | | | | | printf
| func_4 [test.c:9]
| | func_5 [test.c:5]
| | | printf
使用情景2:搭配dot命令生成调用图,如果没有dot命令可执行“sudo apt-get install graphviz”进行安装
book@book-VirtualBox:~$ calltree -np -g -b depth=10 list=main test.c -dot > test.dot
book@book-VirtualBox:~$ dot -T png test.dot -o test.png
参考文章