Para depurar el código del kernel con una máquina virtual, vea si puede ser de mayor ayuda para aprender el kernel. . Realizó la próxima verificación.
Los comandos en el medio son solo de referencia y son algunos registros en el proceso de uso. El ajuste correspondiente debe hacerse cuando se utiliza la ruta de acceso del nombre del archivo.
1. Descargue el código fuente del kernel de Linux.
https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/
$ wget https://cdn.kernel.org/pub/linux/kernel/v3.x/linux-3.10.104.tar.xz # decompress $ tar xvf linux-3.10.104.tar.xz
$ cd linux-3.10.104
Configurar
$ make menuconfig
编译。
$ make -j8 #
$ cp linux-3.10.104 / arch / x86_64 / boot / bzImage
Instalar máquina virtual
$ sudo apt-get install qemu
2. Hacer rootfs
nosotros hola.c
gcc -static -o helloworld hello.cecho helloworld | cpio -o --format = newc> rootfs
Crea un sistema de archivos raíz.
emu-system-x86_64 -kernel linux_3.10 / linux-3.10.92 / arch / x86_64 / boot / bzImage -initrd ./rootfs/rootfs -append "root = / dev / ram rdinit = / helloworld"
3. Inicie el kernel de Linux.
cd LinuxKernel
qemu-system-i386 -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img -s -S # 关于-s和-S选项的说明:
# -S freeze CPU at startup (use ’c’ to start execution)
# -s shorthand for -gdb tcp::1234 若不想使用1234端口,则可以使用-gdb tcp:xxxx来取代-s选项
qemu-system-x86_64 -kernel linux_3.10 / linux-3.10.92 / arch / x86_64 / boot / bzImage -initrd ./rootfs/rootfs -append "root = / dev / ram rdinit = / helloworld" -s -S
4 Inicie la depuración de gdb
gdb
(gdb)file linux-3.18.6/vmlinux # 在gdb界面中targe remote之前加载符号表
(gdb)target remote:1234 # 建立gdb和gdbserver之间的连接,按c 让qemu上的Linux继续运行
(gdb)break start_kernel # 断点的设置可以在target remote之前,也可以在之后