ubuntu用qemu调试linux内核

本文详细介绍了一种通过虚拟机调试Linux内核的方法,包括下载Linux内核源码、配置和编译内核、制作根文件系统(rootfs)、安装和配置虚拟机(QEMU)、启动Linux内核并使用gdb进行远程调试的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出于对用虚拟机 调试 内核代码,看是否能对学习内核有比较大的帮助。。 进行了下验证  。

中间 的命令仅作为参考, 是使用过程中的一些记录, 文件命名 路径名 使用时 作出对应调整 需。

1,  下载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 #  解压 $ tar xvf linux-3.10.104.tar.xz

$ cd linux-3.10.104

进行配置

$ make menuconfig
编译。

$ make -j8 #  

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

安装虚拟机

$ sudo apt-get install qemu
 

2. 制作rootfs

vi hello.c

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

制作根文件系统。

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, 启动 启动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 启动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之前,也可以在之后
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值