使用 QEMU 调试内核
Table of Contents
时间:2015-07-24
1 QEMU
完全模拟硬件,可通过参数来指定硬件配置
需要用到的快捷键:
- Ctrl + Alt + G:捕获输入
2 调试内核的环境
- QEMU
- 一个 bzImage
- 一个启动镜像,我直接用 http://wiki.qemu.org/Testing 上的 linux-0.2.img.bz2
3 内核编译
运行:
$ make menuconfig
开启 KGDB 支持:
Kernel Hacking ---> KGDB: kernel debugger ---> KGDB: use kgdb over the serial console
4 用 QEMU 启动内核
运行:
$ qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage -hda linux-0.2.img -append "root=/dev/sda"
SDA 的内容就是 linux-0.2.img,启动后可以查看内核版本来确定是否正确:
$ cat /proc/version
因为 linux-0.2.img 里没有 uname 命令。
5 GDB 远程调试
qemu 启动时加上 -S 参数,然后启动后会挂载 CPU,按 Ctrl+Alt+2 切换到 QEMU monitor,输入:
$ gdbserver tcp::1234
嫌麻烦可以加 -s 参数进入调试模式。
在宿主机上调试:
$ gdb vmlinux
输入:
(gdb) target remote localhost:1234
下个断点:
b start_kernel
再继续运行:
(gdb) c
需要注意的是,在 QEMU 中启动 x86_64 的镜像,宿主机也是 x86_64 的架构时,GDB 远程调试会报出以下错误:
Remote 'g' packet reply is too long
我改成 i386 架构运行就没问题了:
$ qemu-system-i386 -kernel ./arch/i386/boot/bzImage -hda ~/下载/linux-0.2.img -append 'root=/dev/sda' -S -gdb tcp::1234