gdb 输出指定断点堆栈到文件再进行分析

背景

  • 需要统计某个c函数哪些地方被调用。
  • 暂时没有想到其他方法,就通过gdb的方式进行测试调试。

解决方案

  • gdb输出堆栈相关指令存入文件,以-x的形式传递,然后输出重定向到文件

代码

void show(int a) {
    if (a) {
        show(a-1);
    }
}
int main() {
    show(10);
}

gdb指令

break show
commands 1
silent
bt
c
end
r
q

gdb执行并重定向输出

gdb -q -x args.txt --args ./a.out 2&> bt.log

输出结果

Reading symbols from ./a.out...done.
Breakpoint 1 at 0x4004dd: file test.cpp, line 2.
#0  show (a=10) at test.cpp:2
#1  0x0000000000400501 in main () at test.cpp:7
#0  show (a=9) at test.cpp:2
#1  0x00000000004004f0 in show (a=10) at test.cpp:3
#2  0x0000000000400501 in main () at test.cpp:7
#0  show (a=8) at test.cpp:2
#1  0x00000000004004f0 in show (a=9) at test.cpp:3
#2  0x00000000004004f0 in show (a=10) at test.cpp:3
#3  0x0000000000400501 in main () at test.cpp:7
#0  show (a=7) at test.cpp:2
#1  0x00000000004004f0 in show (a=8) at test.cpp:3
#2  0x00000000004004f0 in show (a=9) at test.cpp:3
#3  0x00000000004004f0 in show (a=10) at test.cpp:3
#4  0x0000000000400501 in main () at test.cpp:7
#0  show (a=6) at test.cpp:2
#1  0x00000000004004f0 in show (a=7) at test.cpp:3
#2  0x00000000004004f0 in show (a=8) at test.cpp:3
#3  0x00000000004004f0 in show (a=9) at test.cpp:3
#4  0x00000000004004f0 in show (a=10) at test.cpp:3
#5  0x0000000000400501 in main () at test.cpp:7
#0  show (a=5) at test.cpp:2
#1  0x00000000004004f0 in show (a=6) at test.cpp:3
#2  0x00000000004004f0 in show (a=7) at test.cpp:3
#3  0x00000000004004f0 in show (a=8) at test.cpp:3
#4  0x00000000004004f0 in show (a=9) at test.cpp:3
#5  0x00000000004004f0 in show (a=10) at test.cpp:3
#6  0x0000000000400501 in main () at test.cpp:7
#0  show (a=4) at test.cpp:2
#1  0x00000000004004f0 in show (a=5) at test.cpp:3
#2  0x00000000004004f0 in show (a=6) at test.cpp:3
#3  0x00000000004004f0 in show (a=7) at test.cpp:3
#4  0x00000000004004f0 in show (a=8) at test.cpp:3
#5  0x00000000004004f0 in show (a=9) at test.cpp:3
#6  0x00000000004004f0 in show (a=10) at test.cpp:3
#7  0x0000000000400501 in main () at test.cpp:7
#0  show (a=3) at test.cpp:2
#1  0x00000000004004f0 in show (a=4) at test.cpp:3
#2  0x00000000004004f0 in show (a=5) at test.cpp:3
#3  0x00000000004004f0 in show (a=6) at test.cpp:3
#4  0x00000000004004f0 in show (a=7) at test.cpp:3
#5  0x00000000004004f0 in show (a=8) at test.cpp:3
#6  0x00000000004004f0 in show (a=9) at test.cpp:3
#7  0x00000000004004f0 in show (a=10) at test.cpp:3
#8  0x0000000000400501 in main () at test.cpp:7
#0  show (a=2) at test.cpp:2
#1  0x00000000004004f0 in show (a=3) at test.cpp:3
#2  0x00000000004004f0 in show (a=4) at test.cpp:3
#3  0x00000000004004f0 in show (a=5) at test.cpp:3
#4  0x00000000004004f0 in show (a=6) at test.cpp:3
#5  0x00000000004004f0 in show (a=7) at test.cpp:3
#6  0x00000000004004f0 in show (a=8) at test.cpp:3
#7  0x00000000004004f0 in show (a=9) at test.cpp:3
#8  0x00000000004004f0 in show (a=10) at test.cpp:3
#9  0x0000000000400501 in main () at test.cpp:7
#0  show (a=1) at test.cpp:2
#1  0x00000000004004f0 in show (a=2) at test.cpp:3
#2  0x00000000004004f0 in show (a=3) at test.cpp:3
#3  0x00000000004004f0 in show (a=4) at test.cpp:3
#4  0x00000000004004f0 in show (a=5) at test.cpp:3
#5  0x00000000004004f0 in show (a=6) at test.cpp:3
#6  0x00000000004004f0 in show (a=7) at test.cpp:3
#7  0x00000000004004f0 in show (a=8) at test.cpp:3
#8  0x00000000004004f0 in show (a=9) at test.cpp:3
#9  0x00000000004004f0 in show (a=10) at test.cpp:3
#10 0x0000000000400501 in main () at test.cpp:7
#0  show (a=0) at test.cpp:2
#1  0x00000000004004f0 in show (a=1) at test.cpp:3
#2  0x00000000004004f0 in show (a=2) at test.cpp:3
#3  0x00000000004004f0 in show (a=3) at test.cpp:3
#4  0x00000000004004f0 in show (a=4) at test.cpp:3
#5  0x00000000004004f0 in show (a=5) at test.cpp:3
#6  0x00000000004004f0 in show (a=6) at test.cpp:3
#7  0x00000000004004f0 in show (a=7) at test.cpp:3
#8  0x00000000004004f0 in show (a=8) at test.cpp:3
#9  0x00000000004004f0 in show (a=9) at test.cpp:3
#10 0x00000000004004f0 in show (a=10) at test.cpp:3
#11 0x0000000000400501 in main () at test.cpp:7
[Inferior 1 (process 10967) exited normally]

拓展

  • 格式问题可以设置.
  • 下面的指令是堆栈不限制长度,不限制宽度。
set height unlimited
set width unlimited

对比 set logging

  • set logging是将交互式的输出到文件,有些指令的输出结果不会输出。
  • 所以有一定的局限性。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值