GDB Print Command Reference Guide

Basic Print Command

print variable_name
p variable_name          # Short form

Use /format after print to specify output format:

Hexadecimal

print/x variable         # Hex (lowercase)
print/x value           # Example: 0x5
p/x 255                 # Output: 0xff

Decimal

print/d variable         # Signed decimal
print/u variable         # Unsigned decimal
p/d 0xff                # Output: 255

Octal

print/o variable         # Octal format
p/o 64                  # Output: 0100

Binary

print/t variable         # Binary (t = "two")
p/t 5                   # Output: 101

Character

print/c variable         # As ASCII character
p/c 65                  # Output: 'A'

Floating Point

print/f variable         # Floating point
p/f 3.14159

Address/Pointer

print/a variable         # As address
p/a 0x555555555189      # Shows as address

String

print/s pointer          # Interpret as C string
p/s argv[1]

Working with Pointers

print pointer            # Shows address
print *pointer           # Dereference (shows value)
print &variable          # Shows address of variable

# Example
print argv               # Address of argv array
print *argv              # First element (argv[0])
print argv[1]            # Second element
print *argv[1]           # First char of argv[1]

Array and String Operations

# Print entire array
print buffer_one
print buffer_two

# Print specific elements
print buffer_one[0]
print buffer_one[3]

# Print array slice (if supported)
print buffer_one@8       # Print 8 elements starting at buffer_one

# View string with length
x/8c buffer_one          # First 8 chars
x/s buffer_one           # Until null terminator

Type Casting

# Cast to different types
print (int *)buffer_one          # Treat as int pointer
print *(int *)buffer_one         # Dereference as int
print (unsigned char)value       # Cast to unsigned char

# Example: View buffer as integers
print *(int *)&buffer_one
print *(long *)&buffer_two

Expressions and Calculations

# Arithmetic
print value + 10
print sizeof(buffer_one)
print strlen(buffer_one)

# Address calculations
print &buffer_one - &buffer_two
print (long)&value - (long)&buffer_one

# Pointer arithmetic
print argv[0]
print *(argv + 1)

Display Commands (Auto-Print)

Set up variables to display automatically after each step:

display variable         # Auto-print after each command
display/x value          # Auto-print in hex
display/8xb buffer_one   # Auto-print 8 bytes in hex

# Manage displays
info display             # List all displays
delete display 1         # Remove display #1
disable display 2        # Temporarily disable
enable display 2         # Re-enable

Useful Combinations

Stack Analysis

# View stack pointer and nearby memory
print $rsp
x/32xw $rsp              # 32 words from stack pointer
x/64xb $rsp              # 64 bytes from stack pointer

Registers

print $rax               # Print register value
print/x $rip             # Instruction pointer in hex
info registers           # All registers
info registers rax rbx   # Specific registers

Function Arguments

print argc
print argv
print argv[0]
print argv[1]
x/s argv[0]              # Program name as string
x/s argv[1]              # First argument as string

Side-by-Side Memory View

# Define a custom command
define show_buffers
    echo \n=== BUFFER_TWO ===\n
    x/8xb &buffer_two
    echo \n=== BUFFER_ONE ===\n
    x/8xb &buffer_one
    echo \n=== VALUE ===\n
    print/x value
    echo \n
end

# Use it
show_buffers

Advanced: Pretty Printing Structures

# Enable pretty printing
set print pretty on
set print array on
set print array-indexes on

# Print structure
print my_struct

# Print specific field
print my_struct.field_name

Quick Reference Table

CommandPurposeExample
p varPrint variablep value
p/x varPrint in hexp/x value
p/d varPrint in decimalp/d 0xff
p/t varPrint in binaryp/t 5
p/c varPrint as charp/c 65
p &varPrint addressp &value
p *ptrDereference pointerp *argv
x/8xb addr8 bytes in hexx/8xb &buffer
x/4xw addr4 words in hexx/4xw $rsp
x/s addrString at addressx/s buffer_one
x/10i addr10 instructionsx/10i main
display varAuto-displaydisplay/x value

Pro Tips

  1. Use tab completion: Type p buf then press TAB to complete variable names
  2. Repeat last command: Press ENTER to repeat the last command
  3. Command history: Use UP/DOWN arrows to navigate command history
  4. Save typing: Use p instead of print, x instead of examine
  5. Combine with info: info locals, info args, info registers
  6. Set convenience variables: set $myvar = value then print $myvar

Troubleshooting Print Issues

Variable not accessible

Error: No symbol "var" in current context

Solution: Make sure you’re at the right scope/breakpoint and compiled with -g

Optimized out

value = <optimized out>

Solution: Compile with -O0 flag (no optimization)

Address not mapped

Cannot access memory at address 0x0

Solution: Check if pointer is NULL or invalid