如何以批处理模式触发gdb断点命令?

时间:2018-05-28 12:38:04

标签: debugging scripting automation gdb non-interactive

我试图以非交互方式使用gdb - 即在每个指定的断点上打印一些内容。

$ cat script.gdb
set pagination off
catch syscall
commands
bt
c
end
# It's a hack to get backtraces only on call not on return - Linux and x86_64 specific.
# For sake of this question it can be removed, it will just output more.
condition $bpnum $rax == -38

问题 - 使用批处理模式的非交互式尝试:

$ gdb /bin/true -batch -x script.gdb
Catchpoint 1 (any syscall)
Catchpoint 2 (syscall 'fork' [57])

Catchpoint 1 (call to syscall brk), 0x00007ffff7df2f9c in brk () from /lib64/ld-linux-x86-64.so.2

但是,如果我以交互方式运行它:

$ gdb /bin/true -x script.gdb
GNU gdb (GDB) 8.1

...

Reading symbols from /bin/true...
(no debugging symbols found)...done.
Catchpoint 1 (any syscall)
Catchpoint 2 (syscall 'fork' [57])
(gdb) run
Starting program: /usr/bin/true

Catchpoint 1 (call to syscall brk), 0x00007ffff7df2f9c in brk () from /lib64/ld-linux-x86-64.so.2
#0  0x00007ffff7df2f9c in brk () from /lib64/ld-linux-x86-64.so.2
#1  0x00007ffff7df2488 in _dl_sysdep_start () from /lib64/ld-linux-x86-64.so.2
#2  0x00007ffff7ddcbc1 in _dl_start () from /lib64/ld-linux-x86-64.so.2
#3  0x00007ffff7ddc178 in _start () from /lib64/ld-linux-x86-64.so.2
#4  0x0000000000000001 in ?? ()
#5  0x00007fffffffe2a5 in ?? ()
#6  0x0000000000000000 in ?? ()

...

Catchpoint 1 (call to syscall exit_group), 0x00007ffff7ad7529 in _exit () from /lib64/libc.so.6
#0  0x00007ffff7ad7529 in _exit () from /lib64/libc.so.6
#1  0x00007ffff7a50a2b in __run_exit_handlers () from /lib64/libc.so.6
#2  0x00007ffff7a50ab5 in exit () from /lib64/libc.so.6
#3  0x000000000040132d in ?? ()
#4  0x00007ffff7a39c05 in __libc_start_main () from /lib64/libc.so.6
#5  0x0000000000401406 in ?? ()
[Inferior 1 (process 35434) exited normally]
(gdb) quit

然后按预期工作 - 执行断点命令。

向非交互式命令添加其他-ex c只会添加另一行Catchpoint 1 ...,但仍然没有回溯。

手册页和Debugging with GDB无效。

我想必须有一些命令可以等到它结束,或者可能是pagination之类的设置。

我知道我可以使用类似于expect脚本的东西,但我想避免它。这样的事情有效:

echo 'run
quit' | gdb /bin/true -x script.gdb

1 个答案:

答案 0 :(得分:0)

在脚本末尾添加run会产生您想要的结果(至少在使用GNU gdb (GDB) Fedora 8.1-15.fc28时会这样做。)

  

这样的工作:

     回声'跑'   退出'| gdb / bin / true -x script.gdb

它有效还是想要它起作用?

quit部分是多余的 - 一旦到达脚本末尾,GDB 退出。并且echo run部分不起作用,因为在批处理模式下,GDB不会读取其标准输入。