作者:zieckey (zieckey@yahoo.com.cn)
All Rights Reserved!
Linux 下有一个叫 gdb 的 GNU 调试程序. gdb 是一个用来调试 C 和 C++ 程序的强有力调试器. 它使你能在程序运行时观察程序的内部结构和内存的使用情况. 以下是 gdb 所提供的一些功能:
* 它使你能监视你程序中变量的值.
* 它使你能设置断点以使程序在指定的代码行上停止执行.
* 它使你能一行行的执行你的代码.
在命令行上键入 gdb 并按回车键就可以运行 gdb 了, 如果一切正常的话, gdb 将被启动并且你将在屏幕上看到类似的内容:
[root@localhost root]# gdb
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 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".
(gdb)
当你启动 gdb 后, 你能在命令行上指定很多的选项.
你也可以以下面的方式来运行 gdb :
gdb <fname>
例如,
[root@localhost exam-source-file]# gdb a.out
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 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"...
(gdb)
下面我们以一个简单的例子来开始我们的gdb调试之旅。
一个调试示例
源程序:testgbd.c
// name: testgbd.c
// This file is used to test how to use gdb to debug a c program .It is very simple ! Enjoy yourself!
// Author : zieckey
// data : 2006/11/13
#include <stdio.h>
int accumulate( int n )
{
int sum = 0 , i ;
for( i=1; i<=n; i++)
{
sum += i;
}
return sum;
}
int main( void )
{
int i , n ;
int result ;