Linux中的GDB调试器

在编译时如果我们仅仅依靠gcc发出的警告或错误信息来进行修改,调试的效率非常低,也不容易发现错误的所在。为此,GNU开发了GDB调试器。
下面我们就来简单认识一下GDB调试器:
Linux中的GDB调试器
[[email protected] 1118]# gcc -g 14.c -o 14
[[email protected] 1118]# gdb 14
GNU gdb Red Hat Linux (6.5-25.el5rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type “show copying” to see the conditions.
There is absolutely no warranty for GDB. Type “show warranty” for details.
This GDB was configured as “i386-redhat-linux-gnu”…Using host libthread_db library “/lib/libthread_db.so.1”.

(gdb) l
10 int cal(int n)
11 {
12 if(n == 1)
13 return 1;
14 else
15 return n * cal(n - 1);
16 }
17 int main(int argc, char **argv)
18 {
19 int n = 5;
(gdb) b 15
Breakpoint 1 at 0x8048399: file 14.c, line 15.
(gdb) r
Starting program: /tjy/C语言/1118/14

Breakpoint 1, cal (n=5) at 14.c:15
15 return n * cal(n - 1);
(gdb) p n
$1 = 5
(gdb) c
Continuing.

Breakpoint 1, cal (n=4) at 14.c:15
15 return n * cal(n - 1);
(gdb) p n
$2 = 4
(gdb) n

Breakpoint 1, cal (n=3) at 14.c:15
15 return n * cal(n - 1);
(gdb) n

Breakpoint 1, cal (n=2) at 14.c:15
15 return n * cal(n - 1);
(gdb) s
cal (n=1) at 14.c:12
12 if(n == 1)
(gdb) s
13 return 1;
(gdb) q
The program is running. Exit anyway? (y or n) y
[[email protected] 1118]#
这就是GDB调试器的运行过程。
主要的几个操作如下:
1.gcc -g 15.c -o 15:把调试信息加入到可执行文件中
2.gdb 15:启动GDB进行调试
3.gdb l:查看所有的代码行数
4.gdb b 15:设置断点
5.gdb info b:查看断点情况
6.gdb r:运行程序,在断点前一行暂停
7:gdb p n:查看变量值
8.gdb c:继续运行程序
9:gdb s:让程序一步一步的往下运行