Although a C compiler will identify problems in your code at the syntax level, you still need a run-time debugging tool. The dbx program provides a convenient facility for source-level debugging and controlled execution of programs under UNIX. It provides an interactive debugging environment with the following features:
To debug a C program using dbx, you must compile all files using the -g option to the compiler. For example:
cc -g foobar.c -o foobar
results in an executable file foobar suitable to run under the control of dbx. To invoke dbx, simply type:
dbx foobar
to debug the executable binary file foobar. The prompt
(dbx)
then appears, and you can begin an interactive debugging session. Type quit to exit from dbx.
This section summarises some of the commands provided by dbx for simple debugging. To begin execution, use the command:
run [args] [< file1] [> file2]
where args are any command line arguments needed by the executable. > and < may be used for I/O indirection. dbx provides the following commands to allow tracing of lines/functions:
| trace at <line number> | trace execution of <line number> |
| trace in <function> | trace calls to and returns from <function> |
| trace change <var> | trace changes to variable <var> |
| list <line1>, <line2> | list the range of lines |
| list <function> | list start of the source for <function> |
To suspend execution at a certain point in a program to inspect values of variables and so on:
| stop at <line number> | suspend execution at <line number> |
| stop in <function> | suspend execution when <function> is called |
| stop change <var> | suspend execution if variable <var> is changed |
| print <exp> | display the value of <exp>ression |
| cont | continue execution from where it was halted |
A point where execution is suspended is termed a break point. After reaching a break point, single lines of source may be stepped through:
| step | execute one line of source |
| step <n> | step through <n> lines of source |
| next | execute up to the next line of source |
If the line contains a call to a function, step will stop at the beginning of the function block, but next will not. To display the current line number and the sequence of function calls which led to this point, use the where command.
As debugging progresses, trace and break points are put in and taken out. To remove trace and break points use:
| delete <number> | remove trace or break point with sequence <number> |
Trace and break points are given a sequence number when they are installed. After a trace or stop command, this number is displayed. The status command may be used to display all the trace and break points in effect.
Note that dbx can be used to examine the state of a program when a core file was generated by typing:
dbx core
dbx commands are then used to examine the program state at the point of the execution error which caused the core file to be produced.
DBX (1), source-level debugger: the individual manual pages for DBX.
Last updated: September 27 2000
CS3007 |
Pete Edwards |
Staff & Students
Computing Science
pedwards@csd.abdn.ac.uk