GDB (GNU Debugger) is a powerful debugging tool, a graphical debugger that is very useful when debugging programs locally, but it is meaningless for debugging on a server. However, GDB can be very useful. Below are the most commonly used GDB features, categorized by starting, running, breakpoints, step debugging, variable viewing, memory checking, and controlling flow:
1. Starting GDB
gdb <program_name> # Load the executable program (must be compiled with debug info -g)gdb -tui <program_name> # Enable TUI mode (with code window)gdb --args <program_name> arg1 arg2 # Pass command line arguments to the program being tested//gdb --args ./my_program arg1 arg2 arg3
2. Running the Program
run (r) [arguments] # Run the program, can include argumentsstart # Run to the main() function and pausekill # Terminate the running program
3. Breakpoints
break (b) <line_number> # Set a breakpoint at a specific linebreak (b) <function_name> # Set a breakpoint at the entry of a specific functionbreak (b) <file_name>:<line_number> # Set a breakpoint at a specific line in a filetbreak <line_number> # Temporary breakpoint, automatically deleted after hitclear <line_number> # Remove a breakpoint at a specific linedelete (d) [number] # Delete a specific breakpoint, delete directly deletes all breakpointsdisable [number] # Disable a breakpoint (will not trigger)enable [number] # Enable a breakpointinfo breakpoints (i b) # View all breakpoints
4. Step Debugging
next (n) # Step over, does not enter functionsstep (s) # Step into, enters functionsfinish # Finish executing the current function, return to the calleruntil (u) <line_number> # Run to a specific linestepi (si) # Step one machine instructionnexti (ni) # Execute the next instruction, does not enter functionsENTER (Enter) # Repeat the last command.
5. Continue Execution
continue (c) # Continue executing the program until a breakpoint is hitjump <line_number> # Jump directly to a specific line to continue execution (use with caution)signal <signal> # Send a signal to the program, e.g., signal SIGKILLjump *<address> # Jump to a specific memory address to executereturn <value> # Immediately return a value from the current function
6. Viewing Variables & Memory
print (p) variable_name # Print the value of a variableprint *pointer_variable # Dereference pointer to view contentwhatis variable_name # View variable typeptype variable_name # View variable struct informationinfo locals # View all local variables in the current functioninfo args # View parameters of the current functionx/<format> <address> # Directly view memory, e.g., x/10xw $rspdisplay variable_name # Automatically print variable each time it stopsundisplay variable_name # Cancel automatic printing
Example:
p x # View variable xp *p # View the value pointed to by pointer p p array[3] # View the 3rd element of the array x/10xb &array # View memory layout of the array (10 bytes, displayed in hexadecimal)
In GDB, the x command is used to view memory content, with the basic format:
<span>x/nfu address</span>
-
<span>n</span>
: Number of memory units to view. -
<span>f</span>
: Display format (e.g., x for hexadecimal, d for decimal, c for character, s for string). -
<span>u</span>
: Size of memory units (e.g., b for byte, h for half-word (2 bytes), w for word (4 bytes), g for giant word (8 bytes)). -
<span>address</span>
: Memory address to view.
7. Stack and Call Information
ulimit -c unlimited # Allow core dump files to be generatedgdb <program> core # Load core file for analysisbacktrace (bt) # Display current call stack (function call path)frame (f) <number> # Switch to a specific stack frameinfo frame # View current frame informationinfo registers # Display all register information