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:
-g option to specify that you want to debug your
program. For example:
% lcc -g trees.c
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
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.
or you can typea.out command-line arguments
to redirect standard input to your program.run command-line arguments <somefile
The command(gdb) break Traverse Breakpoint 2 at 0x2290: file main.c, line 20
break mainstops at the beginning of execution. You can also set breakpoints at a particular line in a source file:
When you run your program and it hits a breakpoint, you'll get a message and prompt like this:(gdb) break 20 Breakpoint 2 at 0x2290: file main.c, line 20
In emacs, you can useBreakpoint 1, Traverse(head=0x6110, NumNodes=4) at main.c:16 (gdb)
C-c C-bto 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).
info breakgives information about each breakpoint.
helplists the possible topics.
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
stepwould put you at the beginning of the function.
nextcommands, without stepping, until the current function returns.
fileif 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.
displayis similar, except every time you execute a next or step, it will print the expression with the current values of the variables.
C-x 0will 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.
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.