1. 启动程序
启动程序的命令为 run
或者 r
,一般用于调试一个程序。r
命令只在使用 gdb
启动被调试的程序时执行一次。比如使用 gdb
来启动 demo
程序,Shell
命令为:
$ gdb demo
然后进入 gdb
的调试窗口,这时程序被暂停,可以执行设置启动参数、设置断点等操作。然后在 gdb
中输入 run
启动程序,直到遇到第一个命中的断点为止,程序才会中断。
(gdb) info b
No breakpoints or watchpoints.
(gdb) b fun_test
Breakpoint 1 at 0x969: file demo.cpp, line 6.
(gdb) r
Starting program: /home/wohu/cppProject/book_debug/chapter_3.1/demo
ret is 8
Breakpoint 1, fun_test (a=10, str=0x555555554b21 "test") at demo.cpp:6
6 printf("a is %d, str is %s\n", a, str);
(gdb)
2. 继续运行
继续运行可以使用命令 continue
或者 c
。当程序处于中断状态时,比如已经命中断点,则可以执行continue
命令恢复或者继续运行程序,直到遇到下一个断点为止
(gdb) c
Continuing.
a is 10, str is test
Breakpoint 1, fun_test (a=10, str=0x555555554b21 "test") at demo.cpp:6
6 printf("a is %d, str is %s\n", a, str);
(gdb)
3. 继续运行并跳过当前断点 N 次
在使用 continue
命令时,还可以设置跳过当前断点的命中次数。语法如下:
continue 次数
先在 fun_test
中设置函数断点。从前面的代码中可以发现,main
函数会循环调用 fun_test
函数 10次,当第一次在 fun_test
中暂停时,如果想忽略接下来的 8 次命中(包括当前这一次),则可以使用命令:
continue 8
那么,继续执行时会忽略接下来的 7 次断点命中,在第 9 次命中的时候暂停
(gdb) r
Starting program: /home/wohu/cppProject/book_debug/chapter_3.1/demo
ret is 8
Breakpoint 1, fun_test (a=10, str=0x555555554b21 "test") at demo.cpp:6
6 printf("a is %d, str is %s\n", a, str);
(gdb) c 8
Will ignore next 7 crossings of breakpoint 1. Continuing.
a is 10, str is test
a is 10, str is test
a is 10, str is test
a is 10, str is test
a is 10, str is test
a is 10, str is test
a is 10, str is test
a is 10, str is test
Breakpoint 1, fun_test (a=10, str=0x555555554b21 "test") at demo.cpp:6
6 printf("a is %d, str is %s\n", a, str);
(gdb) c
Continuing.
a is 10, str is test
Breakpoint 1, fun_test (a=10, str=0x555555554b21 "test") at demo.cpp:6
6 printf("a is %d, str is %s\n", a, str);
(gdb) c
Continuing.
a is 10, str is test
[Inferior 1 (process 9633) exited normally]
(gdb)
4. 继续运行直到当前函数执行完成
如果在一个比较长的函数中设置了断点,当命中函数断点时,我们可能不想逐步执行代码,而是跳过部分代码的调试过程,直接回到调用函数的位置,此时可以使用这个功能。语法如下:
finish
示例如下:
(gdb) b fun_test
Breakpoint 1 at 0x969: file demo.cpp, line 6.
(gdb) r
Starting program: /home/wohu/cppProject/book_debug/chapter_3.1/demo
ret is 8
Breakpoint 1, fun_test (a=10, str=0x555555554b21 "test") at demo.cpp:6
6 printf("a is %d, str is %s\n", a, str);
(gdb) finish
Run till exit from #0 fun_test (a=10, str=0x555555554b21 "test") at demo.cpp:6
a is 10, str is test
main (argc=1, argv=0x7fffffffdea8) at demo.cpp:18
18 for (int i = 0; i < 10; i++)
(gdb) c
Continuing.
Breakpoint 1, fun_test (a=10, str=0x555555554b21 "test") at demo.cpp:6
6 printf("a is %d, str is %s\n", a, str);
(gdb) finish
Run till exit from #0 fun_test (a=10, str=0x555555554b21 "test") at demo.cpp:6
a is 10, str is test
main (argc=1, argv=0x7fffffffdea8) at demo.cpp:18
18 for (int i = 0; i < 10; i++)
(gdb)
5. 单步执行
gdb
单步执行的命令如下:
$ step
或者
$ s
当进入到断点所在代码时,可以输入 step
命令执行该行代码。如果该行代码有函数调用,会直接进入该函数内部继续执行;如果没有函数调用,则直接执行下一行。
(gdb) b demo.cpp:16
Breakpoint 1 at 0x9a9: file demo.cpp, line 16.
(gdb) r
Starting program: /home/wohu/cppProject/book_debug/chapter_3.1/demo
Breakpoint 1, main (argc=1, argv=0x7fffffffdea8) at demo.cpp:16
16 int ret = add(3, 5);
(gdb) s
add (a=3, b=5) at demo.cpp:11
11 return a + b;
(gdb) s
12 }
(gdb) s
main (argc=1, argv=0x7fffffffdea8) at demo.cpp:17
17 std::cout << "ret is " << ret << std::endl;
(gdb) s
ret is 8
18 for (int i = 0; i < 10; i++)
(gdb) s
20 fun_test(10, "test");
(gdb) s
fun_test (a=10, str=0x555555554b21 "test") at demo.cpp:6
6 printf("a is %d, str is %s\n", a, str);
(gdb)
6. 逐过程执行
逐过程执行与单步执行类似,执行一次就会进入下一行。但是,如果当前代码行有函数调用,单步执行会进入到函数中,逐过程执行则不会进入到函数中。无论有多少个函数调用,逐过程执行都会进入到下一行代码。逐过程执行的语法如下:
$ next
或者
$ n
它的用法与 step
类似,单步执行也可以称之为逐语句执行。
逐语句和逐过程执行命令后面都可以跟一个数字参数,例如 s 5
或者 5,表示往后执行 5 行代码,遇到 s
会进入函数,遇到 n
则不会进入函数。如果在往后执行的过程中遇到了断点,首先会在断点处中断。