| Debugging a Program With dbx |      | 
Evaluating and Displaying Data
In
dbx, you can perform two types of data checking:
- Evaluate data (
- Display data (
display) - Monitors the value of an expression each time the program stopsThis chapter is organized into the following sections:
Evaluating Variables and Expressions
This section discusses how to use
dbxto evaluate variables and expressions.Verifying Which Variable
dbxUsesIf you are not sure which variable
dbxis evaluating, use thewhichcommand to see the fully qualified namedbxis using.To see other functions and files in which a variable name is defined, use the
whereiscommand.For information on the commands, see "which Command" and"where Command" in the Using dbx Commands section of the Sun WorkShop online help.
Variables Outside the Scope of the Current Function
When you want to evaluate or monitor a variable outside the scope of the current function:
- Qualify the name of the function. See Qualifying Symbols With Scope Resolution Operators.
- or
- Visit the function by changing the current function. See Visiting Code.
Printing the Value of a Variable or an Expression
An expression should follow current language syntax, with the exception of the meta syntax that
dbxintroduces to deal with scope and arrays.To evaluate a variable or expression, type:
For more information, see "print Command" in the Using dbx Commands section of the Sun WorkShop online help.
Note dbxsupports the C++dynamic_castandtypeidoperators. When evaluating expressions with these two operators,dbxmakes calls to certain rtti functions made available by the compiler. If the source doesn't explicitly use the operators, those functions might not have been generated by the compiler, anddbxfails to evaluate the expression.
Printing C++
In C++ an object pointer has two types, its static type (what is defined in the source code) and its dynamic type (what an object was before any casts were made to it).
dbxcan sometimes provide you with the information about the dynamic type of an object.In general, when an object has a virtual function table (a vtable) in it,
dbxcan use the information in the vtable to correctly determine an object's type.You can use the
displaycommand with the-r(recursive) option.dbxdisplays all the data members directly defined by a class and those inherited from a base class.These commands also take a
-dor+doption that toggles the default behavior of thedbxenvironment variableoutput_derived_type.Using the
-dflag or setting thedbxenvironment variableoutput_dynamic_typetoonwhen there is no process running generates a "program is not active" error message because it is not possible to access dynamic information when there is no process. An "illegal cast on class pointers" error message is generated if you try to find a dynamic type through a virtual inheritance. (Casting from a virtual base class to a derived class is not legal in C++.)Evaluating Unnamed Arguments in C++ Programs
C++ lets you define functions with unnamed arguments. For example:
void tester(int){};main(int, char **){tester(1);};Though you cannot use unnamed arguments elsewhere in a program, the compiler encodes unnamed arguments in a form that lets you evaluate them. The form is as follows, where the compiler assigns an integer to
%n:
_ARG%nTo obtain the name assigned by the compiler, type the
whatiscommand with the function name as its target.
(dbx)whatis testervoid tester(int _ARG1);(dbx)whatis mainint main(int _ARG1, char **_ARG2);For more information, see "whatis Command" and "whatis Command Used With C++" in the Using dbx Commands section of the Sun WorkShop online help.
To evaluate (or display) an unnamed function argument, type:
(dbx)print _ARG1_ARG1 = 4Dereferencing Pointers
When you dereference a pointer, you ask for the contents of the container to which the pointer points.
To dereference a pointer,
dbxdisplays the evaluation in the command pane; in this case, the value pointed to byt:
(dbx)print *t*t = {a = 4}You can also dereference a pointer in the Sun WorkShop Debugging window. See "Dereferencing a Pointer" in the Using the Debugging Window section of the Sun WorkShop online help.
Monitoring Expressions
Monitoring the value of an expression each time the program stops is an effective technique for learning how and when a particular expression or variable changes. The
displaycommand instructsdbxto monitor one or more specified expressions or variables. Monitoring continues until you turn it off with theundisplaycommand.To display the value of a variable or expression each time the program stops, type:
displayexpression, ...You can monitor more than one variable at a time. The
displaycommand used with no options prints a list of all expressions being displayed.For more information, see "display Command" in the Using dbx Commands section of the Sun WorkShop online help.
You can also monitor the value of an expression in the Sun WorkShop Debugging window. See "Adding an Expression" in the Using the Debugging Window section of the Sun WorkShop online help.
Turning Off Display (Undisplay)
dbxcontinues to display the value of a variable you are monitoring until you turn off display with theundisplaycommand. You can turn off the display of a specified expression or turn off the display of all expressions currently being monitored.To turn off the display of a particular variable or expression, type:
undisplayexpressionTo turn off the display of all currently monitored variables, type:
For more information, see "undisplay Command" in the Using dbx Commands section of the Sun WorkShop online help.
You can turn off the display of a specified expression or all expressions in the Sun WorkShop Debugging window. See "Removing an Expression" in the Using the Debugging Window section of the Sun WorkShop online help.
Assigning a Value to a Variable
To assign a value to a variable, type:
assignvariable=expressionYou can also a value to a variable in the Sun WorkShop Debugging window. See "Changing Data Values" in the Using the Debugging Window section of the Sun WorkShop online help.
Evaluating Arrays
You evaluate arrays the same way you evaluate other types of variables.
Here is a sample Fortran array:
integer*4 arr(1:6, 4:7)To evaluate the array, type:
The
dbx
- Array Slicing - Prints any rectangular, n-dimensional box of a multidimensional array.
- Array Striding - Prints certain elements only, in a fixed pattern, within the specified slice (which may be an entire array).
You can slice an array, with or without striding. (The default stride value is 1, which means print each element.)
Array Slicing
Array slicing is supported in the
displaycommands for C, C++, and Fortran.Array Slicing Syntax for C and C++
For each dimension of an array, the full syntax of the
[first-expression..last-expression:stride-expression]where:
The first, last, and stride expressions are optional expressions that should evaluate to integers.
For example:
print arr[2..4]arr[2..4] =[2] = 2[3] = 3[4] = 4(dbx)print arr[..2]arr[0..2] =[0] = 0[1] = 1[2] = 2(dbx)print arr[2..6:2]arr[2..8:2] =[2] = 2[4] = 4[6] = 6Array Slicing Syntax for Fortran
For each dimension of an array, the full syntax of the
(first-expression..last-expression:stride-expression)where:
The first, last, and stride expressions are optional expressions that should evaluate to integers. For an n-dimensional slice, separate the definition of each slice with a comma.
For example:
print arr(2:6)arr(2:6) =(2) 2(3) 3(4) 4(5) 5(6) 6(dbx)print arr(2:6:2)arr(2:6:2) =(2) 2(4) 4(6) 6To specify rows and columns, type:
To print row 3, type:
(dbx)print a(3:3,1:4)'ShoSli'MAIN'a(3:3, 1:4) =(3,1) 31(3,2) 32(3,3) 33(3,4) 34(dbx)
To print column 4, type:
(dbx)print a(1:3,4:4)'ShoSli'MAIN'a(3:3, 1:4) =(1,4) 14(2,4) 24(3,4) 34(dbx)
Slices
Here is an example of a two-dimensional, rectangular slice, with the default stride of 1 omitted.
print arr(201:203, 101:105)This command prints a block of elements in a large array. Note that the command omits stride-expression, using the default stride value of 1.
The first two expressions (
201:203) specify a slice in the first dimension of this two-dimensional array (the three-row column). The slice starts at the row201and ends with203. The second set of expressions, separated by a comma from the first, defines the slice for the 2nd dimension. The slice begins at column101and ends after column 105.Strides
When you instruct
dbxevaluates certain elements in the slice only, skipping over a fixed number of elements between each one it evaluates.The third expression in the array slicing syntax, stride-expression, specifies the length of the stride. The value of stride-expression specifies the elements to print; the number of elements skipped is equal to stride-expression
-1. The default stride value is 1, meaning: evaluate all of the elements in the specified slices.Here is the same array used in the previous example of a slice. This time the print command includes a stride of 2 for the slice in the second dimension.
A stride of 2 prints every second element, skipping every other element.
For any expression you omit, print takes a default value equal to the declared size of the array. Here are examples showing how to use the shorthand syntax.
For a one-dimensional array, use the following commands:
For a two-dimensional array, the following command prints the entire array.
print arrTo print every third element in the second dimension of a two-dimensional array, type:
print arr (:,::3)
| Sun Microsystems, Inc. Copyright information. All rights reserved. Feedback | Library | Contents | Previous | Next | Index |