GNU的调试工具gdb
九月 22, 2009 – 5:21 下午写代码的时候如果没有个好用的调试工具,那简直就是种折磨。
gdb就是这样的一个好工具,帮助你提高效率的好工作。
下面通过个小例子来介绍下gdb的简单知识。
sunwg.c
————-
#include
int main()
{
int a=0;
a++;
a++;
printf(”%d\n”,a);
return 1;
}
创建个简单的c程序sunwg.c。
要想通过gdb来调试程序,那么在编译c程序的时候一定要加入调式信息。
$ gcc -g -o sunwg sunwg.c
-g就是告诉编译器要增加调试信息
下面开始调试程序
$ gdb sunwg
GNU gdb Red Hat Linux (6.3.0.0-1.153.el4rh)
Copyright 2004 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 “x86_64-redhat-linux-gnu”…Using host libthread_db library “/lib64/tls/libthread_db.so.1″.
(gdb) l –命令l为显示当前代码
1 #include
2
3 int main()
4 {
5 int a=0;
6 a++;
7 a++;
8 printf(”%d\n”,a);
9 return 1;
10 }
(gdb) break 6 –在第6行增加断点
Breakpoint 1 at 0×4004b7: file sunwg.c, line 6.
(gdb) r –运行程序,程序会在有断点的地方停止
Starting program: /home/oracle/sunwg/c/sunwg
Breakpoint 1, main () at sunwg.c:6
6 a++;
(gdb) p a –p表示查看变量a的值
$1 = 0
(gdb) n –执行下一条代码
7 a++;
(gdb) p a
$2 = 1
(gdb) n
8 printf(”%d\n”,a);
(gdb) p a
$3 = 2
(gdb) c –继续执行其余的代码
Continuing.
2
Program exited with code 01.
(gdb) q –退出gdb
gdb是很强大的调试工具,其他命令可以参考用户手册
><