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
-gflag tellsgccto generate source-level debugging information. - The
-ggdbflag tellsgccto generate more debugging information forgdb. - The
-Ogflag tellsgccto optimize your code without affecting debugging.
gcc hello.c -g -ggdb -Og
Tips of Starting GDB
- The
--tuiflag tellsgdbto start in the TUI (Text User Interface) mode. - The
-qflag tellsgdbnot to print version info on startup. - The
-xoption tellsgdbto execute some commands (.gdbfiles only in the past, now even.pyfiles 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
runorr: start to run the program (run to complete unless met a breakpoint)- You can also put args after
runif you don't useset argsto set them before, e.g.run arg1 arg2 arg3
- You can also put args after
kill: stop running the program
file program: loadprogramand start to debug itquit: exit the debuggerset print pretty on: make the outputs prettier
Stopping and Rerunning Commands
breakorb: 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.cbreak main: set a breakpoint to stop at the beginning of themainfunctionbreak: stop at the current linebreak *0x400522: stop at a specific address (0x400522here)
tbreakortb: set a temporary breakpointbreak ... if ...: set a conditional breakpoint
watch a: stop when the value of variableachanged
clear: clear a breakpointclear main: remove the breakpoint for themainfunctionclear hello.c:10: remove the breakpoint at line 10 ofhello.c
info breakpointori 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 tofilesource file: load breakpoints fromfile
stepors: step forward one line of code (goes into functions)step 2: step forward two line of code (goes into functions)stepiorsi: step a single assembly instruction forward (goes into functions)
nextorn: step forward one line of code (does not go into functions)next 2: step forward two line of code (does not go into functions)nextiorni: step a single assembly instruction forward (does not go into functions)
finishorfin: continue running until the current function finishedreturn: stop running the current function and return immediatelyreturn expression: stop running and return the value ofexpressionas the function's return value
continueorc: continue running until the next breakpoint
Inspecting Variable Values
-
print aorp 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: printsnmemory values of length unitustarting fromaddrinfformat, wheref:xfor hex output andofor oct output, etc. In particular,iis for instruction output andsasks gdb to print a string.u:bfor byte,hfor half word (two bytes),wfor word (four bytes), andgfor giant word (eight bytes)
Tracing
backtraceorbt: 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