【Linux】GDB/CGDB 调试器学习笔记
GDB/CGDB 调试器学习笔记
🚀 前言
GDB 是 GNU 项目下功能强大的命令行调试器,适用于 C/C++ 等多种语言。CGDB 则是在 GDB 之上构建的轻量级 curses 界面,适合喜欢终端操作且习惯 vi 风格的人。
一、GDB 入门篇
1. 编译时带调试信息
gcc -g -O0 -Wall -o myprog myprog.c
其中:
-g
:生成调试信息;-O0
:关闭优化,调试体验更准确 ([Red Hat Developers][1])。
2. 启动 GDB
最基本的启动方式:
gdb myprog
(gdb) run
也可以附加参数:
gdb --args myprog arg1 arg2
。
3. 常用命令
命令 | 作用 |
---|---|
help / apropos | 查看命令帮助 |
break LOCATION (b ) | 设置断点 |
run (r ) | 运行程序 |
next (n ) | 单步,不进入函数 |
step (s ) | 单步,进入函数 |
continue (c ) | 继续运行到下一个断点 |
print VAR (p ) | 打印变量值 |
info locals/args/breakpoints | 查看本地变量、参数、断点信息 |
watch VAR | 监视变量读写 ([TechBeamers][2], [Stanford University][3]) |
list
(l
): 显示当前源代码附近几行 ([Stanford University][3])。p arr@count
: 打印数组或指针连续元素 ([Stanford University][3])。
4. 多线程 & 回溯调试
- 调试多线程:使用
info threads
查看线程;结合watch
可观察线程变量读写 。 - 可逆调试(reverse debugging):支持
reverse-continue (rc)
、reverse-step (rs)
等命令,回溯执行路径 ([Stack Overflow][4])。
二、CGDB:GDB 的终端 GUI 增强
1. CGDB 简介
CGDB 是基于 curses 的终端调试界面,提供源代码与 GDB 窗口的分屏显示,键盘操作类似 vi ([cgdb.github.io][5])。
2. 安装方法
一般用源码编译:
./configure
make
sudo make install
依赖 ncurses
与 readline
([heather.cs.ucdavis.edu][6])。
3. 快速入门操作
-
启动:
cgdb myprog
-
窗口之间切换:
- 源码窗:按
Esc
; - GDB 窗:按
i
([cgdb.github.io][5], [Siddharth’s Blog][7])。
- 源码窗:按
-
设置断点:源码窗中移动光标,按
space
。 -
调整源码窗大小:用
-
或=
。 -
常用 GDB 命令:在 GDB 窗中使用
b
,n
,s
,c
,bt
,info threads
等 ([cseweb.ucsd.edu][8], [Android blog][9])。
4. 为什么使用 CGDB?
- 比 GDB 的 TUI 模式更稳定、有颜色显示 ([heather.cs.ucdavis.edu][6])。
- 操作流畅、界面简洁,适合 SSH 终端环境。
- 支持正则搜索、语法高亮、滚动命令历史、Tab 补全等 ([cgdb.github.io][5])。
三、高级技巧与定制篇
1. .gdbinit
与自动加载
-
启用历史记录:
set history save on set history size 10000 set history filename ~/.gdb_history
-
支持自动加载本地文件:
add-auto-load-safe-path .
。
-
项目级
.gdbinit
:通过gdb -x project.gdbinit
加载,方便团队共享 ([Interrupt][10])。
2. 条件断点、命令、watchpoint
-
条件断点:
break foo if count > 100
-
在断点上附加命令:
break foo commandsprint xcontinue end
-
动态监视变量读写:
watch var
、watch -l var
([Reddit][11])。
3. Python 脚本 & 自定义宏
- 使用 GDB Python API,编写自定义命令和输出美化功能 ([Reddit][12])。
- 示例:定义函数
my_function
自动打印$arg0
,能在断点时执行一系列操作 ([Medium][13])。
4. Pretty Printers & 插件
- 利用 pretty-printer 美化 STL、protobuf 等复杂结构 。
- 安装插件如 GEF、pwndbg 来扩展内存剖析、反汇编能力 。
5. 核心转储 & 远程调试
- 使用
gdb myprog --core core.1234
查看崩溃现场 ([Red Hat Developers][1])。 - 使用
gdbserver
+target remote
实现远端目标调试。
6. 脚本化 & 批处理
-
启动时预设命令:
gdb -ex "break main" -ex run myprog
-
使用
-batch
模式运行脚本并自动退出,适用于 CI 环境 。
四、延伸
以下是关于 Red Hat 系列 GDB 教程 及 Interrupt(由 Memfault 发布)的进阶技巧与 .gdbinit
配置 的详细介绍。
4.1 Red Hat 系列 GDB 教程
Red Hat 提供了一套结构清晰、由浅入深的 GDB 教学系列,适合从入门到进阶的用户。
1. 初学者指南 ▶ “Getting started with the debugger”(第一篇)
-
作者:Keith Seitz,发表于 2021 年4月 30日。
-
涵盖 GDB 的基础使用流程,包括:
- 使用
gcc -g3 -O0
编译以生成完整调试信息; - 通过
help
、apropos
探索 GDB 命令; - 帮助新用户移除命令分页、打开历史记录等设置 ([Red Hat Developers][1], [Stanford University][2])。
- 使用
2. Printf 样式调试 ▶ “Printf‑style debugging using GDB, Part 3”
-
作者:Kevin Buettner,发表于 2021 年12月 9日。
-
展示如何使用断点触发函数调用,例如:
- 设置
break insert
; - 使用 GDB 命令在每次断点触发时自动调用程序内部的打印函数,实现“打印式”调试 ([Red Hat Developers][3])。
- 设置
3. 逆向执行与时间旅行 ▶ “Using GDB to time travel” 和 “Advanced time manipulation with GDB”
- 最新一篇发表于 2025 年6月 4日,由 Guinevere Larsen 撰写。
- 展示 GDB 的逆向调试功能 —— 如
record stop
、reverse-continue
等命令。 - 介绍使用“时间循环”调试随机行为程序(例如游戏中的 hit/miss 逻辑) ([Red Hat Developers][4])。
总结:Red Hat 教程覆盖了从编译、断点与打印调试、命令自动化,再到逆向调试的完整流程,非常适合构建扎实调试能力。
4.2 Interrupt:Memfault 的进阶 GDB 使用技巧 & .gdbinit
由 Memfault 发布的文章“Advanced GDB Usage”深入提升调试效率,提供大量技巧和 .gdbinit
配置建议 ([Interrupt][5])。
核心亮点包括:
1. apropos
搜索命令
通过:
(gdb) apropos regex
快速查找相关命令,尤其适用于超过1500条 GDB 指令环境 。
2. 启用命令历史记录
在 .gdbinit
中加入:
set history save on
set history size 10000
set history filename ~/.gdb_history
支持 Ctrl+R
进行交互式命令搜索 ([Interrupt][5])。
3. 项目级 .gdbinit
自动加载
建议为团队创建统一的 .gdbinit
,并通过脚本或 CLI 工具加载:
gdb -ix project.gdbinit myprog
便于共享宏、插件、加载配置 ([Interrupt][5])。
4. 源码路径映射
在代码路径不一致时,使用:
set substitute-path <orig> <local>
directory /local/src/…
确保调试器能正确定位源代码 。
5. 条件断点与 watchpoint
-
条件断点示例:
break foo if count > 100
-
watchpoint 示例:
watch var watch -l var
可指定变量被 “读/写” 时触发 ([Stack Overflow][6])。
6. Pretty‑printers 与 Python 脚本
- 自定义打印复杂结构(如 STL、protobuf);
- 使用 Python API 编写命令或自动化脚本。
7. 多线程调试 & Backtrace for All Threads
使用 thread apply all backtrace
展示所有线程堆栈;适合 RTOS / 嵌入式环境调试 ([Medium][7], [Interrupt][8])。
8. 插件支持
推荐使用 GEF、pwndbg 等插件增强 GDB,包括内存剖析、汇编视图等功能。
4.3 .gdbinit
示例模板
# 启用历史记录
set history save on
set history size 10000
set history filename ~/.gdb_history# 禁用分页
set pagination off# 自动加载本地 init 文件
add-auto-load-safe-path .# 源码路径映射
set substitute-path /build/dir /home/dev/project
directory /home/dev/project/src# 常用断点宏
define bpfuncbreak $arg0commandssilentbacktracecontinueend
end# Condition breakpoint 示例
# break process_data if data_size > 1024# Watchpoint 示例
# watch -l config# 加载 pretty‑printer 和插件
# source ~/gef/gef.py
# source ~/pwndbg/gdbinit.py
可以将此内容保存为 ~/.gdbinit
(或项目下的 project.gdbinit
),并通过 gdb -ix project.gdbinit
加载。
4.4 小结
资源 | 内容重点 |
---|---|
Red Hat GDB 教程 | 从基础编译、printf 风格调试到逆向调试,内容循序渐进。 |
Interrupt (Memfault) | 高效调试实用技巧:命令历史、源码映射、watchpoint、Python 自动化、插件支持等全面覆盖。 |
推荐做法:
- 先阅读 Red Hat 系列建立基础;
- 再应用 Interrupt 的
.gdbinit
和高级操作; - 最后通过 Python 脚本 + 插件(如 GEF/pwndbg)定制调试工具链。
5 参考资料
[1]: https://developers.redhat.com/articles/the-gdb-developers-gnu-debugger-tutorial-part-1-getting-started-with-the-debugger?utm_source=chatgpt.com "Get Started with our GNU Debugger Tutorial - Red Hat Developer"
[2]: https://web.stanford.edu/class/archive/cs/cs107/cs107.1194/resources/gdb?utm_source=chatgpt.com "CS107 GDB and Debugging"
[3]: https://developers.redhat.com/articles/2021/12/09/printf-style-debugging-using-gdb-part-3?utm_source=chatgpt.com "Printf-style debugging using GDB, Part 3 - Red Hat Developer"
[4]: https://developers.redhat.com/articles/2025/06/04/advanced-time-manipulation-gdb?utm_source=chatgpt.com "Advanced time manipulation with GDB - Red Hat Developer"
[5]: https://interrupt.memfault.com/blog/advanced-gdb?utm_source=chatgpt.com "Advanced GDB Usage - Interrupt - Memfault"
[6]: https://stackoverflow.com/questions/71966464/gdbs-gdbinit-issues-annoying-feedback-when-focus-cmd-is-used?utm_source=chatgpt.com "GDB's .gdbinit issues annoying feedback when \"focus cmd\" is used"
[7]: https://olof-astrand.medium.com/advanced-debugging-with-gdb-reverse-execution-pretty-printer-and-asan-b27ef335d036?utm_source=chatgpt.com "Advanced debugging with gdb (Reverse execution , Pretty-Printer ..."
[8]: https://community.memfault.com/t/advanced-gdb-usage-interrupt/284?utm_source=chatgpt.com "Advanced GDB Usage | Interrupt - Blog"[1]: https://developers.redhat.com/articles/the-gdb-developers-gnu-debugger-tutorial-part-1-getting-started-with-the-debugger?utm_source=chatgpt.com "Get Started with our GNU Debugger Tutorial - Red Hat Developer"
[2]: https://techbeamers.com/how-to-use-gdb-top-debugging-tips/?utm_source=chatgpt.com "GDB Tutorial: Essential GDB Tips to Learn Debugging - TechBeamers"
[3]: https://web.stanford.edu/class/archive/cs/cs107/cs107.1258/resources/gdb.html?utm_source=chatgpt.com "CS107 GDB and Debugging - Stanford University"
[4]: https://stackoverflow.com/questions/1471226/most-tricky-useful-commands-for-gdb-debugger?utm_source=chatgpt.com "Most tricky/useful commands for gdb debugger - Stack Overflow"
[5]: https://cgdb.github.io/docs/cgdb.html?utm_source=chatgpt.com "CGDB Manual 0.8.0"
[6]: https://heather.cs.ucdavis.edu/~matloff/cgdb.html?utm_source=chatgpt.com "A Quick-Start Tutorial on the CGDB Debugging Interface"
[7]: https://r3x.github.io/posts/gdb_advanced/?utm_source=chatgpt.com "Advanced GDB Debugging - Siddharth Muralee"
[8]: https://cseweb.ucsd.edu/classes/fa09/cse141/tutorial_gcc_gdb.html?utm_source=chatgpt.com "Tutorial of gcc and gdb - UCSD CSE"
[9]: https://mhandroid.wordpress.com/2011/01/23/using-cgdb-with-ndk-debug-and-cgdb-tutorial/?utm_source=chatgpt.com "Using cgdb with ndk-debug (and cgdb tutorial) - Android blog"
[10]: https://interrupt.memfault.com/blog/advanced-gdb?utm_source=chatgpt.com "Advanced GDB Usage - Interrupt - Memfault"
[11]: https://www.reddit.com/r/C_Programming/comments/13tyt0z/a_quick_intro_to_gdb/?utm_source=chatgpt.com "A quick intro to gdb. : r/C_Programming - Reddit"
[12]: https://www.reddit.com/r/cpp/comments/iw3h9w/protips_for_gdb/?utm_source=chatgpt.com "Protips for GDB? : r/cpp - Reddit"
[13]: https://altmannmarcelo.medium.com/gdb-advanced-techniques-expanding-gdb-functionality-with-custom-function-execution-ccf50894f61b?utm_source=chatgpt.com "GDB Advanced Techniques: Expanding GDB Functionality with ..."