STM32 gotchas
258. Too many breakpoints in gdb

Typical debugging setup with STM32 uses the hardware breakpoint facilities built into the debug extensions of the processor core. These feature a certain number of possible breakpoints (around half a dozen, depending on particular Cortex-M model of the core). When debugging using gdb, it sometimes refuses to insert a breakpoint, with a "too many breakpoints" message.

It may be, that there are genuinely many breakpoints set up already; the info br[eakpoints] command can be used to inspect them. Users sometimes complained about this problem also when using an IDE such as CubeIDE, which uses gdb under its hood, and there were some "stuck" breakpoints from history, not displayed in the editor pane. These then could be seen and removed in the breakpoint pane.

If compiler is set to optimize aggressively, it may attempt to inline a function even if it is called (directly or indirectly) from several places. In such case, the function has several instances in the binary and if a breakpoint is to be placed at some line of such function, gdb attempts to place them at all such instances. This may lead to too many breakpoints, even is the user attempted to place only one.

The physical action of inserting the breakpoints into the processor's debugging extension is performed upon a c[ontinue] command in gdb. However, if the core was not stopped previously (e.g. through another breakpoint or a Ctrl-C), the breakpoint insertion fails. In some (all?) cases gdb incorrectly reports "too many breakpoints" in such case, too.