Gdb Tutorial

Starting Gdb · Gdb Commands · Gdb Under Emacs · Gdb Manual

A debugger is a program that runs other programs under the control of the user. You can stop the program at any source line and examine variables, etc. In short, you can find and bugs by exploring the state of your program. Gdb is a popular UNIX debugger. Gdb has tons of features, but you need only a few for it to be helpful. There is complete online documentation for gdb, or you can read the man page, and the quick reference sheet is handy.

Also, the reference card is useful when first learning the gdb commands. It's in PostScript, so you can print it out with lpr or use ghostview.

When your program does not behave as expected, 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

Use lcc's -g option to specify that you want to debug your program. For example:

% lcc -g trees.c

creates a.out as usual, but also includes the extra information necessary to run gdb. To run this under the control of gdb, you type

% gdb a.out

This starts up the text interface to the debugger. It's much easier to use gdb under emacs, but we'll use this interface to describe the commands

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 all the commands on the quick reference sheet available to you.

run command-line arguments
Starts your program as if you had typed
a.out command-line arguments
or you can type
run command-line arguments <somefile
to redirect standard input to your program.
break place
Creates a breakpoint; the program will halt when it gets there. The most common breakpoints are at the beginnings of functions, as in
(gdb) break Traverse
Breakpoint 2 at 0x2290: file main.c, line 20
The command break main stops at the beginning of execution. You can also set breakpoints at a particular line in a source file:
(gdb) break 20
Breakpoint 2 at 0x2290: file main.c, line 20
When you run your program and it hits a breakpoint, you'll get a message and prompt like this:
Breakpoint 1, Traverse(head=0x6110, NumNodes=4) at main.c:16
In emacs, you can use C-c C-b to set a breakpoint at the current point in the program (the line you have stepped to, for example) or you can move to the line at which you wish to set a breakpoint, and type C-x SPC (ctrl-X followed by a space).
delete N
Removes breakpoint number N. Omit N to remove all breakpoints. info break gives information about each breakpoint.
help command
Provides a brief description of a gdb command or topic. Plain help lists the possible topics.
Executes the current line of the program and stops on the next statement to be executed.
Like step, but if the current line of the program contains a function call, it executes the function and stops at the next line. Typing just step would put you at the beginning of the function.
Keeps doing next commands, without stepping, until the current function returns.
Continues execution of the program until a breakpoint is reached or the program stops.
file filename
Reloads the debugging information for filename. You need to do use file if you are debugging under emacs, and you recompile in a different executable. You must tell gdb to load the new file, or else you will keep trying to debug the old program, which will drive you crazy.
Produces a backtrace--the chain of function calls that brought the program to its current place. The command backtrace is equivalent.
print expr
prints the value of expr in the current frame in the program, where expr is a C expression (often just a variable). display is similar, except every time you execute a next or step, it will print the expression with the current values of the variables.
Leave gdb. If you are running gdb under emacs, C-x 0 will get you back to your code.

The goal of gdb is to give you enough information to pinpoint where your program crashes, and to find the bad pointer that is often 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. Before seeking help from a TA or preceptor, you should try to figure out where your error is occurring.

Gdb Under Emacs

Emacs provides an interface to gdb that saves a lot of typing and confusion. Executing the emacs command M-x gdb starts up a new window running gdb. If you set a breakpoint on the main function and then run the program with the correct arguments, gdb splits the window in two, with your source code on the bottom and a gdb command line in the top. Emacs intercepts output from gdb and interprets it. When you stop at a breakpoint, emacs uses the file name and line number reported by gdb to display the file's contents with cursor at the breakpoint. As you step through a program, emacs follows your progess in the source file. The command C-x SPC sets a breakpoint at the current point of the file you are in.

Original version by Benji Jasik /
Revised by David R. Hanson /
$Revision: 1.3 $ $Date: 1996/10/11 13:36:22 $