MIT6.S081的Ubuntu环境下的调试方法
在进行pgtbl实验的时候遇到了很多问题,必须要进行调试了,发现课程上的暴怒能直接使用,因为我是ubuntu,需要另外配置。下面是参考别人的配置过程记录一下
1 GDB调试
GDB调试(第一次运行)
echo "add-auto-load-safe-path $(pwd)/.gdbinit " >> ~/.gdbinit
运行xv6
make CPUS=1 qemu-gdb
连接
gdb-multiarch kernel/kernel # 第二个终端
配置
# 进入gdb后执行
set confirm off
set architecture riscv:rv64
set riscv use-compressed-breakpoints yes
target remote localhost:26000
tips
# 调试用户态程序
file user/_uthread
# 打开汇编/c源代码窗口,退出ctrl+x a
layout split
# 查看某地址对应指令
x/i 0x630
# 16进制打印
p/x 1536
# 打印寄存器的值
p $a0
i r a0
# 查看栈帧
bt
i f num
# 单步执行(不进函数)
n
# 单步执行(进函数)
s
# 汇编单步执行(不进函数)
ni
# 汇编单步执行(进函数)
si
# 打断点
b label
# 删除断点
delete breakpoints
delete num
2 VSCode调试
2.1 配置
比较推荐,功能更全。
首先在xv6-labs-2021目录下创建.vscode文件夹,在这个文件夹下新建launch.json、tasks.json,内容分别如下:
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"
}
]
}
tasks.json文件
{
"version": "2.0.0",
"options": { //指定make qemu的执行位置
"cwd": "${workspaceFolder}"
},
"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.",
"endsPattern": "."
}
}
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
然后进行调试测试,我们可以找到 kernel/main.c 下的第13行,打一个断点,然后按F5。
错误1:如果出现 /Program path 'xv6-labs-2021/kernel/kernel' is missing or invalid 错误,先不用管,等待终端的gdb跑起来。
错误2:如果出现 Unexpected GDB output from command "-target-select remote 127.0.0.1:26000". Remote communication error. Target disconnected.: Connection reset by peer. 错误,需要在xv6-labs-2020下找到.gdbinit文件,注释掉target remote这行:
# target remote 127.0.0.1:26000
注意1:如果make clean后再按F5(它会执行make qemu-gdb),.gdbinit这个文件会被刷新,所以我们又要去注释掉target remote那行,然后再按F5,目前我还没找到更好的解决方法。
注意2:由于xv6默认多核运行,所以调试代码的时候可能会有点混乱,可以通过修改tasks.json中command字段为make qemu-gdb CPUS=1,即单核运行。这仅供调试使用,因为正常情况下代码要通过多核的运行。
📝 引用信息
- 原文档:
MIT 6.S081 操作系统学习记录
XV6调试

浙公网安备 33010602011771号