Start and attach
gdb <prog>
Load a program
gdb --args <prog> <args>
Load a program with arguments
gdb -p <pid>
Attach to a running process
Often needs root or ptrace permission
(kernel.yama.ptrace_scope)
gdb <prog> <core>
Inspect a core dump
gdb -q -batch -ex 'bt' <prog>
<core>
Non-interactive: print a backtrace and exit
run [args]
Start the program
start
Run and stop at the start of main
starti
Run and stop at the first instruction
set args <args>
Set arguments for later runs
run < <file>
Redirect input as in a shell
attach <pid>
Attach from inside GDB
detach
Release the process and keep it running
kill
Kill the program being debugged
set env <VAR>=<val>
Set an environment variable for the program
set follow-fork-mode child
Follow the child after fork
set detach-on-fork off
Keep debugging both parent and child
file <prog>
Load symbols and executable
shell <cmd>
Run a shell command
Breakpoints
break <func>
Break at a function
break <file>:<line>
Break at a line of a file
break <line>
Break at a line of the current file
break *<addr>
Break at an address
break <loc> if <cond>
Break only when the condition holds
tbreak <loc>
One-shot breakpoint, deleted after first stop
rbreak <regex>
Break on all functions matching the regex
hbreak <loc>
Hardware-assisted breakpoint
info breakpoints
List breakpoints and watchpoints
delete [<n>]
Delete one breakpoint, or all
disable <n> / enable <n>
Turn a breakpoint off / on
condition <n> <expr>
Set or change a breakpoint condition
ignore <n> <count>
Skip the next hits of a breakpoint
commands <n>
Attach commands run when it hits (end with 'end')
clear <loc>
Delete breakpoints at a location
catch syscall <name>
Stop on a system call
catch throw
Stop when a C++ exception is thrown
handle <SIG> nostop noprint pass
Control how a signal is treated
Run control
continue [<n>]
Resume; optionally skip n-1 further hits here
next [<n>]
Step over calls to the next line
step [<n>]
Step into functions with debug info
finish
Run until the current function returns
Prints the returned value
until
Finish the loop without stepping through it
until <loc>
Run to a location in this frame
advance <loc>
Run to a location or until the frame returns
stepi / nexti
One machine instruction
jump <loc>
Resume execution at another location
return [<expr>]
Pop the frame now, optionally with a value
call <func>(<args>)
Call a function in the program
Ctrl+C
Interrupt the running program
reverse-step
Step backward (after 'record')
Inspect data
print <expr>
Evaluate and show an expression
print/x <expr>
Print in hex (also d, o, t, c, a)
print *<ptr>
Dereference a pointer
print <arr>@<n>
Show n elements from the first one
print $<n> / print $
Reuse an earlier value / the last one
set var <var> = <val>
Change a variable
display <expr>
Show an expression at every stop
undisplay <n>
Remove an auto-display
x/<n><f><u> <addr>
Examine memory
Format x d s i c; unit b h w g, e.g. x/8xw $sp or x/s
<str>
x/<n>i $pc
Disassemble n instructions at the PC
ptype <expr-or-type>
Show the full type definition
whatis <expr>
Show the type of an expression
info registers
Show general-purpose registers
print $<reg>
Read one register (e.g. $rip)
disassemble [/s]
Disassemble the current function (/s adds source)
set print pretty on
Print structs one field per line
info line <loc>
Address range of a source line