GDB

GDB, or the GNU Debugger, is a terminal-based debugging application. Debuggers are powerful tools that allow a developer to observe how an application is working at run-time. It allows one to create break-points and stop the program at pre-defined positions. It also allows you to stack-trace, view memory, and even change variables at run-time. To run an application with GDB, you must build your program with the "-g" or "-ggdb" option with GCC or g++ so that debugging information is linked with your executable.

Installation
For Linux, use your distribution's package manager to install the client:

On Red Hat or other yum-based systems: On Ubuntu or other apt-based systems: On Arch or other pacman-based systems:

Basic Use
First, start gdb in the command line, passing the executable you want to debug:

Then, break any lines or functions you want:

or

Start the program:

After hitting a breakpoint, use "next", with a count number, to execute a number of lines:

Also, use "step" to step into a function when executing line-by-line:

Lastly, use

to see the values of a variable, and

to be notified whenever a variable's value is changed.

While using gdb, your previous command can be executed again by simply hitting enter, and you can scroll through previous commands by hitting "up" on your keyboard.

Stack Crawling
When faced with a seemingly hopeless segmentation fault, a stack trace, which can show you the last executed functions and the variables passed to them, can be invaluable. In gdb, after a program has segfaulted, you can use

to show you a list of functions all the way up to the first one called by main, and traverse it using the commands "up" or "down", allowing you to print the variables in each function to discover where the bad call was made.

For more on using gdb, visit: