gdb Command
Launch the GNU Debugger to debug programs, analyze crashes, and examine program execution step by step for C/C++ applications.
Syntax
gdb [options] [program]
gdb [options] [program] [core-file]
gdb [options] --args program [program-args]
gdb [options] -p process-id
The gdb command launches the GNU Debugger, a powerful tool for debugging C/C++ programs, analyzing crashes, and examining program execution step by step.
Basic Usage
Start debugging a program
# Start debugging a program
gdb program_name
# Debug with core dump
gdb program_name core_file
# Attach to running process
gdb -p process_id
# Debug with command line arguments
gdb --args program_name arg1 arg2
Launch gdb with different target types for debugging
Basic debugging commands
# Set breakpoint at main function
(gdb) break main
# Start program execution
(gdb) run
# Continue execution
(gdb) continue
# Step into function
(gdb) step
# Step over function
(gdb) next
# Print variable value
(gdb) print variable_name
Essential commands for controlling program execution and examining state
Examine program state
# Show current line
(gdb) list
# Show call stack
(gdb) backtrace
# Show local variables
(gdb) info locals
# Show function arguments
(gdb) info args
# Show breakpoints
(gdb) info breakpoints
Commands to examine the current state of the program
Common Options
Program selection options
# Debug specific program
gdb program_name
# Debug with core dump
gdb program_name core_file
# Attach to running process
gdb -p 1234
# Debug with arguments
gdb --args program arg1 arg2
Choose the target program or process to debug
Output and interface options
# Quiet mode (no intro messages)
gdb -q program_name
# Batch mode (non-interactive)
gdb -batch program_name
# Command file execution
gdb -x commands.gdb program_name
# TUI mode (text user interface)
gdb -tui program_name
Control the interface and output behavior of gdb
Symbol and debugging options
# Read symbols from file
gdb -s symbol_file program_name
# Set working directory
gdb -d directory program_name
# Set environment variable
gdb -e environment program_name
Configure symbol loading and debugging environment
Practical Examples
Debugging a simple program
# Compile with debug symbols
gcc -g -o test_program test_program.c
# Start debugging
gdb test_program
# Set breakpoint and run
(gdb) break main
(gdb) run
# Examine variables
(gdb) print variable_name
(gdb) print array[0]
# Step through code
(gdb) next
(gdb) step
Basic debugging workflow for a C program
Analyzing core dumps
# Enable core dumps
ulimit -c unlimited
# Run program (it crashes)
./crash_program
# Analyze core dump
gdb crash_program core
# Examine crash location
(gdb) backtrace
(gdb) info registers
(gdb) print *pointer_variable
Use gdb to analyze program crashes and core dumps
Advanced debugging techniques
# Conditional breakpoints
(gdb) break function_name if condition
(gdb) break 15 if i > 10
# Watch variables
(gdb) watch variable_name
(gdb) watch *pointer_variable
# Execute commands at breakpoints
(gdb) commands breakpoint_number
> print variable_name
> continue
> end
Advanced debugging features for complex scenarios
Best Practices
gdb Best Practices
- Always compile with debug symbols (-g flag)
- Use meaningful breakpoint names and conditions
- Examine variables and memory carefully
- Use backtrace to understand crash context
- Set up watchpoints for critical variables
- Use command files for repetitive debugging tasks
Common Pitfalls
- Missing debug symbols - Programs compiled without -g are harder to debug
- Optimized code - -O2 or higher optimization can make debugging difficult
- Stripped binaries - Stripped executables lack symbol information
- Timing issues - Race conditions may not reproduce in debugger
- Environment differences - Debug environment may differ from production