gdb调试

简介:  1 GDB的主要功能: A 启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。 B 可以让被调试的程序在你所指定的调试的断点处停住。(断点可以是条件表达式) C 当程序被停止住是,可以检查此时你的程序中所发生的事。 D 动态的改变你程序的执行环境。   2 载入程序的两种方式 前提:编译程序的时候加上了调试命令  -


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 单步调试

 

 

 

相关实践学习
阿里云图数据库GDB入门与应用
图数据库(Graph Database,简称GDB)是一种支持Property Graph图模型、用于处理高度连接数据查询与存储的实时、可靠的在线数据库服务。它支持Apache TinkerPop Gremlin查询语言,可以帮您快速构建基于高度连接的数据集的应用程序。GDB非常适合社交网络、欺诈检测、推荐引擎、实时图谱、网络/IT运营这类高度互连数据集的场景。 GDB由阿里云自主研发,具备如下优势: 标准图查询语言:支持属性图,高度兼容Gremlin图查询语言。 高度优化的自研引擎:高度优化的自研图计算层和存储层,云盘多副本保障数据超高可靠,支持ACID事务。 服务高可用:支持高可用实例,节点故障迅速转移,保障业务连续性。 易运维:提供备份恢复、自动升级、监控告警、故障切换等丰富的运维功能,大幅降低运维成本。 产品主页:https://www.aliyun.com/product/gdb
目录
相关文章
|
2月前
|
NoSQL 搜索推荐 openCL
【C/C++ 调试 GDB指南 】gdb调试基本操作
【C/C++ 调试 GDB指南 】gdb调试基本操作
68 2
|
4月前
|
NoSQL Linux 开发工具
【深入解析git和gdb:版本控制与调试利器的终极指南】(下)
【深入解析git和gdb:版本控制与调试利器的终极指南】
|
4月前
|
NoSQL 小程序 C语言
GDB调试学习(四):段错误
GDB调试学习(四):段错误
43 0
|
4月前
|
NoSQL
GDB调试学习(三):观察点
GDB调试学习(三):观察点
41 0
|
4月前
|
NoSQL
GDB调试学习(二):断点
GDB调试学习(二):断点
39 0
|
18天前
|
NoSQL Ubuntu 测试技术
【GDB自定义指令】core analyzer结合gdb的调试及自定义gdb指令详情
【GDB自定义指令】core analyzer结合gdb的调试及自定义gdb指令详情
12 1
|
18天前
|
NoSQL 编译器 C语言
【GDB调试技巧】提高gdb的调试效率
【GDB调试技巧】提高gdb的调试效率
11 1
|
18天前
|
NoSQL Ubuntu 开发工具
【gdb调试】在ubuntu环境使用gdb调试一棵四层二叉树的数据结构详解
【gdb调试】在ubuntu环境使用gdb调试一棵四层二叉树的数据结构详解
8 1
|
2月前
|
NoSQL C++ 开发者
【C/C++ 调试 GDB指南 】GDB中的‘info’命令:一次全面的探索
【C/C++ 调试 GDB指南 】GDB中的‘info’命令:一次全面的探索
43 0
|
2月前
|
NoSQL Shell 程序员
【C/C++ 调试 GDB指南 】GDB调试工具介绍:从基础到高级
【C/C++ 调试 GDB指南 】GDB调试工具介绍:从基础到高级
68 0