Debugging C++ Applications Using HP WDB (766162-001, March 2014)

Debugging expressions with type-casting
Converting an expression of a given type into another type is known as type-casting. WDB
expression allows type-casting of an object or a variable. It displays the value of the object or the
variable according to the specified type. The syntax for type-casting is the same as in C
programming. In general, you can use the following command to print an object, after type-casting
the object to a new type:
print (<new_type>) <object_name>
Example 8 shows a sample program for debugging expressions with type-casting.
Example 8 Sample Program for Debugging Expressions with Type-Casting
1 #include <stdio.h>
2
3 struct Manager {
4 struct BaseElement {
5 int baseElementMember;
6 virtual void get() {printf("%d\n",baseElementMember);}
7 };
8
9 struct DerivedElement : BaseElement {
10 int derivedElementMember;
11 void get() {printf("%d\n",derivedElementMember);}
12 };
13 };
14
15 void foo(struct Manager::BaseElement * p) {
16 printf("%p\n", p);
17 }
18
19 int main() {
20 Manager::DerivedElement d;
21 d.baseElementMember = 3;
22 d.derivedElementMember = 4;
23 foo(&d);
24 return 0;
25 }
The WDB output snippet for this program is as shown below:
(gdb) p *p
$2 = {baseElementMember = 3}
(gdb) p *(Manager::DerivedElement*)p // Typecasting
$3 = {<Manager::BaseElement>={baseElementMember = 3},
derivedElementMember = 4}
NOTE: Type-casting of an object or a variable to some other type at the WDB prompt does not
change the object or the variable permanently for the rest of the program execution. It only prints
the value of the object or variable according to the specified type. However, to get the correct
value, ensure that you type-cast the object correctly.
Debugging namespaces
Namespaces allow to group entities, such as classes, objects, and functions under a name.
Therefore, the global scope can be divided into "sub-scopes", each one with its own name.
HP WDB provides full support for debugging namespaces. You need not use fully-qualified names
to access symbols within a namespace. The debugger can compile a list of namespaces active in
the scope that you are in, and when possible, choose an appropriate symbol. The debugger
recognizes using declarations, directives, namespace aliases, nested namespaces, and unqualified
16