Table of Contents

GDB Inspecting Memory

Memory inspection dumps raw bytes at addresses. The x command (examine) shows bytes in various formats. Useful for inspecting memory layout, stack contents, or raw pointer data.

(gdb) x/10x &variable           # show 10 hex values at address of variable
(gdb) x/10i $pc                 # show 10 instructions at program counter
(gdb) x/20x $sp                 # show 20 hex words on stack
(gdb) x/s ptr                   # print string at pointer
(gdb) x/d &x                    # show value at address (decimal)
(gdb) print sizeof(int)         # size of type
(gdb) print sizeof(arr)         # size of array
(gdb) print (char *)ptr         # cast and print

The x syntax is x/[count][format] address. Count is how many units to show (default 1). Format codes:

The unit size depends on format: x shows words (4 bytes by default on 32-bit, 8 on 64-bit); i shows full instructions; s reads until null terminator. Prefix format with size modifier: b (byte), h (halfword / 2 bytes), w (word / 4 bytes), g (giant / 8 bytes). Example: x/10bx shows 10 bytes in hex.

$pc (program counter) and $sp (stack pointer) are special registers. &variable gives the address of a local variable. Examine stack to see function arguments and return addresses; examine code memory to see instructions at an address.

sizeof() returns the byte size of a type or variable—useful for calculating array sizes or understanding structure layout.