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
tui 关联源码失败,则用layout src
重新关联源码
set substitute-path 原路径 新路径
vscode 调试
利用vscode远程调试Linux内核-良许Linux教程网
eclipse图形化调试
使用 GDB + Qemu 调试 Linux 内核 | 深入浅出 eBPF
高级本地调试
linux驱动之调试技巧--- 应用程序远程gdb+vscode调试应用程序, 串口kgdboc调试.ko驱动程序_gdb linux 驱动-CSDN博客