Firmware debugging with GDB: watchpoints, hard faults and ISRs
About this lesson
When firmware crashes there is no error message, only a frozen board. With a debug probe, GDB can halt the chip and read the evidence the CPU leaves behind. Follow one real bug on an STM32F4DISCOVERY (STM32F407VGT6, Cortex-M4): a serial command freezes the board, the stacked PC and the fault status registers say where and why, and a hardware watchpoint catches the interrupt handler that wrote one byte too many. The Hardware lesson “Test and debug” explains what SWD and a debug probe are, and the Embedded lesson “Interrupts in firmware” explains ISRs; here you use them to find a bug.
GDB, a probe and two wires
GDB, the GNU debugger, runs on your laptop. OpenOCD passes its commands to an ST-LINK probe, which talks to the chip's debug port over SWD. Now GDB can halt the Cortex-M4 and read any register or memory.
board STM32F4DISCOVERY (STM32F407VGT6, Cortex-M4) $ openocd -f board/stm32f4discovery.cfg $ arm-none-eabi-gdb app.elf (gdb) target extended-remote :3333 (gdb) monitor reset halt laptop -> USB -> ST-LINK -> SWD (SWDIO, SWCLK) -> chip
Read the lesson step by step
GDB, a probe and two wires
GDB, the GNU debugger, runs on your laptop. OpenOCD passes its commands to an ST-LINK probe, which talks to the chip's debug port over SWD. Now GDB can halt the Cortex-M4 and read any register or memory.
board STM32F4DISCOVERY (STM32F407VGT6, Cortex-M4) $ openocd -f board/stm32f4discovery.cfg $ arm-none-eabi-gdb app.elf (gdb) target extended-remote :3333 (gdb) monitor reset halt laptop -> USB -> ST-LINK -> SWD (SWDIO, SWCLK) -> chipThe board freezes
We send the line "set led blink 50" and the board stops responding. Press Ctrl-C in GDB to halt it: the CPU is stuck in the default fault handler, an endless loop. Something went badly wrong.
sent "set led blink 50\n" (17 bytes) (gdb) continue ^C Program received signal SIGINT pc is in Default_Handler: b Infinite_Loop HardFault_Handler is a weak alias of Default_HandlerThe stack remembers where
On a fault the CPU pushes eight registers onto the stack, including PC, the address of the instruction being run. SP, the stack pointer, points to the stack's top. The pushed PC, at SP + 24, is where it failed: 0x0800030A.
(gdb) x/8xw $sp sp+0 R0 sp+4 R1 sp+8 R2 sp+12 R3 sp+16 R12 sp+20 LR sp+24 PC 0x0800030A <- where it failed sp+28 xPSRFault registers say why
Fault status registers record the cause. CFSR reads 0x00020000: INVSTATE, an invalid state. HFSR reads 0x40000000: FORCED, a usage fault turned into a hard fault because its own handler was off.
(gdb) x/xw 0xE000ED28 CFSR = 0x00020000 bit 17 = UFSR bit 1 = INVSTATE (gdb) x/xw 0xE000ED2C HFSR = 0x40000000 bit 30 = FORCED: escalated to HardFaultAn even address is wrong
A Cortex-M4 only runs Thumb code, so a code pointer's lowest bit must be 1: it must be odd. 0x0800030A is even. main() called rx.cb, so check it: it holds that same broken address.
stacked PC 0x0800030A bit 0 = 0: not Thumb (gdb) p/x rx.cb $1 = 0x800030a expected 0x08000351 on_line(), bit 0 = 1 line 6: if (ready) rx.cb(); jumped through a bad pointerSet a watchpoint
Who broke rx.cb? Reset and type watch rx.cb. GDB loads that address into the chip's watch unit, which checks every write in hardware at full speed. Continue, and send the line again.
(gdb) monitor reset halt (gdb) watch rx.cb Hardware watchpoint 2: rx.cb (gdb) continue hardware: no slowdown; software: single-steps, very slowEach byte, one interrupt
Each byte that arrives raises an interrupt. The ISR stores it at rx.buf[n] and adds 1 to n. After 16 bytes, "set led blink 50", the buffer is exactly full and n is 16.
USART2 byte -> USART2_IRQHandler -> rx.buf[n++] = c n = 0 's' -> rx.buf[0] n = 1 'e' -> rx.buf[1] n = 2 't' -> rx.buf[2] n = 15 '0' -> rx.buf[15] buffer full: n = 16Caught in the act
The 17th byte, the newline 0x0A, goes to rx.buf[16]: past the end, onto the lowest byte of rx.cb. The watch unit fires, and GDB stops just after line 4, inside USART2_IRQHandler, with n = 17.
byte 17 '\n' = 0x0A -> rx.buf[16] (one past the end) Hardware watchpoint 2: rx.cb Old value = (void (*)(void)) 0x8000351 <on_line> New value = (void (*)(void)) 0x800030a USART2_IRQHandler () at main.c n = 17The writer was an ISR
main() never calls USART2_IRQHandler: the hardware does, between two of main's instructions. LR, the link register, normally holds a return address, but p/x $lr shows 0xFFFFFFF9, a special return code. While halted here, bytes keep arriving.
(gdb) p/x $lr $2 = 0xfffffff9 EXC_RETURN: return to thread mode, main stack the ISR interrupts main(); nothing in main() calls it halting in an ISR stops the CPU, not the outside worldFix: check the bounds
Guard line 4 with "if (n < 16) rx.buf[n++] = c;". Now bytes past the end are dropped, and n stops at 16. Run again with the watchpoint set: it never fires, and rx.cb keeps 0x08000351.
line 4 if (n < 16) rx.buf[n++] = c; byte 17 dropped: n stays 16 rx.cb 0x08000351 (unchanged, watchpoint silent) a real driver would also flag the overflow
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A Cortex-M4 takes an exception. Where on the stack is the PC (program counter) it pushed? SP is the stack pointer.
The CPU pushes eight words: R0, R1, R2, R3, R12, LR, PC and xPSR, with R0 at the lowest address. The PC is the seventh word, so it sits at SP + 24. x/8xw $sp shows all eight.
02After a crash, CFSR reads 0x00020000 and HFSR reads 0x40000000. What happened?
CFSR bit 17 is UFSR bit 1, INVSTATE. HFSR bit 30 is FORCED: a configurable fault that could not be handled, here because its handler was not enabled, so it became a hard fault.
03Why is jumping to 0x0800030A a fault on a Cortex-M4?
Branch-with-exchange instructions such as BLX copy bit 0 of the address into the Thumb bit. A Cortex-M4 supports only Thumb state, so an even target gives an INVSTATE fault. The real function, on_line, is at 0x08000351: odd.
04Why use a hardware watchpoint (watch rx.cb) instead of breakpoints on every line that might write rx.cb?
The watchpoint does not need you to guess where the write happens. The comparator in the chip's debug unit stops the CPU on the write itself. Here that was USART2_IRQHandler, which main() never calls.
05GDB stops inside USART2_IRQHandler and p/x $lr shows 0xFFFFFFF9. What does that tell you?
On exception entry the CPU writes an EXC_RETURN value into LR. 0xFFFFFFF9 means: return to thread mode, using the main stack, without floating-point state. Loading it into the PC ends the handler.
Glossary
- GDB
- The GNU debugger. It can halt a program, step it, set breakpoints and watchpoints, and print registers and memory.
- OpenOCD and debug probe
- OpenOCD is a program that serves GDB's requests; a probe such as ST-LINK turns them into signals on the chip's debug port (here SWD).
- Hard fault
- The Cortex-M exception taken when the CPU cannot carry on, either directly or because another fault could not be handled.
- PC, SP and LR
- Three CPU registers. PC, the program counter, holds the address of the instruction being run. SP, the stack pointer, points to the top of the stack. LR, the link register, normally holds the return address. On an exception the CPU pushes eight registers, PC among them, onto the stack.
- CFSR and HFSR
- Fault status registers at 0xE000ED28 and 0xE000ED2C. Their bits record why a fault happened, such as INVSTATE or FORCED.
- Thumb state
- The instruction set a Cortex-M runs. Code addresses carry bit 0 = 1 to say so; an even branch target is invalid.
- Watchpoint
- A debugger stop that triggers when a chosen variable is written (or read). A hardware watchpoint uses comparators in the chip, so it costs no speed.
- ISR and EXC_RETURN
- An interrupt service routine is run by the hardware between instructions of main(). On entry, LR holds an EXC_RETURN code such as 0xFFFFFFF9.
Notes and sources
- The card is an excerpt. The full ISR first reads the byte with char c = USART2->DR; main() calls rx.cb() when ready is set and then resets n. Writing rx.buf[16] is undefined behaviour in C; in practice, with this struct, it lands on the first byte of cb.
- The addresses come from one example build: flash starts at 0x08000000 on the STM32F407, so on_line() at 0x08000351 is plausible, but your linker may place it elsewhere. Thumb function pointers are odd, and the Cortex-M4 stores words little-endian here.
- With the floating-point unit in use the CPU can push a longer frame, but R0 to xPSR still come first, so the PC is still at SP + 24. In the default handler (a plain loop) SP still points at the frame; a handler written in C may push more first.
- The usage fault handler is off after reset, so the fault escalates and HFSR shows FORCED. Enable it (USGFAULTENA in SHCSR) and the same fault goes to UsageFault_Handler instead. CFSR and HFSR bits stay set until written with 1 or a reset.
- A Cortex-M has only a few hardware comparators. The Cortex-M4 FPB has up to 8, but only 6 match code; OpenOCD reports 6 code breakpoints and 4 watchpoints on this chip. EXC_RETURN is 0xFFFFFFE9 instead of 0xFFFFFFF9 when floating-point state was stacked. GDB output is trimmed.
- Arm, Cortex-M4 Devices Generic User Guide (ARM DUI 0553B): exception model, fault handling, CFSR and HFSR
- J. W. Valvano, Embedded Systems: Shape the World, ch. 12 Interrupts (UT Austin)
- GDB manual: Setting Watchpoints
- GDB manual: Examining Memory (x/nfu)
- OpenOCD User's Guide: GDB and OpenOCD
- OpenOCD board configuration for the STM32F4DISCOVERY
- ST STM32CubeF4: STM32F407VGTX_FLASH.ld (flash and RAM addresses)
- ST cmsis-device-f4: startup_stm32f407xx.s (Default_Handler and weak aliases)
Quiz Unlocks after the last step.