gdb调试

来源:互联网 发布:淘宝女装退货率在多少 编辑:程序博客网 时间:2024/06/10 18:40

1、编译时包含调试信息,用参数-g。如

gcc -o gdb_sample gdb_sample.c -g

2、启动GDB的方法有:
    1) gdb <program> 
       如:gdb gdb_sample,也可以用gdb直接进入调试环境,用file <program>挂接调试程序。
    2) gdb <program> core
       用gdb同时调试一个运行程序和core文件,core是程序非法执行后core dump后产生的文件。
    3) gdb <program> <PID>
       指定服务程序运行时的进程ID,gdb会自动挂接上去。
3、常用命令
    1)file: 加载调试程序
    2)b:打断点,如:b 26、 b main、b myMax等
    3)r:运行程序
    4)p:打印。如p m、p/x m。
      x  按十六进制格式显示变量。
      d  按十进制格式显示变量。
      u  按十六进制格式显示无符号整型。
      o  按八进制格式显示变量。
      t  按二进制格式显示变量。 
      a  按十六进制格式显示变量。
      c  按字符格式显示变量。
      f  按浮点数格式显示变量 
    5)s:单步运行,step into
    6)n:step over
    7)d:删除断点
    8)display:自动显示,如display g_max
    9)undisplay/delete display <num>:删除自动显示,num为编号。如:undisplay 5、underplay 2-5
    10)info display:打印出所有自动显示的信息
    11)set var/variable:修改变量值,如:set variable m=22
    12) tab键:敲击一次补全,敲击两次显示可补全的选项

    13)c:继续运行

    14)q:退出gdb



4、实例演示


详细步骤:

[zjy@livedvd ~]$ gcc -o gdb_sample gdb_sample.c -g

[zjy@livedvd ~]$ gdb

GNU gdb (GDB) Red Hat Enterprise Linux (7.2-92.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 "x86_64-redhat-linux-gnu".

For bug reporting instructions, please see:

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

(gdb) file gdb_sample

Reading symbols from /home/zjy/gdb_sample...done.

(gdb) b 13

Breakpoint 1 at 0x40050e: file gdb_sample.c, line 13.

(gdb) b myMax 

Breakpoint 2 at 0x4004d2: file gdb_sample.c, line 6.

(gdb) display g_max 

(gdb) r

Starting program: /home/zjy/gdb_sample 


Breakpoint 1, main () at gdb_sample.c:13

13     n = 10;

1: g_max = 0

Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.209.el6.x86_64

(gdb) n

14     m = 20;

1: g_max = 0

(gdb) undisplay 1

(gdb) p m

$1 = 2

(gdb) n

15     g_max = myMax(m, n);

(gdb) p m

$2 = 20

(gdb) set variable m=44

(gdb) s


Breakpoint 2, myMax (a=44, b=10) at gdb_sample.c:6

6     printf("Function myMax is called, a = %d, b = %d \n", a, b);

(gdb) n

Function myMax is called, a = 44, b = 10 

7     return ((a > b) ? a : b);

(gdb) n

8 }

(gdb) n

main () at gdb_sample.c:16

16     printf("max = %d \n", g_max);

(gdb) n

max = 44 

17     return 0;

(gdb) n

18 }(gdb) n

0x00007ffff7a66d1d in __libc_start_main () from /lib64/libc.so.6

(gdb) n

Single stepping until exit from function __libc_start_main,

which has no line number information.


Program exited normally.

(gdb) q

[zjy@livedvd ~]$ 




原创粉丝点击