xv6-riscv をWSLにインストールしVisual Studio Codeでデバッグ

1.はじめに

xv6-riscvをWindows10で稼働するWSLで動作させ、Vscode上でデバッグする方法です。

 

2.環境

ホストOS:Windows 10 Pro

ゲストOS:Ubuntu 20.04.4 LTS (Focal Fossa)

IDE:Visual Studio Code 1.40.1

プラグイン:Remote - WSL, C/C++, Japanese Language Pack for VS Code

 

3.ツールのインストール

$ sudo apt-get install git build-essential gdb-multiarch qemu-system-misc gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu

 

4.ソースの取得とビルド

ディレクトリを作成し、移動する。

$ mkdir xv6

$ cd xv6

 

githubからソースコードを取得する。

$ git clone https://github.com/mit-pdos/xv6-riscv

 

ソースをビルドしてqemuで実行する。

$ cd xv6-riscv

$ make qemu

成功すると下記の通りに表示され、コマンドが入力可能となる。

==================================================

xv6 kernel is booting

 

hart 2 starting

hart 1 starting

init: starting sh

$

==================================================

ctrl+a x で終了

 

5.Vscode でデバッグ

(1) task.jsonを作成

下記の通りに.vscode/task.jsonを作成する。

==================================================

{

  "version": "2.0.0",

  "type":"shell",

  "echoCommand": true,

  "tasks": [

      {

          "label": "xv6 build",

          "command": "make",

          "problemMatcher": [

              "$gcc"

          ],

          "group": {

              "kind": "build",

              "isDefault": true

          }

      },

      {

          "label": "xv6 clean",

          "command": "make clean",

          "problemMatcher": [

              "$gcc"

          ],

          "group": {

              "kind": "build",

              "isDefault": true

          }

      },

      {

          "label": "xv6 run qemu",

          "command": "make qemu-gdb",

          "problemMatcher": [

              "$tsc"

          ],

      }

  ]

}

==================================================

 

(2) launch.jsonを作成

下記の通りに、.vscode/launch.json

==================================================

{

  "version": "0.2.0",

  "configurations": [

      {

          "name": "debug xv6",

          "type": "cppdbg",

          "request": "launch",

          "program": "/home/taka/develop/xv6/xv6-riscv/kernel/kernel",

          "miDebuggerServerAddress": "127.0.0.1:26000",

          "args": [],

          "stopAtEntry": false,

          "cwd": "${workspaceFolder}",

          "miDebuggerPath": "/usr/bin/gdb-multiarch",

          "environment": [],

          "externalConsole": false,

          "MIMode": "gdb",

          "setupCommands": [

              {

                "description": "pretty printing",

                "text": "-enable-pretty-printing",

                "ignoreFailures": true

              }

          ],

          "logging": {

              "engineLogging": true,

              "programOutput": true,

          }

      }

  ]

}

==================================================

 

(3) .gdbinit.tmpl-riscv の編集

.gdbinit.tmpl-riscv を開き、3行目”target remot …”をコメントアウトする。

==================================================

set confirm off

set architecture riscv:rv64

# target remote 127.0.0.1:1234

symbol-file kernel/kernel

set disassemble-next-line auto

set riscv use-compressed-breakpoints yes

==================================================

 

(4) デバッグ実行

タスクを実行

メニューの「ターミナル」-> 「タスク実行」-> 「xv6 run qemu」を選択する。

下記の表示となり、デバッガからの接続待ちとなる。

==================================================

> Executing task: make qemu-gdb <

 

*** Now run 'gdb' in another window.

qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 128M -smp 3 -nographic -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 -S -gdb tcp::26000

==================================================

 

デバッガ接続

左メニュー「実行とデバッグ」を押下する。

表示された画面で、「debug xv6」を選択し、「デバッグの開始」(再生ボタン)を押下する。

接続が成功すると、下記の表示となり、デバッガによる操作が可能となる。

==================================================

xv6 kernel is booting

 

hart 2 starting

hart 1 starting

init: starting sh

==================================================

 

6.参考情報

(1) MITの講義資料(2021)

(2) テキストの和訳版

電気通信大学 菅原研究室 の下記のサイトで公開されています。

 

写真素材のピクスタ