What is the difference between a breakpoint and a watchpoint?
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.