Documentation/translations/zh_TW/dev-tools/gdb-kernel-debugging.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_TW/dev-tools/gdb-kernel-debugging.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_TW/dev-tools/gdb-kernel-debugging.rst- Extension
.rst- Size
- 6214 bytes
- Lines
- 165
- Domain
- Support Tooling And Documentation
- Bucket
- Documentation
- Inferred role
- Support Tooling And Documentation: documentation
- Status
- atlas-only
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
- No top-level syscall, struct, function, initcall, or export declaration detected by the generator.
Annotated Snippet
.. highlight:: none
.. include:: ../disclaimer-zh_TW.rst
:Original: Documentation/process/debugging/gdb-kernel-debugging.rst
:Translator: 高超 gao chao <gaochao49@huawei.com>
通過gdb調試內核和模塊
=====================
Kgdb內核調試器、QEMU等虛擬機管理程序或基於JTAG的硬件接口,支持在運行時使用gdb
調試Linux內核及其模塊。Gdb提供了一個強大的python腳本接口,內核也提供了一套
輔助腳本以簡化典型的內核調試步驟。本文檔爲如何啓用和使用這些腳本提供了一個簡要的教程。
此教程基於QEMU/KVM虛擬機,但文中示例也適用於其他gdb stub。
環境配置要求
------------
- gdb 7.2+ (推薦版本: 7.4+) 且開啓python支持 (通常發行版上都已支持)
設置
----
- 創建一個QEMU/KVM的linux虛擬機(詳情請參考 www.linux-kvm.org 和 www.qemu.org )。
對於交叉開發,https://landley.net/aboriginal/bin 提供了一些鏡像和工具鏈,
可以幫助搭建交叉開發環境。
- 編譯內核時開啓CONFIG_GDB_SCRIPTS,關閉CONFIG_DEBUG_INFO_REDUCED。
如果架構支持CONFIG_FRAME_POINTER,請保持開啓。
- 在guest環境上安裝該內核。如有必要,通過在內核command line中添加“nokaslr”來關閉KASLR。
此外,QEMU允許通過-kernel、-append、-initrd這些命令行選項直接啓動內核。
但這通常僅在不依賴內核模塊時纔有效。有關此模式的更多詳細信息,請參閱QEMU文檔。
在這種情況下,如果架構支持KASLR,應該在禁用CONFIG_RANDOMIZE_BASE的情況下構建內核。
- 啓用QEMU/KVM的gdb stub,可以通過如下方式實現
- 在VM啓動時,通過在QEMU命令行中添加“-s”參數
或
- 在運行時通過從QEMU監視控制檯發送“gdbserver”
- 切換到/path/to/linux-build(內核源碼編譯)目錄
- 啓動gdb:gdb vmlinux
注意:某些發行版可能會將gdb腳本的自動加載限制在已知的安全目錄中。
如果gdb報告拒絕加載vmlinux-gdb.py(相關命令找不到),請將::
add-auto-load-safe-path /path/to/linux-build
添加到~/.gdbinit。更多詳細信息,請參閱gdb幫助信息。
- 連接到已啓動的guest環境::
(gdb) target remote :1234
使用Linux提供的gdb腳本的示例
----------------------------
- 加載模塊(以及主內核)符號::
(gdb) lx-symbols
loading vmlinux
scanning for modules in /home/user/linux/build
loading @0xffffffffa0020000: /home/user/linux/build/net/netfilter/xt_tcpudp.ko
loading @0xffffffffa0016000: /home/user/linux/build/net/netfilter/xt_pkttype.ko
Annotation
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: atlas-only.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.