(gdb) break 10 Breakpoint 1 at 0x5555555552d5: file open_file.cpp, line 10.
设置好断点后启动程序,会停在断点位置:
1 2 3 4 5
(gdb) run Starting program: /home/wyk/straid/code/open_file 4 5 6
Breakpoint 1, main (argc=4, argv=0x7fffffffe178) at open_file.cpp:10 10 cout << "argc = " << argc << endl;
(2) 根据函数名设置断点 break <funcname>
1 2
(gdb) break main Breakpoint 2 at 0x5555555552a9: file open_file.cpp, line 8.
(3) 执行非当前源文件的某行或某函数时停止执行(为非当前源文件设置断点)
1 2 3
(gdb) break filename:linenum # or (gdb) break filename:funcname
(4) 根据条件停止执行程序
1 2 3
(gdb) break linenum ifexpr # or (gdb) break funcname ifexpr
⭐清除断点:
clear <source-line>:清除源文件某一行的所有断点
delete <breakpoint-id>:删除 info b 中对应 ID 的断点
1 2 3 4 5 6 7 8 9 10 11 12 13 14
(gdb) info b Num Type Disp Enb Address What 1 breakpoint keep y 0x00005555555553ad in main(int, char**) at open_file.cpp:17 2 breakpoint keep y 0x00005555555553e3 in main(int, char**) at open_file.cpp:19 3 breakpoint keep y 0x00005555555552d5 in main(int, char**) at open_file.cpp:10 (gdb) clear 17 # 清除源文件 line 17 位置的断点 (gdb) info b Num Type Disp Enb Address What 2 breakpoint keep y 0x00005555555553e3 in main(int, char**) at open_file.cpp:19 3 breakpoint keep y 0x00005555555552d5 in main(int, char**) at open_file.cpp:10 (gdb) delete 2 # 清除 Num=2 的断点 (gdb) info b Num Type Disp Enb Address What 3 breakpoint keep y 0x00005555555552d5 in main(int, char**) at open_file.cpp:10
# 不生效 (gdb) show args | wc -l Argument list to give program being debugged when it is started is "1 2 3". # 使用 pipe 即可打通 gdb 与 shell 之间的传输 (gdb) pipe show args | wc -l 1 (gdb) pipe p argv | wc -l 1 # | 原本用于 shell 与 shell 之间的命令传输 (gdb) !ls -al | wc -l 17
8. s 命令 == step「单步进入」
1 2 3 4
(gdb) help s Step program until it reaches a different source line. Usage: step [N] Argument N means step N times (or till program stops for another reason).
9. finish 命令「单步跳出」
1 2 3 4
(gdb) help finish Execute until selected stack frame returns. Usage: finish Upon return, the value returned is printed and put in the value history.
10. n 命令 == next「单步跳过」
1 2 3 4 5 6
(gdb) help n Step program, proceeding through subroutine calls. Usage: next [N] Unlike "step", if the current source line calls a subroutine, this command does not enter the subroutine, but instead steps over the call, in effect treating it as a single source line.
11. c 命令 == continue「跳到下一个断点」
1 2 3 4 5 6 7 8 9 10 11
(gdb) help c Continue program being debugged, after signal or breakpoint. Usage: continue [N] If proceeding from breakpoint, a number N may be used as an argument, which means to set the ignore count of that breakpoint to N - 1 (so that the breakpoint won't break until the Nth time it is reached).
If non-stop mode is enabled, continue only the current thread, otherwise all the threads in the program are continued. To continue all stopped threads in non-stop mode, use the -a option. Specifying -a and an ignore count simultaneously is an error.
12. return n 命令直接跳过当前函数后面的语句并直接返回 n,该 n 值是自定义的返回值
1 2 3 4 5 6 7 8 9 10
(gdb) return 6 Make main(int, char**) return now? (y or n) y #0 __libc_start_main (main=0x5555555552a9 <main(int, char**)>, argc=4, argv=0x7fffffffe178, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe168) at ../csu/libc-start.c:342 342 ../csu/libc-start.c: No such file or directory. (gdb) n [Inferior 1 (process 148790) exited with code 06] (gdb) n The program is not being run.
对于缩写无歧义的 gdb 命令,通常可以截断使用;如果有些以相同字母开头可能造成歧义的命令,可以使用 help 命令来判别该缩写命令是属于哪一条具体的命令,比如 help s
直接按下「回车」会重复上一步命令,但是对于某些可能带来麻烦的命令不会生效,比如 run;对于 list 和 x 命令,按下回车会构造新的参数来重复命令,这样方便扫描资源和内存(连续按下 list 会往下不断展示 10 行代码) ,ctrl+o 同 Enter
# 表示注释,同 shell 脚本
gdb 使用 Tab 按钮也可「补全命令」
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 盘点一些 gdb 系统命令 [Useful] (gdb) help (gdb) help <command> (gdb) complete <alphabet> # 列出以 alphabet 开头的所有命令, 比如 complete sh: sharedlibrary shell show (gdb) show (gdb) info (gdb) set # Here are several miscellaneous show subcommands, all of which are exceptional in lacking corresponding set commands: (gdb) show version (gdb) show copying (gdb) info copying (gdb) show warranty (gdb) info warranty (gdb) show configuration