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:
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
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 argumentsor you can typea.out command-line arguments
to redirect standard input to your program.run command-line arguments <somefile
break
placeThe command(gdb) break Traverse Breakpoint 2 at 0x2290: file main.c, line 20
break main
stops 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-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
Ninfo break
gives information about each breakpoint.help
commandhelp
lists the possible topics.step
next
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.finish
next
commands, without stepping, until the
current function returns.continue
file
filenamefile
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.where
backtrace
is equivalent.print
expr
display
is similar, except every time you execute a next or step, it will print the
expression with the current values of the variables.quit
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.
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.