使用gdb调试大型C++程序的时候,经常遇到的场景是反复调试某个bin/out,简单修改再编译调试,因此每次进入gdb后重复执行某些操作很麻烦(如打多个断点),这时就可以用gdb命令脚本了,它能够很好的完成这些工作。
以设置多个断点为例,我写的命令脚本为
[dev@szvphicprd90255 linux_output]$ cat ~/.gdbinit
#filename: .gdbinit
#gdb will read it when starting
file testrunner
b main
b L2xxMemxxstub.cpp:220
b L2memoub2.cpp:213
b l2xxhrinit.c:147
r
.gdbinit文件位于何处,如何使用,配置
创建一个名为.gdbinit的新文件。现在每次gdb启动它都将执行此文件中的命令。
可以将.gdbinit放入调试程序所在文件夹中,这样可以更适用于调试不同的应用程序。GDB启动将自动加载./.gdbinit以进行当前调试。
也可以放在用户主目录中 ~/.gdbinit,使其配置对所有调试程序生效
但使用过程中一定要注意在 .gdbinit中要指明调试的文件 filename,否则后续命令不知道是打给谁的。
For help, type “help”.
Type “apropos word” to search for commands related to “word”…
No symbol table is loaded. Use the “file” command.
Make breakpoint pending on future shared library load? (y or [n]) [answered N; input not from terminal]
No symbol table is loaded. Use the “file” command.
Make breakpoint pending on future shared library load? (y or [n]) [answered N; input not from terminal]
No symbol table is loaded. Use the “file” command.
Make breakpoint pending on future shared library load? (y or [n]) [answered N; input not from terminal]
No symbol table is loaded. Use the “file” command.
Make breakpoint pending on future shared library load? (y or [n]) [answered N; input not from terminal]
Reading symbols from ./testrunner…
(gdb) info b
No breakpoints or watchpoints.
(gdb) q
[dev@szvph
添加filename后,gdbinit符合预期
For help, type “help”.
Type “apropos word” to search for commands related to “word”.
Breakpoint 1 at 0x41b30c: file …/l2_pcst/testrunner/base/main.cpp, line 30.
Breakpoint 2 at 0x1403be8: file …stub2.cpp, line 220.
Breakpoint 3 at 0x1403bb0: file …stub1.cpp, line 213.
Breakpoint 4 at 0xecb618: file xxxnit.c, line 147.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000041b30c in main(int, char**) at …/l2_pcst/testrunner/base/main.cpp:30
2 breakpoint keep y 0x0000000001403be8 in sssMemllocAlign
at …/l2_tssxx/lx2ixnf_stub/l2oxs/L2oxsemorStub.cpp:220
3 breakpoint keep y 0x0000000001403bb0 in xxxAllocAlign(
at …/xxxxmemorystub.cpp:213
4 breakpoint keep y 0x0000000000ecb618 in L2AllocChrTagMem at xxx_init.c:147
(gdb)
有两种方式来使用这个脚本:
1)启动gdb时候
gdb在启动的时候,会在当前目录下查找".gdbinit"这个文件,并把它的内容作为gdb命令进行解释,所以如果我把脚本命名为".gdbinit",这样在启动的时候就会处理这些命令。
2)gdb运行期间
可以使用 source script-file 来解释gdb命令脚本script-file
常用 .gdbInit 文件参考:
https://github.com/gdbinit/Gdbinit/blob/master/gdbinit
部分ref:https://blog.csdn.net/benbendy1984/article/details/6105757