Linux操作系统分析lab3:基于VS Code的Linux内核调试环境搭建及start_kernel跟踪分析

实验步骤

1、下载Linux内核源代码

sudo apt install axel
axel -n 20 https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.34.tar.xz
xz -d linux-5.4.34.tar.xz
tar -xvf linux-5.4.34.tar
cd linux-5.4.34

2、下载编译工具

sudo apt install build-essential gcc-multilib
sudo apt install qemu 
sudo apt install libncurses5-dev bison flex libssl-dev libelf-dev

3、配置内核选项

make defconfig
make menuconfig
# 打开debug相关选项
Kernel hacking --->
    Compile-time checks and compiler options --->
        [*] Compile the kernel with debug info
        [*] Provide GDB scripts for kernel debugging
[*] Kernel debugging
# 关闭KASLR,否则会导致打断点失败
Processor type and features ---->
    [] Randomize the address of the kernel image (KASLR)

4、编译和运行内核

make -j$(nproc)
# 测试内核能否正常运行,因为没有文件系统最终会kernel panic
qemu-system-x86_64 -kernel arch/x86/boot/bzImage

5、制作根文件系统

mkdir rootfs
cd rootfs
cp ../busybox-1.31.1/_install/* ./ -rf
mkdir dev proc sys home
sudo cp -a /dev/{
    
    null,console,tty,tty1,tty2,tty3,tty4} dev/

6、init脚本文件

cd rootfs
vim init
mount -t proc none /proc
mount -t sysfs none /sys
echo "Welcome!"
echo "--------"
cd home
/bin/sh

7、内存根文件系统镜像打包

# 给init文件加权限
chmod +x init
# 打包
find . -print0 | cpio --null -ov --format=newc|gzip -9 > ../rootfs.cpio.gz
#测试挂载根文件系统
qemu-system-x86_64 -kernel linux-5.4.34/arch/x86/boot/bzImage -initrd rootfs.cpio.gz

8.配置vscode

1、在命令行输入python ./scripts/gen_compile_commands.py
2、新建.vscode文件夹,将提供的配置文件放入文件夹内

9.跟踪分析

qemu-system-x86_64 -kernel ./arch/x86/boot/bzImage -initrd rootfs.cpio.gz -S -s

设置断点start_kernel,在vscode中按F5进行调试
在这里插入图片描述
在start_kernel的结尾arch_call_reset_init(),点开函数定义,发现执行了reset_init()函数,再设置一个函数断点"reset_init",继续点击单点跳过进入reset_init函数内部,由0号进程执行。在这里插入图片描述
继续执行,遇到1号进程kernel_init,,它是所有用户进程的祖先,由kernel_thread函数创建,kernel_thread函数创建一个新的内核线程(实际linux不支持线程所以是一个内核进程),该线程的入口地址是kernel_init()函数。
设置kernel_thread,继续执行,进入kernel_thread函数定义。
在这里插入图片描述
得到kernel_thread函数通过_do_fork函数来创建进程,于是设置_do_fork断点,进入_do_fork函数定义。
在这里插入图片描述
_do_fork函数主要完成了调用copy_process()复制父进程、调用wake_up_new_task将子进程加入就绪队列等待调度执行等。

猜你喜欢

转载自blog.csdn.net/weixin_43363720/article/details/130694077
今日推荐