Debugging a Program With dbx

Chapter 8 Evaluating and Displaying Data

In dbx you can perform two types of data checking:

This chapter describes how to evaluate data, display the value of expressions, variables, and other data structures, and assign a value to an expression.

The chapter is organized into the following sections:

Evaluating Variables and Expressions

This section shows how to evaluate variables and expressions.

Verifying Which Variable dbx Uses

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.

Variables Outside the Scope of the Current Function

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:


print 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. 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 figure out what an object's type is.

You can use the commands print or display 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 dbxenv output_derived_type.

Using the --d flag or setting the dbxenv 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 baseclass to a derived class is not legal in C++).

Evaluating Unnamed Arguments in C++ Programs

C++ allows you to 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, dbx encodes unnamed arguments in a form that allows you to evaluate them. The form is:


_ARG_%n_

where dbx assigns an integer to %n.

To obtain an assigned argument name from dbx, issue the whatis command with the function name as its target:


(dbx) whatis tester
void tester(int _ARG_0_);
(dbx) whatis main
int main(int _ARG_1_, char **_ARG_2_);

To evaluate (or display) an unnamed function argument,


(dbx) print _ARG_1_
_ARG_1_ = 4

Dereferencing Pointers

When you dereference a pointer, you ask for the contents of the container the pointer points to.

To dereference a pointer, dbx prints the evaluation in the command pane; in this case, the value pointed to by t:


(dbx) print *t
*t = {
a = 4
}

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 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:


display expression,
...     

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:


display

Turning Off Display (Undisplay)

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:


undisplay expression

To turn off the display of all currently monitored variables:


undisplay 0

Assigning a Value to a Variable

To assign a value to a variable:


assign variable = expression

Evaluating Arrays

You evaluate arrays the same way you evaluate other types of variables. For more information on working with arrays in Fortran, see Chapter 17, Debugging Fortran Using dbx."

Here is an example, using an array:


integer*4 arr(1:6, 4:7)

To evaluate the array:


print arr(2,4)

Array Slicing for Arrays

The dbx print command allows you to 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).

Syntax for Array Slicing and Striding

Array-slicing is supported in the print and display commands for C, C++, and Fortran.

Array-slicing syntax for C and C++, where:


print arr-exp [first-exp
 .. last-exp : stride-exp
]

arr-exp

Expression that should evaluate to an array or pointer type. 

first-exp

First element to be printed. Defaults to 0. 

last-exp

Last element to be printed. Defaults to its upper bound. 

stride-exp

Stride. 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..8: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 following:


(dbx) print arr(exp1:exp2:exp3)

exp1

start_of_slice 

exp2

end_of_slice 

exp3

length_of_stride (the number of elements skipped is exp3 - 1)

For an n-dimensional slice, separate the definition of each slice with a comma:


(dbx) print arr(exp1:exp2:exp3, exp1:exp2:exp3,...)
 

Slices

Here is an example of a two-dimensional, rectangular slice, with the default stride of 1 omitted:


print arr(201:203, 101:105)

Graphic

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 row 201 and ends with 203. The second set of expressions, separated by a comma from the first, defines the slice for the 2nd dimension. The slice begins at column 101 and ends after column 105.

Strides

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, (exp3), specifies the length of the stride. The value of exp3 specifies the elements to print; the number of elements skipped is equal to exp3 -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.


print arr(201:203, 101:105:2)

Graphic

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.

print arr

Prints entire array, default boundaries. 

print arr(:)

Prints entire array, default boundaries and default stride of 1. 

print arr(::exp3)

Prints the whole array with a stride of exp3.

For a one-dimensional array:

For a two-dimensional array the following command prints the entire array:


print arr

To print every third element in the second dimension of a two-dimensional array:


print arr (:,::3)

Command Reference

print

To print the value of the expression(s) expression:


print expression, ...

In C++, to print the value of the expression expression including its inherited members:


print -r expression

In C++, to not print the expression expression's inherited members when the dbxenv output_inherited_members is on:


print +r expression

In C++, to print the derived type of expression expression instead of the static type:


print -d [-r] expression

In C++, to print the static type of expression expression when the dbxenv output_dynamic_type is on:


print +d [-r] expression

To call the prettyprint function:


print -p expression

To not call the prettyprint function when the dbxenv output_pretty_print is on:


print +p expression

To not print the left hand side (the variable name or expression). If the expression is a string (char *), do not print the address, just print the raw characters of the string, without quotes:


print -l expression

To use format as the format for integers, strings, or floating point expressions:


print -f format expression

To use the given format without printing the left hand side (the variable name or expression):


print -F format expression

To signal the end of flag arguments. Useful if expression starts with a plus or minus:;


print -- expression