xv6调试用户程序
本文介绍使用vscode来调试用户程序。
vscode远程连接虚拟机上的Linux
- 安装Remote-SSH,同时,虚拟机上的Linux系统也要安装并启动openssh-server
- 利用Remote-SSH远程连接虚拟机上的Linux
- 在虚拟机上安装插件
C/C++和Native Debug
工程配置
在.vscode新建launch.json和c_cpp_properties.json
把.gdbinit.tmpl-riscv中target remote 127.0.0.1:26000注释1
2
3
4
5set confirm off
set architecture riscv:rv64
#target remote 127.0.0.1:26000
symbol-file kernel/kernel
set disassemble-next-line auto
根据target remote 127.0.0.1:26000来设置launch.json的target1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "gdb",
"request": "attach",
"name": "Attach to gdbserver",
"executable": "${workspaceRoot}/kernel/kernel",
"gdbpath": "/usr/bin/gdb-multiarch",
"remote": true,
"target": "127.0.0.1:26000",
"cwd": "${workspaceRoot}",
"stopAtEntry": true,
}
]
}
为了在vscode上代码跳转,
在Ubuntu上安装bear,接着make clean,然后bear make qemu生成compile_commands.json。
把compile_commands.json移动到.vscode里面。
之后如果修改源代码的话,同样也要bear make qemu重新生成compile_commands.json。1
2
3
4
5
6
7
8
9
10
11// c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
// make clean; bear make qemu
"compileCommands": "${workspaceFolder}/.vscode/compile_commands.json"
}
],
"version": 4
}
调试用户程序ls
- 在下方命令行输入
make qemu-gdb。qemu会自动启动,gdb开始等待接入。 - 按下F5, 或者 点击左侧按钮运行与调试,并点击左上角绿色三角(Attach to gdbserver)。
如果不想开启多核心,可以在运行make qemu-gdb时使用CPUS=1,以单核模式启动,即输入命令变为make qemu-gdb CPUS=1。
每次调试完成,务必使用红色按钮 断开 GDB调试,并在命令行中 Ctrl-A, X以停止qemu。
- 我们知道,在进入Trampoline切换前最后一行C代码位于
kernel/trap.c:128处,我们将断点打在此处,继续点击“运行”。 - 在xv6的shell中输入ls,以启动ls程序;程序停留在
kernel/trap.c:128处。 - 接下来,我们需要确认对应xv6的用户程序入口点,我们有两种方法可以确认应用程序的入口点:
- 通过readelf确认应用程序入口点,
readelf -h user/_ls,可见其中显示Entry point address: 0x27a,应用程序入口点位于0x27a处。我们前往调试控制台,在其中输入b *0x27a,即将断点置于ls程序入口处。 - 在VSCode上直接打开该应用程序ls的源代码,找打main()函数,并在main()函数里打上断点。
- 通过readelf确认应用程序入口点,
- 接下来,我们需要在调试窗口左下角删除原有的内核态断点,并通过调试控制台,加载ls的调试符号。在其中输入
file user/_ls。 - 点击“运行”。可以看到已经进入了ls.c的main函数中。
更多详细内容请参考:http://hitsz-cslab.gitee.io/os-labs/remote_env_gdb/#444

