Fault Injection & Debugging

Cortex-M HardFault triage in CI and reverse GDB

Stop chasing intermittent HardFaults over SWD: reproduce the crash deterministically on a board twin and step backward out of HardFault_Handler.

A HardFault handler that prints CFSR/HFSR/BFAR and halts gives you the crime scene, not the sequence that corrupted the stack or pointer 400 instructions earlier. On physical silicon, reproducing a timing-sensitive crash under a probe can burn days.

Under CircuitTwin, every run with the same inputs and seed executes identically. When CI catches a HardFault, launch ct debug locally with the exact suite inputs, break on HardFault_Handler and execute reverse-stepi or reverse-continue to a watchpoint.

Fastest CFSR triage table

CFSR hexFlagFirst check in GDB
0x00008200PRECISERR + BFARVALIDRead BFAR (0xE000ED38): unclocked peripheral or bad MMIO pointer
0x00000400IMPRECISERRSet ACTLR.DISDEFWBUF=1 to force precise BusFault on store
0x00010000UNDEFINSTRDisassemble stacked PC: erased flash, bad branch, or wrong -mcpu
0x00020000INVSTATEStacked PC bit 0 is 0 (ARM mode jump on Thumb-only Cortex-M)
0x00080000NOCPFPU instruction executed before SCB->CPACR CP10/CP11 enabled
0x02000000DIVBYZEROSDIV/UDIV zero divisor with CCR.DIV_0_TRP=1

Local CLI workflow

Shell — reproduce & reverse-debug HardFault
ct debug --board nucleo-f767zi --gdb-port 3333 build/app.elf

# In arm-none-eabi-gdb:
# (gdb) target remote :3333
# (gdb) b HardFault_Handler
# (gdb) continue
# (gdb) x/6wx 0xE000ED28
# (gdb) reverse-stepi 20

Suite & fault configuration

tests/hardfault-gate.suite.yaml
version: 1
board: nucleo-f767zi
firmware: build/app.elf
tests:
  - name: stress-without-hardfault
    timeout_ms: 4000
    expect:
      console_not_contains: "HARDFAULT"
      exit_code: 0

CI pipeline integration

.github/workflows/hardfault-gate.yml
name: HardFault Regression Gate
on: [push, pull_request]
jobs:
  fault-gate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: cmake --build build
      - uses: circuittwin/action@v1
        with:
          suite: tests/hardfault-gate.suite.yaml

Catching Cortex-M stack overflows, MemManage and STKOF faults

Stop chasing corrupted globals days after a stack overflow. Trap the exact overflowing push instruction in CI.

Traditional stack-canary checks only notice an overflow at the next context switch, long after the stack overwrote neighbor structs. Deep nested interrupts make worst-case stack depth hard to trigger by hand.

CircuitTwin models Cortex-M MPU guard regions and ARMv8-M MSPLIM/PSPLIM hardware stack limits, then lets you attach ct debug with GDB reverse-step right back to the offending function call.

Stack protection mechanisms

ArchitectureHardware guardFault bit set
ARMv8-M (M33)MSPLIM / PSPLIM registersUFSR.STKOF (CFSR bit 20)
ARMv7-M (M3/M4/M7)32-byte MPU no-access guard pageMMFSR.DACCVIOL (CFSR bit 1)
All Cortex-MReverse-step in ct debugExact call frame & SP before corruption
Shell - inspect stack overflow in GDB reverse mode
ct debug --board nucleo-l552ze --gdb-port 3333 build/app.elf
# In arm-none-eabi-gdb:
# (gdb) target remote :3333
# (gdb) break UsageFault_Handler
# (gdb) continue
# (gdb) reverse-stepi
tests/stack-guard.suite.yaml
version: 1
board: nucleo-l552ze
firmware: build/app.elf
tests:
  - name: stress-nested-isr-zero-stkof
    timeout_ms: 2500
    expect:
      console_contains: "STRESS_PASS"
      exit_code: 0

Troubleshooting checklist

SymptomFix
IMPRECISERR hides the exact store instructionTemporarily set ACTLR bit 1 (DISDEFWBUF) or use reverse-continue on a data watchpoint in CircuitTwin GDB.
Stack trace shows corrupt framesCheck EXC_RETURN bit 2 in LR (0=MSP, 1=PSP) and inspect the 8-word hardware frame at that SP.
HardFault only on Cortex-M33Check UFSR bit 20 (STKOF) for MSPLIM/PSPLIM hardware stack-limit overflow.
Need instant bit decode from CI logPaste CFSR/HFSR/MMFAR/BFAR directly into the Cortex-M HardFault Decoder.
CFSR reads 0x00100000 on Cortex-M33Bit 20 is STKOF (Stack Overflow); increase task stack size or trim large local stack arrays.
HardFault occurs inside printf()Newlib printf uses 1-2 KB of stack; switch to nano specs (--specs=nano.specs) or lightweight snprintf.

Frequently asked questions

QuestionAnswer
How do I decode a CFSR hex dump from a stack overflow crash?Paste the CFSR, HFSR, MMFAR and BFAR hex words into our free Cortex-M HardFault Decoder to inspect every bit flag and GDB inspection command.

Related boards & component twins

More in Fault Injection & Debugging