GDB (GNU Debugger) is a powerful debugger for C, C++, and other programming languages. Here’s a cheat sheet to help you use GDB effectively:
Basic Commands
Start GDB:
gdb executable
Run Program:
run [args]
Set Breakpoint:
break function_name
break filename:line_number
Continue Execution:
continue
Step Into:
step
Next Line:
next
Finish Execution of Function:
finish
Print Variable Value:
print variable_name
Display Variable Value on Every Stop:
display variable_name
List Source Code:
list
Breakpoint Commands
Enable/Disable Breakpoint:
enable [breakpoint_number]
disable [breakpoint_number]
Delete Breakpoint:
delete [breakpoint_number]
Delete All Breakpoints:
delete
Watchpoints
Set Watchpoint:
watch variable_name
Show Watchpoints:
info watchpoints
Stack and Frame Commands
Backtrace (Print Call Stack):
backtrace
Select Frame:
frame [frame_number]
Memory Inspection
Examine Memory:
x/nfu address
n
: Number of units to display.f
: Display format (e.g.,x
for hexadecimal).u
: Unit size (e.g.,b
for byte).
Execution Control
Interrupt Execution:
Ctrl+C
Quit GDB:
quit
Source Navigation
Set Source File:
directory /path/to/source/files
Search for Source File:
directory /path/to/search
Miscellaneous
Load Core Dump:
gdb executable core
Set Variable Value:
set variable variable_name = value
Run GDB with Core Dump:
gdb -c core
Set Environment Variable:
set environment VARNAME=VALUE
This cheat sheet covers some of the fundamental GDB commands. For more detailed information and advanced features, refer to the official GDB documentation.