ENع
←GDB on firmware
Lesson S1 · Special topics

Firmware debugging with GDB: watchpoints, hard faults and ISRs

14 min10 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig S1 · Interactive
01 / 10

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. The 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_Handler
  3. The 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  xPSR
  4. Fault 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 HardFault
  5. An 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 pointer
  6. Set 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 slow
  7. Each 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 = 16
  8. Caught 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 = 17
  9. The 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 world
  10. Fix: 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

Quiz Unlocks after the last step.