GDB简单使用手册

1.   主程序:

#include <stdio.h>

 

void emit(int i);

 

int main(){

    int a = 0;

    int i;

    for(i=0;i<10;i++){

        emit(i);

    }

    printf("Exit");

    return 0;

}

 

void emit(int i){

    printf("i is %d/n",i);

}

 

2.   编译

为了能生成可以使用GDB进行调试的程序,我们需要增加“-g”选项。gcc –g test.c –o test.out

 

3.   调试

a)        程序装入

可以使用两种方法装入程序:

                      i.              直接从命令行装入代调试程序,如:gdb test.out

nickcen@susecompaq:~/test > gdb test.out

GNU gdb 5.0rh-5 Red Hat Linux 7.1

This GDB was configured as "i386-redhat-linux"...

(gdb)

 

                   ii.              gdb装入,即通过file test.out,装入程序:

nickcen@susecompaq:~/test > gdb

GNU gdb 5.0rh-5 Red Hat Linux 7.1

This GDB was configured as "i386-redhat-linux".

(gdb) file test.out

Reading symbols from test.out...done.

(gdb)

 

b)        程序启动

我们可以通过“run(r)”命令来启动程序:

(gdb) r

Starting program: /data1/users/nickcen/test/test.out

i is 0

i is 9

Exit

Program exited normally.

(gdb)

 

c)        显示程序:

我们可以通过“list(l)”来显示源代码,以便设置断点,“list”命令每次显示10行源代码。“list”命令可以带行号作为参数,即“l 10”表示从第10行开始,显示。

(gdb) l

1       #include <stdio.h>

2

3       void emit(int i);

4

5       int main(){

6           int a = 0;

7           int i;

8           for(i=0;i<10;i++){

9               emit(i);

10          }

(gdb)

 

d)        设置断点:

我们可以通过“break(b)”命令来设置断点。“break”命令可以用行号,函数名,或者表达式的名称作为参数。

(gdb) b main

Breakpoint 3 at 0x8048416: file test.c, line 6.

(gdb) b 9

Breakpoint 4 at 0x8048430: file test.c, line 9.

(gdb)

 

e)        显示所有断点:

我们可以通过“info b”命令,来显示所有已经设置的断点。

(gdb) info b

Num Type           Disp Enb Address    What

3   breakpoint     keep y   0x 08048416 in main at test.c:6

4   breakpoint     keep y   0x 08048430 in main at test.c:9

 

f)        禁用/启用断点:

我们可以通过“disable”和“enable”命令来禁用和启动断点,这两个命令的参数都是断点的编号。

 

(gdb) info b

Num Type           Disp Enb Address    What

3   breakpoint     keep y   0x 08048416 in main at test.c:6

4   breakpoint     keep y   0x 08048430 in main at test.c:9

(gdb) disable 3

(gdb) info b

Num Type           Disp Enb Address    What

3   breakpoint     keep n   0x 08048416 in main at test.c:6

4   breakpoint     keep y   0x 08048430 in main at test.c:9

(gdb) enable 3

(gdb) info b

Num Type           Disp Enb Address    What

3   breakpoint     keep y   0x 08048416 in main at test.c:6

4   breakpoint     keep y   0x 08048430 in main at test.c:9

(gdb)

 

g)        删除断点:

我们可以通过“clear”和“delete(d)”命令来删除断点。其中“clear”的参数值与“break”同,只能是函数名,行号,表达式等,而“delete”命令的参数值,则是断点的编号。

(gdb) info b

Num Type           Disp Enb Address    What

5   breakpoint     keep y   0x 08048416 in main at test.c:6

6   breakpoint     keep y   0x 08048430 in main at test.c:9

7   breakpoint     keep y   0x 08048466 in emit at test.c:16

(gdb) clear 9

Deleted breakpoint 6

(gdb) d 7

(gdb) info b

Num Type           Disp Enb Address    What

5   breakpoint     keep y   0x 08048416 in main at test.c:6

(gdb)

 

h)        断点操作:

我们可以通过“step(s)”和“next(n)”命令进行断点的执行。其中“step”表示单步进入,而“next”表示单步执行。

(gdb) n

main () at test.c:8

8           for(i=0;i<10;i++){

(gdb) n

9               emit(i);

(gdb) n

i is 5

8           for(i=0;i<10;i++){

(gdb) n

9               emit(i);

(gdb) s

emit (i=6) at test.c:16

16          printf("i is %d/n",i);

(gdb) n

i is 6

17      }

(gdb) n

main () at test.c:8

8           for(i=0;i<10;i++){

(gdb)

 

i)        打印变量值:

通过“print(p)”命令,我们可以打印出某一变量的值。

(gdb) p i  

$1 = 6

(gdb)

 

j)        打印栈中的值:

通过“backtrace(bt)”我们可以打印出当前栈中的值。

(gdb) bt

#0  main () at test.c:9

#1  0x 40054c 6f in __libc_start_main () from /lib/libc.so.6

(gdb)

 

k)        退出

我们可以通过“quit(q)”命令退出当前的调试环境。

(gdb) q

nickcen@susecompaq:~/test >

 

l)        使用帮助

我们可以使用“help”命令获取帮助。其中“help”的参数值为需要帮助的内容。

(gdb) help breakpoints

Making program stop at certain points.

 

List of commands:

 

awatch -- Set a watchpoint for an expression

break -- Set breakpoint at specified line or function

xbreak -- Set breakpoint at procedure exit

 

Type "help" followed by command name for full documentation.

Command name abbreviations are allowed if unambiguous.

(gdb)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值