Skip to content

Debug emulated firmware with cortex-debug

Every dev board in the Lab can be debugged at source level — breakpoints, stepping, variables, registers, peripheral view — with the cortex-debug VS Code extension (or a fork of it; the configuration is identical). No OpenOCD, no probe, no hardware: Renode has a GDB server built in, and the emulated CPU is the target.

VS Code + cortex-debug ──> arm-none-eabi-gdb ──TCP :3333──> Renode GDB stub ──> emulated Cortex-M

How it fits together

  • Renode's Monitor command machine StartGdbServer <port> starts a GDB remote stub inside the emulation.
  • cortex-debug is told the server already exists (servertype: "external") and simply points its arm-none-eabi-gdb at the TCP port.
  • The emulation does not start until the debugger connects, so the CPU is still sitting at the reset vector — you never miss boot code, and every breakpoint can be planted before anything runs.
  • Renode controls execution itself, so breakpoints are unlimited (no flash-patch hardware unit), single steps are exact, and virtual time only advances while the CPU runs — stopping at a breakpoint freezes the timers and the UART exactly where they were.

A firmware project of your own

python -m embeddyn.mechatronics.devboards setup <board> <project> --elf build/app.elf [--pinout x.zpinproj] sets up a whole project at once: debug.sh, gdb.sh, VS Code tasks that start and stop Renode around the session, the cortex-debug configuration and a Lab project. See tutorial 13.

Step 1 — generate the VS Code configuration

python -m embeddyn.mechatronics.devboards debug lp_mspm0c1104 --write-vscode

This adds (or updates) an entry in .vscode/launch.json:

{
    "name": "LP-MSPM0C1104 LaunchPad (Renode)",
    "type": "cortex-debug",
    "request": "attach",
    "servertype": "external",
    "gdbTarget": "localhost:3333",
    "executable": "${workspaceFolder}/examples/mechatronics/firmware/devboards/bin/lp_mspm0c1104.elf",
    "runToEntryPoint": "main",
    "showDevDebugOutput": "off",
    "gdbPath": "C:/arm-none-eabi/11_3/bin/arm-none-eabi-gdb.exe"
}

The fields that matter:

Field Why
request: attach the .resc already loaded the ELF into emulated flash; there is nothing to load
servertype: external cortex-debug must not start OpenOCD/pyOCD; Renode is the server
gdbTarget where Renode's GDB stub listens (--port changes it)
runToEntryPoint on connect, continue straight to main — drop it to stop at the reset handler instead
gdbPath your arm-none-eabi-gdb; auto-detected, or set armToolchainPath in VS Code instead

Useful options:

# debug your own firmware instead of the demo
python -m embeddyn.mechatronics.devboards debug lp_mspm0c1104 --firmware path/to/your.elf --write-vscode

# peripheral registers in the debug sidebar, from a CMSIS-SVD file
python -m embeddyn.mechatronics.devboards debug lp_mspm0c1104 --svd path/to/mspm0c110x.svd --write-vscode

# several boards at once need distinct ports
python -m embeddyn.mechatronics.devboards debug lp_mspm0c1104 --port 3334 --write-vscode

Step 2 — start Renode

Either let the helper launch it:

python -m embeddyn.mechatronics.devboards debug lp_mspm0c1104 --run

or copy the command line it prints — it is the board's normal boot script plus the GDB server:

renode --disable-gui --console --plain -e "$bin=@<firmware>.elf; include @<board>.resc; machine StartGdbServer 3333"

Renode sits idle in the Monitor console, waiting. (Use the Renode build of the embeddyn fork — see Build Renode with custom cores; upstream Renode does not have the MSPM0 models.)

Step 3 — debug in VS Code

Open the firmware folder, press F5 and pick the configuration. cortex-debug connects, the emulation starts, runs to main and halts. From there everything works as on real hardware:

  • breakpoints, conditional breakpoints, single step, step into/out, instruction stepping in disassembly;
  • variables, watches, the call stack, memory view;
  • the peripheral register view when an SVD file is configured;
  • the Renode Monitor itself through gdb: in the Debug Console type
-exec monitor sysbus ReadWord 0x20000000
-exec monitor iomux PinLevel 22
-exec monitor cpu PC

any Renode Monitor command works this way, which is something no real probe can do.

Working notes

  • Rebuilds: the ELF is loaded when the .resc runs, so after rebuilding, quit Renode and start it again (then reconnect). Rebuilding with -g is required for source lines — the demo firmware already is. -Os inlines aggressively; for a comfortable debugging session build your own firmware with -O0 or -O1.
  • Live variables: the GDB stub only answers while the CPU is stopped, so cortex-debug's continuous refresh looks frozen during a run. That is normal; everything updates on the next halt.
  • UART output still goes where the board sends it — run Renode with its console visible, or add uart0 CreateFileBackend @/path/console.txt true to the -e commands before StartGdbServer.
  • Two boards at once work the same as two real ones: separate Renode processes, separate ports (--port 3333 / --port 3334), separate VS Code debug sessions.

Inside the Lab co-simulation

The session above is the board running standalone. To debug firmware while the Lab drives its pins — the button switch pressing in real time, the plant feeding the ADC — start Renode yourself with both servers, the GDB stub and the External Control server the adapter uses:

renode --disable-gui --console --plain -e '$bin=@<firmware>.elf; include @<board>.resc;
    machine StartGdbServer 3333; emulation CreateExternalControlServer "embeddyn" <port>'

then set launch: false and the matching port in the board instance's options in the Lab project (see the Renode reference). The Lab connects to the running instance and advances virtual time with RunFor, while you debug over GDB. Be aware the two interact: the CPU only moves when the co-simulation runs it, so breakpoints halt the co-simulation's notion of progress too — which is exactly what you want when inspecting a control loop.

If the connection fails

Symptom Cause
Connection refused on port 3333 Renode is not running, or the .resc failed — read the Monitor console; with upstream Renode the MSPM0 platform types are unknown
gdb connects but the CPU is running free another gdb client already connected; the server starts the emulation on the first connection
No source lines, only disassembly firmware built without -g, or executable in launch.json points at a different ELF than the one Renode loaded
monitor commands error typo — they go to the Renode Monitor verbatim; try them in Renode's own console first