由于6.s081使用的Xv6是基于rsicv架构的,而我使用的虚拟机为x86-64架构,故而需要使用交叉编译的技术。

幸运的是,6.s081课程平台上已经提供了交叉编译需要的工具,例如gdb,gcc等;调试xv6,gdb是必不可少的。

具体而言,我们在一个终端中使用单核启动xv6:

1
2
3
➜  xv6-labs-2020 git:(traps) ✗ make CPUS=1 qemu-gdb
*** Now run 'gdb' in another window.
qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 128M -smp 1 -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::26001

在另一个终端中,我们运行gdb即可(注意,是运行rsicv对应的gdb!):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
➜  xv6-labs-2020 git:(traps) ✗ riscv64-unknown-elf-gdb          
GNU gdb (GDB) 10.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=x86_64-pc-linux-gnu --target=riscv64-unknown-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
warning: File "/home/cs18/myproj/6.828/xv6-labs-2020/.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/cs18/myproj/6.828/xv6-labs-2020/.gdbinit
line to your configuration file "/home/cs18/.gdbinit".
To completely disable this security protection add
set auto-load safe-path /
<q to quit, c to continue without paging--
line to your configuration file "/home/cs18/.gdbinit".
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"
(gdb)

在我的机器上给出了一些报错,阅读之后,不难发现是因为我的默认.gdbinit屏蔽了交叉编译工具中的Gdb,在报错提示的地方加入相应的命令即可。

修改之后,再次运行gdb,这一次就可以开始愉快地debug之旅了!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
➜  xv6-labs-2020 git:(traps) ✗ riscv64-unknown-elf-gdb 
GNU gdb (GDB) 10.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=x86_64-pc-linux-gnu --target=riscv64-unknown-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
The target architecture is set to "riscv:rv64".
warning: No executable has been specified and target does not support
determining executable automatically. Try using the "file" command.
0x0000000000001000 in ?? ()
(gdb) b _entry
Breakpoint 1 at 0x8000000a
(gdb) c
Continuing.

Breakpoint 1, 0x000000008000000a in _entry ()
=> 0x000000008000000a <_entry+10>: f3 25 40 f1 csrr a1,mhartid