(gdb)ST-LINK · SWDSTM32F407CFSRHFSRDWTHardFaultUSART2 RX1struct { char buf[16];2 void (*cb)(void); } rx;3void USART2_IRQHandler(void) {4 rx.buf[n++] = c; // no check if (n < 16) rx.buf[n++] = c;5 if (c == '\n') ready = 1; }6if (ready) rx.cb(); // mainrx.buf[16]rx.cbstack: sp+240800030A00020000400000000800030A&rx.cbss0x0ALR FFFFFFF9
Back: laptop (GDB), ST-LINK probe, and the board with the STM32F407 and a serial senderFront: the receive ISR on a card, and three words of SRAM: rx.buf, rx.cb and the stacked PCTokens: the command line and its bytes, values GDB reads, and the watch addressA debugging session on an STM32F4DISCOVERY: GDB on a laptop, an ST-LINK probe, and the STM32F407 (Cortex-M4) with its fault registers and watch unit. The stacked PC (0x0800030A) and CFSR (0x00020000, INVSTATE) show the CPU jumped to an even address through rx.cb. A hardware watchpoint on rx.cb catches the receive ISR writing the 17th byte past rx.buf[16]; a bounds check fixes it.

Connect

  1. 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.

Read the crash

Catch the writer

What happens

;; step 1/10 · connect
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

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.