GDB调试

来源:互联网 发布:php 动态实例化对象 编辑:程序博客网 时间:2024/06/06 04:19

另外有:

Gdb+core调试技术

    http://zhwen.org/xlog/?p=453


一、gdb简介和安装

GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具。一般来说,GDB主要帮忙你完成下面四个方面的功能:

1、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。
2、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)
3、当程序被停住时,可以检查此时你的程序中所发生的事。
4、动态的改变你程序的执行环境。

要使用GDB,首先确认是否安装了gdb,在命令行中执行gdb,会自动进入dgb命令界面中,看到下面信息说明已经安装。

GNU gdb (GDB) CentOS (7.0.1-42.el5.centos.1)
Copyright (C) 2009 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) Quit

没有安装的话,通过yum命令来安装。

yum install gdb

二、gdb常用命令

file 装入想要调试的可执行文件.
kill 终止正在调试的程序.
List 查看源代码
list lineNum 在lineNum的前后源代码显示出来
list + 列出当前行的后面代码行
list - 列出当前行的前面代码行
list function 列出函数
set listsize count 设置显示代码的行数
show listsize 显示打印代码的行数
list first,last  显示从first到last的源代码行
next 执行一行源代码但不进入函数内部.
step 执行一行源代码而且进入函数内部.
run 执行当前被调试的程序
quit 终止 gdb
watch 使你能监视一个变量的值而不管它何时被改变.
break 在代码里设置断点, 这将使程序执行到这里时被挂起.
make 使你能不退出 gdb 就可以重新产生可执行文件.
shell 使你能不离开 gdb 就执行 UNIX shell 命令.

三、调试c/c++程序

1、新建 hello.c 文件

#include <stdio.h>

int main(){
char str[]="hello!\n";
printf(str);
int a = 5;
int b = 6;
int c = add(a,b);
printf("%d\n",c);
return 1;
}

int add(int a,int b){
return a+b;
}

2、编译

gcc -o hello hello.c
gcc -g -o hello hello.c  要使用gdb调试,必须使用-g 参数

3、调试代码

gdb hello #载入hello程序

(gdb) l  #列出源码
1       #include <stdio.h>
2
3       int main(){
4           char str[]="hello!\n";
5           printf(str);
6           int a = 5;
7           int b = 6;
8           int c = add(a,b);
9           printf("%d\n",c);
10          return 1;
(gdb) break 5  #在第5行下断
Breakpoint 1 at 0x4004ab: file hello.c, line 5.
(gdb) info break #查看当前的断点信息
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004004ab in main at hello.c:5
(gdb) r #运行程序 可以看到下面在第5行自动拉截了
Starting program: /opt/apps/gdb/hello

Breakpoint 1, main () at hello.c:5
5           printf(str);
(gdb) p str #打印str变量值
$1 = "hello!\n"
(gdb)  #单步执行下行代码
hello!
6           int a = 5;
(gdb) n #单步执行下行代码
7           int b = 6;
(gdb) p a #打印变量a
$2 = 5
(gdb) p b #打印变量b
$3 = 0
(gdb) n #单步执行下行代码
8           int c = add(a,b);
(gdb) p b #打印变量b
$4 = 6
(gdb) break add #在函数add方法里面下断
Breakpoint 2 at 0x4004fd: file hello.c, line 14.
(gdb)  #继续执行,直到断点位置
Continuing.

Breakpoint 2, add (a=5, b=6) at hello.c:14
14          return a+b;
(gdb) bt  #查看函数调用信息
#0  add (a=5, b=6) at hello.c:14
#1  0x00000000004004d7 in main () at hello.c:8
(gdb) finish #跳出函数
Run till exit from #0  add (a=5, b=6) at hello.c:14
0x00000000004004d7 in main () at hello.c:8
8           int c = add(a,b);
Value returned is $5 = 11
(gdb) c  #继续执行,直到断点位置
Continuing.
11

Program exited with code 01.
(gdb) q #退出gdb 上面已经正常执行完毕

 

除了通过gdb <program>  调试外,也可以执行下面2个调试命令

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

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

四、参考

原创粉丝点击