How to use GDB
Below are some useful GDB commands that can help us debug our programs. This assumes you have flashed a program onto your microcontroller and attached to an OpenOCD session.
General Debugging
NOTE: Many of the commands you see below can be executed using a short form. For example,
continue
can simply be used asc
, orbreak $location
can be used asb $location
. Once you have experience with the commands below, try to see how short you can get the commands to go before GDB doesn't recognize them!
Dealing with Breakpoints
break $location
: Set a breakpoint at a place in your code. The value of$location
can include:break *main
- Break on the exact address of the functionmain
break *0x080012f2
- Break on the exact memory location0x080012f2
break 123
- Break on line 123 of the currently displayed filebreak main.rs:123
- Break on line 123 of the filemain.rs
info break
: Display current breakpointsdelete
: Delete all breakpointsdelete $n
: Delete breakpoint$n
(n
being a number. For example:delete $2
)
clear
: Delete breakpoint at next instructionclear main.rs:$function
: Delete breakpoint at entry of$function
inmain.rs
clear main.rs:123
: Delete breakpoint on line 123 ofmain.rs
enable
: Enable all set breakpointsenable $n
: Enable breakpoint$n
disable
: Disable all set breakpointsdisable $n
: Disable breakpoint$n
Controlling Execution
continue
: Begin or continue execution of your programnext
: Execute the next line of your programnext $n
: Repeatnext
$n
number times
nexti
: Same asnext
but with machine instructions insteadstep
: Execute the next line, if the next line includes a call to another function, step into that codestep $n
: Repeatstep
$n
number times
stepi
: Same asstep
but with machine instructions insteadjump $location
: Resume execution at specified location:jump 123
: Resume execution at line 123jump 0x080012f2
: Resume execution at address 0x080012f2
Printing Information
print /$f $data
- Print the value contained by the variable$data
. Optionally format the output with$f
, which can include:x: hexadecimal d: signed decimal u: unsigned decimal o: octal t: binary a: address c: character f: floating point
print /t 0xA
: Prints the hexadecimal value0xA
as binary (0b1010)
x /$n$u$f $address
: Examine memory at$address
. Optionally,$n
define the number of units to display,$u
unit size (bytes, halfwords, words, etc),$f
anyprint
format defined abovex /5i 0x080012c4
: Print 5 machine instructions staring at address0x080012c4
x/4xb $pc
: Print 4 bytes of memory starting where$pc
currently is pointing
disassemble $location
disassemble /r main
: Disassemble the functionmain
, using/r
to show the bytes that make up each instruction
Looking at the Symbol Table
info functions $regex
: Print the names and data types of functions matched by$regex
, omit$regex
to print all functionsinfo functions main
: Print names and types of defined functions that contain the wordmain
info address $symbol
: Print where$symbol
is stored in memoryinfo address GPIOC
: Print the memory address of the variableGPIOC
info variables $regex
: Print names and types of global variables matched by$regex
, omit$regex
to print all global variablesptype $data
: Print more detailed information about$data
ptype cp
: Print detailed type information about the variablecp
Poking around the Program Stack
backtrace $n
: Print trace of$n
frames, or omit$n
to print all framesbacktrace 2
: Print trace of first 2 frames
frame $n
: Select frame with number or address$n
, omit$n
to display current frameup $n
: Select frame$n
frames updown $n
: Select frame$n
frames downinfo frame $address
: Describe frame at$address
, omit$address
for currently selected frameinfo args
: Print arguments of selected frameinfo registers $r
: Print the value of register$r
in selected frame, omit$r
for all registersinfo registers $sp
: Print the value of the stack pointer register$sp
in the current frame
Controlling OpenOCD Remotely
monitor reset run
: Reset the CPU, starting execution over againmonitor reset
: Same as above
monitor reset init
: Reset the CPU, halting execution at the startmonitor targets
: Display information and state of current target