~/.gdbinit
在处理命令行中的目标文件之前运行。
虽然它需要一些设置,但将断点保存为可以自动加载的 gdb 脚本可能是最简洁的方法。
首先,创建一个目录来保存脚本,并将适当的行添加到~/.gdbinit
.
~/devel$ mkdir ~/gdbscripts
~/devel$ cat ~/.gdbinit
add-auto-load-safe-path /home/mp/gdbscripts
add-auto-load-scripts-directory /home/mp/gdbscripts
set debug auto-load on
一切正常后,我们将删除最后一行。
现在让我们看看 gdb 在哪里寻找它的脚本文件。
~/devel$ gdb -q sigw
Reading symbols from sigw...done.
auto-load: Attempted file "/home/mp/devel/sigw-gdb.gdb" does not exist.
auto-load: Expanded $-variables to "/usr/lib/debug:/usr/share/gdb/auto-load:/home/mp/gdbscripts".
auto-load: Searching 'set auto-load scripts-directory' path "$debugdir:$datadir/auto-load:/home/mp/gdbscripts".
auto-load: Attempted file "/usr/lib/debug/home/mp/devel/sigw-gdb.gdb" does not exist.
auto-load: Attempted file "/usr/share/gdb/auto-load/home/mp/devel/sigw-gdb.gdb" does not exist.
auto-load: Attempted file "/home/mp/gdbscripts/home/mp/devel/sigw-gdb.gdb" does not exist.
auto-load: Attempted file "/home/mp/devel/sigw-gdb.py" does not exist.
auto-load: Expanded $-variables to "/usr/lib/debug:/usr/share/gdb/auto-load:/home/mp/gdbscripts".
auto-load: Searching 'set auto-load scripts-directory' path "$debugdir:$datadir/auto-load:/home/mp/gdbscripts".
auto-load: Attempted file "/usr/lib/debug/home/mp/devel/sigw-gdb.py" does not exist.
auto-load: Attempted file "/usr/share/gdb/auto-load/home/mp/devel/sigw-gdb.py" does not exist.
auto-load: Attempted file "/home/mp/gdbscripts/home/mp/devel/sigw-gdb.py" does not exist.
那里有几个不错的候选者,其中一些只能由 root 写入,其中一些需要添加到安全路径中。让我们使用~/gdbscripts/$PWD/sigw-gdb.gdb
.
(gdb) shell mkdir -p ~/gdbscripts/$PWD
(gdb) b main
Breakpoint 1 at 0x84f: file sigw.c, line 15.
(gdb) save breakpoints ~/gdbscripts/$PWD/sigw-gdb.gdb
Unable to open file '/home/mp/gdbscripts/$PWD/sigw-gdb.gdb' for saving (No such file or directory)
看起来save
命令扩展~
但不扩展环境变量。我们将不得不使用完整路径。
(gdb) save breakpoints ~/gdbscripts/home/mp/devel/sigw-gdb.gdb
Saved to file '/home/mp/gdbscripts/home/mp/devel/sigw-gdb.gdb'.
测试一下。
(gdb) quit
~/devel$ gdb -q sigw
Reading symbols from sigw...done.
...
auto-load: Matching file "/home/mp/gdbscripts/home/mp/devel/sigw-gdb.gdb" to pattern "/home/mp/gdbscripts"
auto-load: Matched - file "/home/mp/gdbscripts" to pattern "/home/mp/gdbscripts".
auto-load: File "/home/mp/gdbscripts/home/mp/devel/sigw-gdb.gdb" matches directory "/home/mp/gdbscripts".
Breakpoint 1 at 0x84f: file sigw.c, line 15.
...
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000000084f in main at sigw.c:15