gdb, the GNU debugger. GDB is dauntingly
chock-full of useful features, but for our purposes, a small set of its features will
suffice. This document describes them. Relatively complete
documentation of gdb is available on-line in Emacs (use C-h i
and select the ``gdb'' menu option).
GDB is a
symbolic or source-level debugger,
creating the fiction that you are executing the C++
statements in your source program rather than the machine code they have
actually been translated into.
To indicate to our compiler (g++) that you intend to debug your
program, and therefore need this extra information, add the -g
switch during both compilation and linking. For example, if your
program comprises the two files main.cc and utils.cc, you
might compile with
g++ -c -g -Wall main.cc g++ -c -g -Wall utils.cc g++ -g -o myprog main.o utils.oor all in one step with
g++ -g -Wall -o myprog main.cc utils.ccBoth of the sample command sequences above produce an executable program
myprog. To run this under control of gdb, you
can type
gdb myprogin a shell. You will be rewarded with the
gdb command prompt:
(gdb)This provides a clumsy but effective text interface to the debugger. I don't actually recommend that you do this; it's much better to use the Emacs facilities described below. However, the text interface will do for describing the commands.
|
|
|
gdb starts, your program is not actually running; it
won't until you tell gdb to start it. Whenever the
program is stopped during execution, gdb is looking at a
particular line of the source program in a particular function call
(or stack frame) either the point in the program where it
actually stopped, or the line containing the call to the function in
which it stopped, or the line containing the call to that function,
etc. In the following, I'll just use the term current frame to
refer to whatever point this is.
Whenever the command prompt appears, you have available the following
commands.
help command
gdb command or topic. Plain
help lists the possible topics.
run command-line-arguments
myprog command-line-argumentsto a Unix shell.
GDB remembers the arguments you pass, and plain
run thereafter will restart your program from the top with
those arguments.
where
bt and
backtrace are synonyms.
up
gdb is examining to the caller of that
frame. Very often, your program will blow up in a library
function---one for which there is no source code available, such as
one of the I/O routines. You will need to do several ups to get
to the last point in your program that was actually executing.
Emacs (see below) provides the shorthand C-c <
(Control-C followed by less-than).
down
up. Emacs provides the shorthand
C-c >.
print expression
gdb numbers its response for future
reference. For example,
(gdb) print A[i] $2 = -16 (gdb) print $2 + ML $3 = -9telling us that the value of
A[i] in the current frame is -16
and that when this value is added to ML, it gives -9.
display expression
print, only print the value
every time the program stops. This is especially useful if
you have a bug narrowed down to one procedure, and you want to repeatedly
examine the contents of one (or several) variables.
undisplay N
info locals
info args
quit
gdb.
C-c
C-c will permanently
halt its execution (usually). In gdb, however, the program is merely
suspended while you poke around at it. In Emacs, use C-c C-c.
break place
(gdb) break MungeData Breakpoint 1 at 0x22a4: file main.C, line 16.The command
break main stops at the beginning of execution. You
may also set breakpoints at a particular lines in a source file:
(gdb) break 19 Breakpoint 2 at 0x2290: file main.C, line 19. (gdb) break utils.C:55 Breakpoint 3 at 0x3778: file utils.C, line 55.When you run your program and it hits a breakpoint, you'll get a message and prompt like this.
Breakpoint 1, MungeData (A=0x6110, N=7)
at main.c:16
(gdb)
In Emacs, you may also 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 may move the point to the line at which you wish to
set a breakpoint, and type C-x SPC (Control-X followed
by a space).
condition N expression
cond 1 x == 6to say, stop only if the variable x (in your program) has value 6.
info breaks
delete N
C-c C-d deletes the breakpoint
you just stopped at.
disable N
enable N
continue
C-c C-r.
step
next (below). In Emacs, you may use C-c
C-s.
next
step, however if the current line of the program contains a
function call (so that step would stop at the beginning of that
function), does not stop in that function. In Emacs, you may use
C-c C-n.
finish
nexts, without stopping, until reaching the end of
the current function. In Emacs, you may use C-c C-f.
gdb from a shell, nobody in his right
mind would want to do so. Emacs provides a much better
interface that saves an enormous amount of typing, mouse-moving, and
general confusion. Executing the Emacs command M-x gdb starts
up a new window running gdb, and enables all the Emacs
shorthands described in the command descriptions above. Furthermore,
Emacs intercepts
output from gdb and interprets it for you. When you stop at a
breakpoint, Emacs will take the file and line number reported by
gdb, and display the file contents, with the point of the
breakpoint (or error) marked. As you step through a program,
likewise, Emacs will follow your progress in the source file.
Finally, the command C-x SPC will place a breakpoint at
the current point in a file you are visiting.