A quick tutorial on using gdb, the GNU debugger

by Brian McNamara

gdb is the GNU debugger. It is an essential tool for the C/C++ programmer programming in a unix environment here at Tech. Having a basic working knowledge of gdb (enough for it to be your friend) will save you many many hours of debugging time when you're coding in C or C++.

This document serves as a quick intro to gdb and its basic features. I'm writing it so I needn't explain it twenty times to different people. The basic document has these parts:

If you've suggestions for changes/additions to the document, let me know.

When should I use gdb to help me debug a program?

You should use gdb to help find the following types of bugs:

(Yes, I know, it's a one-element list. There are probably other good times to use gdb, but this is the only one I can think of right now.)

How do I invoke gdb?

First, you should compile your program with gcc and the -g option, as in

   gcc  -g  my_file.c
If you're, say, using a Makefile given to you in CS2430, this has probably already been done for you.

That done, there are two main ways to invoke the debugger:

   gdb  <executable_program_name> 
or
   gdb  <executable_program_name>  <core_file>
The first way starts the debugger with the given program without running the program (yet). The second way takes a core dump that your program already made, and starts the debugger at the place where the core dump happened.

What commands can I use inside gdb?

Once gdb is started, there are many many commands you can do. Here are the most useful ones.

run

If you start up gdb without a core file, then run will start running the program inside the debugger. If the program is stopped, run will continue where the program left off.

bt

If a running program has stopped (due to a segmentation fault, user break, etc.) you can use bt (backtrace) to see the current stack trace. This is very very useful when you've a coredump, because it shows you exactly what line of code caused the coredump.

up and down

When the program is stopped, up and down move between stack frames. (This makes more sense when you see the illustrated example below.)

print

print prints the value of a variable or expression. It is way cool, because not only can you print simple stuff like ints, but you can also print strings, structs, etc. print simply prints its argument out in some reasonable way. This is a great way to check out the value of some variables to see if they look right.

Can you show me a typical example of using gdb in a debugging session?

No.

I'm kidding! Sheesh.

Ok, here's a sample run. The code for a sample (broken) program is here. The sample interaction with gdb, with commentary, is here.

Where can I get more info about using gdb?

The man pages are skimpy, but the info pages have good info. If you've not used info before, you should learn how. info is GNU's replacement for man pages, since man pages suck. It's a hypertext environment with an emacs-like interface. For more info on info, do a man info or an info info.

Also, check out the "gdb quick reference" linked off this page.


Go back to my public services page.
Last updated on Wed Apr 21 14:06:14 EDT 1999 by Brian McNamara