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 hex | Flag | First check in GDB |
|---|---|---|
0x00008200 | PRECISERR + BFARVALID | Read BFAR (0xE000ED38): unclocked peripheral or bad MMIO pointer |
0x00000400 | IMPRECISERR | Set ACTLR.DISDEFWBUF=1 to force precise BusFault on store |
0x00010000 | UNDEFINSTR | Disassemble stacked PC: erased flash, bad branch, or wrong -mcpu |
0x00020000 | INVSTATE | Stacked PC bit 0 is 0 (ARM mode jump on Thumb-only Cortex-M) |
0x00080000 | NOCP | FPU instruction executed before SCB->CPACR CP10/CP11 enabled |
0x02000000 | DIVBYZERO | SDIV/UDIV zero divisor with CCR.DIV_0_TRP=1 |
Local CLI workflow
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 20Suite & fault configuration
version: 1
board: nucleo-f767zi
firmware: build/app.elf
tests:
- name: stress-without-hardfault
timeout_ms: 4000
expect:
console_not_contains: "HARDFAULT"
exit_code: 0CI pipeline integration
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.yamlCatching 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
| Architecture | Hardware guard | Fault bit set |
|---|---|---|
ARMv8-M (M33) | MSPLIM / PSPLIM registers | UFSR.STKOF (CFSR bit 20) |
ARMv7-M (M3/M4/M7) | 32-byte MPU no-access guard page | MMFSR.DACCVIOL (CFSR bit 1) |
All Cortex-M | Reverse-step in ct debug | Exact call frame & SP before corruption |
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-stepiversion: 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: 0Troubleshooting checklist
| Symptom | Fix |
|---|---|
| IMPRECISERR hides the exact store instruction | Temporarily set ACTLR bit 1 (DISDEFWBUF) or use reverse-continue on a data watchpoint in CircuitTwin GDB. |
| Stack trace shows corrupt frames | Check EXC_RETURN bit 2 in LR (0=MSP, 1=PSP) and inspect the 8-word hardware frame at that SP. |
| HardFault only on Cortex-M33 | Check UFSR bit 20 (STKOF) for MSPLIM/PSPLIM hardware stack-limit overflow. |
| Need instant bit decode from CI log | Paste CFSR/HFSR/MMFAR/BFAR directly into the Cortex-M HardFault Decoder. |
| CFSR reads 0x00100000 on Cortex-M33 | Bit 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
| Question | Answer |
|---|---|
| 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. |