Overview
Curated: · Written: · Reviewed:
Embedded Debugging & Toolchains
In one line
Embedded debugging finds bugs with no stdout, no OS and often no UART, by tracing a chain of evidence from source revision to what the silicon actually did. Reach for it whenever the answer depends on the exact binary, the reset state or the run-control state.
Key definitions
- ELF with symbols: the build output whose symbols and line tables must match the bytes programmed into flash.
- Observation-to-binary fidelity: every claim names the binary, target, reset state, observation mechanism and its perturbation.
- Exception frame: the eight words (r0-r3, r12, LR, PC, xPSR) the hardware stacks on a fault, plus FP context when active.
- FPB and DWT: the breakpoint unit (instruction comparators) and the watchpoint unit (data comparators).
- RDP: flash readout protection. Level 2 is irreversible on many STM32 parts.
- Halt is not a camera: halting the core leaves timers, DMA and other cores running.
Formulas and complexity
The basic exception frame. Lazy FP stacking adds more words.
A 256-byte no-init region holding r0-r3, r12, LR, PC, xPSR, CFSR, HFSR, MMFAR, BFAR, reset cause, SP, eight extra stacked words and a 16-bit CRC is enough to classify most field faults. It survives reset only if the bootloader does not zero it, so exclude it from BSS. A 64-byte breadcrumb (stacked PC, CFSR, HFSR, reset cause and a 32-byte ring) is the fleet-scale alternative.
How it works
- The toolchain turns source into an ELF and a map file. The linker script decides where .text, .data, .bss, the vector table and the no-init region land.
- The programmed image must hash-match the ELF's load segments, or the symbols name the wrong functions.
- On a precise fault the stacked PC is the faulting instruction, and the stacked LR holds the caller's return address.
- Read CFSR (0xE000ED28), HFSR (0xE000ED2C), MMFAR (0xE000ED34) and BFAR (0xE000ED38) before clearing them. Validate the stacked SP before dereferencing it.
- Every observation travels probe, transport, debug server, then debugger UI, and each link can fail silently.
When to use and when not to
| Situation | Attach a debugger? | Use instead |
|---|---|---|
| Logic bug on a reproducible bench unit | Yes | Halt, read, step |
| Timing or race bug | No, the halt changes it | GPIO plus logic analyzer, ITM/SWO timestamps |
| Field fault on one unit, port sealed | Not possible | Fault dump in no-init RAM, breadcrumb |
| Fleet statistics | No | Retained crash records and counters |
| Port fused or pins remuxed | Not possible | Trace, counters, recovery reflash |
Traps interviewers probe
- Trusting a backtrace from the wrong ELF. Fix: hash the programmed load segments against the ELF before trusting any stack.
- Reading the source line as what the CPU did. Fix: optimized code reorders, merges and inlines. Cross-check disassembly, line tables and registers.
- Ignoring what kept running during a halt. Fix: list the peripherals and cores still active, then reproduce with a non-halting instrument.
- Accepting a stack scan hit as the PC. Fix: use the stacked frame. Check that SP lies in the stack region and is 8-byte aligned first.
- Assuming all resets are identical. Fix: POR, NRST and a debugger reset-init script leave different states. Reproduce with a power cycle.
Interview tips
Open with the chain: "I'd first confirm the binary matches the flash, then the reset state, then pick the least perturbing instrument."
Expect these follow-ups:
- Bring-up order: power rails, clock, reset-cause register, is the CPU executing, is the image at the boot address.
- Why a watchpoint on a DMA-written variable shows the wrong value.
- How you know ITM dropped packets, and what you do about it.
- Debugger reset versus power cycle.
- How the debug port is secured across the lifecycle.
30-second answer outline:
- Name the binary and the hash check that ties it to flash (5 s).
- State the reset state and the observation mechanism (10 s).
- Name the least intrusive instrument and what it perturbs (10 s).
- Name the evidence that would falsify the hypothesis (5 s).
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.