>>> ZEPHYR FATAL ERROR: what the number means

Zephyr’s kernel decided it could not go on and called its fatal error handler. The number after the words is the reason code, and the lines printed just above it usually hold the detail.

The line

[00:00:01.204,000] <err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
[00:00:01.204,000] <err> os: Current thread: 0x20000a48 (sensor_thread)
[00:00:01.251,000] <err> os: Halting system

The thread address and name are illustrative; the three line formats are those of kernel/fatal.c.

What it means

The line comes from z_fatal_error(). It prints the reason code and its name, then the thread that was running, then hands over to k_sys_fatal_error_handler(). The default handler logs Halting system and stops the board; it does not reboot. A board that restarts after this line has an application or SDK handler that reboots on purpose.

On an Arm core the register dump and, for reason 0, the fault block (***** BUS FAULT ***** and similar) are printed before this line, so read upwards from it.

Reason codes

Applies to: Zephyr v4.2.0 (enum k_fatal_error_reason); architecture-specific codes start at 16.

CodeText on the lineRaised when
0CPU exceptionThe CPU took a fault the architecture code could not handle, for example a bus fault or an illegal instruction.
1Unhandled interruptAn interrupt fired on a line with no handler installed.
2Stack overflowA stack check failed: a guard region, a stack sentinel or a stack canary was hit.
3Kernel oopsk_oops() was called, or a user mode thread did something it is not allowed to do.
4Kernel panick_panic() was called, or an __ASSERT() failed in supervisor mode.

Source: Zephyr: kernel/fatal.c (v4.2.0), the code that prints the line

Common causes

Reason 0: a CPU fault

The architecture layer caught a fault and passed it on as K_ERR_CPU_EXCEPTION. The cause is in the fault block printed above the line: on Cortex-M that is a ***** ... FAULT ***** banner with its decoded status bits and, often, the faulting address.

Sources: Zephyr docs: Fatal Errors · Zephyr: arch/arm/core/cortex_m/fault.c (v4.2.0)

Reason 2: a thread stack that is too small

The thread on the Current thread line pushed more onto its stack than the buffer holds. In user mode this is always caught. In supervisor mode it is caught only when the build has hardware stack protection (CONFIG_HW_STACK_PROTECTION) or the software stack sentinel (CONFIG_STACK_SENTINEL); otherwise the overflow corrupts memory and shows up later as something else.

Source: Zephyr docs: Fatal Errors

Reason 4 after ASSERTION FAIL: a failed assertion

With CONFIG_ASSERT on, a failed __ASSERT() prints ASSERTION FAIL [expression] @ file:line and then calls assert_post_action(), whose default raises a kernel panic for a supervisor thread and a kernel oops for a user mode thread. The assertion line above the fatal error line is the real message.

Source: Zephyr docs: Fatal Errors

Reason 3: code called k_oops(), or a user thread overstepped

A driver or subsystem met a condition it treats as unrecoverable for the current thread and called k_oops(). A thread running in user mode that calls k_panic() also ends up here, because user threads are not allowed to panic the kernel.

Source: Zephyr docs: Fatal Errors

Reason 1: an interrupt with no handler

An interrupt line was enabled, by the application or left on by a bootloader, and it fired without an ISR connected through IRQ_CONNECT() or irq_connect_dynamic(). On Arm, Zephyr prints Unhandled IRQn: n just before the fatal error line; that number is the interrupt to look up.

Sources: Zephyr docs: Fatal Errors · Zephyr docs: Interrupts

What to check first

  1. Read the code and the text on the line, then the Current thread line under it: together they say what kind of failure it was and which thread it happened in.
  2. Scroll up a few lines. For reason 0 there is a fault block, for reason 4 usually an ASSERTION FAIL line with a file and line number, for reason 1 an Unhandled IRQn line.
  3. Resolve the Faulting instruction address (r15/pc) value against your ELF with addr2line -f -e build/zephyr/zephyr.elf <address> to get the function and source line.
  4. For reason 2, enlarge the stack of the named thread (CONFIG_MAIN_STACK_SIZE, or the size given to K_THREAD_STACK_DEFINE) and rebuild; the thread analyzer reports how much of each stack is used.
  5. If the build has no stack protection and the failures look random, turn on CONFIG_HW_STACK_PROTECTION or CONFIG_STACK_SENTINEL and CONFIG_ASSERT before you chase the symptom.
  6. If the board restarts instead of printing Halting system, a fatal error handler in the application reboots it; the boot banner that follows is covered on the Zephyr reboot page.

In NeuroTerm

On a serial tab with pattern markers on, NeuroTerm recognises this line as “Board crash: Zephyr fatal error”. A coding agent connected through Terminal Use gets that name in its list of tabs and among the markers of the output it reads, with a cursor for reading the lines around it. Setup is in Terminal Use (MCP).