HTML>
Also, if you did not pick one up, this 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.
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 TraverseThe command break main stops at the beginning of execution. You can also set breakpoints at a particular line in a source file:
Breakpoint 2 at 0x2290: file main.c, line 20
(gdb) break 20When you run your program and it hits a breakpoint, you'll get a message and prompt like this.
Breakpoint 2 at 0x2290: file main.c, line 20
Breakpoint 1, Traverse(head=0x6110, NumNodes=4)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).
at main.c:16
(gdb)
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