Skip to content

Latest commit

 

History

History
339 lines (252 loc) · 13.7 KB

Debugging.md

File metadata and controls

339 lines (252 loc) · 13.7 KB

Debugging

Overview

Meadow.OS is debugged using OpenOCD as the GDB server and gdb (with Python scripting) as the client. The ST-Link SWD interface connects to the target. QEMU is also supported for software-only testing.

Prerequisites

  • OpenOCD installed and accessible on PATH (or referenced via MEADOW_OPENOCD_COMMAND)
  • ARM cross-toolchain (arm-none-eabi-gdb / arm-none-eabi-gdb-py)
  • ST-Link v2/v3 debugger connected to the board's SWD port
  • A completed build (symbol files nuttx/nuttx.elf and nuttx/nuttx_user.elf must exist)

Quick Start

Open two terminals:

Terminal 1 -- Start the OpenOCD server:

./debug.sh --server

Terminal 2 -- Launch GDB:

./debug.sh

GDB will automatically connect to localhost:4242, load symbol files, and apply the .gdbinit configuration.

debug.sh Options

Flag Effect
--server Start OpenOCD GDB server (blocks)
--stlink Use st-util instead of OpenOCD
--ocd / --openocd Use OpenOCD (default)
--qemu Use QEMU instead of hardware

OpenOCD Configuration

Debug session (debug.cfg)

source [find interface/stlink.cfg]
source [find target/stm32f7x.cfg]
reset_config none separate

gdb port 4242
debug_level 1
tcl port 6666

# NuttX RTOS awareness
stm32f7x.cpu configure -rtos nuttx

init
reset init

NuttX RTOS awareness is enabled in OpenOCD. This allows OpenOCD to display NuttX tasks as threads in GDB. The excerpt leaves out comments and a disabled (if {0}) image-loading block. Because of reset init, starting a debug session halts and resets the board.

Flash session (flash.cfg)

flash.cfg writes whichever image is present, and the first branch is the one that normally runs. If nuttx/Meadow.OS.bin exists -- the combined bootloader and OS image that build.sh produces -- it is written to 0x08000000 and the script stops there:

set image nuttx/Meadow.OS.bin
if {[file exists $image]} {
  flash write_image erase $image 0x08000000
  verify_image $image 0x08000000
  shutdown
}

Only when that image is absent does it fall back to the separate binaries, writing nuttx.bin at 0x08000000 and nuttx_user.bin at 0x08040000:

flash write_image erase nuttx/nuttx.bin 0x08000000
flash write_image erase nuttx/nuttx_user.bin 0x08040000

Note: flash.sh uses flash.cfg by default. DFU mode (--dfu) is also supported using dfu-util.

GDB Initialisation (gdb/.gdbinit)

The .gdbinit file in the gdb directory is loaded automatically when GDB is started from that directory. It:

  1. Configures history, output radix (hex), and remote hardware limits:
    • 8 hardware breakpoints
    • 4 hardware watchpoints
  2. Defines load-nuttx-symbols to load symbol files:
    file -readnow ../nuttx/nuttx.elf
    add-symbol-file -readnow ../bootloader/Debug/Meadow.BL.elf  (if present)
    add-symbol-file -readnow ../nuttx/nuttx_user.elf  (if present)
    
  3. Loads the Python helper scripts in gdb/ and the upstream NuttX scripts (nuttx/tools/pynuttx/gdbinit.py)
  4. Connects to the GDB server: target extended-remote :4242, forces hardware breakpoints and turns on cortex_m vector_catch all, so the core halts on every fault and on reset. The setting is held in the core's DEMCR register and is not cleared when GDB detaches; turn it off with monitor cortex_m vector_catch none before leaving the board to run on its own.

NuttX RTOS awareness needs no set-up from GDB: OpenOCD reads the task control block offsets from g_tcbinfo in the firmware. NuttX 12 changed the layout of g_tcbinfo (it added stack_off/stack_size_off); OpenOCD needs the FixGDBPythonScripts change to src/rtos/nuttx.c in $MEADOW_OPENOCD_DIR to read it. Without that change (upstream OpenOCD included, as of November 2026) the registers OpenOCD reports for tasks that are not running are wrong, and bt, thread N and IDE call stacks show garbage for those tasks. nx_bt <pid> works with either build, as it unwinds from the context saved in the TCB. Any error in .gdbinit stops the rest of the file from running, so keep commands that can fail (for example add-symbol-file of a file that may be missing) guarded.

Useful GDB Macros

Macro Description
load-nuttx-symbols Reload all symbol files
reset-qemu Load image and reset QEMU target
enable_hardfault Enable hard fault on divide-by-zero and unaligned access (sets DEMCR bit)

Python GDB Extensions (gdb)

Custom Python scripts extend GDB with Meadow-specific commands:

Script Purpose
Nuttx.py nx_bt backtraces (any task, syscall and exception annotations, Mono interpreter frames), a dispatch_syscall frame unwinder so bt crosses from the kernel into user space, register decoding, nx_dmesg; helpers used by the other scripts, so it is loaded first
Nuttx_Tasks.py Task list and task details (nx_tasks, nx_task, show regs, show check_stack, ...)
NuttxHeap.py Heap analysis: show heap, show freeheap, nx_check_heap
Tracing.py Backtrace recording and heap allocation tracing
Svd.py svd: peripheral registers decoded from STM32F777.svd
Meadow.py Board and fault diagnostics: nx_alive, nx_reset, nx_fault, nx_mpu, nx_irqs, espcp_status
Mono.py nx_jit, nx_jit_compile; names JIT code frames in nx_bt

See gdb/readme.md for the full command list, and for which of the upstream pynuttx commands work in this build.

./build.sh --clean --force --gdbdiag builds with STACK_COLORATION, which gives stack high-water marks in GDB (nx_tasks, nx_task). Use it for debugging builds only.

Flashing the Board

Using OpenOCD (default)

./flash.sh

Writes nuttx/Meadow.OS.bin to 0x08000000 via ST-Link when that combined image is present, falling back to nuttx/nuttx.bin (kernel) and nuttx/nuttx_user.bin (user) when it is not.

Using DFU

./flash.sh --dfu

Writes nuttx/Meadow.OS.bin to 0x08000000 using dfu-util. Requires the board to be in DFU mode.

Including the Mono Runtime

./flash.sh --includeruntime

Also writes Meadow.OS.Runtime.bin to the board using the Meadow CLI tool (meadow mono update rt).

Resetting the Board

./flash.sh --reset

Uses STM32CubeProgrammer CLI (CUBE_APP env var must point to STM32_Programmer_CLI) to perform a hard reset after flashing.

QEMU Debugging

QEMU support allows debugging without physical hardware.

Start QEMU server (ARM):

./debug.sh --server --qemu

Start QEMU server (ESP32/Xtensa):

./debug.sh --server --qemu --esp

The ESP32 firmware is no longer in this repository; it is built from the separate Meadow-ESP32 repository. debug.sh boots the image from Meadow-ESP32/Source/MeadowComms/MeadowComms-qemu.bin and runs QEMU from qemu-esp32/build/xtensa-softmmu/qemu-system-xtensa, both relative to the workspace root, so clone (or symlink) Meadow-ESP32 and build its QEMU image there first.

QEMU ARM machine configuration:

  • Machine: meadow
  • Boot image: nuttx/Meadow.OS.bin (or nuttx.bin fallback)
  • QSPI flash: qemu/meadow_qspi_flash.raw (32 MB raw file, created on first run)
  • GDB port: 4242
  • hcom serial port: TCP 1234

Debugging the Bootloader

.gdbinit loads the bootloader symbols when bootloader/Debug/Meadow.BL.elf has been built. They can also be loaded by hand with the read-bootloader-symbols macro:

add-symbol-file -readnow ../bootloader/Debug/Meadow.BL.elf

Crash recording

When the OS panics (a fault, an assert()/PANIC() in the kernel or in a user task; every assertion is fatal with CONFIG_BOARD_RESET_ON_ASSERT=2), the crash is recorded so that it can be read after the board resets. Design: Documentation/Plans/CoreDump.md.

At the crash. A panic notifier (F7MeadowOS/meadow_os/meadow_os_crash.c) runs before _assert() prints anything and writes to DTCM, which keeps its content through a reset:

  1. a crash record (meadow_os_crash_record_t): OS version and build hash, uptime, task and process, assertion file/line/message, the fault status registers and the saved registers;
  2. the last 8 KB of the syslog (the RAMLOG), in order;
  3. an ELF core of every thread: registers and the used part of each stack.

It also sets the OS fault bits in BK29R. The normal text dump follows on the live syslog.

At the next boot. hcom writes the crash to /meadow0/crash/ and logs one line, for example OS crash 7: HardFault in hcom_main (pid 11), PC 081898d4, LR 0818956f, uptime 54387 ms.:

File Contents
os-<seq>-record.txt The record in words: exception, CFSR/HFSR bit names, fault address, registers, task, version
os-<seq>-log.txt The syslog leading up to the crash
os-<seq>-core.elf ELF core, 20-80 KB (about 3 KB per thread)
os-watchdog-resets.txt A line per independent watchdog reset that left no crash record

The files of the last 4 crashes are kept. <seq> restarts at 1 after a power cycle.

Reading a crash on the host with tools/crash/meadow_crash.py (see its README):

tools/crash/meadow_crash.py fetch          # copy the files from the board
tools/crash/meadow_crash.py show           # the latest record and the end of its log
tools/crash/meadow_crash.py gdb            # open the core with the local build
tools/crash/meadow_crash.py gdb --release  # ... with the ELF files of the release that crashed

Or by hand, with the ELF files of the build that crashed (a release's are in its Meadow.Debug.zip):

arm-none-eabi-gdb -nx nuttx/nuttx.elf
(gdb) add-symbol-file nuttx/nuttx_user.elf
(gdb) core-file os-7-core.elf
(gdb) info threads
(gdb) bt
(gdb) thread apply all bt

GDB reads the threads and their registers from the core itself; the NuttX Python commands in gdb/ are not needed (and do not work on a core, which holds only the stacks).

Limits:

  • Crash data survives a software reset, meadow device reset, a debugger reset and the reset button (NRST), not a power cycle (VBAT is not backed), and not a boot that applies or checks an OTA image (the bootloader uses all of DTCM then; the next boot logs "OS crash flagged ... but the crash record in DTCM was lost").
  • Reflashing over SWD can lose an unprocessed crash: OpenOCD uses DTCM as its flash work area. Let the board boot once (or run meadow_crash.py dtcm) before flashing.
  • GDB does not unwind through arm_dispatch_syscall, so the backtrace of a thread in a system call stops at the kernel side.
  • Assertion file names are only recorded if the build keeps them (CONFIG_ASSERTIONS_FILENAME).
  • Mono JIT frames have no symbols.
  • A fault before board_late_initialize() registers the notifier is not recorded.

Watchdog. The independent watchdog (IWDG, CONFIG_MEADOW_IWDG_BACKSTOP) resets the board about 30 s after the system tick stops, for example when a fault handler hangs before it reaches _assert(). Such a reset leaves no crash record; the next boot logs "Independent watchdog reset with no OS crash record" and adds a line to /meadow0/crash/os-watchdog-resets.txt. It is kicked every 10 s from the system tick, by the panic notifier, and from Stop mode (pwrmgmt wakes every 10 s while asleep). A debugger halt does not trip it. Test it with meadow developer -p 902 -v 2 (hang with interrupts off).

Testing it. Build with --unittests=os and force a fatal error: meadow developer -p 900 -v 0 (user space NULL write), -p 900 -v 1 (user space PANIC()), -p 902 -v 0 (kernel NULL write) or -p 902 -v 1 (kernel PANIC()).

Stack Dumps via UART

To get kernel panic stack dumps on USART1 (COM1) instead of using RAMLOG:

./build.sh --esd

This reconfigures the build to route syslog output to USART1 and disables RAMLOG. Useful for post-mortem analysis of crashes where the RAMLOG cannot be retrieved via the debugger.

Measuring a thread stack

The NSH ps command reports stack use per thread when stack coloration is on. The default configuration does not enable it, so measure on a developer build:

./build.sh --clean --force --configure --nsh --dualcdc
kconfig-tweak --file nuttx/.config --enable STACK_COLORATION
./build.sh --nsh --dualcdc

Flash it, write the matching Mono runtime (Meadow.OS.Runtime.bin) and reset the board. NSH runs on the second Meadow CDC port (/dev/cu.usbmodem<serial>3). Send ps and read the thread's row:

  TID   PID  PPID PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK            STACK    USED FILLED    CPU COMMAND
   15     9     0 100 RR       pthread   - Waiting  Semaphore 0000000000000000 0003880 0000704  18.1%   0.0% MonoMonitor 0x808bfc9 0x2007fe28

STACK is the usable size in bytes, USED is the peak so far and FILLED is USED as a percentage.

  • Measure while the thread does its real work, not only while it waits. USED is a high-water mark, so a path that has not run yet is not counted.
  • Choose the size from USED plus a margin, and record the measurement in the comment next to the size in F7MeadowOS/include/meadow/meadow_thread_config.h.
  • CONFIG_SYSTEM_MONO is only set when the generated apps/system/Kconfig knows about apps/system/mono. If a build has no Mono task, run the --clean --force configure above.

Tips

  • If GDB fails to connect, ensure the OpenOCD server is running in Terminal 1 first. The script checks port 4242 with nc before launching GDB.
  • For ST-Link clone adapters, reset_config none separate in debug.cfg is required when the RST pin is not wired.
  • The bootloader is at 0x08000000 and NuttX kernel at 0x08040000. Ensure your flash offsets are correct when loading images manually.
  • Use mon reset halt in GDB to reset and halt the target before re-loading symbols after a reflash.