lab3:基于VS Code的Linux内核调试环境搭建

本文详细介绍了如何搭建实验环境,包括安装开发工具和QEMU虚拟机,下载并编译Linux内核,配置内核选项,以及使用BusyBox创建根文件系统。接着,文章提到了如何使用VSCode进行C/C++的调试,包括安装必要的插件和配置文件。最后,文章展示了如何打包根文件系统和测试自编译的内核。
摘要由CSDN通过智能技术生成

一、实验环境搭建

1.下载开发工具

sudo apt install build-essential
 
sudo apt install qemu # install QEMU#作为一个虚拟机
 
sudo apt install libncurses5-dev bison flex libssl-dev libelf-dev
#编译内核所需的依赖---一些基础库

2.下载源码

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

3.解压内核源码

xz -d linux-5.4.34.tar.xz
 
tar -xvf linux-5.4.34.tar #解压源码
 
cd linux-5.4.34

4.配置内核选项

make defconfig # Default configuration is based on 'x86_64_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
 
Processor type and features ---->
      [] Randomize the address of the kernel image (KASLR)

5.编译

make -j$(nproc) #在linux-5.4.34文件夹下使用qemu测试内核是否能加载,结果会显示“kernel panic”
qemu-system-x86_64 -kernel arch/x86/boot/bzImage

二、使用busyboy

  1. 使用busybox制作根文件系统

$ axel -n 20 https://busybox.net/downloads/busybox-1.32.0.tar.bz2 # download
$ tar -jxvf busybox-1.32.0.tar.bz2
$ cd busybox-1.32.0

2.编译配置

$ make menuconfig
Settings  --->
    [*] Build static binary (no shared libs)
$ make -j14 && make install
$ mkdir rootfs
$ cd rootfs
$ cp ../busybox-1.32.0/_install/* ./ -rf
$ mkdir dev proc sys home
$ sudo cp -a /dev/{null,console,tty,tty1,tty2,tty3,tty4} dev/
 
$ nvim init #添加以下内容
    #!/bin/sh
    mount -t proc none /proc
    mount -t sysfs none /sys
    echo "Wellcome Mengning OS!"
    echo "--------------------"
    cd home
    /bin/sh
$ chmod +x init
$ find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../rootfs.cpio.gz 

3.添加权限,打包成镜像,测试系统

chmod +x init
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../rootfs.cpio.gz 
qemu-system-x86_64 -kernel ./arch/x86/boot/bzImage -initrd rootfs.cpio.gz

三、VS Code 调试

  1. 安装VS Code及插件

sudo apt install ./code_1.76.2-1678817801_amd64.deb #安装vscode

安装VSCode插件C/C++ Intellisense和C/C++ Themes。由于插件C/C++ Intellisense需要GNU Global,还需要使用如下命令安装GNU Global。

sudo apt install global 
  1. 调试

python ./scripts/gen_compile_commands.py #linux目录下执行

VS Code添加配置文件.vscode/c_cpp_properties.json

启动调试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值