Documented in the UNIX Programmers Manual.
% cc -g pgm.c
the -g makes the compiler produce additional symbol table information for
the dbx dynamic debugger in the executable (named a.out, in this case).
After compiling as above, you can use this interactive debugger like this:
% dbx a.out
and it will produce something like this:
dbx version of 9/26/83 10:42 (ucbmonet).
Type 'help' for help.
reading symbolic information ...
(dbx) help
run - begin execution of the program
cont - continue execution
step - single step one line
next - step to next line (skip over calls)
trace [line#] - trace execution of the line
trace [proc] - trace calls to the procedure
trace [ var ] - trace changes to the variable
trace [exp] at [line#] - print [exp] when [line] is reached
stop at [line] - suspend execution at the line
stop in [proc] - suspend execution when [proc] is called
status - print trace/stop's in effect
delete [number] - remove trace or stop of given number
call [proc] - call the procedure
where - print currently active procedures
print [exp] - print the value of the expression
whatis [name] - print the declaration of the name
list [line], [line] - list source lines
edit [proc] - edit file containing [proc]
gripe - send mail to the person in charge of dbx
quit - exit dbx
(dbx) quit
%
HINT: to trace variable x in subroutine sub1, use:
(dbx) trace sub1.x
From: Ed H. Coughran
The UNIX command dbx can be a valuable tool in program
development. It provides an interactive debugging tool to help you
find the source of failures or aberrant behavior of your program.
The only prerequisite for using dbx is to include the -g
flag on the compilation command line as in the following examples:
% cc -g -o myprog myprog.c
% f77 -g -o execut execut.f
% ps -g -o pasprog pasprog.p
If you have a program that is failing in execution, the simplest way to
use dbx is to invoke it and have the program run until the error condition
has been detected, but before the error exit is taken.
At this point, the source program line number on which the error
occurs will be displayed along with the source code line itself and control
is returned to you.
Now you have the ability to print the value of any variables as they
stand at this point, to invoke any functions or to list any source code lines.
You may also set up a "trace" on any variable name or function so that the
value of that variable or the return value of that function are displayed
on the screen every time they are encountered in the execution of the program
from this point on.
To invoke (cause execution of) a program named objfile
under the control of dbx, enter the command:
% dbx -r objfile
and the program will begin execution and will stop just before the error
termination. If you omit the name objfile, dbx will default
to the file named a.out.
After invoking your program under the watchful eye of dbx, your
program will run until the error condition has been detected, but just
before execution is terminated. You will receive a message something like
floating point exception in objfile at 0xdd
000000dd mulf2 r1,r0
that tells you exactly which machine instruction failed and the registers
involved (this is too detailed to be generally useful unless you are using
assembly language.) To find out why your program failed, you can now print
out the current values of any variables in the program. To display the
value of the variable x, for instance, you simply enter
(dbx) print x
243.67
It may be the case that by the time the program failure is imminent,
it is too late to find out how you got there. In this case, you can use dbx
interactively and cause the program execution to be halted on one of several
conditions:
when a particular variable reaches a given value
when a particular function is called
when a particular source code line is reached
when a given condition is true
To use this capability, invoke dbx without the -r option and dbx will
start execution with the dbx prompt of (dbx). You instruct dbx to specify
under what circumstances to stop execution of the program and give to you
(through dbx) detailed control of the continued execution. You enter
conditions of the form
(dbx) stop variable [if condition]
(dbx) stop at source-line-number [if condition]
(dbx) stop in function [if condition]
If you want to stop execution at the point that a variable named clout
exceeds 3.2, you enter
(dbx) stop clout if clout > 3.2
(dbx) run
The program will execute until the condition is satisfied and then
stop and display a line like
stopped in MAIN at line 6
6 clout=gamma(clin)*i
at which point you can have the values of any variables displayed by
using the print command
(dbx) print x
345.82
(dbx)
If you want the values of several variables to be printed out each time
they are used, you can set the trace mode by entering commands of the form
(dbx) trace varname
(dbx) trace funcname
and the value of the variable named varname or the value returned by the
function named funcname will be displayed every time they are encountered
in the further execution of the program.
You should take some care in your choice of conditions and in the amount
of output you generate. If you specify a trace command that is repeatedly
executed dbx can seem exceedingly slow.
To advance the program from the point at which you are stopped, you have
three options
(dbx) continue
(dbx) step
(dbx) next
where 'continue' will allow the execution of the program to proceed
normally, 'step' will execute one source line and 'next' will execute up
to the next source line. The difference between 'step' and 'next' is
that if the line contains a procedure or a function, 'step' will stop at
the beginning of that block while 'next' will not.
At any point when the execution is stopped, you can determine what
procedure is active by entering the where command
(dbx) where
read(0x0, 0x223c, 0x2000) at 0x167d
_filbuf(0x1db8) at 0xaff
_instr(0x1f7c, 0x7fffebe5) at 0x723
_innum(0x7ec44, 0x7ebe4) at 0x40c
_doscan(0x1db8, 0x7fffec44) at 0x304
scanf(0x1c64, 0x1fec) at 0x1da
main(0x1, 0x7fffec88, 0x7fffec90),
line 30 in "dater.c"
(dbx)
which lists a function walkback list, along with pointers to the arguments of
each function call.
You can also use dbx when you have an operational program that operates on
a large data set and data values are not properly recognized (the inference
being that the program executes and does not produce an error termination,
but produces incorrect results.)
When you are through, you can exit from the dbx processor by entering
(dbx) quit
If the dbx processor seems hung up, or out to lunch, you can interrupt
the current dbx command by pressing the DELETE key.
One feature missing in dbx which was present in the older sdb
is the ability to trace stacks, but unless you have that specific need,
dbx is the best interactive debugger on the system.