C H A P T E R 8 |
Evaluating and Displaying Data |
In dbx, you can perform two types of data checking:
This chapter is organized into the following sections:
This section discusses how to use dbx to evaluate variables and expressions.
If you are not sure which variable dbx is evaluating, use the which command to see the fully qualified name dbx is using.
To see other functions and files in which a variable name is defined, use the whereis command.
For information on the commands, see which Command and whereis Command.
When you want to evaluate or monitor a variable outside the scope of the current function:
An expression should follow current language syntax, with the exception of the meta syntax that dbx introduces to deal with scope and arrays.
To evaluate a variable or expression in native code, type:
You can use the print command to evaluate an expression, local variable, or parameter in Java code.
For more information, see print Command.
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). dbx can 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, dbx can use the information in the vtable to correctly determine an object's type.
You can use the print or display command with the -r (recursive) option. dbx displays all the data members directly defined by a class and those inherited from a base class.
These commands also take a -d or +d option that toggles the default behavior of the dbx environment variable output_derived_type.
Using the -d flag or setting the dbx environment variable output_dynamic_type to on when 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++.)
C++ lets you define functions with unnamed arguments. For example:
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:
To obtain the name assigned by the compiler, type the whatis command with the function name as its target.
For more information, see whatis Command.
To evaluate (or display) an unnamed function argument, type:
When you dereference a pointer, you ask for the contents of the container to which the pointer points.
To dereference a pointer, dbx displays the evaluation in the command pane; in this case, the value pointed to by t:
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 display command instructs dbx to monitor one or more specified expressions or variables. Monitoring continues until you turn it off with the undisplay command.
To display the value of a variable or expression each time the program stops, type:
You can monitor more than one variable at a time. The display command used with no options prints a list of all expressions being displayed.
For more information, see display Command.
dbx continues to display the value of a variable you are monitoring until you turn off display with the undisplay command. 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:
To turn off the display of all currently monitored variables, type:
For more information, see undisplay Command.
To assign a value to a variable, type:
You evaluate arrays the same way you evaluate other types of variables.
Here is a sample Fortran array:
To evaluate the array, use the print command. For example:
The dbx print command lets you evaluate part of a large array. Array evaluation includes:
You can slice an array, with or without striding. (The default stride value is 1, which means print each element.)
Array slicing is supported in the print and display commands for C, C++, and Fortran.
For each dimension of an array, the full syntax of the print command to slice the array is:
Expression that should evaluate to an array or pointer type. |
|
Length of the stride (the number of elements skipped is stride-expression-1). Defaults to 1. |
The first, last, and stride expressions are optional expressions that should evaluate to integers.
(dbx) 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..6:2] = [2] = 2 [4] = 4 [6] = 6 |
For each dimension of an array, the full syntax of the print command to slice the array is:
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.
(dbx) 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) 6 |
To specify rows and columns, type:
Here is an example of a two-dimensional, rectangular slice, with the default stride of 1 omitted.
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.
As illustrated in FIGURE 8-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 with row 201 and ends with 203. The second set of expressions, separated by a comma from the first, defines the slice for the second dimension. The slice begins with column 101 and ends with column 105.
When you instruct print to stride across a slice of an array, dbx evaluates 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 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.
As shown in FIGURE 8-2, 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:
Prints the entire array with default boundaries and default stride of 1. |
|
For a two-dimensional array, the following command prints the entire array.
To print every third element in the second dimension of a two-dimensional array, type:
Copyright © 2004, Sun Microsystems, Inc. All Rights Reserved.