CS 537 Notes, Debugging


What Is A Debugger?

A debugger is a program that can control other programs. A programmer uses it to: A debugger helps to answer the following questions: Knowledge and familiarity with a debugger is critical to your success as a programmer.

What Is GDB?

GDB is the GNU Project's debugger, and is the most commonly used UNIX debugger. Google for "gdb tutorial" and you'll see what we mean. In these notes we will briefly describe how GDB works; the tutorials available on-line go into much more detail. GDB can:

We'll go over these here.

First, you should compile your program with full debugging information; this means that GDB can return more useful information to you. This is done by putting "-g" on the command line:

gcc -g -o simulation simulation.c
When using multiple source files, each file gets the "-g", as well as the link line:
gcc -g -c process.c
gcc -g -c unix.c
gcc -g -c binary.c
gcc -g -o program process.o unix.o binary.o

In general, there is no reason not to always use "-g".

Once you have a compiled program, you run it under GDB:

gdb program
This will get you a screen like so:
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or 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.
This GDB was configured as "i686-pc-linux-gnu"...
Using host libthread_db library "/lib/tls/libthread_db.so.1".

(gdb)
The (gdb) is the prompt, much like a command prompt. Here are some commands:
Copyright © 2008 Andrew R. Bernat