fedora 35 + Vscode remote ssh 连接 + QEMU 6.1.0 + Linux v4.19.1 + gcc 11.3.1 + glibc 2.34 + busybox 1.36.1

内核、qemu、 busybox编译参考:

使用Docker编译32位Linux内核并在Qemu中运行

QEMU+busybox 搭建Linux内核运行环境

使用 VSCode + qemu 搭建 Linux 内核调试环境

双机调试Linux内核

内核编译

先去 kernel 仓库 下载源代码,解压后进入目录。

预先安装一些package:

sudo apt-get update
sudo apt-get install build-essential libncurses-dev bison flex libssl-dev

sudo dnf groupinstall "Development Tools"
sudo dnf install gcc make patch ncurses-devel genisoimage gzip bzip2 glibc-devel glibc-headers git openssl doxygen

加入调试Patch

根据这篇文章Re: [PATCH 0/5] kernel hacking: GCC optimization for debug experience (-Og)内核默认情况下都是开启了编译优化的,不方便我们进行动态调试。我们可以将这个篇文章中提供的 5 个patch 加上去手动地关闭掉内核的编译优化。直接将所有的 patch 文件放到/usr/src/kernels/linux-XXXX文件中,使用patch -p1< (name of patch).patch,以此安装所有的 patch 文件。


make menuconfig 配置内核的编译选项,可以选择 Save 配置文件方便下次直接加载。

# 下面选项如果没有选上的,选上,然后 save 保存设置,退出 exit。
Kernel hacking  --->
     Compile-time checks and compiler options  ---> 
         [*] Compile the kernel with debug info
         [*]     Provide GDB scripts for kernel debugging

# KASLR 是一种安全特性,它在每次启动时随机化内核映像的地址,以增加攻击者预测特定内核结构地址的难度。
# 通常内核配置中关闭 KASLR,或者在后续grub中关闭
Processor type and features  --->
    [*] Randomize the address of the kernel image (KASLR)

保存后会将配置默认保存在 .config 文件,也可以自己重命名。