调试机使用WSL,相比用虚拟机的Linux更加快捷和方便,并且功能上也基本没有太大差异。 这里使用了Ubuntu-20.04,其他版本的WSL肯定也可以。
C:\>wsl -l -v
NAME STATE VERSION
* Ubuntu-20.04 Running 1
[09:59:19]:~$ uname -r
4.4.0-19041-Microsoft
需要的软件包,有的直接apt源,如果没有就到网上找一下。
# qemu-system 模拟工具,模拟riscv 32位或者64位的计算机硬件,可以运行xv6操作系统
[10:01:06]:bin$ qemu-system-riscv
qemu-system-riscv32 qemu-system-riscv64
# gcc 编译相关工具,一套都搞来就好啦
[10:01:58]:bin$ ls
riscv64-unknown-elf-addr2line riscv64-unknown-elf-gcc-ar riscv64-unknown-elf-ld.bfd
riscv64-unknown-elf-ar riscv64-unknown-elf-gcc-nm riscv64-unknown-elf-nm
riscv64-unknown-elf-as riscv64-unknown-elf-gcc-ranlib riscv64-unknown-elf-objcopy
riscv64-unknown-elf-c++ riscv64-unknown-elf-gcov riscv64-unknown-elf-objdump
riscv64-unknown-elf-c++filt riscv64-unknown-elf-gcov-dump riscv64-unknown-elf-ranlib
riscv64-unknown-elf-cpp riscv64-unknown-elf-gcov-tool riscv64-unknown-elf-readelf
riscv64-unknown-elf-elfedit riscv64-unknown-elf-gdb riscv64-unknown-elf-size
riscv64-unknown-elf-g++ riscv64-unknown-elf-gdb-add-index riscv64-unknown-elf-strings
riscv64-unknown-elf-gcc riscv64-unknown-elf-gprof riscv64-unknown-elf-strip
riscv64-unknown-elf-gcc-8.3.0 riscv64-unknown-elf-ld
# gdb-multiarch 有图形比较直观,调试更加方便
【方便是学习的最大动力,不方便谁会想学习呢?】
[10:02:42]:bin$ gdb-multiarch --version
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
用gdb调试xv6-riscv如下设置
# 下载源码 https://github.com/mit-pdos/xv6-riscv
# 编译执行,如果缺少某些包就安装一下
[10:13:49]:xv6-riscv$ make qemu-gdb
*** Now run 'gdb' in another window.
qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 128M -smp 1 -nographic -global virtio-mmio.force-legacy=false -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
# QEMU 快捷键:
# Ctrl+A-X 退出
# Ctrl+A-C 切换到qemu控制台
# 打开另一个窗口,执行gdb-multiarch就可以进入调试, Makefile会自动生成.gdbinit的内容
[10:15:47]:xv6-riscv$ gdb-multiarch -x ./.gdbinit
用vscode调试xv6-riscv可以做如下设置:
# 准备两个文件lauch.json和task.json就行,网上也有不少如何编写这两个文件的教程
# 需要注意的是要在默认的.gdbinit上注释掉 target remote 127.0.0.1:26000,vscode与这个会冲突
# 然后在vscode上F5就行
[10:32:59]:xv6-riscv$ cat .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "xv6debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/kernel/kernel",
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"miDebuggerServerAddress": "127.0.0.1:26000", //见.gdbinit 中 target remote xxxx:xx
"miDebuggerPath": "/usr/bin/gdb-multiarch", // which gdb-multiarch
"MIMode": "gdb",
"preLaunchTask": "xv6build"
}
]
}
[10:33:04]:xv6-riscv$ cat .vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "xv6build",
"type": "shell",
"isBackground": true,
"command": "make qemu-gdb",
"problemMatcher": [
{
"pattern": [
{
"regexp": ".",
"file": 1,
"location": 2,
"message": 3
}
],
"background": {
"beginsPattern": ".*Now run 'gdb' in another window.",
// 要对应编译成功后,一句echo的内容. 此处对应 Makefile Line:170
"endsPattern": "."
}
}
]
}
]
}
# 现在就可以进行xv6-riscv代码调试了。
稍微修改一下Makefile,可以同时使用gdb和vscode来调试
## 默认情况下会自动根据userid生成不同的GDBPORT,这使得不能同时使用vscode和gdb
## 所以直接为gdb准备一个固定端口26001拉倒
.gdbinit-gdb: .gdbinit.tmpl-riscv
sed "s/:1234/:26001/" < $^ > $@
qemu-gdb: $K/kernel fs.img
@echo "*** Now run 'gdb' in another window." 1>&2
$(QEMU) $(QEMUOPTS) -S -gdb tcp::26001
gdb: .gdbinit-gdb
@echo "--------- start gdb multiarch ------------------------"
$(GDB) -q -x ./.gdbinit-gdb --nx
# for vscode
# 而且vscode还需要删除.gdbinit中的target,所以单独给vscode准备一个.gdbinit
# 并且修改一下.vscode/tasks.json中的command为 make qemu-gdb-vscode 即可。
.gdbinit: .gdbinit.tmpl-riscv
sed "/:1234/d" < $^ > $@
qemu-gdb-vscode: $K/kernel .gdbinit fs.img
@echo "*** Now run 'gdb' in another window." 1>&2
$(QEMU) $(QEMUOPTS) -S -gdb tcp::26000