πDebugging, Testing & Tools
Interviewers often finish with "tell me about the hardest bug you fixed". These questions give you the vocabulary and the toolbox to answer well. Each answer gives the short version first, then the details and follow-ups.
01How do you debug firmware that crashes only occasionally?Mediumβ
Short answer: Make it observable, then make it reproducible, then narrow it down. Capture as much state as possible when it happens (fault handler that saves registers, the stacked PC and LR, and fault status registers to a no-init RAM area or flash; reset-cause registers; a trace buffer of recent events), increase the failure rate with stress (temperature, voltage, load, timing changes), and form hypotheses around the usual suspects: race conditions, stack overflow, memory corruption, uninitialised variables, interrupt priority mistakes, timing-dependent hardware issues and power problems. Then confirm each one with targeted experiments.
Step by step
- Collect data from every occurrence: reset reason (watchdog, brown-out, software, pin), crash dump (fault registers, PC/LR, stack contents, current task), firmware version, uptime, and what the system was doing (an event log in RAM that survives reset).
- Look for patterns: only on some boards (hardware variation), at certain temperatures, after a long uptime (overflow of counters, leaks, fragmentation), only with a certain peripheral active (interrupt interaction), or only in release builds (optimisation exposing undefined behaviour or missing
volatile). - Increase the failure rate: stress tests, faster timers, injected interrupts, random delays, heat gun/freezer spray, supply voltage at the limits.
- Add instrumentation that does not change timing much: GPIO toggles, a RAM trace buffer, ITM/SWO or RTT, data watchpoints on the corrupted variable.
- Check the classics:
- stack high-water marks and overflow detection (MPU guard, canaries),
-
volatileon hardware registers and ISR-shared flags, - critical sections around read-modify-write of shared data, - interrupt priorities vs the RTOS's syscall priority, - buffer overruns and array indices from external data, - DMA buffers and cache coherence (Cortex-M7), - power supply dips (watch the rail and reset line on a scope with a trigger), - errata for the MCU and peripherals. - Bisect in time (
git bisectbetween a good and a bad version) or in code (disable features until it stops). - Fix the root cause and keep the instrumentation that helped (crash logs, assertions) for the field.
Interview story structure: symptom β impact β data you collected β hypotheses you ruled out β root cause β fix β what you changed so it cannot happen again (test, assertion, process).
02How do you analyse a HardFault on a Cortex-M?Hardβ
Short answer: On a fault, the CPU stacks R0βR3, R12, LR, PC and xPSR on the active stack. A HardFault handler written in assembly picks the correct stack pointer (MSP or PSP, from bit 2 of the EXC_RETURN value in LR) and passes it to a C function that reads the stacked PC (the faulting instruction, or the one after it for imprecise faults) and LR (the caller), together with the fault status registers: CFSR (UsageFault/BusFault/MemManage bits), HFSR (FORCED = an escalated configurable fault), MMFAR and BFAR (faulting data address when the valid bits are set). Then look up the PC in the map file or with addr2line.
#include <stdint.h>
typedef struct { uint32_t r0, r1, r2, r3, r12, lr, pc, xpsr; } stack_frame_t;
/* kept in a .noinit section so it survives a reset; inspect after reboot */
__attribute__((section(".noinit"))) volatile struct {
uint32_t magic, pc, lr, cfsr, hfsr, mmfar, bfar, sp;
} crash;
__attribute__((naked)) void HardFault_Handler(void)
{
__asm volatile(
"tst lr, #4 \n" /* EXC_RETURN bit 2: 0 = MSP, 1 = PSP */
"ite eq \n"
"mrseq r0, msp \n"
"mrsne r0, psp \n"
"b hardfault_c \n");
}
void hardfault_c(stack_frame_t *f)
{
crash.magic = 0xDEADFA17;
crash.pc = f->pc;
crash.lr = f->lr;
crash.sp = (uint32_t)f;
crash.cfsr = *(volatile uint32_t *)0xE000ED28; /* SCB->CFSR */
crash.hfsr = *(volatile uint32_t *)0xE000ED2C; /* SCB->HFSR */
crash.mmfar = *(volatile uint32_t *)0xE000ED34; /* SCB->MMFAR */
crash.bfar = *(volatile uint32_t *)0xE000ED38; /* SCB->BFAR */
for (;;) { } /* or: __BKPT(0) when debugging; NVIC_SystemReset() in production */
}
arm-none-eabi-addr2line -e app.elf -f -i 0x08001a3c # function and source line of the stacked PC
Decoding CFSR (common bits)
| Bit (CFSR) | Meaning | Typical cause |
|---|---|---|
| IACCVIOL / DACCVIOL (MemManage) | MPU violation on instruction/data access | stack overflow into a guard region, NULL page protection |
| PRECISERR + BFARVALID (BusFault) | bus error at the address in BFAR | access to a non-existent address, peripheral clock not enabled |
| IMPRECISERR (BusFault) | bus error on a buffered write; PC is after the real fault | write to an invalid address; set DISDEFWBUF (ACTLR) while debugging to make it precise |
| STKERR / UNSTKERR | fault while stacking/unstacking | stack overflow, corrupted SP |
| UNDEFINSTR (UsageFault) | undefined instruction | executing data, corrupted function pointer, wrong instruction set |
| INVSTATE | Thumb bit not set | function pointer without the low bit set, jumping to an ARM-state address |
| INVPC | invalid EXC_RETURN | corrupted LR in an exception handler |
| UNALIGNED / DIVBYZERO | only if trapping enabled (CCR) |
unaligned access, integer divide by zero |
Tips
- Enable the separate MemManage, BusFault and UsageFault handlers (
SCB->SHCSR) for more precise reporting. - A PC of
0xFFFFFFFxor similar garbage usually means the stack was corrupted and a return address was overwritten. - If the stacked PC is in the middle of a function that "cannot" fault, check the LR and the stack contents for what called it, and suspect memory corruption.
- Debuggers and IDEs (STM32CubeIDE fault analyser, Ozone, SEGGER's HardFault handler, Memfault) automate this decoding.
03What is the difference between JTAG and SWD, and what can a debug probe do?Easyβ
Short answer: JTAG (IEEE 1149.1) is a 4/5-wire debug and boundary-scan interface (TCK, TMS, TDI, TDO, optional TRST) that can daisy-chain several devices. SWD (Serial Wire Debug) is Arm's 2-wire alternative (SWCLK, SWDIO) that gives the same debug access to Cortex cores with fewer pins; it is the default on Cortex-M. Both connect a debug probe (J-Link, ST-LINK, CMSIS-DAP/DAPLink, Lauterbach) to the chip's debug access port, which lets the host flash memory, halt and step the CPU, read and write memory and registers while running, set hardware breakpoints and watchpoints, and receive trace.
| JTAG | SWD | |
|---|---|---|
| Pins | TCK, TMS, TDI, TDO (+ nTRST) | SWCLK, SWDIO (+ optional SWO for trace) |
| Daisy chain | yes (several chips on one chain) | no (point to point; SWD multi-drop exists in v2) |
| Boundary scan (testing board connections) | yes | no |
| Cores | any JTAG-capable chip (Arm, RISC-V, FPGAs, DSPs) | Arm Cortex |
Debug connector advice: always route SWD (plus nRESET and the SWO pin) to a standard 10-pin Cortex debug header or Tag-Connect footprint, plus a UART console. Many teams also keep the debug port usable in production for failure analysis, protected with readout protection and authenticated debug.
Common connection problems: firmware reconfigures the SWD pins as GPIOs or enters a low-power mode that disables the debug clock (connect under reset, enable DBGMCU low-power debug bits during development); readout protection blocks access (a mass erase may be the only way back); wrong target voltage reference on the probe.
Security: lock debug access in production (RDP levels on STM32, APPROTECT on nRF, debug authentication on Armv8-M) because an open debug port gives full access to memory and keys.
04What is the difference between a breakpoint and a watchpoint?Easyβ
Short answer: A breakpoint stops execution when the CPU reaches a particular instruction address. A watchpoint (data breakpoint) stops execution when a particular data address is read or written (or both), possibly only when the value matches. Watchpoints are the fastest way to find out who corrupts a variable. In flash-based MCUs, breakpoints use a limited number of hardware comparators (Cortex-M3/M4: 6 instruction comparators in the FPB, 4 data watchpoints in the DWT; Cortex-M0: 4 and 2).
| Breakpoint | Watchpoint | |
|---|---|---|
| Triggers on | executing an instruction address | reading/writing a data address |
| Hardware unit (Cortex-M) | FPB (Flash Patch and Breakpoint) | DWT (Data Watchpoint and Trace) |
| Typical count | 2β8 | 1β4 |
| GDB command | break file.c:42, hbreak |
watch var, rwatch, awatch |
| Software version | BKPT instruction (RAM code only, or flash patching by the probe) |
single-stepping and comparing (extremely slow) |
Useful variants
- Conditional breakpoints:
break foo if len > 512(the debugger halts every time and checks, which can be slow; prefer a code-levelif (...) __BKPT(0);in hot paths). - Watchpoint with a value: some DWT implementations can match on data value, e.g. stop when a variable becomes 0.
- Tracepoints / logpoints: record or print without stopping (GDB
dprintf, IDE logpoints). __BKPT(0)in code: an explicit breakpoint (e.g. in an assertion handler); without a debugger attached it causes a HardFault, so guard it with a "debugger attached" check (CoreDebug->DHCSR & C_DEBUGEN).
Watchpoint example: a global config.mode is randomly overwritten. Set watch config.mode (or watch *(uint32_t *)0x20001234), run, and the CPU halts on the instruction that writes it; bt shows the culprit, often a buffer overrun in a neighbouring array.
05What are the essential GDB commands for embedded debugging?Easyβ
Short answer: Connect to the probe's GDB server with target extended-remote :3333 (OpenOCD) or :2331 (J-Link), then load to flash the ELF, monitor reset halt to reset, break/hbreak and watch to stop, continue/next/step/finish to run, bt for the call stack, info registers, print/p/x and x/16wx addr to inspect data and memory, frame N to switch stack frames, and set var x = 5 to change values.
$ arm-none-eabi-gdb build/app.elf
(gdb) target extended-remote :3333 # OpenOCD GDB server
(gdb) monitor reset halt
(gdb) load # program flash from the ELF
(gdb) hbreak main
(gdb) continue
(gdb) next / step / finish # over, into, out of a function
(gdb) bt # backtrace
(gdb) frame 2 # select a caller's frame
(gdb) info locals
(gdb) p/x *((GPIO_TypeDef *)0x40020000) # peripheral registers as a struct
(gdb) x/8wx 0x20000000 # 8 words of RAM in hex
(gdb) watch rx_count # stop when written
(gdb) info registers # r0-r15, xPSR
(gdb) p $pc ; p/x $sp
(gdb) set var debug_level = 3
(gdb) display/x status # show after every stop
(gdb) monitor reset # probe-specific commands
Productivity tips
- Build with
-g3(macros available in GDB) and keep the exact ELF for every release so you can debug field crash dumps. -Ogor-O1makes stepping more predictable than-O2; "value optimized out" means the variable lives only in registers at that point.- A
.gdbinitfile automates connection and common commands; GDB's Python API can pretty-print RTOS structures. - Use RTOS-aware debugging (OpenOCD
-rtos auto, J-Link RTOS plugins) to list tasks and their stacks. - Post-mortem: load a RAM dump into GDB, or analyse a core dump with tools like Memfault or
CrashDebug. arm-none-eabi-addr2line -e app.elf 0x0800abcdconverts a crash PC to a source line without a debugger.
06Why can printf debugging change program behaviour?Mediumβ
Short answer: printf is slow (formatting plus a blocking UART at 115200 baud takes about 87 Β΅s per character, so a 40-character line costs ~3.5 ms), uses a lot of stack (often 0.5β1 KB or more with floating-point formatting), may allocate memory or take locks, and is usually not safe to call from interrupts. Adding it changes timing, stack usage and memory layout, so race conditions and timing bugs may disappear or new ones may appear (a "Heisenbug").
Better ways to get information out
| Technique | Cost | Notes |
|---|---|---|
| GPIO toggle + scope/logic analyser | ~10 ns | best for timing, ISR entry/exit, latency |
| RAM trace buffer (event ID + timestamp + value) | a few cycles | dump later with the debugger or over a link |
| SEGGER RTT | ~1 Β΅s per message, no blocking | writes to a RAM ring buffer read by the J-Link while running |
ITM/SWO (ITM_SendChar) |
fast, single-wire | requires an SWO pin and probe support |
| Deferred logging (log IDs + arguments, formatted on the host) | very low | e.g. Trice, defmt (Rust), Zephyr dictionary logging |
| Asynchronous log task with a queue | moderate | formatting happens later, at low priority |
If you must use printf
- Use a non-blocking, buffered implementation with DMA, and never call it from ISRs.
- Retarget
_write(newlib) to your UART or RTT; use newlib-nano and avoid%f(or enable-u _printf_floatonly if needed). - Remember that disabling printf in release builds also changes timing β test the release configuration.
07What are ITM, SWO, ETM and RTT, and when do you use trace?Mediumβ
Short answer: They are ways to get data out of a running Cortex-M without stopping it. ITM (Instrumentation Trace Macrocell) sends software messages (printf-style output, event IDs) and hardware events (DWT data trace, PC sampling, exception entry/exit) over the single-wire SWO pin. ETM (Embedded Trace Macrocell) streams every executed instruction through a parallel trace port (or into an on-chip buffer like the ETB/MTB), giving a full execution history for timing analysis and code coverage. RTT (SEGGER Real-Time Transfer) uses ring buffers in RAM that the debug probe reads in the background, so it works on any core with just SWD.
| ITM/SWO | ETM (+ trace port) | MTB (Cortex-M0+) | RTT | |
|---|---|---|---|---|
| Data | software messages, PC samples, exception trace, data watch trace | full instruction trace | recent branches in RAM | software messages, bidirectional |
| Pins | 1 (SWO) | 4β5 (TRACECLK, TRACEDATA[3:0]) | none | none (SWD) |
| Probe | most probes | trace probes (J-Trace, Lauterbach, ULINKpro) | any | J-Link (and some others) |
| Use | logging, profiling, ISR timing | "what happened before the crash", coverage, WCET | post-mortem branch history | fast logging, shell |
Uses of trace
- Finding the code path just before a HardFault (ETM/MTB history).
- Statistical profiling: PC sampling every N cycles shows where CPU time goes.
- RTOS event trace: Percepio Tracealyzer and SEGGER SystemView record context switches, ISRs and API calls (via RTT or a RAM buffer) and display timelines.
- Code coverage on target with ETM, without instrumentation.
DWT cycle counter: DWT->CYCCNT (enable with CoreDebug->DEMCR |= TRCENA; DWT->CTRL |= 1) gives cycle-accurate timing of code sections, available on Cortex-M3 and above.
08How do you find memory corruption and stack overflows in firmware?Mediumβ
Short answer: Detect them as close to the cause as possible: stack overflows with MPU guard regions below each stack (immediate MemManage fault), the Armv8-M stack limit registers (MSPLIM/PSPLIM), RTOS stack checking (canary patterns checked at context switches), and high-water marks from stack painting; memory corruption with data watchpoints on the corrupted variable, canary values around buffers and heap blocks, MPU regions that make code and constant data read-only and catch NULL pointer access, and host-side tools (AddressSanitizer, Valgrind) by running the same code in unit tests on a PC.
Stack painting and high-water mark
#include <stdint.h>
#include <stddef.h>
#define STACK_WORDS 512
static uint32_t task_stack[STACK_WORDS]; /* grows down from the top */
void stack_paint(void) { for (size_t i = 0; i < STACK_WORDS; i++) task_stack[i] = 0xA5A5A5A5u; }
size_t stack_free_words(void) /* words never touched since painting */
{
size_t n = 0;
while (n < STACK_WORDS && task_stack[n] == 0xA5A5A5A5u) n++;
return n;
}
Typical sources of corruption
| Source | How to catch it |
|---|---|
Buffer overrun (array index, strcpy, missing length check on received data) |
watchpoints, canaries, bounds-checked functions, static analysis |
| Stack overflow into adjacent RAM (other stacks, globals) | MPU guard, PSPLIM, RTOS checks, large margins |
| Use after free / double free | pools with poisoning (fill freed blocks with a pattern), heap integrity checks, ASan on host |
| DMA writing beyond its buffer or into a freed buffer | check DMA lengths, MPU, guard patterns after DMA buffers |
| Cache maintenance errors (Cortex-M7) | aligned DMA buffers, invalidate/clean correctly or use non-cacheable regions |
| ISR and task race on shared structures | critical sections, review all ISR-shared data |
| Wild pointers (uninitialised, stale function pointers) | initialise everything, -Wall -Wextra, static analysis |
Linker tricks: place stacks at the bottom of RAM (below globals) so an overflow runs into non-existent memory or a guard region and faults immediately, rather than silently overwriting variables.
09How do you unit test embedded C code?Mediumβ
Short answer: Separate hardware access from logic behind thin interfaces (HAL functions, function pointers or link-time substitution), then compile and run the logic on the host PC with a test framework (Unity, CppUTest, GoogleTest, CMocka), replacing hardware functions with fakes or mocks (CMock, FFF, hand-written fakes). Run the tests on every commit in CI, measure coverage, and add on-target and hardware-in-the-loop tests for what only real hardware can show.
Example: testing a debounce module with Unity
/* debounce.h */
#include <stdbool.h>
#include <stdint.h>
typedef struct { uint8_t count; bool state; } debounce_t;
bool debounce_update(debounce_t *d, bool raw); /* call every 1 ms; returns stable state */
/* debounce.c */
bool debounce_update(debounce_t *d, bool raw)
{
if (raw == d->state) { d->count = 0; return d->state; }
if (++d->count >= 5) { d->state = raw; d->count = 0; } /* 5 consecutive samples */
return d->state;
}
/* test_debounce.c β runs on the PC */
#include "unity.h"
#include "debounce.h"
void setUp(void) {}
void tearDown(void) {}
void test_glitch_is_ignored(void)
{
debounce_t d = {0};
debounce_update(&d, true); debounce_update(&d, true);
debounce_update(&d, false); /* bounce back */
for (int i = 0; i < 4; i++) TEST_ASSERT_FALSE(debounce_update(&d, true));
TEST_ASSERT_TRUE(debounce_update(&d, true)); /* 5th consecutive sample */
}
int main(void) { UNITY_BEGIN(); RUN_TEST(test_glitch_is_ignored); return UNITY_END(); }
Techniques for replacing hardware
| Technique | How | Pros/cons |
|---|---|---|
| Link-time substitution | link fake_gpio.c instead of gpio.c in the test build |
simple, no production code changes |
| Function pointers / interfaces | driver struct with ops, tests pass fakes | flexible, small run-time cost |
| Generated mocks (CMock, FFF) | mocks record calls and return programmed values | verifies interactions; tests can become brittle |
| Register-level fakes | point the peripheral base pointer at a RAM struct | tests driver register logic on the host |
| Emulation | run the firmware in QEMU or Renode | tests whole images, including startup and RTOS |
Good practices: test-driven development for logic-heavy modules (parsers, state machines, protocol handling, filters), keep hardware-dependent code thin, run tests with sanitizers (-fsanitize=address,undefined) on the host, and track coverage with gcov/lcov. Book: Test-Driven Development for Embedded C (James Grenning).
10What is hardware-in-the-loop (HIL) testing, and how do you set up CI for firmware?Mediumβ
Short answer: CI for firmware builds every configuration and target on each commit, runs static analysis and host unit tests, checks code size, and produces versioned artifacts. Hardware-in-the-loop testing then flashes the build onto real boards (or a board farm) connected to test equipment that simulates the environment β sensors, buses, power supplies, network β and runs automated system tests that check behaviour, timing and recovery. Simulation-based testing (QEMU, Renode) sits in between: whole firmware images on virtual hardware, faster and easier to scale than real boards.
Typical pipeline
commit β build (all boards, debug + release, warnings as errors)
β static analysis (cppcheck/clang-tidy/MISRA checker), formatting check
β host unit tests + sanitizers + coverage report
β size report (fail if flash/RAM grows beyond a budget)
β simulation tests (Renode/QEMU) for boot, protocols, RTOS behaviour
β HIL: flash test boards, run system tests (pytest + serial/CAN/SPI tools, power cycling)
β artifacts: signed images, map files, ELF with symbols, release notes
HIL rig components: a host PC or Raspberry Pi per rack, debug probes for flashing, USB-controlled relays/power switches for power cycling, programmable supplies (brown-out tests), signal generators or DACs to simulate sensors, CAN/UART/SPI adapters, logic analysers with scripted captures, and a test framework (pytest, Robot Framework, LabGrid, Lava).
What HIL catches that unit tests cannot: timing and interrupt interactions, peripheral configuration errors, boot and update procedures (including failed updates and rollbacks), power-loss behaviour, long-duration stability (leaks, counter overflows), and hardware revision differences.
Tips: make the firmware testable β a test command shell or RPC over UART/USB, deterministic logs, and version reporting β and keep HIL tests stable (flaky tests get ignored).
11What is code coverage, and what coverage levels do safety standards require?Mediumβ
Short answer: Code coverage measures which parts of the code the tests executed. Statement coverage checks every line ran; branch (decision) coverage checks every if/switch took each outcome; MC/DC (Modified Condition/Decision Coverage) checks that each individual condition in a decision independently affects the outcome. Coverage shows what is not tested β it does not prove the tested code is correct. Safety standards require higher levels for more critical software: e.g. DO-178C requires MC/DC for Level A avionics software, and ISO 26262 recommends MC/DC for ASIL D.
Example: why branch coverage is not enough
if (speed > LIMIT && !override) brake();
Two tests (true/false outcome) give branch coverage, but MC/DC needs tests showing that speed > LIMIT alone changes the outcome (with override fixed) and that override alone changes it: e.g. (T,F)βbrake, (F,F)βno brake, (T,T)βno brake.
Measuring
- On the host:
gcc --coverage/ gcov + lcov/gcovr reports; LLVM source-based coverage. - On the target: instrumented builds that dump counters over a debug link (gcov with semihosting or custom dump), or non-intrusive ETM trace (no code changes).
- Commercial tools: VectorCAST, LDRA, Testwell CTC++, Cantata (support MC/DC and certification evidence).
Practical targets: for ordinary product firmware, aim for high statement and branch coverage on logic modules (parsers, state machines, safety checks) rather than a single number for the whole codebase; review untested code to decide whether it needs tests, is defensive code, or is dead code.
12What is static analysis and which tools are used?Easyβ
Short answer: Static analysis examines source code without running it to find bugs and rule violations: NULL dereferences, buffer overruns, uninitialised variables, integer overflow, resource leaks, dead code, concurrency issues and violations of coding standards such as MISRA C. It runs in CI on every change and catches issues that tests miss because they occur only on rare paths.
| Tool | Type |
|---|---|
Compiler warnings (-Wall -Wextra -Wshadow -Wconversion -Wcast-align -Werror), GCC -fanalyzer |
free, first line of defence |
clang-tidy, Clang Static Analyzer (scan-build) |
free, deep path analysis |
| cppcheck | free, low false positives, MISRA addon |
| Coverity, Klocwork, Polyspace, PC-lint Plus, Parasoft C/C++test, Helix QAC | commercial, MISRA/CERT checking, certification support |
| CodeQL, SonarQube | queries, dashboards |
| Polyspace Code Prover, Frama-C, AstrΓ©e | abstract interpretation: prove absence of some run-time errors |
Stack analysis (-fstack-usage, puncover), WCET tools (aiT) |
resource bounds |
Coding standards: MISRA C:2012/2023 (automotive, industrial; rules with mandatory/required/advisory categories and a deviation process), CERT C (security), AUTOSAR C++14 / MISRA C++:2023, and the Barr Group Embedded C Coding Standard.
Making it work in practice: start with compiler warnings as errors; add one analyser to CI; fix or explicitly suppress (with a justification) each finding so the baseline stays clean; run analysis on new code first for legacy codebases.
Dynamic analysis complements it: sanitizers (ASan, UBSan, TSan) and Valgrind on host builds, fuzzing (libFuzzer, AFL++) for parsers and protocol handlers.
13What is the difference between Make and CMake?Easyβ
Short answer: Make is a build tool that executes rules from a Makefile: targets, their dependencies and the commands to build them, rebuilding only what changed. CMake is a build-system generator: you describe targets, sources, include paths and options in CMakeLists.txt, and CMake generates Makefiles or Ninja files (or IDE projects) for the chosen toolchain. CMake handles cross-platform details, dependencies and configuration better, so most modern embedded projects (Zephyr, Pico SDK, ESP-IDF, STM32 CMake projects) use it, often with Ninja for speed.
Minimal cross-compilation setup
# arm-gcc-toolchain.cmake
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
# CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(blinky C ASM)
add_executable(blinky.elf src/main.c src/startup_stm32f4.s)
target_compile_options(blinky.elf PRIVATE -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16
-Os -ffunction-sections -fdata-sections -Wall -Wextra)
target_link_options(blinky.elf PRIVATE -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16
-T${CMAKE_SOURCE_DIR}/stm32f4.ld -Wl,--gc-sections -Wl,-Map=blinky.map --specs=nano.specs)
add_custom_command(TARGET blinky.elf POST_BUILD
COMMAND arm-none-eabi-objcopy -O binary blinky.elf blinky.bin
COMMAND arm-none-eabi-size blinky.elf)
cmake -B build -G Ninja -DCMAKE_TOOLCHAIN_FILE=arm-gcc-toolchain.cmake
cmake --build build
Make basics interviewers ask: targets, prerequisites, recipes (tab-indented), automatic variables ($@ target, $< first prerequisite, $^ all prerequisites), pattern rules (%.o: %.c), .PHONY, and dependency generation with -MMD -MP so header changes trigger rebuilds.
Other build systems: Meson, Bazel, SCons; Kbuild/Kconfig for the Linux kernel; Yocto/BitBake and Buildroot for whole Linux distributions; vendor IDE projects (convertible to CMake for CI).
14What does a map file tell you?Mediumβ
Short answer: The linker map file (-Wl,-Map=app.map) shows where everything ended up in memory: the memory regions and how full they are, the address and size of every section (.text, .rodata, .data, .bss, stacks, heap), which object files and library members contributed to each section, the address of every symbol, which sections were discarded by --gc-sections, and why library objects were pulled in. It is the primary tool for understanding and reducing flash and RAM usage and for mapping crash addresses to functions.
What to look for
| Question | Where in the map |
|---|---|
| How much flash/RAM is used? | memory configuration and the end addresses of sections (or arm-none-eabi-size) |
| What is using all the flash? | sizes per input section/object under .text and .rodata |
Why is printf/float code present? |
"archive member included to satisfy reference by file (symbol)" lines at the top |
| Where is a variable or function? | symbol addresses (for watchpoints, crash analysis) |
| Is a buffer in the right RAM (DTCM, non-cacheable, backup SRAM)? | output section placement |
| Initialised data cost | .data takes space in both flash (initial values) and RAM |
Helpful tools: arm-none-eabi-nm --size-sort -S -C app.elf | tail -30 (largest symbols), bloaty, puncover (per-function code and stack size in a web view), Amap / MapViewer GUIs, and size -A for per-section totals.
Interview example: "Flash grew by 20 KB after a small change; the map file showed _dtoa_r and floating-point emulation pulled in by a %f in one debug message. Removing it (or using a fixed-point print) recovered the space."
15How do you reduce firmware code size?Mediumβ
Short answer: Measure first (map file, nm --size-sort, bloaty), then: compile with -Os (or -Oz with Clang), enable -ffunction-sections -fdata-sections with -Wl,--gc-sections so unused functions are removed, use link-time optimisation (-flto), use newlib-nano (--specs=nano.specs) and avoid heavy library functions (printf with floats, sscanf, iostream), remove unused vendor HAL code or switch to LL/register-level drivers, move constant tables to compact formats, and avoid code duplication from macros and templates.
Techniques ranked by typical impact
| Technique | Typical saving |
|---|---|
Remove floating-point printf/scanf, use integer formatting |
10β20+ KB |
--gc-sections with function/data sections |
large if libraries are linked whole |
-Os vs -O2 |
10β30% of code |
| LTO | 5β15% (cross-module inlining, dead code removal) |
| newlib-nano | several KB |
| Replace vendor HAL with LL drivers | often 10β30 KB in HAL-heavy projects |
| Thumb-2 (default on Cortex-M) vs ARM state on Cortex-A/R | ~25β30% |
Compress assets (fonts, images), store tables as const |
depends |
| Avoid inline expansion of large macros/inline functions in many places | depends |
| Logging with IDs instead of strings (deferred logging) | string data |
Be careful: size optimisation can make code slower (-Os disables some loop unrolling) β keep hot paths fast with function attributes (__attribute__((optimize("O2"))) sparingly) or by placing them in RAM; aggressive optimisation can expose latent undefined behaviour, so run the full test suite on the optimised build.
RAM reduction: check .bss and .data in the map file, size stacks from measured high-water marks, shrink buffers to measured needs, use const so tables stay in flash, and share buffers between features that never run at the same time (unions or overlays, carefully).
16What is git bisect and how does it help?Easyβ
Short answer: git bisect performs a binary search through the commit history to find the commit that introduced a bug. You mark a known-good and a known-bad commit; git checks out the commit halfway between; you test it and mark it good or bad; git repeats until it identifies the first bad commit. 1,000 commits take only about 10 steps. With a test script that returns 0 (good) or non-zero (bad), git bisect run automates the whole search, even including flashing and testing on hardware.
git bisect start
git bisect bad # current HEAD has the bug
git bisect good v2.3.0 # this release was fine
# build, flash, test... then:
git bisect good # or: git bisect bad, or: git bisect skip (doesn't build)
# ...repeat until git prints "<sha> is the first bad commit"
git bisect reset # return to where you started
# automated, e.g. with a HIL test script
git bisect run ./ci/flash_and_test.sh
Tips: small, focused commits make the result meaningful; every commit should build (otherwise use git bisect skip); for intermittent bugs, make the test repeat enough times to be confident a "good" verdict is really good; and bisecting configuration or dependency changes (toolchain, SDK versions) is also possible by bisecting the commits that changed them.
Related git tools: git log -S "symbol" (find commits that added or removed text), git blame (who changed a line and why), git log -p --follow file.
17What is a logic analyser and when do you use it instead of an oscilloscope?Easyβ
Short answer: A logic analyser captures many digital signals (8β32+ channels) as 1s and 0s over long periods and decodes protocols (SPI, I2C, UART, CAN, 1-Wire, I2S, SWD), so it answers "what data was sent, in what order and with what timing between events". An oscilloscope shows the analog waveform of a few channels β voltage levels, rise times, ringing, noise, power-supply ripple β so it answers "is this signal electrically correct". Use the logic analyser for protocol and timing logic; use the oscilloscope when you suspect signal integrity, power or analog problems.
| Logic analyser | Oscilloscope | |
|---|---|---|
| Channels | 8β34+ | 2β8 |
| Shows | logic levels vs time, decoded protocols | real voltage waveform |
| Capture length | long (seconds to minutes, streaming) | short at high sample rates (deep memory helps) |
| Finds | wrong commands, missing ACKs, CS timing, ISR timing (with GPIO markers), sequence errors | ringing, slow edges, glitches, wrong levels, noise, supply dips, crosstalk |
| Typical tools | Saleae Logic, DSLogic, sigrok/PulseView with cheap analysers | Rigol, Siglent, Keysight, Tektronix; mixed-signal scopes (MSO) combine both |
Typical workflow: the logic analyser shows that an I2C sensor sometimes NACKs; the oscilloscope then shows that SDA's rising edge is too slow because the pull-up is too weak β two tools, one bug.
Timing measurements: toggle GPIOs at points in firmware (ISR entry/exit, task start, DMA complete) and capture them with the logic analyser alongside the bus signals to measure latency, execution time and jitter precisely without debugger interference.
18How do you design logging and diagnostics for devices in the field?Mediumβ
Short answer: Make failures reportable after the fact: store a persistent crash record (fault registers, PC/LR, stack snippet, task name, firmware version and reset reason) in no-init RAM or flash, keep a compact ring-buffer event log with timestamps and severity levels, maintain counters and health metrics (resets by cause, communication errors, watchdog events, minimum free stack/heap, battery), and upload them when connectivity is available or expose them over a service interface. Keep logging cheap (binary records or log IDs instead of formatted strings), rate-limited and safe to call from any context.
Design elements
| Element | Detail |
|---|---|
| Log levels | ERROR / WARN / INFO / DEBUG, configurable at run time per module |
| Format | binary records: {timestamp, module, event ID, args}; decoded on the host using the build's string table (keeps flash small and logging fast) |
| Storage | RAM ring buffer, flushed to flash in batches (wear levelling, power-fail safe), with a bounded size |
| Context safety | ISR-safe enqueue (lock-free SPSC or a short critical section); formatting and I/O in a low-priority task |
| Crash path | minimal code that does not depend on the heap, RTOS or peripherals that may have failed |
| Versioning | firmware version, build ID (hash of the ELF) in every report so addresses can be symbolised |
| Privacy/security | no secrets or personal data in logs; authenticate the upload channel |
Reset-cause handling at boot: read the reset-cause register (POR, BOR, watchdog, software, pin), log it, check for a crash record from the previous run, then clear both.
Tools/services: Memfault, Zephyr's logging and coredump subsystems, AWS IoT Device Defender, or a custom backend; on embedded Linux, journald, pstore/ramoops, and kdump.
Interview point: "Without field diagnostics, an intermittent bug that happens once a month on 1 in 1,000 devices is nearly impossible to fix; with a crash record and the matching ELF file, it is often a 10-minute job."