Note: In order for gdb to interpret the program, you need to compile with debugging options on. With g++, this means passing the -g flag. It may also be beneficial to compile with the -O0 (non)optimization flag. With ccmake, this means setting CMAKE_CXX_FLAGS_DEBUG to -g -O0 and setting CMAKE_BUILD_TYPE to DEBUG.

Note also that if you need to step into any library functions, the library must also be compiled with debugging flags on.

# Start a program
$ gdb ./MyProgram

# Run a program
(gdb) run [arguments]
# r for short
# Specify arguments here

# Run a program, but break at main
(gdb) start

# Toggle curses window
# Ctrl + x, a

# Quit gdb
(gdb) quit # q

# Kill a program
(gdb) kill # k

# Setting breakpoints
# NB: tbreak (tb) can be used in place of be to set a temporary breakpoint
# Temporary breakpoints only stop the program once, and then are removed
(gdb) break 10 # Set a breakpoint at line 10 of the current file
(gdb) b 10 # For short
(gdb) b file.cxx:10 # Set a breakpoint at line 10 of file.cxx
(gdb) b myFunction # Set a breakpoint at the function myFunction
(gdb) b TestClass::TestMethod(int) # Set a breakpoint for a class method

# Watchpoints are set for a particular variable
# They allow the value to be printed out whenever it is read/written
(gdb) watch var # A *write* watchpoint
(gdb) rwatch var # A *read* watchpoint
(gdb) awatch var # A *read/write* watchpoint

# Get info about...stuff.
(gdb) info b # Print out a table of breakpoints (and watchpoints)
(gdb) info locals # Print out local variables and values
(gdb) info args # Print a list of arguments to the current function

# Get a backtrace
(gdb) backtrace # bt

# Get stack frame
(gdb) frame

# Delete a specific breakpoint, <n>
(gdb) delete <n> # A specific breakpoint, <n>
(gdb) delete # Delete all breakpoints

# Step *over* function call
(gdb) next # n

# Step *into* function call
(gdb) step # s

# View source code
(gdb) list # l, default gives 10 lines at present position
(gdb) l # Repeating the command gives the next 10 lines
(gdb) l 25 # Gives 10 lines centered around line 25 of the current file
(gdb) l myFile.cxx:25 # Gives 10 lines centered around like 25 of file myFile.cxx

# Return with no arguments repeats previous command

# Run until next breakpoint
(gdb) continue # c

# Disable/Enable breakpoints
(gdb) disable # Disable all
(gdb) disable 5 # Disable breakpoint 5
(gdb) enable # Enable all
(gdb) enable 5 # Enable breakpoint 5
(gdb) ignore 5 10 # Ignore breakpoint 5 10 times
# Note that the ignore count can be changed/reset

# Print value of variable var
(gdb) print var # p var

# Set the value of a variable var
(gdb) set var = 3

# Call a function myFunction()
(gdb) call myFunction()

# Return from a function
(gdb) finish

# Stop program execution with Ctrl + c
# Unlike doing this in the terminal, you can "continue"

Resources and Tutorials:

http://www.unknownroad.com/rtfm/gdbtut/gdbuse.html

https://sourceware.org/gdb/onlinedocs/gdb/index.html

http://lldb.llvm.org/lldb-gdb.html