gdb调试

文章介绍了如何使用GDB进行批处理调试,包括启动选项如-quiet,-batch,-ex等,并展示了在无符号表情况下查看结构体数据和反汇编代码的方法。同时,文章提出了一个调试问题,即在设备上缺少符号表时,调用返回类型为longlong的函数会返回0,而在服务器上没有此问题,可能是由于GDB版本差异导致。
摘要由CSDN通过智能技术生成
1. 官网

https://www.sourceware.org/gdb/documentation/
参数:

  • -quiet -silent -q
    “Quiet”. Do not print the introductory and copyright messages. These messages are also suppressed in batch mode.
    This can also be enabled using set startup-quietly on. The default is
    off. Use show startup-quietly to see the current setting. Place set
    startup-quietly on into your early initialization file (see Section 2.1.4
    [Initialization Files], page 17) to have future gdb sessions startup quietly.

  • -batch
    Run in batch mode. Exit with status 0 after processing all the command files
    specified with ‘-x’ (and all commands from initialization files, if not inhibited
    with ‘-n’). Exit with nonzero status if an error occurs in executing the gdb
    commands in the command files. Batch mode also disables pagination, sets unlimited terminal width and height see Section 22.4 [Screen Size], page 360, and acts as if set confirm off were in effect (see Section 22.9 [Messages/Warnings], page 371).
    Batch mode may be useful for running gdb as a filter, for example to download
    and run a program on another computer; in order to make this more useful, the message Program exited normally.
    (which is ordinarily issued whenever a program running under gdb control
    terminates) is not issued when running in batch mode.

  • -ex command
    Execute a single gdb command.
    This option may be used multiple times to call multiple commands. It may also
    be interleaved with ‘-command’ as required.

gdb -ex ’target sim’ -ex ’load’ \
-x setbreakpoints -ex ’run’ a.out
2. 不阻塞调试
gdb -quiet -batch -ex='call func()' -p `pidof xxx`
3. x命令

在没有符号表的情况下,查看结构体的数据

gdb -quiet -batch -ex='x/128db &stdata' -p `pidof xxx`

d:十进制
b:字节
以十进制打印128个字节

4. 反汇编

使用场景示例:想获取或修改静态变量的值,但是没有相关接口gdb调用的时候,可以反汇编相关接口看看能不能算出静态变量的地址,然后进行操作。不过计算不对的话会有风险。

gdb -quiet -batch -ex='disassemble functionxxx' -p `pidof processxxx`
问题
1. gdb调用返回类型为long long的函数

现象:在设备上调试,有符号表时,返回值正确;没符号表时,始终返回0。
定位:在服务器上写了demo测试没这个问题,具体原因未知,可能设备与服务器上的gdb版本不一致。

shell# ls
test_proc.sym
shell# gdb -quiet -batch -ex='call (long long)test_func()' -p `pidof test_proc`
$1 = 11083
shell# gdb -quiet -batch -ex='call (long long)test_func()' -p `pidof test_proc`
$1 = 11181

shell# mv test_proc.sym test_proc.symbak
shell# gdb -quiet -batch -ex='call (long long)test_func()' -p `pidof test_proc`
$1 = 0
shell# gdb -quiet -batch -ex='call (long long)test_func()' -p `pidof test_proc`
$1 = 0

待续…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值