gdb调试



1 GDB的主要功能:

A 启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。

B 可以让被调试的程序在你所指定的调试的断点处停住。(断点可以是条件表达式)

C 当程序被停止住是,可以检查此时你的程序中所发生的事。

D 动态的改变你程序的执行环境。

 

2 载入程序的两种方式

前提:编译程序的时候加上了调试命令  -g,比如gcc demo.c–g app

 

A 在启动gdb后(也就是说在终端窗口先执行gdb这个命令),接着执行以下命令:

file 可执行文件路径   比如:在当前路径下有app,那么命令就是:fileapp

toto@toto-pc:~/test$ gdb

GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1

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

Type "show configuration" for configuration details.

For bug reporting instructions, please see:

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

Find the GDB manual and other documentation resources online at:

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

For help, type "help".

Type "apropos word" to search for commands related to "word".

(gdb) file app

B gdb启动时就载入程序:

toto@toto-pc:~/test$ gdb app

GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1

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

Type "show configuration" for configuration details.

For bug reporting instructions, please see:

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

Find the GDB manual and other documentation resources online at:

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

For help, type "help".

Type "apropos word" to search for commands related to "word"...

Reading symbols from app...done.

(gdb)

 

3 一个调试示例(test.c

  #include<stdio.h>

   

   int func(int n) {

      int sum = 0,i;

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

         sum += i;

      }

    

      return sum;

}

  

  int main(void)

  {

      int i;

      long result = 0;

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

          result += i;

      }

  

      printf("result[1-100] = %d \n",result);

      printf("result[1-250] = %d \n",func(250));

}

编译生成执行文件(Linux):

gcc -g test.c -o test

使用GDB调试:

gdb test

toto/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 <stdio.h>
2
3        int func(int n)
4        {
5                int sum=0,i;
6                for(i=0; i<n; 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/toto/test/test

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
toto/test>

4  运行GDB

gdb <program>program也就是你的执行文件,一般在当前目录下。

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

gdb <program><PID>  -- 调用正在运行的程序。Program为需要调用的程序文件,PID为当前正在运行的程序。或是先用gdb <program>关联源代码进入gdb,后用attatch命令来挂接进程PID,并用detach来取消挂接的进程。

5 常用命令

命令

简写

作用

help

h

按模块列出命令类

help class

 

查看某一类型的具体命名

list

l

查看代码,可跟行号和函数名

quit

q

退出gdb

run

r

全速运行程序

step

s

逐语句执行,遇到函数,跳到函数内执行

backtrace

bt

查看函数的调用的栈帧和层级关系。

info

i

查看函数内部局部变量的数值

frame

f

切换函数的栈帧

finish

 

结束当前函数,返回函数调用点

set

 

设置变量的值

run argv[1] argv[2]

 

调试时命令行传参

print

p

打印变量和地址

break

b

设置断点,可根据行号和函数名

delete

d

删除断点,d breakpoints NUM

display

 

设置观察变量

undisplay

 

取消观察变量

continue

c

继续全速运行剩下的代码

enable breakpoints

 

启用断点

disable breakpoints

 

禁用断点

x

 

查看没存  x/20xw  显示20个单元,16进制,4字节每单元

watch

 

被设置观察点的变量发生修改时,打印显示。

i watch

 

现实观察点

core

 

ulimit –c 1024 开启core文件,调试时 gdb a.out core

6  gdb调试模式

run 全速运行

start 单步调试

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值