一、GDB是啥?
GDB是GUN开源组织所发布的一个强大的UNIX下的程序调试工具。可能我们都比较喜欢像VC、BCB及IDE等图形化界面方式的调试工具。但是,如果是在UNIX下开发软件,就不得不提GDB调试工具,因为它具有比上述图形化调试工具更强大的功能,如修复网络断点及恢复链接等。
二、GDB有啥用?
一般来说,GDB可以帮助我们完成下面四点功能:
- 启动程序,可以按照自定义的要求随心所欲地运行程序;
- 设置断点,并让被调试的程序在指定的断点处停住;
- 当程序被停住时,可以检查此时程序中所发生的事,如查看某个变量的值;
- 动态改变程序的执行环境。
三、GDB怎么用?
在此引用一个写的比较好的例子(引自https://blog.csdn.net/dadalan/article/details/3758025)。
#include <stdio.h> int func(int n) { int sum=0,i; for(i=1; i<=100; i++) { sum+=i; } return sum; } int main() { int i; long result = 0; for(i=1; i<=100; i++) { result += i; } printf("result[1-100] = %ld", result ); printf("result[1-250] = %d", func(250) ); return 0; }
一般来说,GDB主要用于调试C/C++程序。要想调试C/C++程序,我们需要将调试信息加到可执行文件中,这可以使用编译器(cc/ gcc/ g++) 的 -g 参数完成。
例如,对于上面的程序可以用 gcc -g -o test test.c 生成可执行文件。然后用 gdb test 进入调试,具体如下:
------------------------------------------------begin-------------------------------------------------------
root@joy:/mnt/shared/gdb-test# gcc -g test.c -o test <---------- 编译
root@joy:/mnt/shared/gdb-test# gdb test <---------- 启动GDB
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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) l <---------- 或list,查看代码(默认从第一行),若想查看特定行只需在后面加上行号即可
8 sum += i;
9 }
10 return sum;
11 }
12
13
14 int main()
15 {
16 int i;
17 long result = 0;
(gdb) <---------- 回车,表示重复执行上一次的命令
18 for(i=1; i<=100; i++)
19 {
20 result += i;
21 }
22
23 printf("result[1-100] = %ld\n", result );
24 printf("result[1-200] = %d\n", func(200));
25 return 0;
26 }(gdb) b 16 <---------- 或break,在源程序第16行设置断点
Breakpoint 1 at 0x400563: file test.c, line 16.
(gdb) b func <---------- 在函数func入口处设置断点
Breakpoint 2 at 0x400534: file test.c, line 5.
(gdb) i b <---------- 或info breakpoints,查看当前设置的所有断点信息
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400563 in main at test.c:16
2 breakpoint keep y 0x0000000000400534 in func at test.c:5
(gdb) r <---------- 或run,运行程序
Starting program: /mnt/shared/gdb-test/test
Breakpoint 1, main () at test.c:17
17 long result = 0;
(gdb) n <---------- 或next,执行单条语句
18 for(i=1; i<=100; i++)
(gdb) n
20 result += i;
(gdb) p result
$1 = 0
(gdb) n
18 for(i=1; i<=100; i++)
(gdb) n
20 result += i;
(gdb) p result <---------- 或print,打印变量result的值
$2 = 1
(gdb) n
18 for(i=1; i<=100; i++)
(gdb) p result
$3 = 3
(gdb) c <---------- 或continue,继续执行程序
Continuing.
result[1-100] = 5050
Breakpoint 2, func (n=200) at test.c:5
5 int sum=0,i;
(gdb) n
6 for(i=1; i<= n; i++)
(gdb) n
8 sum += i;
(gdb) n
6 for(i=1; i<= n; i++)
(gdb) p i
$4 = 1
(gdb) n
8 sum += i;
(gdb) p i
$5 = 2
(gdb) n
6 for(i=1; i<= n; i++)
(gdb) p sum
$6 = 3
(gdb) bt <---------- 或backtrace,查看函数堆栈
#0 func (n=200) at test.c:6
#1 0x00000000004005a7 in main () at test.c:24
(gdb) finish <---------- 结束当前函数,返回到函数调用点,并打出印函数的返回值
Run till exit from #0 func (n=200) at test.c:6
0x00000000004005a7 in main () at test.c:24
24 printf("result[1-200] = %d\n", func(200));
Value returned is $7 = 20100
(gdb) c
Continuing.
result[1-200] = 20100
[Inferior 1 (process 2752) exited normally]
(gdb) quit <---------- 退出gdb
root@joy:/mnt/shared/gdb-test#
------------------------------------------------end-------------------------------------------------------
三、GDB常用命令汇总
list 数字n:查看第n行代码,简写l
break 数字n:在第n行设置断点,简写b
run :运行程序,如果没有设置断点,则执行整个程序;若有断点,则暂停在第一个可用断点处,简写r
step :执行一行源程序代码,若此行有函数调用则进入该函数,简写s
next :执行一行源程序代码,此行中的函数调用也一起执行,简写n
print 变量名:打印指定变量的值,简写p
continue :继续执行程序,简写c
delete breakpoints 数字:删除所设置的第几个断点,简写d
info breakpoints :查看当前设置的所有断点信息,简写i b
enable breakpoints :启用断点
disable breakpoints :禁用断点
finish :结束当前函数,并返回到函数调用点
display 变量名:追踪查看指定变量的值
undisplay 数字:取消追踪观察指定变量
quit :退出gdb调试环境简写q
------------------------------------------------The end------------------------------------------------------