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 a tremendous amount of 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 actions. 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 a wizard at using gdb is a major superpower for a programmer to have!

Pro-tip The table below lists the full name of the command but most can be abbreviated to save your overworked fingers. For example, info registers can be invoked as i r, break can be invoked b, print/x is 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()
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 function
   break 13 Set breakpoint at line number
   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 argument N to repeat N actions, 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 Can 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
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
   print/d *ptr Evaluate and print result in decimal
   print/x (val & 0xff) Evaluate and print result in hex
   print/t mask Evaluate and print result in binary
display EXPR Auto-print EXPR each time execution stops
   display/d i Auto-print uses 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