Debuggers

by David Ropp and Anu Rao


Table of Contents


Introduction

Debuggers are a very useful when programming, for they allow you to run your program one command at a time and hence see where any problems are occuring. Debuggers can tell you when and where a program terminates abnormally, show you the value of variables, and in general provide much information so that you, the programmer, can better find where mistakes are being made. Many of the debuggers available on the Math Department network are based on dbx. The original dbx is still available, but it is based on line editors and hence is rather clunky to use. It may be useful if you are remote logging in to the network and only have an xterm available.

There are two other versions of dbx that make running it much easier: xdbx, an X windows version; and dbxtool, an OpenWindows version. Both of these have similar functionality and layout, so we will only review xdbx, which seems more popular. The instructions will be nearly identical for using dbxtool. Both xdbx and dbxtool have man pages, as does dbx.

Another debugger available is gdb, a GNU product. It is not windows based, so it doesn't have quite the ease of use as xdbx and dbxtool, but it can be run under emacs.

For the purposes of this talk we will discuss xdbx and gdb.

The debuggers can read programs written in C, C++, Fortran, Pascal, or Modula-2. First, your program must compile successfully. Second, any program or parts of a program that are to be debugged must be compiled with the -g option, which creates a set of tables for the debugger to interpret. This option makes the program run slower and negates any optimization flags like -O or -cg89, so you should remove the -g flag once your code is fully debugged. Also, although the main program must be compiled with -g, any subroutines that are fully debugged do not; so if your program calls subroutines in another file, once they are fully debugged you can compile those subroutines with any optimization.

[Back]

XDBX

To use xdbx, at the Unix prompt type

    xdbx prog

where prog is the name of the executable file. A window pops up that has three main parts: the source code in the top half, the command window in the middle, and the dialogue window in the bottom. Clicking the left mouse button on in the source window will position a caret at that point, which is useful for setting or deleting breakpoints. Clicking twice will highlight the nearest variable or function name, useful for displaying values of variables or skipping to a function.

Any dbx command can be entered at the prompt in the dialogue window, but most of the commonly used commands have buttons in the command window, so below is a description of each button. Each is activated with a clicking the left mouse button on the appropriate command button.

Here is a list of useful commands/buttons:

run
Run the program.
stop at
Set a breakpoint at the position of the caret in the source window. When running, the program will stop at that line and wait for your command.
stop in
Set a breakpoint in the function highlighted in the source window.
delete
Delete current breakpoint; the caret should be positioned at this point.
status
Display all breakpoints.
cont
Continue execution.
next
Execute the next line, skipping over function calls.
step
Execute the next line, stepping into function calls.
print
Print the value of the variable currently highlighted
print*
Same as print, but for pointers.
display
Print the value of the currently highlighted variable in a display window, which pops up below the dialogue window. This variable remains there and is continually updated until it is removed with undisplay.
undisplay
Remove currently highlighted variable from the display window.
dump
Print the value of all local variables
search
Search the source code for a particular string. Xdbx will query or the string.
where
Shows the stack of function calls that got you where you currently are.
up,down
Move the cursor up or down the stack. Only the cursor is moved, not the line of execution.
func
States what function you are in, or moves cursor to highlighted function.
file
Switch files. (Sometimes xdbx complains of not being able to find a file that it currently has. Reloading the file usually fixes this.)

Some of these can be entered on the command line and given additional arguments. For example, typing next 50 will execute the next 50 lines, which is useful for jumping over a section of the program. Also, conditionals can be given, such as stop if i==300, which stops the program if i=300. Other useful commands include:

trace
Like display, but more general; You can trace function calls, source lines, and can set up conditionals.
edit
Invoke your default editor (usually set to emacs).
make
Run make on you program.
sh
Invoke a shell.
help
Provide help on any command or topic.

For those programming in C, print has a nice (though primitive) tool for showing pointers and structures: if one is highlighted, click on print with the right mouse button and its value will be displayed in a popup window. If the value is a pointer, clicking on its value will create another popup showing the object at that address. These popups are removed by clicking on their labels.

Unfortunately, this does not allow you to increment the address to see other values. This is a problem if you habitually dynamically allocate your arrays. One fix for this is to statically allocate the array while debugging, then switch to dynamic allocation.

[Back]

GDB

Gdb is a GNU product and is not windows based, but can be run under emacs. To get started, compile your program with the -g option, and then open an emacs window. Once in emacs, invoke with M-x gdb. You will be prompted for the name of your execulable -- enter it and hit return. The debugging symbol table will be loaded in and the gdb prompt "(gdb)" will apprear. Now you're ready to go!

With gdb one can get a source window and a dialogue window so that one can step through the execution of the program line by line and see what is happeneing. The screen will not split until the program runs, so it helps to put a break point early in the code to get the split screen.

Some handy commands:

help
get online help -- *very* useful!
file progname
load in executable progname; useful to reload program after recompilation
run progname arg1 arg2 ...
run your program executable named progname with command-line arguments arg1, arg2, ...
list m, n
display lines m through n
break n
place a breakpoint at line n so that when the program is run it will stop at line n
step
once a breakpoint has been inserted and the code is running, step through code execution line by line, including subroutines
next
step through code, but treat subroutine calls as one execution line
display varname
display value of variable varname at each step
continue
run program to the end
info breakpoint
display all breakpoints

For xdbx users, gdb has some differences. For example, since gdb is not a version of dbx, some of the commands are different. Most notably, the command for setting a breakpoint at line 17, say, is break 17. Some commands have abbreviations, such as n for next, s for step, and c for cont.

The advantages of gdb are that it is free and that it can run on a wide variety of machines (SUN, Linux, etc.). Also, the fact that it can run under emacs makes it more attractive than dbx if you cannot run window applications.

[Back]

Handy Web Sites

The xdbx manpage has been put online at UC Santa Barbara. The University of Houston also has a page about dbx, and the University of South Florida has some nice examples. Finally, here is a rather thorough gdb manual.
[Back]
http://math.arizona.edu/~swig/documentation/debuggers/index.php
Last modified: Fri, 14 Dec 2007 15:50:51 -0700
E-mail: swig@math.arizona.edu
Valid XHTML 1.0! Valid CSS!