UP | HOME

YAGNI
You are(n't)? gonna need it…

Debug a FreeRTOS RISC-V application running on QEMU

About

The previous post explained how to use OpenBSD to build and run a FreeRTOS RISC-V "demo" inside QEMU. This post will explain how to build the same application for debugging and setup the tools to debug it.

Install GDB for RISC-V

In OpenBSD, the package riscv32-esp-elf-gdb provides the debugger for the RISC-V architecture targeted for bare-metal applications, it can be installed by running:

pkg_add riscv32-esp-elf-gdb

The debugger executable will be installed in /usr/local/riscv32-esp-elf/bin/riscv32-esp-elf-gdb. For convenience it's a good idea to define an alias for this long path:

alias gdb-rv=/usr/local/riscv32-esp-elf/bin/riscv32-esp-elf-gdb

Build application with debugging symbols

Previously we used the FreeRTOS' RISC-V-Qemu-virt_GCC demo as an example; in this post we'll continue to use the same example. To build the application for debugging, we need to edit the Makefile so it uses the correct CFLAGS for debugging.

Replace the following flags:

ifeq ($(DEBUG), 1)
  CFLAGS += -Og -ggdb3

With these flags (refer to the See also section for details):

ifeq ($(DEBUG), 1)
  CFLAGS += -O0 -gdwarf-2

The application can now be built for debugging:

DEBUG=1 gmake

Run the application

Run the application with qemu using the -s switch, this will enable the gdb server and will make it reachable on port 1234:

  # run the demo
qemu-system-riscv32 -s -nographic -machine virt -net none \
-chardev stdio,id=con,mux=on -serial chardev:con \
-mon chardev=con,mode=readline -bios none \
-smp 4 -kernel ./build/RTOSDemo.axf

Connect GDB with the remote target

Once the application is running, we can execute gdb in another terminal and connect to the remote target. Using the alias previously defined:

gdb-rv build/RTOSDemo.axf

From the gdb's prompt, set the architecture, then connect to the remote target and finally set a breakpoint:

(gdb) set architecture riscv:rv64
(gdb) target remote localhost:1234
(gdb) b main_blinky.c:81
Breakpoint 1 at 0x800001f4: file main_blinky.c, line 81.
(gdb) continue

After entering the continue command, the application will to execute until it hits the breakpoint; at this point we have our debugging session ready to use.

See also

Date: 2025-05-01