GDB Cheatsheet

Memory and Registers

Use this GDB reference while you build software engineering projects, review code, or refresh the syntax you reach for most.

x, the Examine Command

x/NFU addr reads raw memory and formats it, ignoring the declared type. The three parts are a repeat count, a format letter, and a unit size.

x/NFU addr
  N  repeat count, decimal, default 1, negative reads backward
  F  format letter
  U  unit size
UnitSize
bbyte, 1
hhalfword, 2
wword, 4, the initial default
ggiant word, 8
FormatReads as
xhex, the initial default
d / usigned / unsigned decimal
o / toctal / binary
ccharacter
ffloating point
aaddress plus nearest symbol
snull-terminated string, unit defaults to b
imachine instruction, unit ignored
mmemory tags, on architectures that have them
(gdb) x/4xw $sp          # 4 words of the stack in hex
0x7fffffffe1c0: 0x00000001 0x00000000 0xffffe2a8 0x00007fff
(gdb) x/16xb buf         # 16 raw bytes
(gdb) x/s name           # as a C string
0x555555556004: "api"
(gdb) x/8xg $rsp         # 8 pointer-sized words
(gdb) x/5i $pc           # next 5 instructions, => marks the pc
(gdb) x/40a $sp          # stack words resolved against symbols
(gdb) x/-3uh 0x54320     # three halfwords BEFORE that address
(gdb) x/7                # continue where the last x left off

Order does not matter between the format and the unit, so x/4xw and x/4wx are identical, but the count must come first. x remembers the format and unit you last used, and remembers the next address, so a bare x/7 keeps scanning. Enter on an empty line repeats with the same count.

(gdb) p $_              # last address x examined
(gdb) p $__             # the contents at that address
(gdb) x/4xw &arr        # take the address, arr is not a pointer

Writing Memory and Searching It

(gdb) set {int}0x555555558010 = 7      # write 4 bytes at an address
(gdb) set var *(char *) buf = 'X'
(gdb) set var buf[0] = 0

(gdb) find &main, +0x10000, 0x554889e5     # search for a byte pattern
(gdb) find /w &data, +4096, 0xdeadbeef     # search word-sized
(gdb) find /b &buf, +256, 'A', 'B', 'C'
(gdb) find &start, &end, "needle"
CommandEffect
set {TYPE}ADDR = valwrite, using TYPE for the width
find start, +len, patternsearch a range, /b /h /w /g set the width
find /1 start, end, patstop after the first match
dump binary memory f.bin S Ewrite memory from S to E into a file
restore f.bin binary ADDRread a raw file back in at ADDR
compare-sectionscheck the loaded image against the executable

dump binary memory is how you pull a corrupted buffer out for inspection with xxd or a parser, without scrolling thousands of x lines.

Registers

(gdb) info registers        # i r, the general-purpose set
rax            0x2a                42
rbx            0x0                 0
rsp            0x7fffffffe1c0      0x7fffffffe1c0
rip            0x555555555149      0x555555555149 <main+16>
eflags         0x246               [ PF ZF IF ]

(gdb) i r rax rdi rsi       # only these
(gdb) i r $eflags
(gdb) info all-registers    # plus SSE, AVX, and the FPU
(gdb) info registers vector # one register group
(gdb) p/x $rip
(gdb) p $pc                 # architecture-neutral aliases
(gdb) set var $rax = 0
CommandShortShows
info registersi rinteger and status registers
info registers rax rbxi r rax rbxnamed registers only
info all-registers—every register, including vector and float
info registers general—one register group, see maint print reggroups
p $pc p $sp p $fp—portable names for pc, stack and frame pointer
p $rax—the x86-64 register by its own name

Register names in expressions always take a $. Without it, p rax looks for a program symbol called rax and fails. In an older frame the registers shown are the saved values gdb unwound, so writing to them in frame 3 is not meaningful.

Disassembling

(gdb) disassemble               # the current function
(gdb) disassemble main
(gdb) disassemble /s main       # interleaved with source, in source order
(gdb) disassemble /r main       # plus raw bytes
(gdb) disassemble /b main       # raw bytes, one per instruction line
(gdb) disassemble 0x401136, 0x401180
(gdb) disassemble 0x401136, +32
(gdb) x/20i $pc-8
CommandEffect
disassemblethe function containing the pc, => marks it
disassemble /sinterleave source lines, in program order
disassemble /minterleave source, in source-line order, deprecated
disassemble /rshow the raw instruction bytes too
set disassembly-flavor intelIntel syntax instead of AT&T
set disassemble-next-line ondisassemble automatically at every stop
set disassembler-options no-aliasespass options through to the disassembler
info line *0x401136which source line an address belongs to

Prefer /s over /m. /m walks source lines in order and cannot represent reordered or inlined code, which is exactly the situation where you are reading disassembly.

Addresses, Symbols, and the Memory Map

(gdb) info symbol 0x555555555149
main + 16 in section .text of /home/me/prog
(gdb) info address total
Symbol "total" is a variable at frame base reg $rbp offset 16+-20.
(gdb) info line prog.c:14
(gdb) p/a $pc
(gdb) info proc mappings
process 4711
Start Addr   End Addr   Size    Offset  Perms  objfile
 0x555555554000 0x555555555000 0x1000 0x0     r--p  /home/me/prog
 0x7ffff7d8b000 0x7ffff7db3000 0x28000 0x0    r--p  /lib/.../libc.so.6
 0x7ffffffde000 0x7ffffffff000 0x21000 0x0    rw-p  [stack]
CommandShows
info symbol ADDRwhich symbol and section an address falls in
info address SYMwhere a symbol lives, register or memory or frame offset
info proc mappingsthe full /proc/pid/maps view, Linux only
info proc cmdline / cwd / exehow the process was started
info proc status / statthe raw /proc status fields
info filessections of every loaded object, with load addresses
info sharedlibrarywhich libraries are mapped and whether symbols are read
maint info sectionsevery section, including gdb's internal view

info proc mappings is the fastest way to decide what a wild pointer was. If the address falls in an r--p region it was a write to read-only data, and if it falls in no region at all it was never mapped. info symbol on the crash address usually names the function or the global that got overrun.