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.
Theory in plain words
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
Watchpoints are the best way to find "who changed this variable": the CPU halts at the exact write.
# 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 stackQuick quiz
1. Which GDB feature stops when a variable changes?
A watchpoint.
2. OpenOCD role?
It is the GDB server.
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.