1. GDB介绍
解释一下三、四条:
第三条:意思是监控部分变量的变化情况
第四条:意思是,如果某个值出错,可以用gdb命令修改这个值,使调试不会中断,可以继续下去
2. 准备工作
使用GDB调试的时候需要在编译的时候加-g参数(可以看到源代码)。
查看一下采用 -g 和不采用 -g 的区别
nowcoder@nowcoder:~/Linux/lession08$ ls
bubble.cpp main.cpp select.cpp test.c
nowcoder@nowcoder:~/Linux/lession08$ gcc test.c -o test -g
nowcoder@nowcoder:~/Linux/lession08$ gcc test.c -o test1
nowcoder@nowcoder:~/Linux/lession08$ ls
bubble.cpp main.cpp select.cpp test test1 test.c
nowcoder@nowcoder:~/Linux/lession08$ ll -h test test1
-rwxrwxr-x 1 nowcoder nowcoder 11K 7月 25 22:37 test*
-rwxrwxr-x 1 nowcoder nowcoder 8.3K 7月 25 22:37 test1*
上图可以看到大小区别,带-g的比不带的要大很多,也就是说-g带了调试信息
3. GDB命令:启动、退出
3.1. 启动和退出命令
gdb 可执行程序
quit
3.2. 给程序设置启动参数/获取设置参数
set args 10 20
show args
整个过程
nowcoder@nowcoder:~/Linux/lession08$ gcc test.c -o test -g
nowcoder@nowcoder:~/Linux/lession08$ gcc test.c -o test1
nowcoder@nowcoder:~/Linux/lession08$ ls
bubble.cpp main.cpp select.cpp test test1 test.c
nowcoder@nowcoder:~/Linux/lession08$ ll -h test test1
-rwxrwxr-x 1 nowcoder nowcoder 11K 7月 25 22:37 test*
-rwxrwxr-x 1 nowcoder nowcoder 8.3K 7月 25 22:37 test1*
nowcoder@nowcoder:~/Linux/lession08$ gdb test
GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
(gdb) set args 10 20
(gdb) show args
Argument list to give program being debugged when it is started is "10 20".
(gdb) quit
nowcoder@nowcoder:~/Linux/lession08$
4. GDB命令 查看代码
4.1. list/l
(gdb) list
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int test(int a);
5
6 int main(int argc, char* argv[]) {
7 int a, b;
8 printf("argc = %d\n", argc);
9
10 if(argc < 3) {
4.2. list/l 文件名
有多个文件,可以用list 文件名指定查看哪个文件的代码
4.3. 设置显示文件行数
5. GDB命令
(gdb) b 14
Breakpoint 1 at 0x705: file test.c, line 14.
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000000705 in main at test.c:14
(gdb) break 15
Breakpoint 2 at 0x71b: file test.c, line 15.
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000000705 in main at test.c:14
2 breakpoint keep y 0x000000000000071b in main at test.c:15
5.2. delete 删除断点信息
(gdb) delete 1
(gdb) info break
Num Type Disp Enb Address What
2 breakpoint keep y 0x000000000000071b in main at test.c:15
5.3. diable 设置断点无效、enable 设置断点有效
(gdb) info b
Num Type Disp Enb Address What
2 breakpoint keep y 0x000000000000071b in main at test.c:15
3 breakpoint keep y 0x000000000000071b in main at test.c:15
(gdb) disable 2
(gdb) info b
Num Type Disp Enb Address What
2 breakpoint keep n 0x000000000000071b in main at test.c:15
3 breakpoint keep y 0x000000000000071b in main at test.c:15
5.4. 设置条件断点
(gdb) break 23 if i=3
Breakpoint 4 at 0x784: file test.c, line 23.
(gdb) info b
Num Type Disp Enb Address What
2 breakpoint keep n 0x000000000000071b in main at test.c:15
3 breakpoint keep y 0x000000000000071b in main at test.c:15
4 breakpoint keep y 0x0000000000000784 in main at test.c:23
stop only if i=3
6. 运行GDB程序
(gdb) info b
Num Type Disp Enb Address What
2 breakpoint keep n 0x000055555555471b in main at test.c:15
3 breakpoint keep y 0x000055555555471b in main at test.c:15
4 breakpoint keep y 0x0000555555554784 in main at test.c:23
stop only if i=3
breakpoint already hit 1 time
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/nowcoder/Linux/lession08/test
argc = 1
a = 10, b = 30
a + b = 40
i = 0
Breakpoint 4, main (argc=1, argv=0x7fffffffe2c8) at test.c:23
23 int res = test(i);
(gdb) display i
2: i = 3