[gdb程序调试工具]GDB程序调试工具的使用例子

更新时间:2020-11-23    来源:linux    手机版     字体:

【www.bbyears.com--linux】


1、启动被调试程序。
2、让被调试的程序在指定的位置停住。
3、当程序被停住时,可以检查程序状态(如变量值)。

接下来实践一下,vim test.c

#include
int main()
{
    int i;
    long result = 0;
    for(i=0; i<100; i++){
        result+=i;
    }
    printf("result=%d\n",result);
    return 0;
}
你可以按照下面步骤来进行调试:

1. 编译生成可执行文件:
gcc -g test.c -o test

2. 启动GDB
gdb test

3. 在main函数处设置断点
break main

4. 运行程序
run

5. 单步运行
next

6. 继续运行
continue

1、启动GDB

两种方法:

(1) gdb 调试程序名

例:gdb test

(2) gdb

file 调试程序名

2、GDB命令

list(l)  查看程序

break(b) 函数名  在某函数入口处添加断点

break(b) 行号   在指定行添加断点

break(b) 文件名:行号  在指定文件的指定行添加断点

break(b) 行号 if 条件  当条件为真时,指定行号处断点生效,例b 5 if i=10,当i等于10时第5行断点生效

info break  查看所有设置的断点

delete 断点编号   删除断点

run(r)  开始运行程序

next(n)  单步运行程序(不进入子函数)

step(s)  单步运行程序(进入子函数)

continue(c) 继续运行程序

print(p) 变量名  查看指定变量值

finish  运行程序,直到当前函数结束

watch 变量名  对指定变量进行监控

quit(q)  退出gdb

上面的命令都很基础简单,依次敲一遍即可掌握使用方法

一个调试示例
——————

源程序:tst.c

     1 #include
     2
     3 int func(int n)
     4 {
     5         int sum=0,i;
     6         for(i=0; i      7         {
     8                 sum+=i;
     9         }
    10         return sum;
    11 }
    12
    13
    14 main()
    15 {
    16         int i;
    17         long result = 0;
    18         for(i=1; i<=100; i++)
    19         {
    20                 result += i;
    21         }
    22
    23        printf("result[1-100] = %d /n", result );
    24        printf("result[1-250] = %d /n", func(250) );
    25 }

编译生成执行文件:(Linux下)
    hchen/test> cc -g tst.c -o tst

使用GDB调试:

hchen/test> gdb tst  <---------- 启动GDB
GNU gdb 5.1.1
Copyright 2002 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-suse-linux"...
(gdb) l     <-------------------- l命令相当于list,从第一行开始例出原码。
1        #include
2
3        int func(int n)
4        {
5                int sum=0,i;
6                for(i=0; 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 tst.c, line 16.
(gdb) break func  <-------------------- 设置断点,在函数func()入口处。
Breakpoint 2 at 0x8048456: file tst.c, line 5.
(gdb) info break  <-------------------- 查看断点信息。
Num Type           Disp Enb Address    What
1   breakpoint     keep y   0x08048496 in main at tst.c:16
2   breakpoint     keep y   0x08048456 in func at tst.c:5
(gdb) r           <--------------------- 运行程序,run命令简写
Starting program: /home/hchen/test/tst

Breakpoint 1, main () at tst.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 tst.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 tst.c:5
#1  0x080484e4 in main () at tst.c:24
#2  0x400409ed in __libc_start_main () from /lib/libc.so.6
(gdb) finish    <--------------------- 退出函数。
Run till exit from #0  func (n=250) at tst.c:5
0x080484e4 in main () at tst.c:24
24              printf("result[1-250] = %d /n", func(250) );
Value returned is $6 = 31375
(gdb) c     <--------------------- 继续运行。
Continuing.
result[1-250] = 31375    <----------程序输出。

Program exited with code 027. <--------程序退出,调试结束。
(gdb) q     <--------------------- 退出gdb。
hchen/test>

好了,有了以上的感性认识,还是让我们来系统地认识一下gdb吧。

 


使用GDB
————

一般来说GDB主要调试的是C/C++的程序。要调试C/C++的程序,首先在编译时,我们必须要把调试信息加到可执行文件中。使用编译器(cc/gcc/g++)的 -g 参数可以做到这一点。如:

    > cc -g hello.c -o hello
    > g++ -g hello.cpp -o hello

如果没有-g,你将看不见程序的函数名、变量名,所代替的全是运行时的内存地址。当你用-g把调试信息加入之后,并成功编译目标代码以后,让我们来看看如何用gdb来调试他。

启动GDB的方法有以下几种:

    1、gdb
       program也就是你的执行文件,一般在当然目录下。

    2、gdb core
       用gdb同时调试一个运行程序和core文件,core是程序非法执行后core dump后产生的文件。

    3、gdb
       如果你的程序是一个服务程序,那么你可以指定这个服务程序运行时的进程ID。gdb会自动attach上去,并调试他。program应该在PATH环境变量中搜索得到。

 

GDB启动时,可以加上一些GDB的启动开关,详细的开关可以用gdb -help查看。我在下面只例举一些比较常用的参数:

    -symbols
    -s
    从指定文件中读取符号表。

    -se file
    从指定文件中读取符号表信息,并把他用在可执行文件中。

    -core
    -c
    调试时core dump的core文件。

    -directory
    -d
    加入一个源文件的搜索路径。默认搜索路径是环境变量中PATH所定义的路径。

本文来源:http://www.bbyears.com/caozuoxitong/112292.html

热门标签

更多>>

本类排行