GDB Commands - Debugging with GDB Cheat Sheet

Master GDB debugging with our comprehensive cheat sheet. Find essential commands for setting breakpoints, stepping through code, inspecting variables, and more.

GDB Debugger Commands

This cheat sheet provides essential commands for using the GNU Debugger (GDB) to debug C, C++, and other compiled programs. GDB is a powerful command-line debugger that allows you to inspect and control the execution of your programs.

Starting and Exiting GDB

Learn how to launch GDB with your executable or a running process, and how to exit the debugger gracefully.

# To start the debugger:
gdb <executable>
gdb --args <executable> [<args>...]
gdb -p <pid>

# To exit gdb (after program terminated):
q, quit

Setting Breakpoints

Control program execution by setting breakpoints at specific functions, lines, or addresses.

# To set a breakpoint at a function:
b, break <function>

# To set a (conditional) breakpoint at a general location:
b, break <loc> [if <condition>]
  with <loc> as <function>|<file>:<line>|<line>|*<address>|-offset|+offset

# To set a watchpoint, i.e., stop when expression changes its value:
watch <expr>

# To show all breakpoints and watchpoints:
info breakpoints, info watchpoints

# To delete all or given breakpoints and watchpoints:
clear, delete
delete <num>
clear <loc>
  with <loc> as <function>|<file>:<line>|<line>|*<address>|-offset|+offset

# To disable a breakpoint:
dis, disable <num>

Running and Controlling Execution

Execute your program, step through code line by line, and continue execution.

# To run the program, optionally with arguments for the program:
r, run [<args>]

# To run the current line, stepping over any invocations:
n, next

# To run the current line, stepping into any invocations:
s, step

# To run until the next line below, i.e., without new loop iteration:
u, until

# Like run, but stop at the first machine instruction:
starti

# To step or go to next line by a machine instruction
si, stepi
ni, nexti

# To run until given location is reached
advance <loc>
  with <loc> as <function>|<file>:<line>|<line>|*<address>|-offset|+offset

# To run until function ends, i.e., jump out of a function:
fin, finish

# To continue execution:
c, continue

Inspecting Program State

Examine the call stack, variables, registers, and memory contents.

# To print a stacktrace, optionally with local variables:
bt, backtrace [full]

# Move to stack frame of given number or a frame up or down:
frame <num>, up, down

# To show summary info on selected frame:
info frame

# To print the arguments or local variables to the current function:
info args, info locals

# To print the registers:
info registers

# To print a list of all the threads:
info threads

# To print a list of all the loaded shared libraries:
info sharedlibrary

# To evaluate an expression and print the result:
p length=strlen(string)

# To print an array of given length:
p *<array>@<len>

# Examine content at address with a format and letter size:
x/<count><fmt><size> <address>
    ------- <fmt> ------    ------- <size> -------
    a | Address             b | 8 bits  - byte
    i | Instruction         h | 16 bits - halfword
    t | Binary              w | 32 bits - word
    o | Octal               g | 64 bits - giant
    h | Hex
    z | Hex, zero padded
    d | Decimal
    u | Unsigned decimal
    f | Float
    c | Char
    s | String

Disassembly and Source Code

View source code and machine instructions to understand program flow at a lower level.

# To list surrounding source code:
l, list

# Disassemble the current or given function:
disas, disassemble [/m] [<func>]
  with /m to show mixed source

# Disassemble the given address range:
disas <start>,<end>
disas <start>,+<length>

GDB Variables and TUI Mode

Utilize GDB's internal variables and the Text User Interface (TUI) for enhanced debugging experience.

# To specify a given register, program counter, frame pointer, stack pointer
$reg, $pc, $fp, $sp

# To enable pretty print of arrays and more:
set pretty print on

# To toggle TUI mode:
C-x C-a, C-x a, C-x A

# To use a TUI layout with only one window:
C-x 1

# To use a TUI layout with two windows:
C-x 2

# To change active window:
C-x o

# To Switch in and out of the TUI SingleKey mode:
C-x s

Further Resources

Explore official documentation and community resources for more in-depth information on GDB.