Embedded Fault Debug
Overview
Use this skill to preserve crash evidence and find the first failing instruction. The agent should read architecture-specific fault state before reset, map addresses to symbols, and separate root cause from secondary symptoms.
When To Use
Use this skill when:
- Firmware hits HardFault, BusFault, UsageFault, trap, illegal instruction, default handler, watchdog reset, assert, or panic.
- The board resets unexpectedly or hangs after a repeatable action.
- The user has a register dump, fault log, crash PC, stack dump, watchdog event, or GDB backtrace.
Do not use this skill when the target cannot yet connect or flash; use mcu-flashing-debug first.
First Questions
Ask for:
- Architecture/chip family and toolchain.
- Exact symptom and whether it is reproducible.
- Register dump, backtrace, fault/trap registers, assert log, or reset reason.
- ELF/MAP file that matches the running image.
- Whether the target has been reset since the fault.
Workflow
Preserve state.
Do not reset or reflash until fault registers, PC, LR/RA, SP, and stack frame are captured.
Confirm image-symbol match.
Ensure the ELF/MAP file matches the firmware currently running on target.
Identify the faulting address.
Map PC, exception return address, mepc, stacked PC, or return address to a symbol and source line.
Classify the cause.
Separate invalid memory access, illegal instruction, stack overflow, alignment, bus/peripheral access, watchdog, assert, and default handler cases.
Walk backward to the first bad state.
Inspect caller, input pointers, stack bounds, interrupt context, DMA/cache interactions, and recent initialization.
Add a focused verification.
Use a watchpoint, assertion, guard pattern, stack watermark, or minimal repro to prove the suspected cause.
Architecture Notes
Cortex-M
Read SCB fault registers before reset:
CFSR = 0xE000ED28
HFSR = 0xE000ED2C
MMFAR = 0xE000ED34
BFAR = 0xE000ED38
Decode stacked r0-r3, r12, lr, pc, and xpsr from MSP or PSP.
RISC-V
Read:
mcause, mepc, mtval, mtvec, mstatus, sp
Check whether mcause is interrupt or exception, and map mepc to source/disassembly.
8051
Use available evidence:
- Reset reason register if the derivative provides one.
- Stack pointer and internal RAM pressure.
- ISR vector selection and default handler loops.
- Watchdog configuration.
- GPIO heartbeat or serial breadcrumbs around suspected code.
Common Root Causes
- Stack overflow or ISR stack pressure.
- Null/corrupt function pointer.
- Wrong vector table, trap vector, or interrupt number.
- Accessing peripheral before clock/reset release.
- DMA writing outside buffers.
- Cache coherency issue on cache-enabled MCUs.
- Compiler target flags not matching core features.
- Watchdog not serviced during long init.
- Bootloader/app offset mismatch.
Verification
Before claiming a fault is understood:
- State the matched ELF/MAP and running firmware identity if known.
- Report faulting PC/address and mapped symbol/source.
- Report architecture-specific fault registers or explain why unavailable.
- State the suspected root cause and the evidence linking it to the fault.
- State the next minimal verification step, such as watchpoint, stack watermark, or assertion.
Common Failures
- Resetting immediately and losing the only useful fault state.
- Trusting a backtrace when the ELF does not match flashed firmware.
- Fixing the line where the fault occurred instead of the earlier corruption.
- Ignoring interrupt context, stack selection, or DMA/cache side effects.
- Treating watchdog reset as a generic crash without reading reset reason.
Example
User:
程序跑一会儿就进 HardFault。
Agent:
- Asks for chip, ELF, register dump, CFSR/HFSR, stacked PC, and whether reset happened.
- Maps stacked PC to source.
- Checks stack bounds, caller, interrupt context, and invalid memory access evidence.
- Suggests a focused watchpoint or stack watermark before changing code.
1---2name: embedded-fault-debug3description: Use when embedded firmware crashes, traps, faults, resets unexpectedly, jumps to default handlers, corrupts stack, or executes invalid instructions4---56# Embedded Fault Debug78## Overview910Use this skill to preserve crash evidence and find the first failing instruction. The agent should read architecture-specific fault state before reset, map addresses to symbols, and separate root cause from secondary symptoms.1112## When To Use1314Use this skill when:1516- Firmware hits HardFault, BusFault, UsageFault, trap, illegal instruction, default handler, watchdog reset, assert, or panic.17- The board resets unexpectedly or hangs after a repeatable action.18- The user has a register dump, fault log, crash PC, stack dump, watchdog event, or GDB backtrace.1920Do not use this skill when the target cannot yet connect or flash; use `mcu-flashing-debug` first.2122## First Questions2324Ask for:2526- Architecture/chip family and toolchain.27- Exact symptom and whether it is reproducible.28- Register dump, backtrace, fault/trap registers, assert log, or reset reason.29- ELF/MAP file that matches the running image.30- Whether the target has been reset since the fault.3132## Workflow33341. Preserve state.35 Do not reset or reflash until fault registers, PC, LR/RA, SP, and stack frame are captured.36371. Confirm image-symbol match.38 Ensure the ELF/MAP file matches the firmware currently running on target.39401. Identify the faulting address.41 Map PC, exception return address, `mepc`, stacked PC, or return address to a symbol and source line.42431. Classify the cause.44 Separate invalid memory access, illegal instruction, stack overflow, alignment, bus/peripheral access, watchdog, assert, and default handler cases.45461. Walk backward to the first bad state.47 Inspect caller, input pointers, stack bounds, interrupt context, DMA/cache interactions, and recent initialization.48491. Add a focused verification.50 Use a watchpoint, assertion, guard pattern, stack watermark, or minimal repro to prove the suspected cause.5152## Architecture Notes5354### Cortex-M5556Read SCB fault registers before reset:5758```text59CFSR = 0xE000ED2860HFSR = 0xE000ED2C61MMFAR = 0xE000ED3462BFAR = 0xE000ED3863```6465Decode stacked `r0-r3`, `r12`, `lr`, `pc`, and `xpsr` from MSP or PSP.6667### RISC-V6869Read:7071```text72mcause, mepc, mtval, mtvec, mstatus, sp73```7475Check whether `mcause` is interrupt or exception, and map `mepc` to source/disassembly.7677### 80517879Use available evidence:8081- Reset reason register if the derivative provides one.82- Stack pointer and internal RAM pressure.83- ISR vector selection and default handler loops.84- Watchdog configuration.85- GPIO heartbeat or serial breadcrumbs around suspected code.8687## Common Root Causes8889- Stack overflow or ISR stack pressure.90- Null/corrupt function pointer.91- Wrong vector table, trap vector, or interrupt number.92- Accessing peripheral before clock/reset release.93- DMA writing outside buffers.94- Cache coherency issue on cache-enabled MCUs.95- Compiler target flags not matching core features.96- Watchdog not serviced during long init.97- Bootloader/app offset mismatch.9899## Verification100101Before claiming a fault is understood:102103- State the matched ELF/MAP and running firmware identity if known.104- Report faulting PC/address and mapped symbol/source.105- Report architecture-specific fault registers or explain why unavailable.106- State the suspected root cause and the evidence linking it to the fault.107- State the next minimal verification step, such as watchpoint, stack watermark, or assertion.108109## Common Failures110111- Resetting immediately and losing the only useful fault state.112- Trusting a backtrace when the ELF does not match flashed firmware.113- Fixing the line where the fault occurred instead of the earlier corruption.114- Ignoring interrupt context, stack selection, or DMA/cache side effects.115- Treating watchdog reset as a generic crash without reading reset reason.116117## Example118119User:120121```text122程序跑一会儿就进 HardFault。123```124125Agent:1261271. Asks for chip, ELF, register dump, CFSR/HFSR, stacked PC, and whether reset happened.1281. Maps stacked PC to source.1291. Checks stack bounds, caller, interrupt context, and invalid memory access evidence.1301. Suggests a focused watchpoint or stack watermark before changing code.