My GDB Cheat Sheet
The GNU debugger is a powerful tool for the debugging of (mainly) C/C++ programs. In this post I document the common operations of gdb and some useful learning resources.
Before Debugging
Tips of Compiling
- The
-g
flag tellsgcc
to generate source-level debugging information. - The
-ggdb
flag tellsgcc
to generate more debugging information forgdb
. - The
-Og
flag tellsgcc
to optimize your code without affecting debugging.
gcc hello.c -g -ggdb -Og
Tips of Starting GDB
- The
--tui
flag tellsgdb
to start in the TUI (Text User Interface) mode. - The
-q
flag tellsgdb
not to print version info on startup. - The
-x
option tellsgdb
to execute some commands (.gdb
files only in the past, now even.py
files are supported) on startup.
gdb ./a.out --tui -x debug.py -q
The example debug.py
:
import gdb
def on_quit():
gdb.execute('kill')
gdb.events.exited.connect(on_quit)
gdb.execute('...')
gdb.Breakpoint('...')
GDB Commands
Basic Commands
set args arg1 arg2 arg3
: set command line arguments of the program to be executedset args
: remove all command line arguments set beforeshow args
: show the current command line arguments
run
orr
: start to run the program (run to complete unless met a breakpoint)- You can also put args after
run
if you don’t useset args
to set them before, e.g.run arg1 arg2 arg3
- You can also put args after
kill
: stop running the program
file program
: loadprogram
and start to debug itquit
: exit the debuggerset print pretty on
: make the outputs prettier
Stopping and Rerunning Commands
break
orb
: set a breakpointbreak 10
: set a breakpoint to stop at line 10 of the current filebreak hello.c:10
: set a breakpoint to stop at line 10 ofhello.c
break main
: set a breakpoint to stop at the beginning of themain
functionbreak
: stop at the current linebreak *0x400522
: stop at a specific address (0x400522
here)
tbreak
ortb
: set a temporary breakpointbreak ... if ...
: set a conditional breakpoint
watch a
: stop when the value of variablea
changed
clear
: clear a breakpointclear main
: remove the breakpoint for themain
functionclear hello.c:10
: remove the breakpoint at line 10 ofhello.c
info breakpoint
ori break
: show all breakpoints
disable 2
: don’t stopPrints n memory values of length unit u starting from addr in f format: at breakpoint #2 but keep it thereenable 2
: stop at breakpoint #2 againdelete 2
: remove breakpoint #2
save breakpoints file
: save breakpoints tofile
source file
: load breakpoints fromfile
step
ors
: step forward one line of code (goes into functions)step 2
: step forward two line of code (goes into functions)stepi
orsi
: step a single assembly instruction forward (goes into functions)
next
orn
: step forward one line of code (does not go into functions)next 2
: step forward two line of code (does not go into functions)nexti
orni
: step a single assembly instruction forward (does not go into functions)
finish
orfin
: continue running until the current function finishedreturn
: stop running the current function and return immediatelyreturn expression
: stop running and return the value ofexpression
as the function’s return value
continue
orc
: continue running until the next breakpoint
Inspecting Variable Values
-
print a
orp a
: print value of variablea
(which must be in the current function)print/x a
: print value of a as a hexadecimal numberprint/o a
: print value of a as a octal numberprint/t a
: print value of a as a binary number (show all bits)print/s a
: print value of a as a string even if it is not oneprint *arr@10
: print the first ten elements of arrayarr
x/nfu addr
: printsn
memory values of length unitu
starting fromaddr
inf
format, wheref
:x
for hex output ando
for oct output, etc. In particular,i
is for instruction output ands
asks gdb to print a string.u
:b
for byte,h
for half word (two bytes),w
for word (four bytes), andg
for giant word (eight bytes)
Tracing
backtrace
orbt
: show the backtrace
Assembly and Registers
-
layout src
: switch to standard layout (of TUI mode) — source on top, and command window on the bottom -
layout asm
: assembly on top, and command on the bottom -
layout split
: source on top, assembly in the middle, and command at the bottom -
layout reg
: open the register window on top of either source or assembly -
tui reg general
: show the general registers -
tui reg float
: show the floating point registers -
tui reg system
: show the “system” registers -
tui reg next
: show the next page of registers -
set disassembly-flavor [intel / att]
: set the look-and-feel of the disassembly