cortex-m-fault-decoder: From a HardFault Dump to the Cause, and How to Fork It

2026-12-03 · Davide Carrese
Cortex-M · HardFault · Firmware Debugging · Open Source · STM32

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:

RegisterAddressWhat it holds
CFSR0xE000ED28MemManage (MMFSR), BusFault (BFSR) and UsageFault (UFSR) status bits
HFSR0xE000ED2CHardFault status: FORCED (escalated), VECTTBL, DEBUGEVT
MMFAR0xE000ED34faulting address for MemManage, valid only if MMARVALID
BFAR0xE000ED38faulting 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:

PathRole
src/decoder.jsparser and decoder, pure functions shared by the page and the CLI
index.html, app.js, style.cssthe web page, served by GitHub Pages straight from the repository
cli/fault-decoder.mjscommand line, exit code 1 when a fault bit is set
firmware/fault_dump.cdrop-in fault handler that prints the values in the right format
gdb/fault.gdba GDB fault command that prints the same values from a debugger
test/decoder.test.mjstest 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:

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:

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 triggeredCFSRBFARDecoder verdict
undefined instruction (.short 0xDE00)0x00010000-Undefined instruction (UNDEFINSTR)
call to 0x08000000 without the Thumb bit0x00020000-Branch with bit 0 clear (INVSTATE)
read from 0xC00000000x000082000xC0000000Data access to an invalid address (PRECISERR)
integer division by zero, DIV_0_TRP set0x02000000-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

  1. Fork carresed/cortex-m-fault-decoder on GitHub and clone your fork.
  2. Run npm test: it needs only Node, and the same tests run in GitHub Actions on every push of your fork.
  3. For your own copy of the web page: Settings, Pages, Deploy from a branch, branch master, folder /. There is no build step: the page loads src/decoder.js directly 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:

  1. Write the dump that shows it as a test in test/decoder.test.mjs, asserting the cause you expect. It fails.
  2. Add the rule next to the related ones, with checks phrased as things to look at, not as certainties.
  3. Run npm test, open index.html locally 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

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

💬 Comments by email

Reply to this article by email — I read and respond to every message.