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

一般来说,GDB主要帮忙你完成下面四个方面的功能:

1、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。

2、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)

3、当程序被停住时,可以检查此时你的程序中所发生的事。

4、动态的改变你程序的执行环境。

从上面看来,GDB和一般的调试工具没有什么两样,基本上也是完成这些功能,不过在细节上,你会发现GDB这个调试工具的强大,大家可能比较习惯了图形化的调试工具,但有时候,命令行的调试工具却有着图形化工具所不能完成的功能。让我们一一看来。

示例程序test.c

#include <stdio.h>

int func(int n)
{
int sum=,i;
for(i=; i<n; i++)
{
sum+=i;
}
return sum;
} main()
{
int i;
long result = ;
for(i=; i<=; i++)
{
result += i;
} printf("result[1-100] = %d \n", result );
printf("result[1-250] = %d \n", func() );
}

编译生成执行文件:(Linux下)

[walt740@localhost 01.第一个示例]$ gcc -g test.c -o test

使用GDB调试:

启动加载文件的方式有两种:

(1)[walt740@localhost 01.第一个示例]$ gdb test<---------- 启动GDB

GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.el7
Copyright (C) 2013 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/walt740/Documents/01.GDB调试练习/01.第一个示例/test...done.
(gdb)

(2)[walt740@localhost 01.第一个示例]$ gdb

GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.el7
Copyright (C) 2013 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/>.
(gdb) file test
Reading symbols from /home/walt740/Documents/01.GDB调试练习/01.第一个示例/test...done.
(gdb)

(gdb) l <-------------------- l命令相当于list,从第一行开始例出原码。

1 #include <stdio.h>

2

3 int func(int n)

4 {

5 int sum=0,i;

6 for(i=0; i<n; i++)

7 {

8 sum+=i;

9 }

10 return sum;

(gdb) <-------------------- 直接回车表示,重复上一次命令

11 }

12

13

14 main()

15 {

16 int i;

17 long result = 0;

18 for(i=1; i<=100; i++)

19 {

20 result += i;

(gdb) break 16 <-------------------- 设置断点,在源程序第16行处。

Breakpoint 1 at 0x8048496: file test.c, line 16.

(gdb) break func <-------------------- 设置断点,在函数func()入口处。

Breakpoint 2 at 0x8048456: file test.c, line 5.

(gdb) info break <-------------------- 查看断点信息。

Num Type Disp Enb Address What

1 breakpoint keep y 0x08048496 in main at test.c:16

2 breakpoint keep y 0x08048456 in func at test.c:5

(gdb) r <--------------------- 运行程序,run命令简写

Starting program: /home/walt740/Documents/01.GDB调试练习/01.第一个示例/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) n

18 for(i=1; i<=100; i++)

(gdb) n

20 result += i;

(gdb) c <--------------------- 继续运行程序,continue命令简写。

Continuing.

result[1-100] = 5050 <----------程序输出。

Breakpoint 2, func (n=250) at test.c:5

5 int sum=0,i;

(gdb) n

6 for(i=1; i<=n; i++)

(gdb) p i <--------------------- 打印变量i的值,print命令简写。

$1 = 134513808

(gdb) n

8 sum+=i;

(gdb) n

6 for(i=1; i<=n; i++)

(gdb) p sum

$2 = 1

(gdb) n

8 sum+=i;

(gdb) p i

$3 = 2

(gdb) n

6 for(i=1; i<=n; i++)

(gdb) p sum

$4 = 3

(gdb) bt <--------------------- 查看函数堆栈。

#0 func (n=250) at test.c:5

#1 0x080484e4 in main () at test.c:24

#2 0x400409ed in __libc_start_main () from /lib/libc.so.6

(gdb) finish <--------------------- 退出函数。

Run till exit from #0 func (n=250) at test.c:5

0x080484e4 in main () at test.c:24

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

Value returned is $6 = 31125

(gdb) c <--------------------- 继续运行。

Continuing.

result[1-250] = 31125 <----------程序输出。

Program exited with code 027. <--------程序退出,调试结束。

(gdb) q <--------------------- 退出gdb。

最新文章

  1. EndNote(一)之基本介绍
  2. [算法总结]partition (quicksort)
  3. Alpha阶段第六次Scrum Meeting
  4. DP专题训练之HDU 2955 Robberies
  5. JAVA线程锁-读写锁应用,简单的缓存系统
  6. svn使用svnsync实现双机热备
  7. OC字符串常用函数
  8. Reference in the manifest does not match the identity of the downloaded assembly
  9. Android SharedPreferences 权限设置
  10. 从零开始运维之旅:如何监控你的 Windows?
  11. js实现a标签超链接提交form表单的方法
  12. Log Collect
  13. [USACO17FEB]Why Did the Cow Cross the Road I S
  14. C++面试笔记(2)
  15. vc++高级班之多线程篇[6]---线程间的同步机制①
  16. linux 安装软件
  17. poj3728The merchant 【倍增】【LCA】
  18. mRemoteNG
  19. 浮点纹理 opengl
  20. RESTORE DATABASE命令还原SQLServer 2005 数据库

热门文章

  1. MySQL GROUP BY 语句
  2. About IndexDB
  3. BZOJ 4241: 历史研究 ( 回 滚 )
  4. 数据结构实验之栈与队列三:后缀式求值(SDUT 2133)
  5. 下载安装Xocde并创建一个C语言的项目工程
  6. ie和vuex的兼容
  7. arcpy workspace already in transaction mode
  8. Spring事务知识点
  9. java工厂模式的测试
  10. UML期末复习题——2.5:System Sequence Diagram &amp; Post-condition