Topics / Debugging, Testing & Tools / Q5
What are the essential GDB commands for embedded debugging?
Short answer: Connect to the probe's GDB server with target extended-remote :3333 (OpenOCD) or :2331 (J-Link), then load to flash the ELF, monitor reset halt to reset, break/hbreak and watch to stop, continue/next/step/finish to run, bt for the call stack, info registers, print/p/x and x/16wx addr to inspect data and memory, frame N to switch stack frames, and set var x = 5 to change values.
$ arm-none-eabi-gdb build/app.elf
(gdb) target extended-remote :3333 # OpenOCD GDB server
(gdb) monitor reset halt
(gdb) load # program flash from the ELF
(gdb) hbreak main
(gdb) continue
(gdb) next / step / finish # over, into, out of a function
(gdb) bt # backtrace
(gdb) frame 2 # select a caller's frame
(gdb) info locals
(gdb) p/x *((GPIO_TypeDef *)0x40020000) # peripheral registers as a struct
(gdb) x/8wx 0x20000000 # 8 words of RAM in hex
(gdb) watch rx_count # stop when written
(gdb) info registers # r0-r15, xPSR
(gdb) p $pc ; p/x $sp
(gdb) set var debug_level = 3
(gdb) display/x status # show after every stop
(gdb) monitor reset # probe-specific commands
Productivity tips
- Build with
-g3(macros available in GDB) and keep the exact ELF for every release so you can debug field crash dumps. -Ogor-O1makes stepping more predictable than-O2; "value optimized out" means the variable lives only in registers at that point.- A
.gdbinitfile automates connection and common commands; GDB's Python API can pretty-print RTOS structures. - Use RTOS-aware debugging (OpenOCD
-rtos auto, J-Link RTOS plugins) to list tasks and their stacks. - Post-mortem: load a RAM dump into GDB, or analyse a core dump with tools like Memfault or
CrashDebug. arm-none-eabi-addr2line -e app.elf 0x0800abcdconverts a crash PC to a source line without a debugger.