Fork me on GitHub

gdb使用教程

GDB介绍

GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具。或许,各位比较喜欢那种图形界面方式的,像VS等IDE的调试,但如果你是在UNIX平台下做软件,你会发现GDB这个调试工具有比VS的图形化调试器更强大的功能。所谓“寸有所长,尺有所短”就是这个道理。
一般来说,GDB主要帮忙你完成下面四个方面的功能:

  • 1、启动程序,按用户要求影响程序的运行行为
  • 2、使运行程序在指定条件处停止(断点可以是条件表达式)
  • 3、当程序被停住时,可以检查此时你的程序中出现的问题
  • 4、动态的改变程序的执行环境,这样能纠正一个错误

案例

源程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>

int func(int n )
{
int sum = 0,i;
for(i=0;i<n;i++)
{
sum += i;
}
return sum;
}

int main()
{
int i;
long result = 0;
for(i=1;i<=100;i++)
{
result += i;
}

printf("result[1-100]= %d\n",result);

printf("result[1-100]= %d\n",func(250));

return 0;
}

编译生成执行文件:

1
[liuxiaokun@localhost day10_1]$ cc test.c -o test

使用GDB调试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
[liuxiaokun@localhost day10_1]$ gdb test   ========>>>启动GDB
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
Copyright (C) 2010 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/liuxiaokun/code/C/day10_1/test...done.
(gdb) l ========>>> l相当于list从第一行列出源程序
7 {
8 sum += i;
9 }
10 return sum;
11 }
12
13 int main()
14 {
15 int i;
16 long result = 0;
(gdb) ========>>>直接回车,表示重复上一次命令
17 for(i=1;i<=100;i++)
18 {
19 result += i;
20 }
21
22 printf("result[1-100]= %d\n",result);
23
24 printf("result[1-100]= %d\n",func(250));
25 }
(gdb) break 19 ========>>>设置断点
Breakpoint 1 at 0x40050b: file test.c, line 19.
(gdb) break func ========>>>设置断点,在函数的入口处
Breakpoint 2 at 0x4004cb: file test.c, line 5.
(gdb) info breakpoints =========>>>查看断点信息
Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000040050b in main at test.c:19
2 breakpoint keep y 0x00000000004004cb in func at test.c:5
(gdb) r =======>>>运行程序,run命令缩写
Starting program: /home/liuxiaokun/code/C/day10_1/test

Breakpoint 1, main () at test.c:19 ========>>>在断点处停住
19 result += i;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64
(gdb) n
17 for(i=1;i<=100;i++)
(gdb) n

Breakpoint 1, main () at test.c:19
19 result += i;
(gdb) n
17 for(i=1;i<=100;i++)
(gdb) c ========>>>继续运行程序,continue的缩写
Continuing.

Breakpoint 1, main () at test.c:19
19 result += i;
(gdb) n
17 for(i=1;i<=100;i++)
(gdb) p i ========>>>打印变量i的值,p是print的缩写
$1 = 5
(gdb) n

Breakpoint 1, main () at test.c:19
19 result += i;
(gdb) n
17 for(i=1;i<=100;i++)
(gdb) n

Breakpoint 1, main () at test.c:19
19 result += i;
(gdb) p result
$2 = 28
(gdb) bt ========>>>查看函数堆栈
#0 main () at test.c:19
(gdb) n
17 for(i=1;i<=100;i++)
(gdb) bt
#0 main () at test.c:17
(gdb) n

Breakpoint 1, main () at test.c:19
19 result += i;
(gdb) n
17 for(i=1;i<=100;i++)
(gdb) n

Breakpoint 1, main () at test.c:19
19 result += i;
(gdb) n
17 for(i=1;i<=100;i++)
(gdb) n

Breakpoint 1, main () at test.c:19
19 result += i;
(gdb) bt
#0 main () at test.c:19
(gdb) finish ========>>>退出函数
"finish" not meaningful in the outermost frame.
(gdb) c
Continuing.

Breakpoint 1, main () at test.c:19
19 result += i;
(gdb) q ==========>>>退出gdb
A debugging session is active.

Inferior 1 [process 4248] will be killed.

Quit anyway? (y or n)

GDB

通过以上案例,我们对GDB有了大概的了解,接下来就系统的认识GDB吧

一般来说GDB主要调试的是C/C++的程序。要调试C/C++的程序,首先在编译时,我们必须要把调试信息加到可执行文件中。使用编译器(cc/gcc/g++)的
-g 参数可以做到这一点。如:

1
2
> gcc -g test.c -o test
> g++ -g test.cpp -o test

==如果没有-g,你将看不见程序的函数名、变量名,所代替的全是运行时的内存地址。当你用-g把调试信息加入之后,并成功编译目标代码以后,让我们来看看如何用gdb来调试他。==

启动gdb的方法有以下几种:

  1. gdb
    program
    就是可执行文件,一般就在当前目录下
  2. gdb core
    用gdb同时调试一个运行程序和core文件,core==是非法执行后==core dump 后产生的文件
  3. gdb
    如果是一个服务程序,就可以指定服务程序运行时的进程ID,gdb会自动的attach上去,并调试他,program应该在path环境变量中搜索得到

当然,gdb启动时我们可以加一些参数,详细的参数可以用gdb -help查看

GDB命令概貌

启动gdb后,就你被带入gdb的调试环境中,就可以使用gdb的命令开始调试程序了,gdb的命令可以使用help命令来查看,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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".

gdb的命令有很多,所以gdb命令有分类,help只是列出了命令的种类,查看类中的命令使用==help == 命令,如:==help breakpoint==,查看设置断点的所有命令

gdb中,可以不用大全命令,只用命令的前几个字符就可以了,当然也可以两次Tab将命令补全,如果有重复,就会列出了

  • 示例一:在进入函数func时,设置一个断点。可以敲入break func,或是直接就是b func

    1
    2
    (gdb) break func
    Breakpoint 1 at 0x4004c8
  • 示例二:敲入b按两次TAB键,你会看到所有b打头的命令:

    1
    2
    (gdb) b
    backtrace bookmark break bt

本文标题:gdb使用教程

文章作者:LiuXiaoKun

发布时间:2018年10月02日 - 10:10

最后更新:2018年10月02日 - 11:10

原始链接:https://LiuZiQiao.github.io/2018/10/02/gdb使用教程/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%