首页 > 代码库 > 嵌入式开发值GDB调试程序实现

嵌入式开发值GDB调试程序实现

GDBGNU发布的一款更能强大的程序调试工具。

GDB主要完成下面三个功能:

1、启动被调试程序,

2、让被调试的程序在指定的位置停住,

3、当程序停住时可以检测变量的值.

 

#include<stdio.h>

Void main()

{

 Int i;

For(i=1;i<100;i++)

  {

Return +=i;

 

}

 

Printf(result i%ds = ,i);

 

}

 

GDB快速进阶值调试步骤:

        1、编译生成可执行文件

        Gcc -g test.c -o test

        2、启动GDB

        Gdb test

        3、main函数处设置断点

        Break main

        4、运行程序

        run

        5、下一步  next

        6、结束     c

GDB命令演示

List(l) 查看程序

gdb) list

1#include<stdio.h>

2void main()

3{

4 int i;

5long result=0;

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

7{

8result +=i;

9}

10

(gdb)

 

        Breakb)函数名 在某函数入口添加断点

         

        Breakb)行号 在指定行添加断点

        Breakb)文件 在指定文件的指定行添加断点

        Breakb)行号加if条件当条件为真时,指定行号处断点生效

        例如:b 5 if i=10

        Info break 查看断点分布

        Delete 断点编号 删除断点

        Run 开始运行程序

        Next 单步执行程序(不进入子函数)

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

        Continue 继续运行程序

        Print 变量名 查看指定变量值

        Finish 运行程序到结束

        Watch 变量名 对指定变量监控

        Quit 退出调试状态

 

 

[oldboy@oldboy2 Scripts]$ gcc -o gdb gdb.c

[oldboy@oldboy2 Scripts]$ ll

total 32

-rwxrwxr-x 1 oldboy oldboy 4707 Mar 13 06:53 gdb

-rw-rw-r-- 1 oldboy oldboy  129 Mar 13 06:52 gdb.c

-rwxrwxr-x 1 oldboy oldboy 4665 Mar 13 06:20 hello

-rw-rw-r-- 1 oldboy oldboy   63 Mar 13 06:18 hello.c

-rw-rw-r-- 1 oldboy oldboy  860 Mar 13 06:43 hello.o

-rw-rw-r-- 1 oldboy oldboy  352 Mar 13 06:42 hello.s

[oldboy@oldboy2 Scripts]$ ./gdb

result=5050

[oldboy@oldboy2 Scripts]$ gcc -g gdb.c  -o

gcc: argument to ‘-o‘ is missing

[oldboy@oldboy2 Scripts]$ gcc -g gdb.c  -o gdbts

[oldboy@oldboy2 Scripts]$ ll

total 40

-rwxrwxr-x 1 oldboy oldboy 4707 Mar 13 06:53 gdb

-rw-rw-r-- 1 oldboy oldboy  129 Mar 13 06:52 gdb.c

-rwxrwxr-x 1 oldboy oldboy 5863 Mar 13 07:00 gdbts

-rwxrwxr-x 1 oldboy oldboy 4665 Mar 13 06:20 hello

-rw-rw-r-- 1 oldboy oldboy   63 Mar 13 06:18 hello.c

-rw-rw-r-- 1 oldboy oldboy  860 Mar 13 06:43 hello.o

-rw-rw-r-- 1 oldboy oldboy  352 Mar 13 06:42 hello.s

[oldboy@oldboy2 Scripts]$ gdb gdbts

GNU gdb (GDB) Red Hat Enterprise Linux (7.2-90.el6)

Copyright (C) 2010 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 "i686-redhat-linux-gnu".

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>...

Reading symbols from /home/oldboy/Scripts/gdbts...done.

(gdb) b main

Breakpoint 1 at 0x80483cd: file gdb.c, line 5.

(gdb) run

Starting program: /home/oldboy/Scripts/gdbts

 

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

5long result=0;

Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.107.el6.i686 glibc-2.12-1.192.el6.i686

(gdb) next

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

(gdb) next

8result +=i;

(gdb) next

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

(gdb) next

8result +=i;

(gdb) next

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

(gdb) c

Continuing.

result=5050

 

Program exited with code 014.

(gdb)

 

 

 

Breakpoint 6, main () at gdb.c:8

8result +=i;

(gdb) print i

$5 = 24

(gdb) next

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

(gdb) print i

$6 = 24

(gdb) next

 

Breakpoint 6, main () at gdb.c:8

8result +=i;

(gdb) print i

$7 = 25

(gdb) quit

A debugging session is active.

 

Inferior 1 [process 3082] will be killed.

 

Quit anyway? (y or n) y

 

嵌入式开发值GDB调试程序实现