GDB Cheatsheet

DOCUMENT KEY
gdb
	The GNU Debugger

Purpose
	Inspect C++ programs at runtime.


COMPILATION

g++ -g [other flags and file names]
	Compiles a C++ program with debugging information.

STARTING GDB

gdb [file]
	Runs GDB and automatically loads binary [file].

gdb
	Runs GDB without loading a binary (see file [file])


USAGE
file [file]
	Loads the binary [file] into GDB (not necessary if the file is passed as a command-line argument.

help
	Prints common commands and help usage information.

help [command]
	Prints help information for [command].


quit
	Exits GDB.


EXECUTING CODE
run
	(Re)Starts program execution from the beginning.

continue
	Continues program execution from the current point in the program.

step
	Execute one instruction (steps into function calls)

next
	Execute one instruction (steps over function calls)



PAUSING PROGRAM EXECUTION

break [param]
	Sets a breakpoint to pause execution of the program where
	[param] may be specified as one of the following:
		line_number, file_name:line_number,
		function_name, or file_name:function_name

break [param] if [condition]
	Sets a conditional breakpoint.  E.g., "break 11 if i > 4" causes the program to break at line 11
	if the condition i > 4 is true (i is a variable in scope).

clear [param]
	Clears all breakpoints at line [param].
	[param] may be specified as one of the following:
		line_number, file_name:line_number,
		function_name, or file_name:function_name
	
EXAMINING PROGRAM STATE
print [variable]
	Prints the contents of a variable, where [variable] may be a variable name, a struct.member, or a pointer->target.

watch [variable]
	Adds a variable to the watchlist.

list
	Prints (10) lines of source code around current line.

list [line_num]
	Prints lines of source code around [line_num].
	[line_num] may also be specified as:
		line_number, file:line_number,
		function_name, file:function_name

list [first],[last]
	Lists code starting with line [first] and ending with line [last].

backtrace
	Prints the current callstack.



ALTERING PROGRAM STATE:
set var [variable]=[value]
	Modifies the value of [variable] by setting it equal to [value].

call [foo]
	Executes function [foo], where [foo] is any function linked into your program (your functions or included standard library functions).


EMACS INTEGRATION
M-x gdb
	Starts gdb in current buffer.

M-x gdb-many-windows
	Starts a fancy (IDE-like) session of gdb (gdb must already be running).
	

Joshua T. Guerin