Ubuntu uses qemu to debug Linux kernel

In order to debug the kernel code with a virtual machine, see if it can be of greater help in learning the kernel. . Performed the next verification.

The commands in the middle are for reference only and are some records in the process of use. The corresponding adjustment needs to be made when the file name path name is used.

1. Download the Linux kernel source code.

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

Configure

$ make menuconfig
编译。

$ make -j8 #  

$ cp linux-3.10.104/arch/x86_64/boot/bzImage

Install Virtual Machine

$ sudo apt-get install qemu
 

2. Make rootfs

we hello.c

gcc -static -o helloworld hello.cecho helloworld | cpio -o --format=newc > rootfs

Make a root file system.

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. Start the Linux kernel.

   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 Start gdb debugging

    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之前,也可以在之后
Released eight original articles · won praise 0 · Views 2874

Guess you like

Origin blog.csdn.net/skyxiaoyan1/article/details/85038742