Debugging C++ Applications Using HP WDB (766162-001, March 2014)
Example 13 Sample Program for Displaying Static and Dynamic Type Information
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) n
23 foo(&d);
(gdb) s
foo (p=0x7ffff010) at rtti.C:16
16 printf("%p\n", p);
(gdb) info rtti 0x7ffff010
RTTI: class Manager::DerivedElement
(gdb) show print object
Printing of object's derived type based on vtable info is off.
// Static type info
(gdb) p *p
$1 = {baseElementMember = 3, __vfp = 0x40010040}
(gdb) set print object on // Dynamic type info
(gdb) p *p
$2 = (Manager::DerivedElement) {<Manager::BaseElement> = {
baseElementMember = 3, __vfp = 0x40010040}, derivedElementMember = 4}
In the above debug log, when print object is set to ON the actual object contents are printed.
Debugging a virtual function
A virtual function is a member function of a class, whose functionality can be over-ridden in its
derived classes. It is a function that is declared as virtual in the base class using the virtual keyword.
The whole function body can be replaced with a new set of implementation in the derived class.
When debugging a C++ application using HP WDB, it is possible to call a virtual member function
from WDB command line similar to any other member function.
Example 14 shows a sample program for debugging a virtual function.
Debugging a virtual function 23