GDB Tutorial

A debugger is a program that runs other programs, allowing the user to exercise control over these programs, and to examine variables when problems arise. The most popular debugger for UNIX systems is GDB, the GNU debugger. GDB has tons of features, however, you only need to use a few for it to be very helpful. There is complete documentation for GDB online, or you can read the man page.

Basic features of a debugger

When you execute a program that does not behave as you like, you need some way to step through your logic other than just looking at your code. Some things you want to know are:

Starting GDB

need program args.c

You need to tell the gcc compiler that you plan to debug your program. You use the -g flag to do this. The command will now look something like:

gcc -g -Wall -o args args.c
which would create the example executable. To run this under the control of gdb, you type

gdb args
This starts up the text interface to the debugger.

GDB commands

When gdb starts, your program is not actually running. It won't run until you tell gdb how to run it. Whenever the prompt appears, you have any command available to you. You will usually only use a small amount of commands though, which are covered below.

The goal of gdb is to give you enough info to pinpoint where your program crashes, and find the bad pointer that is the cause of the problem. Although the actual error probably occurred much earlier in the program, figuring out which variable is causing trouble is a big step in the right direction.