The dbx source code debugger is one of the most powerful tools available for programmers because it helps you locate run time errors in your source code. Run time errors, in contrast to compile time errors, are errors in the program's logic, not errors in its syntax. Run time errors only show up when you execute a program.
Before you can use dbx on a program, you must compile the program using CC's -g option. That is:
CC -g source.cpp -o executablefileUnder this -g option, CC creates additional information about the program and deposits it in a symbol table. The debugger must have this symbol table to do its work. You start dbx by typing:
dbx executablefilewhere executablefile is the executable version of the program. If you have used CC without the -o option, the executablefile in this case is a.out.
The dbx debugger helps you debug a program by letting you control the program's execution. The debugger allows you to set breakpoints, or places in the program where execution is suspended. With the execution suspended, you can use dbx to examine the values of the program's variables and expressions. The following is an outline of a simple debugging session:
The following is a list of the most useful commands inside dbx; the others are documented in the dbx man page (type man dbx at the UNIX command line). You will see some of these commands in action in the next section.
assign variable = variable - 1subtracts 1 from the initial value of the variable.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int fahr, celsius;
int lower, upper, step;
lower = 0;
step = 20;
fahr = lower;
upper = 10;
cout << setw(10) << "fahr" << setw(10) << "celsius" << endl;
while (fahr <= upper)
{
celsius = 5*(fahr - 32)/9;
cout << setw(10) << fahr << setw(10) << celsius << endl;
fahr = fahr + step;
}
return 0;
}
The following is an example of using dbx on hercules, a UNIX computer.
Look at the table below to find out how to compile the program
for using dbx and how to start dbx.
The red text in the table denotes comments.
Compile C++ program with -g option
to generate information the debugger needs to connect to the source
and executable files.
hercules[6]% CC -g example1.cpp -o example1
hercules[7]%
hercules[7]%
hercules[7]% example1
fahr celsius
0 -17
hercules[8]%
Somehow it only prints one line of output.
We will try to use dbx to debug it.
Start dbx now. It works only with executable files.
hercules[9]% dbx example1
changes (topic)
The major new features of this release relative to 6.0 are:
o The dbx environment variable "cfront_demangling" has been removed.
The "-F" option to dbx (allow cfront-demangling) has also been removed.
Use an earlier version of dbx to get cfront support.
o This release continues to support Fortran intrinsics on Intel, even
though the Intel Fortran compiler was not shipped with WorkShop 6.
The next dbx release will not support Fortran intrinsics on Intel.
See also `help changes60'
To suppress this message, add the following line to your .dbxrc file:
dbxenv suppress_startup_message 5.0
Reading example1
Reading ld.so.1
Reading libCrun.so.1
Reading libm.so.1
Reading libw.so.1
Reading libc.so.1
Reading libdl.so.1
Reading libc_psr.so.1
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx)
Use list to display the source code.
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) list
10 lower = 0;
11 step = 20;
12 fahr = lower;
13 upper = 10;
14 cout << setw(10) << "fahr" << setw(10) << "celsius" << endl;
15 while (fahr <= upper)
16 {
17 celsius = 5*(fahr - 32)/9;
18 cout << setw(10) << fahr << setw(10) << celsius
<< endl;
19 fahr = fahr + step;
Use list to display the source code from line 15 to line 19.
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) list 15,19
15 while (fahr <= upper)
16 {
17 celsius = 5*(fahr - 32)/9;
18 cout << setw(10) << fahr << setw(10) << celsius
<< endl;
19 fahr = fahr + step;
The program seems to stop too soon. We will set a breakpoint at the loop.
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) stop at 15
(2) stop at "example1.cpp":15
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) run
Running: example1
(process id 112903)
fahr celsius
stopped in main at line 15 in file "example1.cpp"
15 while (fahr <= upper)
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) next
stopped in main at line 17 in file "example1.cpp"
17 celsius = 5*(fahr - 32)/9;
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) next
stopped in main at line 18 in file "example1.cpp"
18 cout << setw(10) << fahr << setw(10) << celsius
<< endl;
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) next
0 -17
stopped in main at line 19 in file "example1.cpp"
19 fahr = fahr + step;
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) next
stopped in main at line 15 in file "example1.cpp"
15 while (fahr <= upper)
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) next
stopped in main at line 21 in file "example1.cpp"
21 return 0;
That's strange. The program only ran for one loop then exited.
Let's print out the values of the variables and take a look.
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) print fahr
fahr = 20
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) print upper
upper = 10
Oops! upper = 10?
There is a typo because the variable "upper" was supposed to be set to 100
instead of setting to 10. We often have bugs that are very simple but are
overlooked. DBX gives us an advantage by providing us with clues to the puzzle.
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) quit
hercules[10]%
This program demonstrates how to manipulate values in an array.
#include<iostream>
using namespace std;
int main ()
{
const int MAX_ARRAY = 10;
int numbers[MAX_ARRAY];
int index;
int sum;
// Stored values in the array.
for (index = 0; index < MAX_ARRAY; index++)
{
numbers[index] = index * index;
cout << "numbers[" << index << "]=" << numbers[index] << endl;
}
// The values in the array are summed.
sum = 0;
for (index = 0; index <= 1000; index++)
{
sum = sum + numbers[index];
}
cout << "Sum is " << sum << endl;
return 0;
}
Now let's compile and run the program.
hercules[18]% CC -g example2.cpp -o example2 hercules[19]% hercules[19]% hercules[19]% example2 numbers[0]=0 numbers[1]=1 numbers[2]=4 numbers[3]=9 numbers[4]=16 numbers[5]=25 numbers[6]=36 numbers[7]=49 numbers[8]=64 numbers[9]=81 Segmentation fault (core dumped) hercules[20]%There is a core dump. Let's use dbx to debug the program example2.cpp.
hercules[21]% dbx example2
changes (topic)
The major new features of this release relative to 6.0 are:
o The dbx environment variable "cfront_demangling" has been removed.
The "-F" option to dbx (allow cfront-demangling) has also been removed.
Use an earlier version of dbx to get cfront support.
o This release continues to support Fortran intrinsics on Intel, even
though the Intel Fortran compiler was not shipped with WorkShop 6.
The next dbx release will not support Fortran intrinsics on Intel.
See also `help changes60'
To suppress this message, add the following line to your .dbxrc file:
dbxenv suppress_startup_message 5.0
Reading example2
Reading ld.so.1
Reading libCrun.so.1
Reading libm.so.1
Reading libw.so.1
Reading libc.so.1
Reading libdl.so.1
Reading libc_psr.so.1
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx)list
6 const int MAX_ARRAY = 10;
7 int numbers[MAX_ARRAY];
8 int index;
9 int sum;
10
11 // Stored values in the array.
12 for (index = 0; index < MAX_ARRAY; index++)
13 {
14 numbers[index] = index * index;
15 cout << "numbers[" << index << "]=" << numbers[index] << endl;
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) list
16 }
17
18 // The values in the array are summed.
19 sum = 0;
20 for (index = 0; index <= 1000; index++)
21 {
22 sum = sum + numbers[index];
23 }
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) list
24 cout << "Sum is " << sum << endl;
25
26 return 0;
27 }
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx)stop at 19
(2) stop at "example2.cpp":19
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) run
Running: example2
(process id 211921)
numbers[0]=0
numbers[1]=1
numbers[2]=4
numbers[3]=9
numbers[4]=16
numbers[5]=25
numbers[6]=36
numbers[7]=49
numbers[8]=64
numbers[9]=81
stopped in main at line 19 in file "example2.cpp"
19 sum = 0;
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) next
stopped in main at line 20 in file "example2.cpp"
20 for (index = 0; index <= 1000; index++)
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) next
stopped in main at line 22 in file "example2.cpp"
22 sum = sum + numbers[index];
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) next
stopped in main at line 22 in file "example2.cpp"
22 sum = sum + numbers[index];
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) next
stopped in main at line 22 in file "example2.cpp"
22 sum = sum + numbers[index];
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) next
stopped in main at line 22 in file "example2.cpp"
22 sum = sum + numbers[index];
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) next
stopped in main at line 22 in file "example2.cpp"
22 sum = sum + numbers[index];
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) status
(2) stop at "example2.cpp":19
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) dump
index = 4
sum = 14
MAX_ARRAY = 10
numbers = ARRAY
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx)trace sum
.
.
.
.
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx)cont
.
.
.
.
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) where
=>[1] main(), line 22 in "example2.cpp"
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx)quit
The core dump was caused by line 22. The loop goes beyond the
bounds of the array.
Here is another way to use the dbx to debug the same program example2.cpp:
hercules[27]% dbx example2
changes (topic)
The major new features of this release relative to 6.0 are:
o The dbx environment variable "cfront_demangling" has been removed.
The "-F" option to dbx (allow cfront-demangling) has also been removed.
Use an earlier version of dbx to get cfront support.
o This release continues to support Fortran intrinsics on Intel, even
though the Intel Fortran compiler was not shipped with WorkShop 6.
The next dbx release will not support Fortran intrinsics on Intel.
See also `help changes60'
To suppress this message, add the following line to your .dbxrc file:
dbxenv suppress_startup_message 5.0
Reading example2
Reading ld.so.1
Reading libCrun.so.1
Reading libm.so.1
Reading libw.so.1
Reading libc.so.1
Reading libdl.so.1
Reading libc_psr.so.1
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) run
Running: example2
(process id 213194)
numbers[0]=0
numbers[1]=1
numbers[2]=4
numbers[3]=9
numbers[4]=16
numbers[5]=25
numbers[6]=36
numbers[7]=49
numbers[8]=64
numbers[9]=81
signal SEGV (no mapping at the fault address) in main at line 22 in file "exampl
e2.cpp"
22 sum = sum + numbers[index];
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) list 20, 27
20 for (index = 0; index <= 1000; index++)
21 {
22 sum = sum + numbers[index];
23 }
24 cout << "Sum is " << sum << endl;
25
26 return 0;
27 }
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) where
=>[1] main(), line 22 in "example2.cpp"
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) print index
index = 520
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) print sum
sum = 1741044306
(/opt/SUNWspro/bin/../WS6U1/bin/sparcv9/dbx) quit
The core dump was produced by line 22 because the index was out of the range.
Through this tutorial, you have learned the basic procedure for using the dbx debugger. There is no in-lab exercise to be marked this week. We hope you will find the dbx debugger helpful in debugging programs for your class assignments and for in future classes. For more information on dbx, you can check out the references listed below.
|
Friday, 18-Nov-2005 15:17:22 CST |
|
| |
|
|