比思論壇

標題: linux程序开发的GDB调试 [打印本頁]

作者: skylor    時間: 2013-6-14 14:56
標題: linux程序开发的GDB调试
无论多么优秀的程序员,必须经常面对的一个问题就是调试。当程序编译完成后,他可能无法正常运行;或许程序会彻底崩溃;或许只是不能正常地运行某些功能;或许它的输出会被挂起;或许不会提示要求正常的输入。无论在何种情况下,跟踪这些问题,特别是在大的工程中,将是开发中最困难的部分,本章将介绍使用gdb(GNU debugger)调试程序的方法,该程序是一个调试器,是用来帮助程序员寻找程序中的错误的软件。
gdbGNU开发组织发布的一个强大的UNIX/Linux下的程序调试工具。或许,有人比较习惯图形界面方式的,像VCBCBIDE环境,但是在UNIX/Linux平台下做软件,gdb这个调试工具有比VCBCB的图形化调试器更强大的功能。所谓寸有所长,尺有所短就是这个道理。
一般来说,gdb主要帮忙用户完成下面4个方面的功能:
       启动程序,可以按照用户自定义的要求随心所欲的运行程序。
       可让被调试的程序在用户所指定的调试的断点处停住 (断点可以是条件表达式)
       当程序停住时,可以检查此时程序中所发生的事。
       动态地改变程序的执行环境。
从上面来看,gdb和一般的调试工具区别不大,基本上也是完成这些功能,不过在细节上,会发现gdb这个调试工具的强大。大家可能习惯了图形化的调试工具,但有时候,命令行的调试工具却有着图形化工具所不能完成的功能。下面通过实例4-4进行说明。
实例4-1 test.c                           


#include <stdio.h>
int func(int n)
{
     int sum=0,i;
     for(i=0; i<n; i++)
     {
        sum+=i;
       }
     return sum;
}
main()
{
     int i;
     long result = 0;
     for(i=1; i<=100; i++)
      {
        result += i;
      }
     printf("result[1-100] = %d \n", result );
     printf("result[1-250] = %d \n", func(250) );
}
编译生成执行文件(Linux)
[david@david david]$ gcc –g test.c -o test
使用gdb调试:
[david@DAVID david]$ gdb test <---------- 启动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)
键入 l命令相当于list命令,从第一行开始列出源码:
[david@DAVID david]$ gdb test
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) l
7                    {
8                           sum+=i;
9                    }
10                  return sum;
11           }
12
13              main()
14           {
15                   int i;
16                   long result = 0;
(gdb)
17                   for(i=1; i<=100; i++)
18                   {
19                           result += i;
20                   }
21                     printf("result[1-100] = %d \n", result );
22                 printf("result[1-250] = %d \n", func(250) );
23           }
(gdb) break 16 <--------------------
设置断点,在源程序第16行处。
Breakpoint 1 at 0x804836a: file test.c, line 16.
(gdb) break func <--------------------
设置断点,在函数func()入口处。
Breakpoint 2 at 0x804832e: file test.c, line 5.
(gdb) info break <--------------------
查看断点信息。
Num Type           Disp Enb Address    What
1   breakpoint     keep y   0x0804836a in main at test.c:16
2   breakpoint     keep y   0x0804832e in func at test.c:5
(gdb) r <---------------------
运行程序,run命令简写
Starting program: /home/david/test

Breakpoint 1, main () at test.c:16 <----------
在断点处停住。
16                   long result = 0;
(gdb) n <---------------------
单条语句执行,next命令简写。
17                   for(i=1; i<=100; i++)
(gdb) n
19                           result += i;
(gdb) n
17                   for(i=1; i<=100; i++)
(gdb) n
19                           result += i;
(gdb) n
17                   for(i=1; i<=100; 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=0; i<n; i++)
(gdb) p I    <---------------------
打印变量i的值,print命令简写。
$1 = 1107620064
(gdb) n
8                           sum+=i;
(gdb) n
6                    for(i=0; i<n; i++)
(gdb) p sum
$2 = 0
(gdb) bt     <---------------------
查看函数堆栈。
#0 func (n=250) at test.c:6
#1 0x080483b2 in main () at test.c:22
#2 0x42015574 in __libc_start_main () from /lib/tls/libc.so.6
(gdb) finish <---------------------
退出函数。
Run till exit from #0 func (n=250) at test.c:6
0x080483b2 in main () at test.c:22
22   printf("result[1-250] = %d \n", func(250) );
Value returned is $3 = 31125
(gdb) c <---------------------
继续运行。
Continuing.
result[1-250] = 31125

Program exited with code 027. <--------
程序退出,调试结束。
(gdb) q     <--------------------- 退出gdb
[david@DAVID david]$
有了以上的感性认识,下面来系统地学习一下gdb





歡迎光臨 比思論壇 (http://184.95.51.83/) Powered by Discuz! X2.5