Windows下使用vscode 调试linux kernel

  1. 安装WSL2
    1. 在microsoft store上安装Ubuntu,当不能安装时可能需要梯子
    2. window中访问Ubuntu的目录使用:
      //wsl$
    3. Ubuntu中需要修改软件源:
      #参考 https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/
      # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
      deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
      # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
      deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
      # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
      deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
      # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
      deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
      # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
      
      # 预发布软件源,不建议启用
      # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
      # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
    4. 安装编译环境:
      sudo apt-get install libncurses5
      sudo apt-get install qemu libncurses5-dev gcc-arm-linux-gnueabi build-essential 
      sudo apt install gcc-arm-none-eabi
      由于Ubuntu中没有arm-none-eabi-gdb ,所以需要到arm网站上下载软件包,
      然后解压bin文件夹到,/usr/local/bin 中
      网址:https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads
      
  2. 安装kernel调试环境
    1. 安装busybox工具包
      1. 下载
        wget https://busybox.net/downloads/busybox-1.35.0.tar.bz2
      2. 解压
        tar -jxvf busybox-1.35.0.tar.bz2
      3. 配置
        cd busybox-1.35.0
        export ARCH=arm
        export CROSS_COMPILE=arm-linux-gnueabi-
        make menuconfig
        配置:
        Settings->build options ->选中 build busybox as a static binary

      4. 安装
        make install
    2. 下载Linux源码
      1. 下载源码
        wget http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/pub/linux/kernel/v5.x/linux-5.17.tar.gz
      2. 解压
        tar -xvf linux-5.17.tar.gz
    3. 制作最小文件系统
      1. 拷贝busybox生成的_install 文件夹到Linux源码目录中
        cd linux-5.17
        cp -r ../busybox-1.35.0/_install/ ./
        cd ./_install/
        mkdir etc/ dev/ mnt/ -p etc/init.d
      2. 进入 linux-5.17/_install/etc/init.d 中,创建rcS文件并写入
        cd ./etc/init.d/
        vim rcS
        mkdir -p /proc
        mkdir -p /tmp
        mkdir -p /sys
        mkdir -p /mnt
        /bin/mount/ -a
        mkdir -p /dev/pts
        mount -t devpts devpts /dev/pts
        echo /sbin/mdev > /proc/sys/kernel/hotplug
        mdev -s
      3. 修改为可执行文件
        chmod +x rcS
      4. 进入 linux-5.17/_install/etc 中,创建 fstab 文件,并写入
        vim fstab
        proc /proc proc defaults 0 0
        tmpfs /tmp tmpfs defaults 0 0
        sysfs /sys sysfs defaults 0 0
        tmpfs /tmp tmpfs defaults 0 0
        debugfs /sys/kernel/debug debugfs defaults 0 0
      5. 进入 linux-5.17/_install/etc 中,创建 inittab文件,并写入
        vim inittab
        ::sysinit:/etc/init.d/rcS
        ::respawn:-/bin/sh
        ::askfirst:-/bin/sh
        ::ctrlaltdel:/bin/umount -a -r
      6. 进入 linux-5.17/_install/dev 中,在root权限下创建如下节点
        sudo mknod console c 5 1
        sudo mknod null c 1 3
    4. 编译Linux kernel
      1. 设置编译变量
        cd linux-5.17
        export ARCH=arm
        export CROSS_COMPILE=arm-linux-gnueabi-
        make vexpress_defconfig
        make menuconfig
      2. 配置 initarmfs
        配置路径:
        1、General setup --> 选中:Initial RAM filessystem and RAM dis
            在这个条目下面设置initialfs 路径:
            initramfs source file(s)-->_install
            _install 为前面我们制作的文件系统目录
        2、Boot options -->default kernel command string
        3、Kernel Features --> Memory split(3G/1G user/kernel split) 选中3G/1G
        
        
      3. 编译命令:
        sudo make bzImage -j4 ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
        sudo make dtbs
      4. 运行QEMU模拟4核Cortex-A9
        qemu-system-arm -M vexpress-a9 -m 1024M -kernel arch/arm/boot/zImage -append "rdinit=/linuxrc console=ttyAMA0 loglevel=8" -dtb arch/arm/boot/dts/vexpress-v2p-ca9.dtb -nographic
      5. 在另外的终端关闭QEMU
        killall qemu-system-arm
  3. 安装vscode
    1. 安装Windows版本的vscode
    2. 安装vscode 插件
      C/C++ (必选)
      C/C++ Intellisense(可选)
      C/C++ Snippets(可选)
      Remote-WSL
      Remote-SSH
      Remote-Containers Embedded Linux Dev(设备树插件) Kconfig(设备树插件依赖)
      GDB
    3. 使用vscode连接wsl,然后检查配置
    4. 配置vscode debug
      路径:运行(R)->添加配置->c++(GDB/LDB)
      
    5. 配置文件如下:
      {
          "configurations": [
          {
              "name": "(gdb) 启动",
              "type": "cppdbg",
              "request": "launch",
              "program": "${workspaceFolder}/linux-5.17/vmlinux",
              "args": [],
              "stopAtEntry": true,
              "cwd": "${workspaceFolder}",
              "environment": [],
              "externalConsole": false,
              "MIMode": "gdb",
              "miDebuggerPath": "/usr/local/bin/arm-none-eabi-gdb",
              "miDebuggerServerAddress": "localhost:1234",
              "setupCommands": [
                  {
                      "description": "为 gdb 启用整齐打印",
                      "text": "-enable-pretty-printing",
                      "ignoreFailures": true
                  },
                  {
                      "description":  "将反汇编风格设置为 Intel",
                      "text": "-gdb-set disassembly-flavor intel",
                      "ignoreFailures": true
                  }
              ]
          }
          ]
      }
    6. 上面的配置注意事项:
      "program": "${workspaceFolder}/linux-5.17/vmlinux" 为wsl上调试文件路径
      "miDebuggerPath": "/usr/local/bin/arm-none-eabi-gdb", 为wsl上gdb文件路径
      "miDebuggerServerAddress": "172.24.99.118:1234",为wsl 的ip地址,端口号为默认的端口号
  4. 调试Linux kernel
    1. 修改完linux kernel 代码后重新编译,并在QEMU中执行:
      qemu-system-arm -M vexpress-a9 -m 1024M -kernel arch/arm/boot/zImage -append "rdinit=/linuxrc console=ttyAMA0 loglevel=8" -dtb arch/arm/boot/dts/vexpress-v2p-ca9.dtb -nographic -S -s

      注意 此命令参数 -S 表示中断CPU,等待操作,-s 在1234 端口接收GDB命令

    2. 在vscode中设置断点,并启动调试,选中刚才的配置,点击开始debug:

你可能感兴趣的:(Linux,kernel,linux)