Section 14.29

Breakpoint and watchpoint

Definition

A breakpoint and a watchpoint are triggers that a debugger, a program that controls and inspects another program, sets to stop that program. A breakpoint fires when the processor reaches the instruction at a chosen address in the code. A watchpoint fires when the program reads or writes a chosen range of memory, whichever kinds of access were selected. A breakpoint is therefore tied to a place in the code, a watchpoint to a place in the data. Each is implemented either in software or by the processor’s debug hardware.

The idea

A variable that counts free buffers suddenly holds 65535 instead of 3, and 40 functions in the program write to it. Reading the code does not show which write went wrong, and a breakpoint in each of the 40 functions would stop the program thousands of times. A watchpoint on the variable stops the program at the faulty write itself, whichever function makes it. A breakpoint answers a different kind of question: whether execution reaches an instruction, and what the registers and memory hold when it does.

A watchpoint is like an alarm on one drawer of a filing cabinet: it sounds whenever the drawer is opened, whoever opens it, so nobody has to watch every person in the office. The picture fails in one important way. A hardware watchpoint does not sense the drawer; it sits inside the processor and checks the addresses of the processor’s own reads and writes. A write made by another device, or a write through a different address that leads to the same memory, never passes that check.

How it works

Four mechanisms are in use. They differ in what they need from the hardware, in how many can be active at once and in how much they slow the program.

  1. Software breakpoint. The debugger saves the instruction at the chosen address and writes a trap instruction in its place, an instruction whose only job is to hand control to the debugger: INT3, the single byte 0xcc, on x86 processors (the processor family of most personal computers), and BKPT on Arm processors. Executing the trap stops the processor or raises a debug exception, a forced jump into code that serves the debugger. To continue, the debugger restores the original instruction, executes it alone (single-stepping) and writes the trap again. Software breakpoints are unlimited in number but need code memory that the debugger can rewrite.

  2. Hardware breakpoint. A comparator, a register in the processor’s debug hardware, holds an address and is compared with the address of every instruction the processor fetches. A match stops the program before the instruction at that address executes. Memory is not changed, so a hardware breakpoint also works on code in flash memory §6.6 or read-only memory, but there are only a few comparators: four on x86 processors, shared with watchpoints, and six for instructions on the Cortex-M3 and Cortex-M4 §6.3, processors made for microcontrollers §6.1 (small single-chip computers).

  3. Hardware watchpoint. A data comparator holds an address range and an access kind: read, write, or either. The processor compares each data access it makes with the range. A match is reported after the access has happened. On x86 processors, each of the same four registers covers 1, 2, 4 or (on 64-bit processors) 8 bytes, at an address that is a multiple of that length, and watches either writes or reads and writes together; there is no setting for reads alone. On the Cortex-M3 and Cortex-M4, the Data Watchpoint and Trace (DWT) unit has four comparators, and each can watch an aligned block: a block whose size is a power of two and whose start address is a multiple of that size.

  4. Software watchpoint. When no comparator is free, or the watched data is wider than a comparator covers, GDB §6.28 (GNU Debugger, the debugger of the GNU free-software project) runs the program one instruction at a time and tests the watched value after every step. The GDB manual calls this hundreds of times slower than normal execution.

What happens at a match depends on how the debug hardware is set up. In halting debug, the processor core stops, and an external debugger, connected through a debug port such as Serial Wire Debug §6.25, reads registers and memory. In monitor mode, the processor instead takes a debug exception and runs a handler on the device itself. Either way only the processor core stops. Timers, radios, direct memory access §4.36 (DMA) controllers, which copy data to and from memory without the processor, and the watchdog timer §6.16, which resets a device that stops responding, keep running unless the chip is configured to pause them during a debug halt.

GDB offers three watch commands with different meanings. watch stops when the program writes the watched expression and its value changes, so a write that stores the value already there is not reported. rwatch stops when the program reads the expression, and awatch stops on every read or write; GDB can set these two only as hardware watchpoints.

Worked example

The x86 rules give exact answers. A debugger sets a hardware watchpoint on writes to the 4 bytes at addresses 0x1000 to 0x1003; with length 4 the start address must be a multiple of 4, and 0x1000 is 4096, which is 4×10244 \times 1024. An x86 processor reports a match when any byte of an access lies in the watched range.

Width and count. An 8-byte counter at 0x2000, on a processor whose comparators cover at most 4 bytes, needs 8/4=28 / 4 = 2 comparators: one for 0x2000 to 0x2003 and one for 0x2004 to 0x2007. With four comparators, of which the debugger already uses one for a hardware breakpoint, 4−1=34 - 1 = 3 remain; after the counter takes 2, 3−2=13 - 2 = 1 is left for any other variable.

Value change versus write. The counter holds 5, and the program writes 5 again. The hardware reports the write. GDB’s watch command compares the old value 5 with the new value 5, finds no change and lets the program continue without stopping. On a target that supports it, awatch also stops for same-value writes. It stops for reads as well, so interpreting a stop requires identifying which access occurred.

Stated precisely

A hardware watchpoint is set on a range of addresses [a,a+n)[a, a + n), meaning the nn addresses from aa up to but not including a+na + n, and on a set KK of access kinds, chosen from read and write. It reports an access if and only if all four of the following hold.

  1. The access is made by the processor core whose debug unit holds the comparator. Accesses by DMA controllers, other cores and other bus masters, meaning devices that start memory transfers on their own, are not seen.

  2. The address the processor issues falls in the range under the architecture’s overlap rule. x86 matches if any accessed byte is in the range. Armv7-M, the architecture of the Cortex-M3 and Cortex-M4, requires a match for the part of an access up to the next 4-byte boundary and leaves it to each processor design whether an unaligned access that crosses that boundary matches beyond it. On a processor with virtual memory, the comparison uses the virtual address, so other mappings of the same memory do not match.

  3. The kind of the access is in KK.

  4. The comparator is enabled and the debug event is delivered. On Armv7-M, if both halting debug and the debug monitor exception are disabled, the processor ignores the event.

The absence of a report therefore shows only that no access meeting all four conditions happened. It shows nothing about writes by other agents, through other addresses, or of other kinds.

A hardware breakpoint on address bb stops the program before the instruction at bb executes, each time execution reaches bb. A software breakpoint stops the program when the trap instruction at bb executes. That needs the trap to be in the memory the processor actually fetches from: code copied or reloaded after the trap was written does not contain it, and on a processor with an instruction cache, the cache must not still hold the original instruction.

The Flash Patch and Breakpoint unit

Armv7-M defines an optional unit called the Flash Patch and Breakpoint unit (FPB). It acts on the Code region, the first 512 MiB of the address map (addresses 0x00000000 to 0x1fffffff), where the program in flash memory usually lives. It has instruction comparators, which match instruction fetches, and literal comparators, which match data reads of literals, the constants that compilers store in code memory next to the instructions. The Cortex-M3 and Cortex-M4 have six instruction comparators and two literal comparators; a field of the control register FP_CTRL reports the numbers.

An instruction comparator does one of two things at a match. It can raise a breakpoint, which is how a debugger gets hardware breakpoints on these processors. Or it can remap the fetch: the processor reads the matching 4-byte word from a table in SRAM §6.7 (static random-access memory, the processor’s working memory) instead of from flash. The register FP_REMAP holds the start address of the table, which must lie in the SRAM region (0x20000000 to 0x3fffffff) and be aligned to at least 8 words; comparator number nn, counting from 0, takes its word from the start address plus 4n4n bytes. Remapping matches whole words and acts on reads only: writes always go to the original address.

A hotpatching §14.33 system, which repairs a running program without restarting it, can use remapping to redirect execution without rewriting flash.

  1. Place the corrected code, the patch, in memory from which the processor can execute and which a branch from the faulty location can reach.

  2. Write into the remap table a word that holds a branch, an instruction that jumps, to the patch.

  3. Program a free instruction comparator with the address of the word in flash where execution must leave the faulty code, select remapping, and enable the comparator.

From then on every fetch of that word returns the branch, and execution continues in the patch, while the bytes in flash stay unchanged. The Armv7-M manual states that the unit is not restricted to debug use and can provide product updates. The research system HERA §21.6 (Hotpatching of Embedded Real-time Applications) uses this mechanism.

Five facts limit the number and reach of such patches.

Limits and common mistakes

Stopping the program changes the behavior under study. Only the processor core halts: peers on a network or radio link time out, timers expire, incoming data fills buffers that nobody empties, and a watchdog timer that keeps running resets the device. A fault caused by the relative timing of two parts of a system can disappear while a breakpoint slows one of them.

A watchpoint reports after the access. On x86 the processor stops after the instruction that made the access has completed. On Armv7-M the watchpoint event is asynchronous: the processor can execute further instructions before it stops, so the program counter §2.9, the register that holds the address of the next instruction, need not point just past the culprit.

A watchpoint that never fires is not evidence that the memory was not written. The write may have come from a DMA controller or another core, through another address, as an access kind that was not selected, or beyond an unaligned boundary that the comparator does not cover; and GDB’s watch ignores writes that leave the value unchanged.

Software watchpoints are slow, and the GDB manual warns that a software watchpoint watches the value in only one thread, one sequence of instructions that the operating system schedules; a change made by another thread may go unnoticed. Hardware watchpoints apply in every thread.

A software breakpoint left in memory after the debugger disconnects becomes a fault. On a Cortex-M processor with neither halting debug nor the debug monitor enabled, executing BKPT raises a HardFault, the processor’s general fault exception.

Questions answered

Does the hardware watchpoint cover every width, alias, and DMA writer?

No. A hardware watchpoint is a comparator in the processor’s debug unit, so it reports only accesses that this processor makes, at the addresses it was set to, of the kinds it was set to, and within the range sizes its hardware supports. Each case in the question falls outside that in its own way.

In addition, GDB’s watch command (GDB is the GNU Debugger) stops only when the value changes, so it misses a write that stores the value already present. To claim that nothing wrote a location, list every agent that can write it and cover each one separately: the processor’s own accesses with a watchpoint whose matching rules the processor manual confirms, and a DMA controller by inspecting its transfer settings or comparing the memory before and after each transfer.

Related entries: Runtime instrumentation (§14.24), Trace versus sampling (§14.30), Instrumentation overhead (§11.32), Dynamic patching and hot update (§14.33), Direct memory access (DMA) (§4.36), GDB (§6.28). Further reading: [5, 81, 96, 173].