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 GDB to a cargo-embed session.
General Debugging
NOTE: Many of the commands you see below can be executed using a short form. For example,
continuecan simply be used asc, orbreak $locationcan 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$locationcan include:break *main- Break on the exact address of the functionmainbreak *0x080012f2- Break on the exact memory location0x080012f2break 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(nbeing a number. For example:delete $2)
clear: Delete breakpoint at next instructionclear main.rs:$function: Delete breakpoint at entry of$functioninmain.rsclear 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$nnumber times
nexti: Same asnextbut with machine instructions insteadstep: Execute the next line, if the next line includes a call to another function, step into that codestep $n: Repeatstep$nnumber times
stepi: Same asstepbut 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 pointprint /t 0xA: Prints the hexadecimal value0xAas binary (0b1010)
x /$n$u$f $address: Examine memory at$address. Optionally,$ndefine the number of units to display,$uunit size (bytes, halfwords, words, etc.),$fanyprintformat defined abovex /5i 0x080012c4: Print 5 machine instructions staring at address0x080012c4x/4xb $pc: Print 4 bytes of memory starting where$pccurrently is pointing
disassemble $locationdisassemble /r main: Disassemble the functionmain, using/rto 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$regexto print all functionsinfo functions main: Print names and types of defined functions that contain the wordmain
info address $symbol: Print where$symbolis 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$regexto print all global variablesptype $data: Print more detailed information about$dataptype cp: Print detailed type information about the variablecp
Poking around the Program Stack
backtrace $n: Print trace of$nframes, or omit$nto print all framesbacktrace 2: Print trace of first 2 frames
frame $n: Select frame with number or address$n, omit$nto display current frameup $n: Select frame$nframes updown $n: Select frame$nframes downinfo frame $address: Describe frame at$address, omit$addressfor currently selected frameinfo args: Print arguments of selected frameinfo registers $r: Print the value of register$rin selected frame, omit$rfor all registersinfo registers $sp: Print the value of the stack pointer register$spin the current frame
Controlling cargo-embed Remotely
monitor reset: Reset the CPU, starting execution over again