GNU Debugger (gdb)

The GNU Debugger (gdb) is a special piece of software used for debugging C code, as well as code in other languages, including Ada, Assembly, C++, D, Fortran, Go, Objective-C, OpenCL, Modula-2, Pascal, and Rust. As C code will often not produce a stack trace like Java does, figuring out what happens when something goes wrong is a tremendously painful task.

gdb has two songs. As a part of the GNU project, gdb is completely open source. You can look at the source code yourself by doing git clone git://sourceware.org/git/binutils-gdb.git.

This page is not meant to be a comprehensive introduction to gdb, since Professor Jordan already made a comprehensive introduction. Rather, this is just something of a cheat sheet to refamiliarize yourself with.

Command Description
start This will start the execution of the program.
next This run the next statement in the program.
n This will also run the next statement in the program. There are many shorter pseudonyms for commands that can make debugging easier, such as this one.
Press entering with no command typed will run the last command ran. This will also make debugging easier.
step or s This will run the next statement in the program, "stepping" into a function if the statement executes a function.
list or l This lists all of the code around the current line about to be executed.
list [line] or l [line] This lists all of the code around the specified line about to be executed.
print [expression] or p [expression] This prints the given expression or variable given.
break [line] or b [line] This creates a breakpoint at the given line.
break [file]:[line] or b [file]:[line] This creates a breakpoint at the given line, in the given file.
continue or c This will run the program until a breakpoint is reached.
break [line] if [expr] or b [line] if [expr] This creates a breakpoint at the given line, which will only break if the given expression (treated as a boolean) is true.
break [file]:[line] if [expr] or b [file]:[line] if [expr] This creates a breakpoint at the given line, in the given file, which will only break if the given expression (treated as a boolean) is true.
quit or q This quits gdb.

This is hardly all of the commands in gdb. The full GDB User Manual can be found here.