# GNU 调试器

GNU 调试器（GDB）是一款可移植的源码级调试器，让程序员能够观察另一个程序在运行时的内部情况，或者查看该程序在崩溃那一刻正在做什么。<sup>[1](https://www.gnu.org/software/gdb/gdb.html)</sup> 它是 [GNU 工程](https://www.edgepedia.org/zh/gnu-project)的调试器，由[自由软件基金会](https://www.edgepedia.org/zh/free-software-foundation)维护，其手册的版权年份从 1988 年延续至 2026 年。<sup>[2](https://www.sourceware.org/gdb/current/onlinedocs/gdb)</sup>

| 关键事实 | 详情 |
|---|---|
| 用途 | 在受控条件下启动程序，在指定条件下使其停止，检查停止时发生了什么，并修改程序中的内容以试验修复方案<sup>[1](https://www.gnu.org/software/gdb/gdb.html)</sup> |
| 目标模式 | 被调试程序可以与 GDB 运行在同一台机器上（本机），也可以运行在另一台机器上（远程），或运行在模拟器上<sup>[1](https://www.gnu.org/software/gdb/gdb.html)</sup> |
| 平台 | 可运行于大多数主流 UNIX 和 Microsoft Windows 变体，以及 Mac OS X<sup>[1](https://www.gnu.org/software/gdb/gdb.html)</sup> |
| 起源 | 理查德·斯托曼（Richard Stallman）是 GDB 及许多其他 GNU 程序的原始作者<sup>[3](https://sourceware.org/gdb/download/onlinedocs/gdb.html/Contributors.html)</sup> |
| 发布历史 | 发布版本可追溯至 1988 年的 2.4/2.8 时期；下载页面上列出的最新版本是 GDB 18.1<sup>[4](https://www.sourceware.org/gdb/download/)</sup> |
| 停止机制 | 断点（在某个位置停止）、观察点（当表达式的值变化时停止）和捕获点（在 C++ 异常抛出或库加载等事件发生时停止）<sup>[5](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Breakpoints.html)</sup> |
| 脚本 | 在配置时具备所需库的情况下，内置 Python 和 Guile 脚本支持<sup>[6](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Configure-Options.html)</sup> |

## GDB 的功能

根据项目自身的概述，GDB 有四项主要功能：以任何可能影响程序行为的参数启动程序，在指定条件下暂停程序，在程序停止后检查其状态，以及修改程序以便用户尝试修复某个缺陷的影响后再调查另一个缺陷。<sup>[1](https://www.gnu.org/software/gdb/gdb.html)</sup> 它支持多种源语言，在项目概述中按字母顺序排列，从 Ada、汇编和 C 开始。<sup>[1](https://www.gnu.org/software/gdb/gdb.html)</sup>

## 断点、观察点和捕获点

断点使程序在到达某个特定位置时停止。断点通过 `break` 命令及其变体设置，可用行号、函数名或精确地址来指定位置；在某些系统上，可以在可执行文件运行之前就在共享库中设置断点。<sup>[5](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Breakpoints.html)</sup> GDB 为每个断点、观察点或捕获点分配一个编号，从 1 开始的连续整数，每个都可以独立启用或禁用。<sup>[5](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Breakpoints.html)</sup>

观察点在表达式的值发生变化时使程序停止，无需程序员预测变化发生的位置；这有时被称为数据断点。表达式可以是单个变量、强制转换为某种数据类型的地址（如 `*(int *)0x12345678`），或使用程序语言中有效运算符的任意复杂表达式。<sup>[7](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Set-Watchpoints.html)</sup>

**硬件观察点与软件观察点。** 取决于系统，观察点由软件或硬件实现。软件观察点通过单步执行程序并每次检测值来工作，比正常执行慢数百倍。在大多数 PowerPC 或 x86 系统等目标上，GDB 使用硬件观察点，不会拖慢运行中的程序，并在变化发生的精确指令处报告变化；而软件观察点则在下一个语句处报告变化。<sup>[7](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Set-Watchpoints.html)</sup> GDB 尽可能设置硬件观察点，`set can-use-hw-watchpoints 0` 命令即使在硬件支持的情况下也强制使用软件观察点。<sup>[7](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Set-Watchpoints.html)</sup> `rwatch` 和 `awatch` 命令分别在读取时和读取或写入时中断，只能设置硬件观察点。<sup>[7](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Set-Watchpoints.html)</sup>

捕获点在指定类型的事件发生时暂停执行——例如，当 C++ 异常被抛出时或当库被加载时。<sup>[5](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Breakpoints.html)</sup>

## 远程调试与 gdbserver

目标是程序所占用的执行环境。尽管 GDB 通常与被调试程序运行在同一台机器上，但 `target` 命令使得可以选择不同的目标——例如，从另一台物理机器上操作 GDB，或通过串行线路或 TCP/IP 链接管理一个自包含系统。<sup>[8](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Targets.html)</sup>

`gdbserver` 是类 Unix 系统上的控制程序，允许程序通过 `target remote` 或 `target extended-remote` 与远程 GDB 连接，而无需链接通常的调试桩。GDB 和 gdbserver 通过串行线路或 TCP 连接使用标准 GDB 远程串行协议进行通信。gdbserver 不需要程序的符号表，因此程序可以被剥离以节省空间；主机上的 GDB 负责所有符号处理。<sup>[9](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Server.html)</sup> 手册中带有警告：gdbserver 没有内置安全性：GDB 连接以与运行 gdbserver 的用户相同的权限提供对目标系统的访问，因此不应在连接到公共网络的情况下运行。<sup>[9](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Server.html)</sup>

远程协议自动检测调试桩支持的数据包，各个数据包可配置为开启、关闭或自动；数据包表映射了诸如 `Z0` 用于软件断点、`Z2` 到 `Z4` 分别用于写、读和访问观察点，以及 `qXfer:exec-file:read` 用于附加和运行等功能。<sup>[10](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Remote-Configuration.html)</sup> 在某些目标上，gdbserver 还支持跟踪点和快速跟踪点，后者需要在被调试程序中加载进程内代理库。<sup>[9](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Server.html)</sup>

## 内部结构

GDB 由三个主要子系统组成：用户界面、符号处理（符号端）和目标系统处理（目标端）。符号端可以描述为在没有运行中的程序时在 GDB 中能做的一切，包括目标文件读取器、调试信息解释器、符号表管理、源语言表达式解析，以及类型和值打印。目标端包括执行控制、栈帧分析和物理目标操作。这种划分并非正式的；例如，核心文件支持涉及两端，基本核心文件读取器在 BFD 中，而内存和寄存器内容由目标元素提供。<sup>[11](https://www.sourceware.org/gdb/5.1/onlinedocs/gdbint_2.html)</sup>

## 调试信息与优化代码

有效的调试要求程序使用 `-g` 标志编译以生成调试信息；这些信息保存在目标文件中，记录每个变量或函数的数据类型，并将源代码行号映射到可执行代码中的地址。GCC 支持 `-g` 配合或不配合 `-O`，使得调试优化后的代码成为可能，手册建议始终使用 `-g`。<sup>[12](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Compilation.html)</sup> 手册中描述 DWARF 是目前 GDB 中表达能力最强、支持最好的调试格式。<sup>[12](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Compilation.html)</sup>

在禁用优化的情况下，编译器生成的汇编以简单直接的方式对应于源代码；随着优化增强，生成的代码与源代码产生偏离。在调试使用 `-g -O` 编译的程序时，调试器显示的是实际存在的内容，执行路径可能与源文件不匹配。手册中给出的一个极端例子：一个定义了但从未使用的变量对 GDB 不可见，因为编译器将其优化掉了。<sup>[13](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Optimized-Code.html)</sup>

## 历史与发布

GDB 的发布版本存档可追溯至 1988 年的 2.4/2.8 时期。<sup>[4](https://www.sourceware.org/gdb/download/)</sup> 手册的贡献者部分列出了发布管理者，包括兰迪·史密斯（Randy Smith，3.0 至 3.2 版）、吉姆·金登（Jim Kingdon，3.3 至 3.5 版）、约翰·吉尔摩（John Gilmore，3.9 至 4.3 版，以及与斯图·格罗斯曼（Stu Grossman）合作的 4.4 至 4.8 版）、弗雷德·菲什（Fred Fish，4.9 至 4.16 版）、斯坦·谢布斯（Stan Shebs，4.14 版）、杰森·莫伦达（Jason Molenda，4.17 版）、吉姆·布兰迪（Jim Blandy，4.18 版）和安德鲁·卡格尼（Andrew Cagney，5.0 至 6.3 版）。<sup>[3](https://sourceware.org/gdb/download/onlinedocs/gdb.html/Contributors.html)</sup>

多个版本标志着项目新闻中记录的功能里程碑。GDB 6.5（2006 年 6 月）在 GNU/Linux 上增加了检查点，即程序状态的快照，调试器可以返回到该状态，被描述为如同回到保存检查点时的过去。<sup>[14](https://www.sourceware.org/gdb/news/)</sup> GDB 7.0（2009 年 9 月）包含了反向调试和进程记录与重放的首次公开发布，以及非停止调试、多架构调试和多被调试对象、多进程调试。<sup>[14](https://www.sourceware.org/gdb/news/)</sup> GDB 7.1（2010 年 3 月）增加了多程序调试和位置无关可执行文件（PIE）调试支持。<sup>[14](https://www.sourceware.org/gdb/news/)</sup>

近期版本延续了这一节奏。GDB 15.1（2024 年 7 月）需要 C++17 编译器（如 GCC 9 或更高版本）来构建 GDB 和 GDBserver。<sup>[14](https://sourceware.org/gdb/news/)</sup> GDB 16.1（2025 年 1 月 18 日）增加了 loongarch*-linux* 上的记录/重放支持、标记数据指针的观察点、AArch64 裸机上的 MTE（内存标记扩展）调试，以及一个新的 `gstack` bash 脚本，使用 GDB 打印运行中进程的栈回溯；GDB 16.2 和 16.3 作为修正版本分别于 2025 年 2 月和 4 月发布。<sup>[14](https://sourceware.org/gdb/news/)</sup> GDB 17.1（2025 年 12 月 20 日）增加了 x86-64 CET 影子栈支持、使用 AArch64 防护控制栈的 Linux 程序调试、rv64gc 架构的记录支持，以及在 Linux 上当 libthread_db 不可用时作为后备的内置 TLS 支持，适用于 x86_64、aarch64、ppc64、s390x 和 riscv 架构。<sup>[14](https://sourceware.org/gdb/news/)</sup>

## GDB 与其他调试器

LLDB 是 LLVM 工程的调试器，作为 Xcode 在 macOS 上的内置调试器，处理桌面机器以及 iOS 硬件和模拟器上的 C、Objective-C 和 C++ 调试；对于已经了解 GDB 的人，LLDB 文档提供了命令映射表，将常见任务对应到它们的 LLDB 等价命令。<sup>[15](https://lldb.llvm.org/)</sup>

## References

1. [GDB: The GNU Project Debugger, gnu.org](https://www.gnu.org/software/gdb/gdb.html)
2. [Debugging with GDB (current online manual), sourceware.org](https://www.sourceware.org/gdb/current/onlinedocs/gdb)
3. [Contributors, Debugging with GDB, sourceware.org](https://sourceware.org/gdb/download/onlinedocs/gdb.html/Contributors.html)
4. [Download GDB, sourceware.org](https://www.sourceware.org/gdb/download/)
5. [Breakpoints, Watchpoints, and Catchpoints, Debugging with GDB](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Breakpoints.html)
6. [Configure Options, Debugging with GDB](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Configure-Options.html)
7. [Setting Watchpoints, Debugging with GDB](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Set-Watchpoints.html)
8. [Specifying a Debugging Target, Debugging with GDB](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Targets.html)
9. [Using the gdbserver Program, Debugging with GDB](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Server.html)
10. [Remote Configuration, Debugging with GDB](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Remote-Configuration.html)
11. [Overall Structure, GDB Internals](https://www.sourceware.org/gdb/5.1/onlinedocs/gdbint_2.html)
12. [Compiling for Debugging, Debugging with GDB](https://sourceware.org/gdb/current/onlinedocs/gdb.html/Compilation.html)
13. [Debugging Optimized Code, Debugging with GDB](https://www.sourceware.org/gdb/current/onlinedocs/gdb.html/Optimized-Code.html)
14. [GDB News, sourceware.org](https://www.sourceware.org/gdb/news/)
15. [The LLDB Debugger, lldb.llvm.org](https://lldb.llvm.org/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/gnu-debugger

English original: https://www.edgepedia.org/gnu-debugger
