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 ..."