4

I am self-studying the 2019 version of MIT 6.828/6.S081: Operating System Engineering.

I was trying to attach GDB to xv6 running on RISC-V using QEMU, to learn about what is going on when context switching happens between user mode and kernel mode.

After doing make qemu-gdb and gdb in the same directory, my GDB connected to QEMU successfully. However:

(gdb) x/2i $pc
=> 0xd8c:   ecall
   0xd90:   ret

The problem is: Now if I stepi, it "jumps over" to 0xd90 instead of stepping into the kernel space.

Additionally, accessing any kernel addresses is not allowed, as if I was debugging a normal userland program:

(gdb) i r stvec
stvec          0x3ffffff000 274877902848
(gdb) x/i $stvec
   0x3ffffff000:    Cannot access memory at address 0x3ffffff000

Environment:

Host VM: Manjaro 19.0.2

sudo pacman -Syy

sudo pacman -S riscv64-linux-gnu-binutils riscv64-linux-gnu-gcc riscv64-linux-gnu-gdb qemu-arch-extra

GDB: 9.1

QEMU: 4.2.0

GCC: 9.2.0

Much appreciate anyone could share some insight about what is going on here. Thanks a lot!

  • I just fixed a single step issue for qemu. Not sure if it is your problem. Check it out here: https://www.mail-archive.com/qemu-devel@nongnu.org/msg681645.html – Changbin Du Mar 22 '20 at 11:21
  • Did you manage to get around this problem? I'm experiencing the exact same thing. – quicoju Nov 13 '21 at 11:51

2 Answers2

0

I guess you run your code on ubuntu, that is the problem I experienced, then I change to mac, and flow mit tools tutorials, finally, it works.

  1. run make CPUS=1 qemu-gdb in one window.
  2. run riscv64-unknown-elf-gdb in another window.

ignore the Python Exception

0

I managed to get around this problem by building the riscv toolchain as explained here.

Building the toolchain as explained in the site, generates a generic ELF/Newlib toolchain identified with the prefix riscv64-unknown-elf- in contrast to the more sophisticated Linux-ELF/glibc toolchain identified by the prefix riscv64-unknown-linux-gnu-. The Newlib build allows the debugger to stepi into kernel space.

For crossdev users it is possible to build the toolchain with Newlib support by running:

crossdev --ex-gcc --ex-gdb --target riscv64-unknown-elf
quicoju
  • 1,661
  • 11
  • 15