GDB调试学习

简介

​GDB 全称“GNU symbolic debugger”,它诞生于 GNU 计划(同时诞生的还有 GCC、Emacs 等),是 Linux 下常用的程序调试器。发展至今,GDB 已经迭代了诸多个版本,当下的 GDB 支持调试多种编程语言编写的程序,包括 C、C++、Go、Objective-C、OpenCL、Ada 等。

在这里插入图片描述

准备工作

用以下命令编译:

gcc -g -Wall program.c -o program
  • -Wall:开启所有警告(可以理解为warinig all),使用它能够使GCC产生尽可能多的警告信息。
  • -g:选项的作用是在可执行文件中加入源代码的信息,比如可执行文件中第几条机器指令对应源代码的第几行,但并不是把整个源文件嵌入到可执行文件中,所以在调试时必须保证 gdb 能找到源文件。

常用命令

启动与退出

  • 启动:gdb 可执行程序
  • 退出:quit/q
(base) user@ubuntu:~/Desktop/OS/NiuKe$ vim test.c
(base) user@ubuntu:~/Desktop/OS/NiuKe$ gcc test.c -o test -g -std=c99
(base) user@ubuntu:~/Desktop/OS/NiuKe$ ls
test  test.c
(base) user@ubuntu:~/Desktop/OS/NiuKe$ 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) q

给程序设置参数/获取设置参数

  • 设置参数:set args 10 20
  • 获取设置参数:show args
(base) user@ubuntu:~/Desktop/OS/NiuKe$ 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使用帮助

  • help
(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.

查看当前文件代码

  • 从默认位置显示:list/l
(gdb) l
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) {
(gdb) list
11	        a = 10;
12	        b = 30;
13	    } else {
14	        a = atoi(argv[1]);
15	        b = atoi(argv[2]);
16	    }
17	    printf("a = %d, b = %d\n", a, b);
18	    printf("a + b = %d\n", a + b);
19	
20	    for(int i = 0; i < a; ++i) {
(gdb) l
21	        printf("i = %d\n", i);
22	        // 函数调用
23	        int res = test(i);
24	        printf("res value: %d\n", res);
25	    }
26	
27	    printf("THE END !!!\n");
28	    return 0;
29	}
  • 从指定的行显示:list/l 行号
(gdb) l 13
8	    printf("argc = %d\n", argc);
9	
10	    if(argc < 3) {
11	        a = 10;
12	        b = 30;
13	    } else {
14	        a = atoi(argv[1]);
15	        b = atoi(argv[2]);
16	    }
17	    printf("a = %d, b = %d\n", a, b);
  • 从指定的函数显示:list/l 函数名
(gdb) l main
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) {

查看非当前文件代码

  • 编译运行并使用gdb main。
  • 从指定文件指定的行显示:list/l 文件名:行号。
  • 从指定文件指定的函数显示:list/l 文件名:函数名。

(这部分实践操作和上面的一样,只不过这部分是将多个文件编译成可执行文件)

查看及设置显示的行数

  • 查看显示的行数:show list/listsize
  • 设置显示的行数:set list/listsize
(gdb) show list
Number of source lines gdb will list by default is 10.
(gdb) set listsize 20
(gdb) show listsize 
Number of source lines gdb will list by default is 20.

断点操作

设置断点

  • b/break 行号
  • b/break 函数名
  • b/break 文件名:行号
  • b/break 文件名:函数

查看断点:i/info b/break

删除断点:d/del/delete 断点编号

设置断点无效:dis/disable 断点编号

设置断点生效:ena/enable 断点编号

设置条件断点(一般用在循环的位置):b/break 10 if i==5

前置文件需求

(base) user@ubuntu:~/Desktop/OS/NiuKe$ ls
bubble.cpp  main.cpp  select.cpp  sort.h  test  test.c
(base) user@ubuntu:~/Desktop/OS/NiuKe$ g++ bubble.cpp select.cpp main.cpp -o main -g
(base) user@ubuntu:~/Desktop/OS/NiuKe$ ls
bubble.cpp  main  main.cpp  select.cpp  sort.h  test  test.c

调试操作(※)

  • 运行 GDB 程序
    • 程序停在第一行:start
    • 遇到断点才停:run
  • 继续运行,到下一个断点停:c/continue
  • 向下执行一行代码(不会进入函数体):n/next
  • 变量操作
    • 打印变量值:p/print 变量名
    • 打印变量类型:ptype 变量名
  • 向下单步调试(遇到函数进入函数体)
    • s/step
    • 跳出函数体:finish
  • 自动变量操作
    • 自动打印指定变量的值:display 变量名
    • 查看自动变量:i/info display
    • 取消自动变量:undisplay 编号
  • 其它操作
    • 设置变量值:set var 变量名=变量值 (循环中用的较多)
    • 跳出循环:until

ex1

(gdb) i b  #还未开始打断电
No breakpoints or watchpoints.
(gdb) start  #停在第一行程序
Temporary breakpoint 1 at 0x400a49: file main.cpp, line 8.
Starting program: /home/user/Desktop/OS/NiuKe/main 

Temporary breakpoint 1, main () at main.cpp:8
8	    int array[] = {12, 27, 55, 22, 67};
(gdb) c    # 继续运行,到下一个断点停。即运行完全部程序
Continuing.
冒泡排序之后的数组: 12 22 27 55 67 
===================================
选择排序之后的数组: 11 25 36 47 80 
[Inferior 1 (process 3989) exited normally]

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值