LabsLand extension · Local debugger practice · QEMU

Find a signed-comparison bug with GDB

Your computer · GDB and QEMU

Download worksheet and GDB project (.zip)

Practise breakpoints, single-stepping, register inspection and memory edits on a local emulated RISC-V processor. A program is supposed to find the largest of five signed readings. Work out where it loses the correct answer.

Start two terminals

This exercise was tested on macOS with xPack RISC-V GCC 15.2.0, GNU GDB 16.3, QEMU 11.1.2 and make. You need riscv-none-elf-gcc, riscv-none-elf-gdb and qemu-system-riscv32 on your command path. RISC-V tools · QEMU downloads

Extract the worksheet pack. In the first terminal enter its starter directory and run make run. QEMU waits for a debugger. In a second terminal change to the same directory and run make debug. These two processes communicate on your own computer.

Catch the first incorrect comparison

Enter these commands at the GDB prompt.

break inspect_sample
break finished
continue
x/5dw &samples
p/d (int)$t1
p/d (int)$t2
x/i $pc
stepi
x/i $pc

The first breakpoint stops just before a branch. Make a prediction before stepping. Continue to the comparison for each of the remaining three samples and record the saved maximum and the incoming value. At finished, inspect the stored answer with x/dw &result.

Incoming sample Saved maximum before branch Should the sample replace it?
-4
12
7
3

Change an input without rebuilding

At finished, enter the following. The two breakpoints above were created in order, so number 1 is the comparison. Use info breakpoints to check.

set {int}((char*)&samples+8) = 25
set $pc = &_start
disable 1
continue
x/dw &result

Repair the comparison in probe.s. Use detach and quit, stop QEMU in the first terminal with Ctrl-C, then run make clean before repeating make run and make debug. Compare the original and changed inputs in both programs. Submit the first branch where signed and unsigned ordering disagree, the two result pairs and your one-instruction repair.

This LabsLand exercise prepares you for the debugger commands in FPGAcademy Computer Organization Lab 1, Part VI. That activity applies GDB to a physical Nios V processor on a DE1-SoC. Original handout · GDB remote debugging