GDB: The GNU Debugger

This document is a very basic, brief introduction to GDB, the GNU Debugger. It is our hope that it will give you enough insight into this powerful program for you to be able to utilize it to fix a wide variety of program bugs without needing to consult your teaching staff.


The Basic Operation -- Getting Started

In its most basic incarnation, GDB simply runs over top of your program and watches for a crash. When it does crash, GDB pipes up and tells you at what point in your program it collapsed.

To make it most useful, it is helpful to instruct g++ to add special information to the files it creates to make debugging easier. To do this, simply add the -ggdb parameter to the compilation. So, if you are currently compiling with g++ -o outfile source1.o source2.o, you would then compile with g++ -ggdb -o outfile source1.o source2.o. Nothing changes aside from the addition of the -ggdb after g++.

To launch GDB, simply type at the prompt:

gdb <program name>

where <program name> is the name of your executable file. This will load the debugger with a display like the following:

GDB is free software and you are welcome to distribute copies of it
 under certain conditions; type "show copying" to see the conditions.
There is absolutely no warranty for GDB; type "show warranty" for details.
GDB 4.16 (alpha-dec-osf4.0), Copyright 1996 Free Software Foundation, Inc...
(gdb)

The (gdb) is your command prompt. There are a few basic commands that will be of most interest to you:

What To Do After A Crash

Those commands should be enough to run your program. If and when it crashes these commands will assist you in diagnosing the problem. You will get a display such as:

Program received signal SIGSEGV, Segmentation fault.
0x8048144 in function() at filename.cc:123
123	   some_code_here();

The actual message may vary. Obviously, the function and file names, as well as the code, will depend on your program. The actual error is likely to be a segmentation fault, but could be certain other errors depending on the nature of your problem. The hexidecimal address (the 0x8048144) will almost assuredly be a different number, but it is of no interest to you. The commands of interest are:


More Advanced Operation -- Breakpoints and Watchpoints

One more very useful feature of debuggers is to instruct them not to wait until the program crashes, but rather tell it to explicitly stop at a certain point in the source code. Breakpoints and watchpoints to exactly this. When one of them is tripped, the program stops, and you are given the (gdb) prompt to use all the commands available to you to print data, change frames of reference, and other such commands just like you can with a crash.


Closing Notes

This tutorial is by far not the entire capability of GDB. It is simply intended to give you enough tools to do basic debugging with this powerful program. You are encouraged to fully explore the 'help' system and learn more about this debugger.

However, for people wanting more, there are a couple options available to you.

If you find any problems with this tutorial, please report them to Kevin Kane.