GDB基础知识

GDB 基础知识


一、简介

GDB, 是 The GNU Project Debugger 的缩写, 是 Linux 下功能全面的调试工具。

GDB 支持断点、单步执行、打印变量、观察变量、查看寄存器、查看堆栈等调试手段。

在 Linux 环境软件开发中,GDB 是主要的调试工具,用来调试 C 和 C++ 程序。

支持命令补全功能

任何时候都可以使用 TAB 进行补全,如果只有一个待选选项则直接补全;否则会列出可选选项,继续键入命令,同时结合 TAB 即可快速输入命令。

GDB 的调用与退出

调用:

gdb 程序

注意:要调试程序,需要在 gcc 编译可执行程序时加上 -g 参数,首先我们编译程序,生成可执行文件

示例

  1. 新建一个目录test
  2. 随意写一个C程序 xxx.c
  3. 编译 gcc xxx.c -o xxx -g
  4. 开始调试 gdb xxx

命令:

gcc xxx.c -o xxx -g // 编译xxx.c源文件,生成xxx可执行文件

gdb xxx // 启动gdb程序进行调试

退出:

(gdb) quit

或者

(gdb) exit

也可以快捷键Ctrl-d退出

代码显示list

list 可简写为l,可以列出所调试程序的代码

(gdb) list
  1. list line
    可以列出line(代码行数)附近的代码

  2. list function
    可以列出函数上下文的源程序

二、GDB 的调试基本指令

指令 说明
run/r 运行程序
break/b 设置断点
info breakpoints 查看断点信息
delete 删除断点
disable 关闭断点
enable 启用断点

如果要在 GDB 中使用外部的 shell 指令

! 指令

例子(使用 ls 查看):

! ls

1. run/r

说明:这个命令将会执行你调试的程序,如果遇到断点将停下来等待新的命令输入

使用方法(gdb程序已经启动):

(gdb) run

或者

(gdb) r

2. break/b

说明:

  • break line

在当前活动源文件line行处设置断点,

例如(在当前活动源文件的12行处设置断点):

(gdb) break 12
  • break function

在函数function()的入口处设置断点,

例如(在main()函数的入口处设置断点):

(gdb) break main

除此之外,还可以是当前工作目录下的其他源文件代码

  • break filename:line/function

假设当前活动源文件的同级目录下有一个名为hi.c的文件(要在该文件第6行设置断点):

(gdb) break hi.c:6

同上(要求在该文件main函数处设置断点):

(gdb) break hi.c:main

注意:如果不是当前工作目录下的源文件需要给出目标文件filename的相对路径或绝对路径

例如(目录test下):

test|
    - xxx.c
    - set|
         - a.c

命令:

gcc xxx.c -o xxx -g // 编译xxx.c源文件,生成xxx可执行文件

gdb xxx // 启动gdb程序进行调试

进入到GDB调试后,设置a.c中main函数的断点:

(gdb) break set/a.c:main

3. info breakpoints

设置一个断点:

(gdb) break main

随后

(gdb) info breakpoints

显示以下内容:

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000040052e in main at xxx.c:7

其中每一项的信息:

  • Num 代表断点编号,该编号可以作为 delete/enable/disable 等控制断点命令的参数
  • Type 代表断点类型,一般为 breakpoint
  • Disp 代表断点选中后状态,断点状态有保留(keep)、删除(del)、关闭(dis)
  • Enb 代表该断点是否启用 enable(y) 或 disable(n)
  • Address 代表该断点处虚拟内存的地址
  • What 代表该断点在源文件中的信息

4. delete

使用说明,需要结合 info breakpoints 中的断点编号

  • delete Num

注意:如果不带参数,直接使用默认全部删除

如果是多个选中可以使用空格:

(gdb) delete 1 3

选择断点编号为1和3的断点

5. disable 与 enable

disable 命令和 enable 命令的使用同样结合 info breakpoints 中的断点编号,与 delete 一样

使用后,作用于info breakpoints 中的 Enb

指令 Enb
disable n
enable y

三、自我学习:help

这个可以说对学习GDB最为重要的命令

在外部

注意:目前没有调用 gdb 程序,不在其中

使用命令:

gdb --help

得到 GDB 这个工具的信息(建议亲试

在 gdb 程序内部

注意:已经在 gdb 程序中

获取所有命令的列表

(gdb) help all

获取指定命令的信息说明

格式:help command

例如(查看 run ):

(gdb) help run

得到帮助文档:

Start debugged program.  You may specify arguments to give it.
Args may include "*", or "[...]"; they are expanded using "sh".
Input and output redirection with ">", "<", or ">>" are also allowed.

With no arguments, uses arguments last specified (with "run" or "set args").
To cancel previous arguments and run with no arguments,
use "set args" without arguments.

其他命令使用方法相同

搜索某一词汇的相关指令

格式:apropos word

例如(查看 run ):

(gdb) apropos run

得到帮助文档:

! -- Execute the rest of the line as a shell command
attach -- Attach to a process or file outside of GDB
file -- Use FILE as program to be debugged
info proc -- Show /proc process information about any running process
interrupt -- Interrupt the execution of the debugged program
load -- Dynamically load FILE into the running program
make -- Run the ``make'' program using the rest of the line as arguments
r -- Start debugged program
rc -- Continue program being debugged but run it in reverse
reverse-continue -- Continue program being debugged but run it in reverse
run -- Start debugged program
running -- Running the program
set annotate -- Set annotation_level
set auto-connect-native-target -- Set whether GDB may automatically connect to the native target
set charset -- Set the host and target character sets
set circular-trace-buffer -- Set target's use of circular trace buffer
set debug infrun -- Set inferior debugging
set disable-randomization -- Set disabling of debuggee's virtual address space randomization
set disconnected-tracing -- Set whether tracing continues after GDB disconnects
set environment -- Set environment variable value to give the program
set exec-wrapper -- Set a wrapper for running programs

相信最后的 help 能够更好的大家自我提升

原文链接: https://www.cnblogs.com/shadow-/p/15781356.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    GDB基础知识

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/184501

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 上午10:44
下一篇 2023年2月12日 上午10:44

相关推荐