Lab 0: 准备工作

0 准备工作

课程主页1
本实验将会在ubuntu 26.04上进行。

安装

sudo apt-get install git build-essential gdb-multiarch qemu-system-misc gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu qemu-system-riscv gcc-riscv64-unknown-elf

检验

$ qemu-system-riscv64 --version
QEMU emulator version 10.2.1 (Debian 1:10.2.1+ds-1ubuntu3.2)
Copyright (c) 2003-2025 Fabrice Bellard and the QEMU Project developers

$ riscv64-linux-gnu-gcc --version
riscv64-linux-gnu-gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# 裸机编译器。
$ riscv64-unknown-elf-gcc --version
riscv64-unknown-elf-gcc (14.2.0+19) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

准备课程文件

git clone git://g.csail.mit.edu/xv6-labs-2025
cd xv6-labs-2025
make qemu
# 进入到shell界面,通过ctrl-a + x 退出。

debug 技巧

首先我们需要将课程提供的 gdbinit 文件放入到safe path中安全加载。将下面的内容写入 ~/.config/gdb/gdbinit

add-auto-load-safe-path /path/to/xv6-labs-2025/.gdbinit

采用tmux进行分屏。在一边的屏幕上执行 make qemu-gdb。编译如果成功会出现:

*** Now run 'gdb' in another window.
qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 128M -smp 3 -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

在另一个窗口,我们执行 gdb-multiarch。然后我们执行:

target remote localhost:26000

# 图形化界面debug。
tui enable    

直接continue,直到显示出来shell的 $ 后输入ctrl + c,线程会自己挂起。我们可以看看有什么。

img

这时候我们就可以用 file user/_xxx ,tab来寻找自己需要加载的ELF文件即可。

img

本文没有采用AI辅助写作。

posted @ 2026-09-03 19:03  木木ちゃん  阅读(9)  评论(0)    收藏  举报