Why does GDB report "automatically using hardware breakpoints for read-only addresses" on the ESP32, and what happens once its two hardware breakpoints are used up?
A software breakpoint works by overwriting an instruction in memory, but ESP32 code runs from flash, which the CPU cannot write. GDB therefore asks for hardware breakpoints. The chip has only two real breakpoint comparators, so Espressif's OpenOCD emulates any further ones by patching the flash itself.
* A software breakpoint changes the code; a hardware breakpoint only watches the address. *
There are two ways to stop a program at an address:
| Software breakpoint | Hardware breakpoint | |
|---|---|---|
| How | The debugger replaces the instruction with a special "break" instruction and restores it when the breakpoint is removed. | A comparator register in the CPU's debug unit is loaded with the address; the CPU halts when it fetches from it. |
| Needs | Writable memory (RAM) | Nothing, works on any address, including flash and ROM |
| How many | Practically unlimited | Fixed by the silicon: 2 on the ESP32 |
ESP-IDF places application code in the external SPI flash and executes it through a cache. GDB sees those addresses marked as read-only in the memory map that OpenOCD reports, so it cannot patch them and falls back to hardware breakpoints on its own, which is the note it prints in the session log.
On a generic OpenOCD that would cap you at two breakpoints in flash code. Espressif's OpenOCD hides the limit: GDB thinks it is setting a hardware breakpoint, and once the two comparators are used up, OpenOCD writes a break instruction into the flash sector itself and restores it later (up to 32 such flash breakpoints, plus 32 in instruction RAM). That costs time, since a flash sector has to be erased and rewritten each time, and it wears the flash. It also breaks Secure Boot's signature check if the flash is modified.
Watchpoints, which stop when data is read or written rather than when code runs, cannot be emulated this way. There are exactly two, which is the reason for the hardware-watchpoint-limit 2 line in gdbinit. If FreeRTOS's stack-overflow watchpoint option is enabled, it takes one of them.
Go deeper:
ESP-IDF — JTAG Tips and Quirks — the breakpoint and watchpoint counts and how flash breakpoints are emulated.
Wikipedia — Breakpoint — software vs hardware breakpoints and data breakpoints (watchpoints).
GDB manual — Setting Watchpoints —
watch,rwatch,awatchand the hardware limits.