GDB 快速指南

本文提供了一份GDB(GNU调试器)的快速使用指南,适用于在命令行环境中进行程序调试。内容涵盖GDB的准备、启动、常用命令,如设置断点、单步执行、查看变量等,旨在帮助开发者高效地在终端中使用GDB。
摘要由CSDN通过智能技术生成

英文原文:http://beej.us/guide/bggdb/

概述

这是一篇关于 GNU 调试器—— gdb 在命令终端使用的快速指南,如果想要了解更多,请参阅 Official GDB Documentation 或者使用man gdb
gdb 经常是通过 IDE 运行,但是很多人由于各种各样的原因不使用 IDE,那么可以在终端中通过命令行来使用 gdb。
另外,有一个比较好的 gdb 前端 DDD(the Data Display Debugger)

准备

要使用 gdb 调试的程序必须包含符号调试信息,这样才能在调试器中看到程序列表。如果使用 gcc 编译器,那么编译时需要加上-g选项,例如:

$ gcc -g hello.c -o hello
$ g++ -g hello.cpp -o hello

开始

命令行中输入 gdb hello进入调试器:

$ gdb hello
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 hello...done.
(gdb) 

在调试器的命令提示行输入 help,来获取命令列表,也可参看文章末尾的命名列表:

(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
(gdb) 

使用quite或者q退出调试器:

(gdb) q
$  

两种打开调试器方式:控制台模式,GUI 窗口模式
让我们先来简单操作一下控制台模式:

$ gdb hello
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 documentat
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值