MIT6.S081 调试xv6

之前学了一些gdb的使用,但是总不能实际上手操作,不如终端IDE可视化调试。这次由于Docker配置环境,不想再折腾连接IDE调试,于是学习GDB。

开始

在课程的lab guidance上有这么一段话:

In many cases, print statements will be sufficient, but sometimes being able to single step through some assembly code or inspecting the variables on the stack is helpful. To use gdb with xv6, run make make qemu-gdb in one window, run gdb (or riscv64-linux-gnu-gdb) in another window, set a break point, followed by followed by ‘c’ (continue), and xv6 will run until it hits the breakpoint. (See Using the GNU Debugger for helpful GDB tips.)

大意是,在一个终端上执行 make qemu-gdb ,在另一个终端上执行 gdb

出现的问题

当我在第二个终端执行gdb时出现以下提示:

1
2
3
4
5
6
7
8
9
10
11
warning: File "/home/xv6-labs-2021/.gdbinit" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
add-auto-load-safe-path /home/xv6-labs-2021/.gdbinit
line to your configuration file "/root/.gdbinit".
To completely disable this security protection add
set auto-load safe-path /
line to your configuration file "/root/.gdbinit".
--Type <RET> for more, q to quit, c to continue without paging--
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual. E.g., run from the shell:
info "(gdb)Auto-loading safe path"

直接按照提示,add-auto-load-safe-path /home/xv6-labs-2021/.gdbinit

关于gdbinit文件

查看 gdbinit文件内容,猜测

1
//

另开一个Terminal窗口,切到xv6-labs-2020目录下,切到util分支,执行

1
riscv64-unknown-elf-gdb kernel/kernel

然后在(gdb)环境下执行

1
(gdb) target remote localhost:26000

不过可以按如下操作简化:在~目录下新建.gdbinit文件,内容为:

1
add-auto-load-safe-path ~/xv6-labs-2020/.gdbinit

其中~改为自己的xv6-labs-2020目录所在路径

调试

在一个终端上执行 make qemu-gdb ,在另一个终端上执行 gdb

https://zhuanlan.zhihu.com/p/466423677

直接使用gdb命令会提示体系结构不支持,因此改用其他命令。

如果想调试特定的文件例如xargs.c,则在一开始启动gdb后执行:

1
(gdb) file user/_xargs

加载特定的符号文件,然后就可以打断点了

1
(gdb) b main

然后调试

1
(gdb) c

如果不加载符号文件,打断点时就会找不到函数位置。

作者

Desirer

发布于

2024-01-03

更新于

2024-01-07

许可协议