A debugger is a program that runs other programs, allowing the user
to exercise control over these programs, and to examine variables when
problems arise. The most popular debugger for UNIX systems is GDB, the
GNU debugger. GDB has tons of features, however, you only need to use a
few for it to be very helpful. There is complete documentation
for GDB online, or you can read the man page, and the quick reference
sheet is very handy.
Basic features of a debugger
When you are execute a program that does not behave as you like, you need some way to step through you logic other than just looking at your code. Some things you want to know are:
lcc -g trees.cwhich will create the a.out executable. To run this under the control of gdb, you type
gdb a.outThis 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
Starts your program as if you had typed
a.out command-line argumentsor you can do the following
a.out < somefileto pipe a file as standard input to your program
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 20The 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 20When 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 (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 can move to the line at which you wish to set a breakpoint, and type C-x SPC (Control-X followed by a space).
Removes breakpoint number N. Leave off N to
remove all breakpoints. info break gives info about each breakpoint
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, however, if the current line of the program
contains a function call, it executes the function and stops at the next
line.
Keeps doing nexts, without stepping, until reaching
the end of the current function
Continues regular execution of the program until a breakpoint
is hit or the program stops
Reloads the debugging info. You need to do this 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, and this 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
prints the value of E in the current framein the program,
where
E is a C expression (usually just a variable). display
is similar, except every time you execute a next or step, it will print
out the expression based on the new variable values
Leave GDB. If you are running gdb under emacs,
C-x 0will get you just your code back
If you have any questions, check out the online manual, or send me mail