Skip to content
Pwnversity
REV-DEBUGintermediate · 5 min · upd. 2026-10

Debugging with GDB

GDB (GNU Debugger) is the central tool for dynamic analysis on Linux: it lets you run a program step by step, stop it at specific points (breakpoints), inspect and modify registers and memory, and see exactly what it does at each moment. For pwn and reversing it’s essential: with GDB you confirm offsets, see the stack/heap state, debug your exploits, and understand code that’s unreadable statically. Powered with pwndbg or GEF, it becomes an exploitation swiss-army knife.

GDB controls another process’s execution using the ptrace syscall: it can stop it, read and write its memory and registers, and intercept signals. On that base, pwndbg/GEF add exploit-oriented views (registers, stack, heap, disassembly) that turn GDB’s spare output into a useful dashboard.

Bare GDB is awkward; pwndbg or GEF add automatic context at each stop:

  • pwndbg (github.com/pwndbg/pwndbg) — register/stack/heap views, pwn commands (heap, bins, rop, cyclic).
  • GEF (github.com/hugsy/gef) — a very complete alternative with equivalent commands.
flowchart LR
    A["gdb ./bin / attach"] --> B["break at function/address"]
    B --> C["run / continue"]
    C --> D["stop at the breakpoint"]
    D --> E["inspect: i r, x/..., disas"]
    E --> F{"continue?"}
    F -->|"step by step"| G["ni / si / finish"]
    G --> E
    F -->|"modify state"| H["set $reg / set {type}addr"]
    H --> C
  • Start: gdb ./binary, gdb -p <pid> (attach), run [args] (r), starti (stop at the 1st instruction).
  • Breakpoints: break main (b main), b *0x401234, b *main+42; info breakpoints, delete <n>.
  • Execution: continue (c), next (n), step (s); at instruction level nexti (ni) and stepi (si); finish (run until the current function returns).
  • Inspection: info registers (i r), p $rip, disassemble func (disas).

The x command follows the format x/<count><format><size> address:

  • Format: x hex, d decimal, s string, i instruction, c char.
  • Size: b byte, h 2, w 4, g 8 (giant).
Ventana de terminal
x/20gx $rsp # 20 qwords in hex from the stack pointer
x/s $rdi # the string pointed to by RDI (e.g. a function argument)
x/5i $rip # the next 5 instructions
from pwn import *
context.binary = './vuln'
# launch with GDB attached and breakpoints from the exploit itself
io = gdb.debug('./vuln', gdbscript='''
break *main+120
continue
''')
# or attach to an already-running process:
# io = process('./vuln'); gdb.attach(io, 'b *0x401234')

To confirm an overflow offset by hand: run < <(cyclic 200) and, on the crash, cyclic -l $rsp (pwndbg). pwntools’s gdb.debug/gdb.attach integrate GDB with your script: you develop the exploit while watching memory at each step.

In crackmes, GDB lets you follow logic that’s hard to read statically and modify state live:

Ventana de terminal
b *check_function
# step forward with ni/si watching registers and memory
# the correct password often appears in a register/memory right before the cmp
set $rax = 1 # force a result
set {int}0x404050 = 0 # change a value in memory
set $rip = 0x401200 # skip a check by changing the flow
  • Watchpoints: watch *0x404050 (stop when a variable/memory changes).
  • Conditional breakpoints: b func if $rdi == 0x1234.
  • Backtrace: bt (call stack); frame <n> to switch frames.

Malware and protected software detect GDB/debuggers (ptrace(PTRACE_TRACEME), timing checks, searching for 0xCC bytes) to hinder dynamic analysis — covered in Anti-debugging and Anti-analysis. The analyst bypasses it by patching the check, with LD_PRELOAD of a fake ptrace, or with plugins that hide the debugger.

  • Install pwndbg/GEF and understand the context they show
  • Breakpoints by function, address, and offset
  • Step-by-step execution (ni/si/finish/continue)
  • Examine memory with x (formats and sizes)
  • View and modify registers/memory/RIP live
  • Confirm overflow offsets (cyclic)
  • Integrate GDB with pwntools (gdb.debug/attach)
  • Use watchpoints and conditional breakpoints