qemu调试linux内核

本文介绍了如何在QEMU中调试Linux内核,特别关注了在开启KASLR(KernelAddressSpaceLayoutRandomization)后设置断点的问题。提供了两种解决方案:重新编译带调试信息的bzImage,或者在启动时禁用KASLR。同时提及了GDB、VSCode等工具的使用技巧。
摘要由CSDN通过智能技术生成

qemu调试

qemu -kernel arch/x86/boot/bzImage -s -S

#-S freeze CPU at startup

#-s shorthandfor-gdb tcp::1234若 不想使用1234端口,则可以使用-gdb tcp:xxxx来取代-s选项

(gdb)file linux-3.18.6/vmlinux # 在gdb界面中targe remote之前加载符号表

(gdb)target remote:1234 # 建立gdb和gdbserver之间的连接

(gdb)breakstart_kernel # 断点的设置可以在target remote之前,也可以在之后

(gdb)continue # 让qemu上的Linux继续运行

挂载磁盘方式启动

新建img,若mount失败,则和/etc/mke2fs.conf 有关

dd if=/dev/zero of=rootfs.img bs=1M count=10
mkfs.ext4 rootfs.img
mkdir fs
mount -t ext4 -o loop rootfs.img ./fs
make install CONFIG_PREFIX=./fs
mkdir proc dev etc home mnt
cp -r ../examples/bootfloppy/etc/* etc/
chmod -R 777 fs/

#制作完成卸载
umount fs

 

若内核编译设置不关闭随机化[KASLR],则gdb启动会卡主

qemu-system-x86_64 -kernel ./linux-4.14.191/arch/x86/boot/bzImage -hda ./busybox-1.35.0/rootfs.img -s -S -smp 1 -nographic -append "root=/dev/sda console=ttyS0"

 启动以调试ls调用内核调试

添加共享磁盘

dd if=/dev/zero of=ext4.img bs=512 count=131072
mkfs.ext4 ext4.img
qemu-system-x86_64 -kernel ./linux-4.14.191/arch/x86_64/boot/bzImage  -hda ./busybox-1.35.0/rootfs.img -hdb ./busybox-1.35.0/ext4.img  -append "root=/dev/sda console=ttyS0" -nographic 

 宿主机挂载

mount -t ext4 -o loop ext4.img ./share

 qemu挂载

mount /dev/sdb mnt/

 A:

 

kernel 内核地址随机化原因分析,及解决方式

开启内核地址随机化KASLR后, qemu 调试 kernel 不能设置断点_qemu不能断点-CSDN博客

https://groups.google.com/g/zh-kernel/c/1A5nk5pExwI

Q:gdb无法断点

A:去除随机化

方案一:重新编bzImage

Kernel hacking  ---> 
    [*] Kernel debugging
    Compile-time checks and compiler options  --->
        [*] Compile the kernel with debug info
        [*]   Provide GDB scripts for kernel debuggin
​
Processor type and features ---->
[] Randomize the address of the kernel image (KASLR)

方案二:加-append nokaslr disables KASRL.

qemu-system-x86_64 -kernel ./linux-4.14.191/arch/x86/boot/bzImage -initrd ./busybox-1.35.0/initramfs.cpio.gz -s -S -nographic -append "init=/init console=ttyS0" -append nokaslr

参考文章

qemu调试l内核在start_kernel打了断点,为何不会停到start_kernel? - 知乎

GDB

GDB使用进阶 - 简书

tui 关联源码失败,则用layout src

重新关联源码

set substitute-path 原路径 新路径

vscode 调试

利用vscode远程调试Linux内核-良许Linux教程网

eclipse图形化调试

使用 GDB + Qemu 调试 Linux 内核 | 深入浅出 eBPF

高级本地调试

linux驱动之调试技巧--- 应用程序远程gdb+vscode调试应用程序, 串口kgdboc调试.ko驱动程序_gdb linux 驱动-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenxuezhou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值