基于VS Code的Linux内核调试环境搭建及start_kernel跟踪分析

1、安装开发工具

(1)安装编译C/C++程序所需要的软件包

sudo apt install build-essential

(2)安装qemu

sudo apt install qemu 

(3)安装curses库(之后会基于curses库的文本窗口界面配置Linux)

sudo apt install libncurses5-dev bison flex libssl-dev libelf-dev

2、下载Linux源代码

(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

3、配置内核

(1)使用文本窗口配置Linux

make menuconfig

在这里插入图片描述
(2)改变如下选项

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

4、编译和运行内核

make -j$(nproc) 
qemu-system-x86_64 -kernel arch/x86/boot/bzImage

在这里插入图片描述

5、制作根文件系统

电脑加电启动首先由bootloader加载内核,内核紧接着需要挂载内存根文件系统,其中包含必要的设备驱动和工具,bootloader加载根文件系统到内存中,内核会将其挂载到根目录/下,然后运行根文件系统中init脚本执行一些启动任务,最后才挂载真正的磁盘根文件系统。

(1)下载busybox源码并解压

axel -n 20 https://busybox.net/downloads/busybox-1.31.1.tar.bz2
tar -jxvf busybox-1.31.1.tar.bz2
cd busybox-1.31.1

(2)改变busybox配置,使用静态链接

make menuconfig
  • Settings —>
    • [ ∗ * ] Build static binary (no shared libs)

(3)编译安装busybox

make -j$(nproc) && make install

(4)制作内存根文件系统镜像

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/

(5)新建init脚本文件,并放到根文件系统根目录下(rootfs/init),init内容如下。

#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
echo "Wellcome MengningOS!"
echo "--------------------"
cd home
/bin/sh

(6)给init脚本文件添加执行权限

chmod +x init

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

find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../rootfs.cpio.gz 

(8)测试挂载根文件系统,检查init文件是否执行

qemu-system-x86_64 -kernel linux-5.4.34/arch/x86/boot/bzImage -initrd rootfs.cpio.gz

在这里插入图片描述

6、跟踪并调试Linux内核

(1)纯命令行启动虚拟机

qemu-system-x86_64 -kernel linux-5.4.34/arch/x86/boot/bzImage -initrd rootfs.cpio.gz -S -s -nographic -append "console=ttyS0"

(2)打开新的Terminal,执行以下命令启动gdb,加载内核符号表

cd linux-5.4.34/
gdb vmlinux
(gdb) target remote:1234
(gdb) b start_kernel

在这里插入图片描述

7、安装和配置Vscode

(1)从浏览器中下载.deb文件,执行如下命令安装

sudo apt install ./code_1.76.2-1678817801_amd64.deb

(2)执行如下命令,这里借助一个 Python 脚本来生成 compile_commands.json 文件帮助 Intellisense 正常提示(包括头文件和宏定义等)。新建一个.vscode目录,将配置文件添加到该目录下。该命令在linux-5.4.34目录下执行。

python ./scripts/gen_compile_commands.py
mkdir .vscode

(3)安装GNU Global,以及C/C++ Intellisense和C/C++ Themes插件

sudo apt install global

在这里插入图片描述

8、跟踪并分析start_kernel过程

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值