Go to main content
Oracle® Developer Studio 12.6: Debugging a Program with dbx

Exit Print View

Updated: June 2017
 
 

Evaluating Variables and Expressions

This section discusses how to use dbx 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.

For information on the commands, see which Command and whereis Command.

Variables Outside the Scope of the Current Function

Printing the Value of a Variable, Expression, or Identifier

An expression should follow current language syntax, with the exception of the meta syntax that dbx introduces to deal with scope and arrays.

Use the print command to evaluate a variable or expression in native code:

print expression

You can use the print command to evaluate an expression, local variable, or parameter in Java code.

For more information, see print Command.


Note -  dbx supports the C++ dynamic_cast and typeid operators. When evaluating expressions with these two operators, dbx makes calls to certain runtime type identification functions made available by the compiler. If the source does not explicitly use the operators, those functions might not have been generated by the compiler, and dbx fails to evaluate the expression.

Printing C++ Pointers

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 command, display command, or watch 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 dbxenv variable output_dynamic_type.

Using the -d flag or setting the dbxenv variable output_dynamic_type to on when no process is running generates a program is not active error message. As when you are debugging a core file, accessing dynamic information is not possible 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

You can define functions in C++ 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%n

To obtain the name assigned by the compiler, use the whatis command with the function name as its target.

(dbx) whatis tester
void tester(int _ARG1);
(dbx) whatis main
int main(int _ARG1, char **_ARG2);

For more information, see whatis Command.

To evaluate (or display) an unnamed function argument:

(dbx) print _ARG1
_ARG1 = 4

Dereferencing Pointers

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:

(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 stop it with the undisplay command. The watch command evaluates and prints expressions at every stopping point in the scope current at that stop point.

Use the display 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.

For more information, see display Command.

Use the watch command to watch the value of the expression at every stopping point:

watch expression, ...

For more information, see watch Command.

Stop the Display (Undisplaying)

dbx continues to display the value of a variable you are monitoring until you stop the display with the undisplay command. You can stop the display of a specified expression or stop the display of all expressions currently being monitored.

To stop the display of a particular variable or expression:

undisplay expression

To stop the display of all currently monitored variables:

undisplay 0

For more information, see undisplay Command.