调试命令
start 只运一行 (gdb) start Temporary breakpoint 7 at 0x92e: file helloGDB.cpp, line 8. Starting program: /home/arvin/gdb/helloGDB Temporary breakpoint 7, main () at helloGDB.cpp:8 8 cout << "Hello GDB\n"; run 一直运行到断点处停下 (gdb) b 10 Breakpoint 1 at 0x954: file helloGDB.cpp, line 10. (gdb) i b Num Type Disp Enb Address What 1 breakpoint keep y 0x0000000000000954 in main() at helloGDB.cpp:10 (gdb) run Starting program: /home/arvin/gdb/helloGDB Hello GDB What is GDB? Breakpoint 1, main () at helloGDB.cpp:10 10 cout << "The GNU Project Debugger\n"; 从断点的位置继续运行到下一个断点的位置 c 等于continue (gdb) b 14 Breakpoint 2 at 0x5555555549a0: file helloGDB.cpp, line 14. (gdb) i b Num Type Disp Enb Address What 1 breakpoint keep y 0x0000555555554954 in main() at helloGDB.cpp:10 breakpoint already hit 1 time 2 breakpoint keep y 0x00005555555549a0 in main() at helloGDB.cpp:14 (gdb) c Continuing. The GNU Project Debugger Hello GDB What is GDB? The GNU Project Debugger Breakpoint 2, main () at helloGDB.cpp:14 14 cout << "Hello GDB\n";打印变量
p 等于print print 变量名 Breakpoint 1, printNumber (size=10) at print.cpp:5 5 for (int i = 0; i < size; ++i) { (gdb) print i $1 = 0打印变量类型
ptype 变量名 Breakpoint 1, printNumber (size=10) at print.cpp:5 5 for (int i = 0; i < size; ++i) { (gdb) ptype i type = int单步调试
进入函数 s 等于 step 向下执行一行, 遇到函数, 进入函数 (gdb) step The GNU Project Debugger 17 printNumber(10); (gdb) step printNumber (size=10) at print.cpp:5 5 for (int i = 0; i < size; ++i) { 不进入函数 n 等于 next 向下执行一行, 遇到函数, 不进入函数 Breakpoint 1, main () at helloGDB.cpp:16 16 cout << "The GNU Project Debugger\n"; (gdb) n The GNU Project Debugger 17 printNumber(10); (gdb) n The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 The number is 6 The number is 7 The number is 8 The number is 9 18 cout << "The GNU Project Debugger\n"; 跳出函数 要跳出的函数中不能有断点, 如果有要删除或者设置为无效的断点 finish Breakpoint 1, main () at helloGDB.cpp:16 16 cout << "The GNU Project Debugger\n"; (gdb) step The GNU Project Debugger 17 printNumber(10); (gdb) step printNumber (size=10) at print.cpp:5 5 for (int i = 0; i < size; ++i) { (gdb) step 6 cout << "The number is " << i << endl; (gdb) finish Run till exit from #0 printNumber (size=10) at print.cpp:6 The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 The number is 6 The number is 7 The number is 8 The number is 9 main () at helloGDB.cpp:18 18 cout << "The GNU Project Debugger\n"; (gdb) n The GNU Project Debugger 19 cout << "Hello GDB\n";变量自动显示
自动打印指定变量的值 (gdb) display i 1: i = 0 查看自动打印哪些变量的值 i 等于 info (gdb) i display Auto-display expressions now in effect: Num Enb Expression 1: y i (gdb) n 6 cout << "The number is " << i << endl; 1: i = 0 (gdb) n The number is 0 5 for (int i = 0; i < size; ++i) { 1: i = 0 取消自动打印 undisplay 编号 (gdb) undisplay 1 (gdb) n 6 cout << "The number is " << i << endl;设置变量等于某一个值
set var 变量名=变量值 (gdb) step The number is 1 5 for (int i = 0; i < size; ++i) { (gdb) set var i = 8 (gdb) n 6 cout << "The number is " << i << endl; (gdb) n The number is 9 5 for (int i = 0; i < size; ++i) {跳出循环
要跳出的循环代码块中不能有断点, 如果有要删除或者设置为无效的断点 until The number is 2 5 for (int i = 0; i < size; ++i) { (gdb) until The number is 3 The number is 4 The number is 5 The number is 6 The number is 7 The number is 8 The number is 9 8 } 有断点时: (gdb) until The number is 1 5 for (int i = 0; i < size; ++i) {VSCode 真香