xref: /openbmc/linux/Documentation/translations/zh_CN/dev-tools/gdb-kernel-debugging.rst (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1*e315b831Sgaochao.. highlight:: none
2*e315b831Sgaochao
3*e315b831Sgaochao.. include:: ../disclaimer-zh_CN.rst
4*e315b831Sgaochao
5*e315b831Sgaochao:Original: Documentation/dev-tools/gdb-kernel-debugging.rst
6*e315b831Sgaochao:Translator: 高超 gao chao <gaochao49@huawei.com>
7*e315b831Sgaochao
8*e315b831Sgaochao通过gdb调试内核和模块
9*e315b831Sgaochao=====================
10*e315b831Sgaochao
11*e315b831SgaochaoKgdb内核调试器、QEMU等虚拟机管理程序或基于JTAG的硬件接口,支持在运行时使用gdb
12*e315b831Sgaochao调试Linux内核及其模块。Gdb提供了一个强大的python脚本接口,内核也提供了一套
13*e315b831Sgaochao辅助脚本以简化典型的内核调试步骤。本文档为如何启用和使用这些脚本提供了一个简要的教程。
14*e315b831Sgaochao此教程基于QEMU/KVM虚拟机,但文中示例也适用于其他gdb stub。
15*e315b831Sgaochao
16*e315b831Sgaochao
17*e315b831Sgaochao环境配置要求
18*e315b831Sgaochao------------
19*e315b831Sgaochao
20*e315b831Sgaochao- gdb 7.2+ (推荐版本: 7.4+) 且开启python支持 (通常发行版上都已支持)
21*e315b831Sgaochao
22*e315b831Sgaochao设置
23*e315b831Sgaochao----
24*e315b831Sgaochao
25*e315b831Sgaochao- 创建一个QEMU/KVM的linux虚拟机(详情请参考 www.linux-kvm.orgwww.qemu.org )。
26*e315b831Sgaochao  对于交叉开发,https://landley.net/aboriginal/bin 提供了一些镜像和工具链,
27*e315b831Sgaochao  可以帮助搭建交叉开发环境。
28*e315b831Sgaochao
29*e315b831Sgaochao- 编译内核时开启CONFIG_GDB_SCRIPTS,关闭CONFIG_DEBUG_INFO_REDUCED。
30*e315b831Sgaochao  如果架构支持CONFIG_FRAME_POINTER,请保持开启。
31*e315b831Sgaochao
32*e315b831Sgaochao- 在guest环境上安装该内核。如有必要,通过在内核command line中添加“nokaslr”来关闭KASLR。
33*e315b831Sgaochao  此外,QEMU允许通过-kernel、-append、-initrd这些命令行选项直接启动内核。
34*e315b831Sgaochao  但这通常仅在不依赖内核模块时才有效。有关此模式的更多详细信息,请参阅QEMU文档。
35*e315b831Sgaochao  在这种情况下,如果架构支持KASLR,应该在禁用CONFIG_RANDOMIZE_BASE的情况下构建内核。
36*e315b831Sgaochao
37*e315b831Sgaochao- 启用QEMU/KVM的gdb stub,可以通过如下方式实现
38*e315b831Sgaochao
39*e315b831Sgaochao    - 在VM启动时,通过在QEMU命令行中添加“-s”参数
40*e315b831Sgaochao
41*e315b831Sgaochao42*e315b831Sgaochao
43*e315b831Sgaochao    - 在运行时通过从QEMU监视控制台发送“gdbserver”
44*e315b831Sgaochao
45*e315b831Sgaochao- 切换到/path/to/linux-build(内核源码编译)目录
46*e315b831Sgaochao
47*e315b831Sgaochao- 启动gdb:gdb vmlinux
48*e315b831Sgaochao
49*e315b831Sgaochao  注意:某些发行版可能会将gdb脚本的自动加载限制在已知的安全目录中。
50*e315b831Sgaochao  如果gdb报告拒绝加载vmlinux-gdb.py(相关命令找不到),请将::
51*e315b831Sgaochao
52*e315b831Sgaochao    add-auto-load-safe-path /path/to/linux-build
53*e315b831Sgaochao
54*e315b831Sgaochao  添加到~/.gdbinit。更多详细信息,请参阅gdb帮助信息。
55*e315b831Sgaochao
56*e315b831Sgaochao- 连接到已启动的guest环境::
57*e315b831Sgaochao
58*e315b831Sgaochao    (gdb) target remote :1234
59*e315b831Sgaochao
60*e315b831Sgaochao
61*e315b831Sgaochao使用Linux提供的gdb脚本的示例
62*e315b831Sgaochao----------------------------
63*e315b831Sgaochao
64*e315b831Sgaochao- 加载模块(以及主内核)符号::
65*e315b831Sgaochao
66*e315b831Sgaochao    (gdb) lx-symbols
67*e315b831Sgaochao    loading vmlinux
68*e315b831Sgaochao    scanning for modules in /home/user/linux/build
69*e315b831Sgaochao    loading @0xffffffffa0020000: /home/user/linux/build/net/netfilter/xt_tcpudp.ko
70*e315b831Sgaochao    loading @0xffffffffa0016000: /home/user/linux/build/net/netfilter/xt_pkttype.ko
71*e315b831Sgaochao    loading @0xffffffffa0002000: /home/user/linux/build/net/netfilter/xt_limit.ko
72*e315b831Sgaochao    loading @0xffffffffa00ca000: /home/user/linux/build/net/packet/af_packet.ko
73*e315b831Sgaochao    loading @0xffffffffa003c000: /home/user/linux/build/fs/fuse/fuse.ko
74*e315b831Sgaochao    ...
75*e315b831Sgaochao    loading @0xffffffffa0000000: /home/user/linux/build/drivers/ata/ata_generic.ko
76*e315b831Sgaochao
77*e315b831Sgaochao- 对一些尚未加载的模块中的函数函数设置断点,例如::
78*e315b831Sgaochao
79*e315b831Sgaochao    (gdb) b btrfs_init_sysfs
80*e315b831Sgaochao    Function "btrfs_init_sysfs" not defined.
81*e315b831Sgaochao    Make breakpoint pending on future shared library load? (y or [n]) y
82*e315b831Sgaochao    Breakpoint 1 (btrfs_init_sysfs) pending.
83*e315b831Sgaochao
84*e315b831Sgaochao- 继续执行::
85*e315b831Sgaochao
86*e315b831Sgaochao    (gdb) c
87*e315b831Sgaochao
88*e315b831Sgaochao- 加载模块并且能观察到正在加载的符号以及断点命中::
89*e315b831Sgaochao
90*e315b831Sgaochao    loading @0xffffffffa0034000: /home/user/linux/build/lib/libcrc32c.ko
91*e315b831Sgaochao    loading @0xffffffffa0050000: /home/user/linux/build/lib/lzo/lzo_compress.ko
92*e315b831Sgaochao    loading @0xffffffffa006e000: /home/user/linux/build/lib/zlib_deflate/zlib_deflate.ko
93*e315b831Sgaochao    loading @0xffffffffa01b1000: /home/user/linux/build/fs/btrfs/btrfs.ko
94*e315b831Sgaochao
95*e315b831Sgaochao    Breakpoint 1, btrfs_init_sysfs () at /home/user/linux/fs/btrfs/sysfs.c:36
96*e315b831Sgaochao    36              btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
97*e315b831Sgaochao
98*e315b831Sgaochao- 查看内核的日志缓冲区::
99*e315b831Sgaochao
100*e315b831Sgaochao    (gdb) lx-dmesg
101*e315b831Sgaochao    [     0.000000] Initializing cgroup subsys cpuset
102*e315b831Sgaochao    [     0.000000] Initializing cgroup subsys cpu
103*e315b831Sgaochao    [     0.000000] Linux version 3.8.0-rc4-dbg+ (...
104*e315b831Sgaochao    [     0.000000] Command line: root=/dev/sda2 resume=/dev/sda1 vga=0x314
105*e315b831Sgaochao    [     0.000000] e820: BIOS-provided physical RAM map:
106*e315b831Sgaochao    [     0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
107*e315b831Sgaochao    [     0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
108*e315b831Sgaochao    ....
109*e315b831Sgaochao
110*e315b831Sgaochao- 查看当前task struct结构体的字段(仅x86和arm64支持)::
111*e315b831Sgaochao
112*e315b831Sgaochao    (gdb) p $lx_current().pid
113*e315b831Sgaochao    $1 = 4998
114*e315b831Sgaochao    (gdb) p $lx_current().comm
115*e315b831Sgaochao    $2 = "modprobe\000\000\000\000\000\000\000"
116*e315b831Sgaochao
117*e315b831Sgaochao- 对当前或指定的CPU使用per-cpu函数::
118*e315b831Sgaochao
119*e315b831Sgaochao    (gdb) p $lx_per_cpu("runqueues").nr_running
120*e315b831Sgaochao    $3 = 1
121*e315b831Sgaochao    (gdb) p $lx_per_cpu("runqueues", 2).nr_running
122*e315b831Sgaochao    $4 = 0
123*e315b831Sgaochao
124*e315b831Sgaochao- 使用container_of查看更多hrtimers信息::
125*e315b831Sgaochao
126*e315b831Sgaochao    (gdb) set $next = $lx_per_cpu("hrtimer_bases").clock_base[0].active.next
127*e315b831Sgaochao    (gdb) p *$container_of($next, "struct hrtimer", "node")
128*e315b831Sgaochao    $5 = {
129*e315b831Sgaochao      node = {
130*e315b831Sgaochao        node = {
131*e315b831Sgaochao          __rb_parent_color = 18446612133355256072,
132*e315b831Sgaochao          rb_right = 0x0 <irq_stack_union>,
133*e315b831Sgaochao          rb_left = 0x0 <irq_stack_union>
134*e315b831Sgaochao        },
135*e315b831Sgaochao        expires = {
136*e315b831Sgaochao          tv64 = 1835268000000
137*e315b831Sgaochao        }
138*e315b831Sgaochao      },
139*e315b831Sgaochao      _softexpires = {
140*e315b831Sgaochao        tv64 = 1835268000000
141*e315b831Sgaochao      },
142*e315b831Sgaochao      function = 0xffffffff81078232 <tick_sched_timer>,
143*e315b831Sgaochao      base = 0xffff88003fd0d6f0,
144*e315b831Sgaochao      state = 1,
145*e315b831Sgaochao      start_pid = 0,
146*e315b831Sgaochao      start_site = 0xffffffff81055c1f <hrtimer_start_range_ns+20>,
147*e315b831Sgaochao      start_comm = "swapper/2\000\000\000\000\000\000"
148*e315b831Sgaochao    }
149*e315b831Sgaochao
150*e315b831Sgaochao
151*e315b831Sgaochao命令和辅助调试功能列表
152*e315b831Sgaochao----------------------
153*e315b831Sgaochao
154*e315b831Sgaochao命令和辅助调试功能可能会随着时间的推移而改进,此文显示的是初始版本的部分示例::
155*e315b831Sgaochao
156*e315b831Sgaochao (gdb) apropos lx
157*e315b831Sgaochao function lx_current -- Return current task
158*e315b831Sgaochao function lx_module -- Find module by name and return the module variable
159*e315b831Sgaochao function lx_per_cpu -- Return per-cpu variable
160*e315b831Sgaochao function lx_task_by_pid -- Find Linux task by PID and return the task_struct variable
161*e315b831Sgaochao function lx_thread_info -- Calculate Linux thread_info from task variable
162*e315b831Sgaochao lx-dmesg -- Print Linux kernel log buffer
163*e315b831Sgaochao lx-lsmod -- List currently loaded modules
164*e315b831Sgaochao lx-symbols -- (Re-)load symbols of Linux kernel and currently loaded modules
165*e315b831Sgaochao
166*e315b831Sgaochao可以通过“help <command-name>”或“help function <function-name>”命令
167*e315b831Sgaochao获取指定命令或指定调试功能的更多详细信息。
168