Linux调试:calltree安装与使用(一个用于静态分析函数调用关系的工具)

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

在这里插入图片描述


参考文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

R-QWERT

你的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值