Starting GDB
gdb - Start GDB with the specified program.
gdb - Start GDB with no program specified; you can load a program later.
gdb -p - Attach GDB to a running process with the specified PID.
Basic Commands
run [args] - Start the program with optional arguments.
quit - Exit GDB.
help - Show help for GDB commands.
info - Display information about various types (e.g., info registers).
list - Display source code around the current line or function.
next - Step over to the next line of code.
step - Step into the next function call.
continue - Resume program execution until the next breakpoint.
finish - Continue execution until the current function returns.
Breakpoints
break - Set a breakpoint at the specified location (e.g., break main or break file.c:10).
delete - Delete the breakpoint with the specified number.
clear - Clear a breakpoint at the specified location.
info breakpoints - List all breakpoints and their statuses.
disable - Disable the breakpoint with the specified number.
enable - Enable the breakpoint with the specified number.
Inspecting Variables
print - Evaluate and display the value of an expression.
display - Display the value of an expression each time the program stops.
undisplay - Stop displaying the expression with the specified number.
info locals - Display the values of local variables in the current function.
info args - Display the values of function arguments.
Stack Frames
backtrace - Display the call stack.
frame - Select a stack frame by its number.
up - Move up the call stack to the previous frame.
down - Move down the call stack to the next frame.
Code Execution
return - Return from the current function with the specified value.
jump - Jump to a specified line or function.
Watchpoints
watch - Set a watchpoint for the specified expression.
rwatch - Set a read watchpoint for the specified expression.
awatch - Set an access watchpoint for the specified expression.
delete - Delete the watchpoint with the specified number.
Threads
info threads - List all threads.
thread - Switch to the thread with the specified number.
thread apply all - Apply a command to all threads (e.g., thread apply all bt).
Signals
handle - Set how GDB handles a signal (e.g., handle SIGINT nostop).
info signals - Display the current signal handling settings.
Files
file - Load a different executable file into GDB.
symbol-file - Load debugging symbols from a specified file.
Scripts
source - Execute GDB commands from a script file.
Other
set - Set a GDB variable to a specified value.
show - Display the value of a GDB variable.
record - Start recording the program's execution for replay.
restore - Restore the program's state from a previously recorded state.
Common Usage Patterns
- Debugging Segmentation Faults:
- Run the program:
run
- If it crashes, check the backtrace:
backtrace or bt
- Inspect local variables:
info locals and function arguments: info args
- Examine memory at a pointer:
x/ (e.g., x/16xb ptr)
- Setting Conditional Breakpoints:
- Break at a line or function only when a condition is true:
break if
- Example:
break main.cpp:42 if x > 10
- Inspecting Memory:
- Examine memory at an address:
x/
- Format examples:
x/16xb (16 bytes in hex), x/8xw (8 words in hex)
- Print pointer contents recursively:
print *ptr
- Debugging Loops:
- Set a breakpoint inside the loop:
break
- Use
commands to run commands automatically when hitting a breakpoint (e.g., print a variable, continue)
- Catch Exceptions or Signals:
- Stop when a signal occurs:
handle SIGSEGV stop
- Stop when a C++ exception is thrown:
catch throw
- Debugging Multi-threaded Programs:
- List threads:
info threads
- Switch thread:
thread
- Apply commands to all threads:
thread apply all bt
- Logging and Automation:
- Log GDB session to a file:
set logging on
- Run a script of commands:
source script.gdb