Home / Learn Chip Programming / GDB and OpenOCD: Breakpoints, Watchpoints, Memory

GDB and OpenOCD: Breakpoints, Watchpoints, Memory

Use the same powerful debugger professionals use, from the command line or VS Code.

In shortUse the same powerful debugger professionals use, from the command line or VS Code.

Theory in plain words

Debug sessionopenocd -f stlink.cfg -f stm32f4x.cfgGDB target remote :3333load firmware.elfbreak, step, watch
Debug session

What it is

OpenOCD talks to the probe and presents a GDB server (port 3333). arm-none-eabi-gdb connects, loads the ELF, sets breakpoints, watchpoints and inspects variables and registers.

Why we use it

A debugger finds bugs that printf cannot: crashes, corrupt memory, timing. It works even with no UART.

How it works

Run openocd, then in GDB: target extended-remote :3333, load, monitor reset halt, break main, continue, step, print var, watch counter, info registers, x/16xw addr.

Where it is used

  • Hard faults and memory corruption
  • Stepping through startup
  • VS Code Cortex-Debug extension

GDB command trainer

What does each command do?Try it Yourself

Watchpoints are the best way to find "who changed this variable": the CPU halts at the exact write.

Typical session
# terminal 1
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg
# terminal 2
arm-none-eabi-gdb build/firmware.elf
(gdb) target extended-remote :3333
(gdb) monitor reset halt
(gdb) load
(gdb) break hardfault_c
(gdb) continue
(gdb) bt
(gdb) info registers
(gdb) x/8xw $sp                      # inspect the stack
Tip:In VS Code install the Cortex-Debug extension and a launch.json with your ELF, OpenOCD config and SVD file: you get peripheral register views and a graphical debugger.

Quick quiz

1. Which GDB feature stops when a variable changes?

2. OpenOCD role?

Common questions

Which GDB feature stops when a variable changes?

watch. A watchpoint.

OpenOCD role?

Bridges GDB and the debug probe. It is the GDB server.