cortex-m-fault-decoder: From a HardFault Dump to the Cause, and How to Fork It
Your Cortex-M stops in HardFault_Handler. You have the fault status registers and eight words of stack in front of you, and the next half hour goes to the Architecture Reference Manual, decoding bit by bit. cortex-m-fault-decoder does that half hour for you: paste the registers, get the fault bits in plain words, what the addresses point to, where the code was running and the usual reasons behind that exact combination. This article explains how it works, how I tested it on a real STM32F401, and how to run it, fork it and teach it new tricks.
What problem it solves
On ARMv7-M (Cortex-M3, M4, M7) and ARMv8-M Mainline (M33, M55...) a fault leaves its evidence in a handful of registers in the System Control Block:
| Register | Address | What it holds |
|---|---|---|
CFSR | 0xE000ED28 | MemManage (MMFSR), BusFault (BFSR) and UsageFault (UFSR) status bits |
HFSR | 0xE000ED2C | HardFault status: FORCED (escalated), VECTTBL, DEBUGEVT |
MMFAR | 0xE000ED34 | faulting address for MemManage, valid only if MMARVALID |
BFAR | 0xE000ED38 | faulting address for BusFault, valid only if BFARVALID |
Add LR on handler entry (the EXC_RETURN value, which says which stack the frame went to) and the eight words the core pushed (R0-R3, R12, LR, PC, xPSR), and you have everything needed to explain most faults. The information is all there; the problem is that it is packed into bit fields, some fields are only meaningful when another bit is set, and the same bits mean different things depending on the combination. PRECISERR with BFAR at 0x00000010 is a NULL struct pointer; IMPRECISERR means the stacked PC is not the culprit; FORCED alone means the real cause is in CFSR.
The decoder knows these rules. It runs in the browser at carresed.github.io/cortex-m-fault-decoder, entirely locally (nothing is uploaded), or from the command line with Node.js.
How it works
The project is small on purpose, about 600 lines with no dependencies:
| Path | Role |
|---|---|
src/decoder.js | parser and decoder, pure functions shared by the page and the CLI |
index.html, app.js, style.css | the web page, served by GitHub Pages straight from the repository |
cli/fault-decoder.mjs | command line, exit code 1 when a fault bit is set |
firmware/fault_dump.c | drop-in fault handler that prints the values in the right format |
gdb/fault.gdb | a GDB fault command that prints the same values from a debugger |
test/decoder.test.mjs | test cases, run with node --test in CI |
Decoding happens in three steps.
1. Parse whatever you paste
The parser accepts NAME=value, NAME: value, GDB x/8wx output and OpenOCD mdw output, mixed in any order. An LR that starts with 0xFF is taken as EXC_RETURN, any other LR as the stacked one. The reason is practical: people paste what their tool printed, and asking them to reformat it is where mistakes creep in.
2. Decode every bit, then look at combinations
Each register has a table of bits with a one-line explanation. After that, a set of rules looks at the combination: which faults are set, whether the fault addresses are valid, which memory region they fall in (code, SRAM, peripheral, private peripheral bus, "near zero"), whether PC or LR hold a fill pattern such as 0xA5A5A5A5 (the FreeRTOS stack fill) or erased flash. Each rule produces a likely cause with a list of checks. A few examples of what it knows:
PRECISERRwithBFARnear 0: a member access through a NULL pointer, and the offset tells you which member.IMPRECISERR: the stacked PC is a few instructions after the culprit; on M3/M4ACTLR.DISDEFWBUFmakes the error precise while you hunt it.NOCP: an FPU instruction before CP10/CP11 were enabled inCPACR, typically a custom startup or a bootloader that skipsSystemInit.MSTKERR/STKERR/STKOF: stack overflow, and a warning that the stacked frame is not trustworthy.- All flags zero: you are probably not in a fault at all but in
Default_Handler, an interrupt whose handler name is misspelled or C++-mangled.
3. Suggest the next command
The output ends with the commands to run next: arm-none-eabi-addr2line for the faulting PC and for the caller (the stacked LR with the Thumb bit removed), and an objdump range around the instruction. That last step matters: the decoder tells you what happened, the ELF file tells you where in your source.
Getting the values out of the target
Three ways, depending on what you have:
- From GDB (OpenOCD, ST-LINK GDB server, J-Link, pyOCD):
source gdb/fault.gdb,break HardFault_Handler,continue, thenfaultas soon as it stops. It must run on the first instruction of the handler: later, LR no longer holdsEXC_RETURN. - From the firmware, without a debugger: add
firmware/fault_dump.cto the build and remove yourHardFault_Handler. It picks MSP or PSP fromEXC_RETURN(so faults inside RTOS tasks give the right frame) and prints throughFAULT_PRINTF, which you should point at a polled UART: inside a fault, interrupts and DMA cannot be trusted. - By hand, reading the addresses in the table above with any probe.
Tested on real silicon
Unit tests prove the rules do what I wrote; they do not prove the rules match what a chip does. So the decoder was checked against a WeAct Black Pill (STM32F401CC, Cortex-M4) with four faults triggered on purpose, read with gdb/fault.gdb through an ST-LINK V2:
| Fault triggered | CFSR | BFAR | Decoder verdict |
|---|---|---|---|
undefined instruction (.short 0xDE00) | 0x00010000 | - | Undefined instruction (UNDEFINSTR) |
| call to 0x08000000 without the Thumb bit | 0x00020000 | - | Branch with bit 0 clear (INVSTATE) |
| read from 0xC0000000 | 0x00008200 | 0xC0000000 | Data access to an invalid address (PRECISERR) |
integer division by zero, DIV_0_TRP set | 0x02000000 | - | Integer division by zero (DIVBYZERO) |
All four had HFSR=0x40000000 (FORCED, because the configurable fault handlers were disabled) and EXC_RETURN=0xFFFFFFF9. Two details only showed up on hardware. First, when MMARVALID/BFARVALID are clear, the address registers hold leftovers (0xE000EDF8 on this chip), so the decoder must say "not valid, ignore it" rather than interpret them. Second, on the Cortex-M4 MMFAR and BFAR share storage, so after a bus fault MMFAR shows the same address even though MMARVALID is clear. Both are handled.
The test also found a cosmetic bug: for the INVSTATE case, with PC exactly at the start of flash, the suggested objdump range started at 0x07FFFFE0, below the memory. It now never starts below the base of the region, and that real dump became a regression test. Later, the MPU example of stm32f4-bare-metal produced two more real dumps: a stack overflow into an MPU guard (CFSR=0x00000092, decoded as "stack overflow during exception entry") and a NULL struct access (MMFAR=0x00000004, "a NULL pointer plus offset 0x4").
Run it, fork it, extend it
Use it
The web page needs nothing. For the CLI you need Node.js 18 or newer, no packages:
git clone https://github.com/carresed/cortex-m-fault-decoder
cd cortex-m-fault-decoder
node cli/fault-decoder.mjs CFSR=0x00008200 HFSR=0x40000000 BFAR=0x00000010
node cli/fault-decoder.mjs < uart-log.txt # output of fault_dump.c
node cli/fault-decoder.mjs --json < dump.txt # for scripts
npm test
Because the exit code is 1 when a fault bit is set, the CLI can gate a hardware-in-the-loop test: capture the UART of the board under test, pipe it into the decoder, fail the job on a fault and keep the decoded report as an artifact.
Fork it
- Fork
carresed/cortex-m-fault-decoderon GitHub and clone your fork. - Run
npm test: it needs only Node, and the same tests run in GitHub Actions on every push of your fork. - For your own copy of the web page: Settings, Pages, Deploy from a branch, branch
master, folder/. There is no build step: the page loadssrc/decoder.jsdirectly as an ES module.
Teach it a new pattern
The rules are plain code in decode() inside src/decoder.js. A rule calls cause(title, why, checks) when its condition holds; has("NAME") tells whether a bit is set, and the parsed frame and addresses are available as values. The NOCP rule, for instance, is a single if (has("NOCP")) cause(...) whose checks list where the FPU should have been enabled. To add a pattern you have seen in the field:
- Write the dump that shows it as a test in
test/decoder.test.mjs, asserting the cause you expect. It fails. - Add the rule next to the related ones, with checks phrased as things to look at, not as certainties.
- Run
npm test, openindex.htmllocally to see it on the page, and open a pull request with the dump.
The quickest contribution needs no code at all: if the decoder explains a real dump badly, open an issue with the dump. That is how the list of patterns grows.
Limits
- Cortex-M0/M0+/M23 have no
CFSR: only the stacked-frame part applies there. - The decoder reads registers; it does not read your ELF. It points at the instruction,
addr2lineturns that into a line of source. - With
STKERR/MSTKERRthe frame was never written, and withIMPRECISERRthe PC is late. The decoder says so, but no tool can recover information the core did not save.
How I use it on a client project
Two things go into the firmware early, long before the first field failure: a fault handler like fault_dump.c that saves the registers (to a UART, or to RAM that survives the reset and is printed on the next boot), and the MemManage, BusFault and UsageFault handlers enabled in SCB->SHCSR, so that a fault arrives already classified instead of as a generic HardFault. Then a crash report from a tester or from the field is a paste away from a first diagnosis, and the conversation starts from "BFAR says NULL plus 0x10" instead of "it froze".
Sources
- Arm, ARMv7-M Architecture Reference Manual (DDI 0403), B3.2.15-B3.2.18: CFSR, HFSR, MMFAR, BFAR; B1.5.6-B1.5.8: exception entry, stack frame and
EXC_RETURN. - Arm, Armv8-M Architecture Reference Manual (DDI 0553):
STKOF,SFSR/SFAR, additional state context. - ST, PM0214 STM32 Cortex-M4 MCUs and MPUs programming manual: System Control Block registers and fault handling.
- Arm, Cortex-M4 Technical Reference Manual:
ACTLR.DISDEFWBUF. - Repository: github.com/carresed/cortex-m-fault-decoder (MIT). Hardware test rig: github.com/carresed/stm32f4-bare-metal.
- Related on this blog: STM32 HardFault debugging at register level, STM32 MPU configuration.
Reply to this article by email — I read and respond to every message.