GDB

The GNU Debugger (GDB) is widely used for debugging software on Linux and UNIX variants. This cheat sheet includes some common commands that can be useful when performing reverse engineering, or developing exploits.


Starting and Running a Program

CommandDescriptionExample
Start GDBLaunch GDB with the specified program.gdb ./a.out
Run ProgramStart the execution of the program inside GDB.run
Set Program ArgumentsSet command-line arguments for the program.set args foo bar

Breakpoints and Execution Control

CommandDescriptionExample
Set a BreakpointSet a breakpoint at a function or a line.break main
break myfile.c:42
List BreakpointsList all breakpoints.info breakpoints
Delete BreakpointDelete a breakpoint by number.delete 1
Disable BreakpointDisable a specific breakpoint.disable 1
Enable BreakpointRe-enable a specific breakpoint.enable 1
Conditional BreakpointSet a breakpoint with a condition.break main if x > 5
Continue ExecutionContinue the program after hitting a breakpoint.continue
Interrupt ExecutionInterrupt running program and return control to GDB.Ctrl + C

Stepping Through Code

CommandDescriptionExample
Step into a FunctionStep into a function (execute the first instruction).step
Step Over a FunctionStep over a function (execute the whole function).next
Step by InstructionStep through a single machine instruction.stepi
Step Over InstructionStep over a single machine instruction.nexti

Inspecting Program State

CommandDescriptionExample
Disassemble a Specific FunctionDisassemble the instructions of a function.disas main
Show Function Argument ValuesDisplay the values of function arguments.info args
Print VariablePrint the value of a variable or expression.print x
print 3 + 4
Print a Hexadecimal ValuePrint a variable as a hexadecimal value.print /x x
Auto-Display Variable ValueDisplay a variable automatically when program stops.display x
List Source CodeList source code around the current line.list
list 42
list main
Print Specific RegisterPrint the value of a specific register.print $rax
print $rbx
Inspect RegistersDisplay the contents of CPU registers.info registers
Inspect Local VariablesShow local variables in the current function.info locals
Show Stack ContentsView current stack information.info stack

Memory Inspection

Memory can be inspected using x followed by the number of units to display. A format modifier can then be supplied.

x/<count><format><size> <address>

The below table lists some common format modifiers.

ModifierDescriptionExample Usage
xHexadecimalx/16x 0x7fffffffe000 — 16 hexadecimal values
dSigned decimalx/16d 0x7fffffffe000 — 16 signed decimal values
uUnsigned decimalx/16u 0x7fffffffe000 — 16 unsigned decimal values
oOctalx/16o 0x7fffffffe000 — 16 octal values
tBinaryx/16t 0x7fffffffe000 — 16 binary values
fFloating-pointx/16f 0x7fffffffe000 — 16 floating-point values
aAddress (pointer)x/16a 0x7fffffffe000 — 16 pointer addresses
iInstruction (disassemble)x/16i 0x7fffffffe000 — 16 disassembled instructions

Memory can be examined using a direct address, or via a register. I.e x/4xw $rsp will inspect memory at the stack pointer.

Core Dump and Multi-Threading

CommandDescriptionExample
Start with Core DumpLoad a core dump file for debugging.gdb ./a.out core
Analyze Core Dump (Backtrace)Get a backtrace to identify where the crash occurred.backtrace
Show ThreadsShow all threads in the program.info threads
Switch to a Specific ThreadSwitch to a thread by ID.thread 2
Show Stack Trace for All ThreadsShow stack trace for all threads.thread apply all bt

Waypoints and Conditional Debugging

CommandDescriptionExample
Set WatchpointSet a watchpoint (break when a variable changes).watch x
Set Breakpoint on AddressSet a breakpoint at a specific memory address.break *0x7fffffffe000
Set Conditional BreakpointSet a breakpoint with a condition.break main if x > 5

Exploit Development Extensions

There are a number of useful extensions that can help speed up exploit development.

GEF – GDB Enhanced Features

GEF works well with multiple architectures, including MIPS and ARM64. It can be installed using;

bash -c "$(curl -fsSL https://gef.blah.cat/sh)"

PEDA – Python Exploit Development Assistance for GDB

Unfortunately, this doesn’t appear to have be maintained in a while, although it can still come in handy. PEDA can be installed with the following commands.

git clone https://github.com/longld/peda.git ~/peda
echo "source ~/peda/peda.py" >> ~/.gdbinit
echo "DONE! debug your program with gdb and enjoy"

pwndbg

Pwndbg is probably the most commonly used and feature rich exploit development environment. It can be installed using the following commands.

git clone https://github.com/pwndbg/pwndbg
cd pwndbg
./setup.sh