You are currently viewing GDB Cheat Sheet

GDB Cheat Sheet

Running

Bash
gdb <program> [core dump]               # Start GDB (with optional core dump).
gdb --args <program> <args>            # Start GDB and pass arguments.
gdb --pid <pid>                         # Start GDB and attach to process.
set args <args...>                      # Set arguments to pass to the program to be debugged.
run                                     # Run the program to be debugged.
kill                                    # Kill the running program.

Breakpoints

Bash
break <where>                           # Set a new breakpoint.
delete <breakpoint#>                    # Remove a breakpoint.
clear                                   # Delete all breakpoints.
enable <breakpoint#>                    # Enable a disabled breakpoint.
disable <breakpoint#>                   # Disable a breakpoint.

Watchpoints

Bash
watch <where>                           # Set a new watchpoint.
delete/enable/disable <watchpoint#>     # Like breakpoints.
<where>                                 # Break/watch the named function, line number, or file:line_number.

Conditions

Bash
break/watch <where> if <condition>      # Break/watch at the given location if the condition is met.
condition <breakpoint#> <condition>     # Set/change the condition of an existing break- or watchpoint.

Examining the Stack

Bash
backtrace                              # Show call stack.
where                                  # Show call stack.
backtrace full                         # Show call stack, also print the local variables in each frame.
where full                             # Show call stack, also print the local variables in each frame.
frame <frame#>                         # Select the stack frame to operate on.

Stepping

Bash
step                                   # Go to the next instruction (source line), diving into function.
next                                   # Go to the next instruction (source line) but don't dive into functions.
finish                                 # Continue until the current function returns.
continue                               # Continue normal execution.

Variables and Memory

Bash
print/format <what>                    # Print content of variable/memory location/register.
display/format <what>                  # Like "print," but print the information after each stepping instruction.
undisplay <display#>                   # Remove the "display" with the given number.
enable display <display#>              # Enable the "display" with the given number.
disable display <display#>             # Disable the "display" with the given number.
x/nfu <address>                        # Print memory. n: How many units to print (default 1). f: Format character. u: Unit.

GDB Format Specifiers

Bash
<what>                                 # Various format specifiers for different variable types.
expression                             # Almost any C expression, including function calls.
file_name::variable_name               # Content of the variable defined in the named file (static variables).
function::variable_name                # Content of the variable defined in the named function (if on the stack).
{type}address                           # Content at address, interpreted as being of the C type type.
$register                              # Content of named register.

Threads

Bash
thread <thread#>                       # Choose thread to operate on.

Manipulating the Program

Bash
set var <variable_name>=<value>         # Change the content of a variable to the given value.
return <expression>                     # Force the current function to return immediately, passing the given value.

Sources

Bash
directory <directory>                   # Add directory to the list of directories that are searched for sources.
list                                    # Shows the current or given source context.

Signals

Bash
handle <signal> <options>               # Set how to handle signals. Options are: (no)print, (no)stop, (no)pass.

Information Commands

Bash
disassemble                            # Disassemble the current function.
disassemble <where>                    # Disassemble the current function or given location.
info args                              # Print the arguments to the function of the current stack frame.
info breakpoints                       # Print information about the break- and watchpoints.
info display                           # Print information about the "displays".
info locals                            # Print the local variables in the currently selected stack frame.
info sharedlibrary                     # List loaded shared libraries.
info signals                           # List all signals and how they are currently handled.
info threads                           # List all threads.
show directories                        # Print all directories in which GDB searches for source files.
show listsize                           # Print how many are shown in the "list" command.
whatis variable_name                    # Print the type of named variable.

Table Of Commands

CategoryCommandDescription
Runninggdb <program> [core dump]Start GDB (with optional core dump).
gdb --args <program> <args…>Start GDB and pass arguments.
gdb --pid <pid>Start GDB and attach to process.
set args <args...>Set arguments to pass to the program to be debugged.
runRun the program to be debugged.
killKill the running program.
Breakpointsbreak <where>Set a new breakpoint.
delete <breakpoint#>Remove a breakpoint.
clearDelete all breakpoints.
enable <breakpoint#>Enable a disabled breakpoint.
disable <breakpoint#>Disable a breakpoint.
Watchpointswatch <where>Set a new watchpoint.
delete/enable/disable <watchpoint#>Like breakpoints.
<where>Break/watch the named function, line number, or file:line_number.
Conditionsbreak/watch <where> if <condition>Break/watch at the given location if the condition is met.
condition <breakpoint#> <condition>Set/change the condition of an existing break- or watchpoint.
Examining the StackbacktraceShow call stack.
whereShow call stack.
backtrace fullShow call stack, also print the local variables in each frame.
where fullShow call stack, also print the local variables in each frame.
frame <frame#>Select the stack frame to operate on.
SteppingstepGo to the next instruction (source line), diving into function.
nextGo to the next instruction (source line) but don’t dive into functions.
finishContinue until the current function returns.
continueContinue normal execution.
Variables and Memoryprint/format <what>Print content of variable/memory location/register.
display/format <what>Like “print”, but print the information after each stepping instruction.
undisplay <display#>Remove the “display” with the given number.
enable display <display#>Enable the “display” with the given number.
disable display <display#>Disable the “display” with the given number.
x/nfu <address>Print memory. n: How many units to print (default 1). f: Format character. u: Unit.
GDB Format Specifiers<what>Various format specifiers for different variable types.
expressionAlmost any C expression, including function calls.
file_name::variable_nameContent of the variable defined in the named file (static variables).
function::variable_nameContent of the variable defined in the named function (if on the stack).
{type}addressContent at address, interpreted as being of the C type type.
$registerContent of named register. Interesting registers are $esp (stack pointer), $ebp (frame pointer), and $eip (instruction pointer).
Threadsthread <thread#>Choose thread to operate on.
Manipulating the Programset var <variable_name>=<value>Change the content of a variable to the given value.
return <expression>Force the current function to return immediately, passing the given value.
Sourcesdirectory <directory>Add directory to the list of directories that is searched for sources.
listShows the current or given source context. The filename may be omitted. If last is omitted, the context starting at start is printed instead of centered around it.
Signalshandle <signal> <options>Set how to handle signals. Options are: (no)print, (no)stop, (no)pass.
Information CommandsdisassembleDisassemble the current function.
disassemble <where>Disassemble the current function or given location.
info argsPrint the arguments to the function of the current stack frame.
info breakpointsPrint information about the break- and watchpoints.
info displayPrint information about the “displays”.
info localsPrint the local variables in the currently selected stack frame.
info sharedlibraryList loaded shared libraries.
info signalsList all signals and how they are currently handled.
info threadsList all threads.
show directoriesPrint all directories in which GDB searches for source files.
show listsizePrint how many are shown in the “list” command.
whatis variable_namePrint the type of the named variable.
gdb Cheat Sheet

Leave a Reply