写在前面
gdb 的更新好快啊…
之前的选项都有改动了, 比如 logging..
需要屏幕重定向不能简单设置:
set logging on
set logging redirect on
了, 而是要多开一个配置, 踩坑了
方法
在此之前先看一下我的 gdbinit 配置:
set debuginfod enabled off
set pagination off
分别用于设置自动下载 Debug 信息和禁止分页(否则显示完一页之后就结束了)
步骤
[arch@archlinux gdb_test]$ gdb /bin/ls
GNU gdb (GDB) 13.2
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /bin/ls...
(No debugging symbols found in /bin/ls)
(gdb) b *0x555555559fa0
Breakpoint 1 at 0x555555559fa0
(gdb) set logging enabled on
Copying output to gdb.txt.
Copying debug output to gdb.txt.
(gdb) set logging redirect on
warning: Currently logging to gdb.txt. Turn the logging off and on to make the new setting effective.
(gdb) set logging debugredirect on
warning: Currently logging to gdb.txt. Turn the logging off and on to make the new setting effective.
(gdb) set logging enabled off
Done logging to gdb.txt.
(gdb) set logging enabled on
Redirecting output to gdb.txt.
Redirecting debug output to gdb.txt.
(gdb) r
(gdb) display /i $pc
(gdb) while 1
>si
>end
这里的测试脚本参考 了二进制分析实战一书.
上面的脚本内容就是从 entry point 进入程序, 然后单指令执行并打印 pc 指针的值(display 为自动变量)
需要注意的是, 上面的:
b *0x555555559fa0
不是随便指定的, 而是通过 starti
和i files
查看的.
starti
是跳到第一个指令的位置并暂停的新命令, 主要用于strip 之后的程序的进入点查询(例如本次测试用到的/bin/ls
系统程序的调试)
核心步骤就是:
- set logging enabled on
- set logging redirect on
- set logging debugredirect on
- set logging enabled off # 用于刷新设置
- show logging # 查看当前的 logging 设置, 非必要
- set logging enabled on # 重新开启 logging 记录, 此时屏幕不会有任何输出
然后就可以通过 watch 监控或者 tail -f 监控来实现内容获取了, 这里还可以指定 logging 的文件名, 默认就是打开 gdb 的目录下的 gdb.txt 文件.