GDB quick reference


Prepared by Julie Zelenski

Opening gdb

Run gdb on the program.elf file to debug and use script file $CS107E/other/gdbsim.commands to properly configure simulator.

$ riscv64-unknown-elf-gdb --command=$CS107E/other/gdbsim.commands program.elf
Reading symbols from program.elf...
Auto-loading commands from CS107E/other/gdbsim.commands...
Connected to the simulator.
Loading section .text, size 0x510c lma 40000000
...

Common GDB commands

The gdb debugger has many features packed into it, allowing you to control program execution, observe and trace runtime behavior, examine and change registers and memory, and much more. Below is a table of gdb commands for common use cases. Make it your goal to get these basic commands under your belt now and plan to keep learning and extending your mastery from here. Becoming facile at wrangling gdb is a major superpower for a programmer to have!

Pro-tip In the table, we identify commands by their full name but most can be abbreviated to save your overworked fingers. For example, info registers can be invoked as i r, break is just b, print/x can be shortened to p/x, and so on.

General  
help Help for gdb
help CMD Help for gdb command CMD
↩️ Return key repeats previous command
▶️ Tab key completes from prefix (works for commands, function/variable names, and more)
⬆️ ⬇️ Up/down arrow keys scroll back/forward through gdb command history
ctrl-r Search backwards through gdb command history for a specific string
quit Quit gdb
Run and stop program  
run Run program, start from beginning
start Run program, start from beginning, stop at entry to main() function
ctrl-c Interrupt executing program, give control back to gdb
kill Kill execution of program being debugged
Breakpoints  
break WHERE Set breakpoint at WHERE
   break sum Set breakpoint at named function
   break 13 Set breakpoint at line number in current file
   break *0x40001234 Set breakpoint at address of instruction
info break List all breakpoints
delete NUM Delete breakpoint NUM. With no argument deletes all.
Executing  
step Execute one line of C source, step into function call
next Execute one line of C source, step over function call
stepi Execute one assembly instruction
nexti Execute one assembly instruction, step over jal (function call)
  Note: All of above accept optional argument N to repeat N times, i.e. step 5
until LINE Execute up to line number LINE
finish Execute up to end of current function
continue Continue execution
Registers  
info registers Show info about all registers
print $a0 Access individual register by $name
set $a0 = 8 Set register's value
Stack  
backtrace Show current stack backtrace
info frame Show info about current stack frame
up Select stack frame that called this one
down Select stack frame called by this one
Viewing code  
list Show C source for currently executing function
   list sum Show C source for named function
   list 45 Show C source around line number in current file
   list other.c:45 Show C source around line number in specified file
disassemble Show disassembly of currently executing function
   disassemble sum Show disassembly of named function
   disassemble 0x40001234 Show disassembly of instruction at address
print/x $pc Print program counter in hex
Print  
print EXPR Evaluate EXPR, print result, optional format modifier after slash
   print/d *ptr Print in decimal format
   print/x (val & 0xff) Print in hex format
   print/t mask Print in binary format
display EXPR Auto-print EXPR each time execution stops
   display/d i Auto-print accepts same format modifiers as print above
info display List all auto-printed expressions
undisplay NUM Delete auto-printed expression NUM. With no argument deletes all.
Examine memory  
x/FMT ADDR Examine memory starting at address ADDR. FMT is repeat count, format letter, and size letter.
  Size letters: b 1-byte, h 2-byte, w 4-byte, g 8-byte
  Format letters: d decimal, x hex, t binary, cchar, s string, i instruction
   x/1gx $sp Examine one 8-byte value, show in hex, address is top of stack
   x/2wt 0x02000030 Examine two 4-byte values, show in binary
   x/10bc ptr Examine ten 1-byte values, show as ASCII

Additional resources for learning gdb