gdb, the GNU debugger.
GDBis 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
gdbis available on-line in Emacs (use
C-h iand select the ``gdb'' menu option).
GDBis 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
switch during both compilation and linking. For example, if your
program comprises the two files
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)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.
gdbstarts, your program is not actually running; it won't until you tell
gdbto start it. Whenever the program is stopped during execution,
gdbis 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.
gdbcommand or topic. Plain
helplists the possible topics.
myprog command-line-argumentsto a Unix shell.
GDBremembers the arguments you pass, and plain
runthereafter will restart your program from the top with those arguments.
gdbis 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).
up. Emacs provides the shorthand
gdbnumbers 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.
C-cwill permanently halt its execution (usually). In
gdb, however, the program is merely suspended while you poke around at it. In Emacs, use
(gdb) break MungeData Breakpoint 1 at 0x22a4: file main.C, line 16.The command
break mainstops 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-bto 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).
cond 1 x == 6to say, stop only if the variable x (in your program) has value 6.
C-c C-ddeletes the breakpoint you just stopped at.
next(below). In Emacs, you may use
step, however if the current line of the program contains a function call (so that
stepwould stop at the beginning of that function), does not stop in that function. In Emacs, you may use
nexts, without stopping, until reaching the end of the current function. In Emacs, you may use
gdbfrom a shell, nobody in his right mind would want to do so.
Emacsprovides a much better interface that saves an enormous amount of typing, mouse-moving, and general confusion. Executing the Emacs command
M-x gdbstarts up a new window running
gdb, and enables all the Emacs shorthands described in the command descriptions above. Furthermore, Emacs intercepts output from
gdband 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 SPCwill place a breakpoint at the current point in a file you are visiting.