xyu

xyu

0个粉丝

18

问答

0

专栏

0

资料

xyu  发布于  2012-12-04 13:42:23
采纳率 0%
18个问答
2982

linux GDB具体调试实例

下面通过使用一个简单的程序使读者进一步熟悉用gdb的调试方法。

源程序名为example1.c,代码如下:

/*******************************************************

* Institute of Automation, Chinese Academy of Sciences

* File Name:    example.c

* Description: introduce how to use gdb

* Author:       Xueyuan Nie

* Date:     

*******************************************************/

#include

static void display(int i, int *ptr);

int main(void)

{

      int x = 5;

int *xptr = &x;

  printf("In main():\n");

      printf("   x is %d and is stored at %p.\n", x, &x);

      printf("   xptr holds %p and points to %d.\n", xptr, *xptr);

      display(x, xptr);

      return 0;

}

void display(int z, int *zptr)

{

  printf("In display():\n");

      printf("   z is %d and is stored at %p.\n", z, &z);

      printf("   zptr holds %p and points to %d.\n", zptr, *zptr);

}

要使用gdb调试程序,一定要在编译程序时,使用-g编译选项,以生成参数符号表( augmented symbol table),提供调试信息。

首先使用gcc –g –o example1 example.c对源代码进行编译,这样就可以使用gdb监视example1的执行细节。在bash提示符下,键入命令:gdb example1,启动了对可执行文件example1 的调试,在屏幕上会出现下面的信息:

[nie@uClinux nie]$ gdb example1

GNU gdb Red Hat Linux 7.x (5.0rh-15) (MI_OUT)

Copyright 2001 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"...

(gdb)

最后一行(gdb)就是进入到gdb调试中的提示符,此时可以在提示符下输入任何想键入的命令。

现在如果要进行断点调试的话,就需要显示一下要调试的源码,以便知道在哪个地方进行断点设置。在gdb下,Linux最常用文本编辑命令vi不能使用,可以使用list命令列出可执行文件的源代码的一部分,为了列出源代码的全部,只要多键入几次list命令即可。具体操作如下:

(gdb) list

1       #include

2       static void display(int i, int *ptr);

3

4        int main(void) {

5          int x = 5;

6          int *xptr = &x;

7          printf("In main():\n");

8          printf("   x is %d and is stored at %p.\n", x, &x);

9          printf("   xptr holds %p and points to %d.\n", xptr, *xptr);

10         display(x, xptr);

(gdb) list

11         return 0;

12      }

13

14      void display(int z, int *zptr) {

15         printf("In display():\n");

16         printf("   z is %d and is stored at %p.\n", z, &z);

17         printf("   zptr holds %p and points to %d.\n", zptr, *zptr);

18      }

(gdb) list

Line number 19 out of range; example1.c has 18 lines.

(gdb)

屏幕上清楚显示出了每一个语句所在的具体行号,比如现在我们想在第五行设置断点,可以在gdb提示符下输入命令:break 5,可以看到下面的显示信息:

(gdb) break 5

Breakpoint 1 at 0x8048466: file example1.c, line 5.

断点已经设置好,现在开始让程序运行起来,键入命令run,也可以键入其缩写形式r,屏幕上出现的信息如下:

(gdb) r

Starting program: /home/nie/example1

Breakpoint 1, main () at example1.c:5

5          int x = 5;

上述信息表明,gdb已经开始执行可执行程序,目前程序运行到example1.c程序中main()函数的第五行处停止,并且显示出即将要执行的第五行语句。

现在我们进行单步调试的工作,输入命令:next,它表明单步执行程序的每一条语句,当用next命令执行到函数display处时,即当屏幕出现如下所示信息时:

(gdb) next

6               int *xptr = &x;

(gdb) next

7               printf("In main():\n");

(gdb) next

In main():

8          printf("   x is %d and is stored at %p.\n", x, &x);

(gdb) next

   x is 5 and is stored at 0xbffffb44.

9          printf("   xptr holds %p and points to %d.\n", xptr, *xptr);

(gdb) next

   xptr holds 0xbffffb44 and points to 5.

10         display(x, xptr);

为了进入到函数display内部进行调试,输入命令step,即:

(gdb) step

display (z=5, zptr=0xbffffb44) at example1.c:15

15              printf("In display():\n");

step命令使执行进入到函数内部,此时在该函数内部,可以继续使用step命令或者是next命令进行单步执行,如果不想单步执行,而是直接将程序一次执行完毕,可以输入命令continue即可。

   要退出gdb,请键入命令quit,如果程序此时仍在进行,gdb会让你确认是否真的要退出,屏幕会出现类似下面的提示信息:

   (gdb) quit

The program is running.  Exit anyway? (y or n)

按下'y' 即退出调试程序,如果程序本身已经运行完毕,则quit命令键入后,会直接退出gdb,而不出现任何提示信息。

当然除了使用gdb进行程序调试外,如果程序比较简短,逻辑又比较简单,此时完全可以不用gdb,采用printf语句在程序当中输出中间变量的值来调试程序,也是一个不错的调试方法。

到此为止,我们已经介绍了uClinux操作系统,GNU工具的使用,有了这些预备知识后,我们将进入到本章的重点内容了。
我来回答
回答0个
时间排序
认可量排序
易百纳技术社区暂无数据
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
+ 添加网盘链接/附件

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

易百纳技术社区