Debugging C++ Applications Using HP WDB HP Part Number: 766162-001 Published: March 1403 Edition: 2
© Copyright 2014 Hewlett-Packard Development Company, L.P. Confidential computer software. Valid license from HP required for possession, use or copying. Consistent with FAR 12.211 and 12.212, Commercial Computer Software, Computer Software Documentation, and Technical Data for Commercial Items are licensed to the U.S. Government under vendor's standard commercial license. The information contained herein is subject to change without notice.
Contents About this document.................................................................................................................4 Intended audience....................................................................................................................4 Document conventions and symbols............................................................................................4 Terms and definitions.......................................................................................
About this document This document describes how to debug C++ programs using HP Wildebeest Debugger (WDB). Intended audience This document is intended for C++ programmers who use HP WDB to debug their applications. Readers of this document must be familiar with the concepts of C++ programming and basic debugging commands that HP WDB supports. Document conventions and symbols Table 1 lists the conventions and symbols used in this white paper.
Related information The following documents provide additional information about HP WDB: Document Location HP WDB Release Notes /opt/langtools/wdb/doc/ Debugging with GDB /opt/langtools/wdb/doc/gdb.pdf GDB Quick Reference Card /opt/langtools/wdb/doc/refcard_a4.pdf /opt/langtools/wdb/doc/refcard_a3.pdf /opt/langtools/wdb/doc/refcard.pdf /opt/langtools/wdb/doc/advrefcard_a4.pdf /opt/langtools/wdb/doc/advrefcard_a3.pdf /opt/langtools/wdb/doc/advrefcard.
Introduction HP Wildebeest Debugger (WDB) is an HP-supported implementation of the GNU Project Debugger (GDB). HP WDB debugs applications written in C, C++, and FORTRAN. HP WDB also supports Java core-file debugging and mixed-language debugging for C, C++ and Java. HP WDB debugs C++ programs compiled using HP aC++ compiler on Itanium®-based systems running HP-UX 11i v2 or HP-UX 11i v3 operating systems, and HP 9000 systems running HP-UX 11i v1, HP-UX 11i v2, or HP-UX 11i v3 operating systems.
specify the name of the variable or the object to be mentioned in the program with appropriate scoping in the WDB command. HP WDB can also display mangled or demangled name as per your requirement. Table 3 shows the commands to display mangled or demangled names. Table 1 Commands to Display Mangled or Demangled Names Command Purpose set print demangle [on | off] Displays the C++ symbols in mangled or demangled form. The default value is on. C++ symbols will be displayed as demangled.
HP WDB scope resolution operator, double colon (::) can also be used as WDB's own scope operator as explained in the following text. The C++ scope resolution operator has the same precedence as WDB’s own scope operator.
3 4 5 6 7 8 static int x=40; void func(void) { static int x=30; printf("%d\n", x); } The WDB output snippet for this program is as shown below: (gdb) b main Breakpoint 1 at 0x40009f0:0: file a.C, line 8 from /C++WhitePaper/scope/a.out. (gdb) r Starting program: /C++WhitePaper/scope/a.out Breakpoint 1, main () at a.C:8 8 int x=20; (gdb) n 9 func(); (gdb) p x $1 = 20 (gdb) p 'a.C'::x $2 = 10 (gdb) p 'b.C'::x $3 = 40 (gdb) s func () at b.
Example 3 Sample Program for Debugging C++ Scope Resolution Operator 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include
$2 = {baseElementMember = 3} Since the object of derived element is passed to the function foo, we can display the complete derived object by using type casting. (gdb) p *(DerivedElement*)p No symbol "DerivedElement" in current context. This did not work since “DerivedElement” is a defined in the scope of “Manager” structure and so is not available outside.
(gdb) p m $1 = {e = {employeeMember = 3}, static managerMember = 2} (gdb) p m.
Example 5 Sample Program for Debugging a Member Function 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include using namespace std; class myclass { public: static int s_member; static void s_func(void); int member; void func(void); }; int myclass::s_member = 1; void myclass::s_func() { s_member++; cout << s_member << endl; } void myclass::func(void) { this->member++; cout << member << endl; } int main() { myclass myobj1, myobj2; myobj1.member = myobj2.
type = class myclass { public: int member; static int s_member; void func(); static void s_func(); } * const NOTE: • You cannot set a breakpoint on object-specific member functions. • You can access this pointer from WDB command line while in a member function as shown in the above example. Using this pointer, the data members of that class can be accessed from the WDB command line.
(gdb) p objB $1 = { = {dataA = 20}, dataB = 40} (gdb) call objB.setA(100) (gdb) p objB $2 = { = {dataA = 100}, dataB = 40} Example 7 shows a sample program for debugging inheritance.
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.
lookup within a namespace. It also recognizes using directives and aliases, and using declarations within namespaces. Example 9 shows how scope resolution operator is used for debugging programs having different namespaces: Example 9 Using Scope Resolution Operator to Debug Programs having Different Namespaces When the debugger has a list of possible resolutions for a given symbol, it displays a menu that shows all names fully qualified whenever namespaces are involved.
Example 10 Sample Program and Output Log for Debugging Namespaces 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include using namespace std; char x = 'a'; char y = 'b'; void func(void){ printf("I am global.\n"); } namespace first { int x = 5; int y = 10; void func(void){ printf("I am first.\n"); } } namespace second { double x = 3.1416; double y = 2.7183; void func(void){ printf("I am second.
(gdb) p &y [1] y [2] first::y [3] second::y > 3 $2 = (double *) 0x40010188 (gdb) call func() [1] func() [2] first::func() [3] second::func() > 1 I am global. (gdb) show namespaces-enabled Enabling of namespace support is on. (gdb) set namespaces-enabled off (gdb) p x $3 = 97 'a' (gdb) ptype y type = char (gdb) ptype first::y type = int (gdb) p second::y $4 = 2.7183000000000002 (gdb) call func() I am global.
The following are available to debug inline functions: • on Enables the inline debugging feature without the inline breakpoints options on HP Integrity systems. This command is enabled by default. • off Disables the inline debugging feature. For this option to be effective, you must enter this command before attaching the debugger to the application. • inline_bp_all Enables inline debugging feature.
Example 12 Debugging using inline_bp_individual Option (gdb) set inline-debug inline_bp_individual (gdb) file inline_func Reading symbols from inline_func...done. (gdb) b ifunc Breakpoint 1 at 0x4000ee0:0: file inline_func.C, line 18 from /tmp/inline_func. Breakpoint 2 at 0x4001590:1: file inline_func.C, line 18 from /tmp/inline_func. Breakpoint 3 at 0x4001c40:1: file inline_func.C, line 18 from /tmp/inline_func. Multiple breakpoints were set. Use the "delete" command to delete unwanted breakpoints.
The display of dynamic type information is supported for C++ class pointers and references. All other types display static type information. In addition, if the dynamic type of an object cannot be determined, the debugger defaults to the use of static type information. To display dynamic type information, the object's static type must have at least one virtual function defined as a part of its interface (either the virtual function it introduced or the virtual function it inherited from a base class).
Example 13 Sample Program for Displaying Static and Dynamic Type Information 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include struct Manager { struct BaseElement { int baseElementMember; virtual void get() {printf("%d\n",baseElementMember);} }; struct DerivedElement : BaseElement { int derivedElementMember; void get() {printf("%d\n",derivedElementMember);} }; }; void foo(struct Manager::BaseElement * p) { printf("%p\n", p); } int main() { Manager::DerivedElement d; d.
Example 14 Sample Program for Debugging a Virtual Function 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include
resolve ambiguous references to overloaded function name func by setting breakpoints in any of the following ways: • By specifying the particular version of the function with arguments, such as break func(types). • By specifying the break func, which causes WDB to offer a menu of numbered choices for all versions of function func present in the program, and then prompt you to make the choice with the `>' prompt. Example 15 shows a sample program for debugging overloaded functions.
Overload resolution is always performed, unless you have turned off the option using set overload-resolution off. When overload resolution is enabled, for overloaded functions, the HP WDB evaluates the arguments and searches for a function whose signature matches the argument types, using the standard C++ conversion rules.
catch-points which can cause the debugger to stop for C++ Exceptions. You can use any of the following commands to catch C++ exceptions: Command Description catch throw WDB stops program when any C++ exception is thrown. catch catch WDB stops program when any C++ exception is caught. Use the following command to display the exceptions that can be caught. info catch [ all | * | [:]] Use info catch for the selected frame.
1 breakpoint breakpoint 2 catch throw catchpoint 3 catch catch (gdb) c Continuing. keep y 0x0000000004001ae1 in main at exception.C:17 already hit 1 time keep y exception throw already hit 1 time keep y exception catch Catchpoint 2 (exception thrown), throw location exception.C:11, catch location exception.C:19 0x200000007b3c3760:0 in _Unwind_RaiseExceptionHook(unsigned long long,unsigned long long,unsigned long long)+0 () from /usr/lib/hpux32/libunwind.so.
Example 17 Sample Program for Debugging Template Class 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 template class A { public : A(T arg) { data = arg; } T foo(); private : T data; }; template T A::foo() { return data++; } int main() { Aa(10); A b(3.14); int i; double f; i = a.foo(); f = b.foo(); return 0; } The WDB output snippet for this program is as shown below: (gdb) b A::foo // Breakpoint on member function of template class.
Example 18 Sample Program for Debugging Template Member Function 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 #include class A { public: int i; }; typedef class A clsA; template class B { public: int j; void funB(T) { printf("generic"); return; } }; template <> class B { public: int j; void funB(clsA data) { j=data.i; } }; int main() { clsA objA; class B objB; objA.i = 10; objB.funB(objA); printf("objB.j = %d",objB.
(gdb) q Example 19 shows a sample program for debugging a function template: Example 19 Sample Program for Debugging a Function Template 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include using namespace std; template T GetMax (T a, T b) { T result; result = (a >b)? a : b; return (result); } int main () { int i=5, j=6, k; long l=10, m=5, n; k=GetMax(i,j); n=GetMax(l,m); cout << k << endl; cout << n << endl; return 0; } The WDB output snippet for this program is as
NOTE: A change in the setting of this subcommand will not take effect until the next time symbols for a file are loaded. Example 20 shows a sample program for debugging opaque type. Example 20 Sample Program for Debugging Opaque Type 1. C: 1 2 3 4 5 6 7 typedef class Manager * pManager; void func(pManager); int main() { pManager pm ; func(pm); return 0; } 2. C: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include
Example 21 Sample Program for Debugging Anonymous Unions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include using namespace std; struct { char title[50]; char author[50]; union { float dollars; int yen; }; } book; int main() { strcpy(book.title, "mybook"); strcpy(book.author, "me"); book.dollars = 2.
NOTE: HP WDB supports debugging of C++ applications with new() and delete() calls only if they internally call malloc() and free() functions. Example 22 shows a sample program for debugging dynamic memory allocation and de-allocation.
elementary operations, such as copying and assignment. STL algorithms are independent of containers, which significantly reduces the complexity of the library. HP WDB enables you to debug a C++ application that uses STL. It is possible to call STL library function calls from the WDB command line by compiling the C++ application with the +d compilation option. The +d compilation option ensures that the functions do not expand inline, which is important while debugging STLs.
NOTE: While debugging an application, only those functions of STL which are used at least once in the application can be called at WDB command line. Debugging C++11 specific features Debugging char16_t and char32_t Types C++11 standard has added new character types, char16_t for 16-bit code units, and char32_t for 32-bit code units. You can use the ptype command to know the new character types.
Debugging enum class C++11 allows a special classification of enumeration called scoped enum. This is expressed using the enum class declaration. This enum class can be debugged using ptype, print commands.
type = char enum class GECC {GECC::aa4, GECC::bb4} (gdb) pt nsec type = enum class myNS::EC {myNS::EC::green, myNS::EC::yellow, myNS::EC::red} (gdb) p ECC::yellow $1 = ECC::yellow (gdb) p ecc $1 = ECC::yellow (gdb) s inline AA::fun(float,int) (y=1) at my_enum_class.
Example 27 (page 39) shows a sample program for debugging template functions: Example 27 Sample program for debugging template functions 1 template void fun(int x, T ...args) 2 { 3 int a[] = {0, args..., 5}; 4 int y; 5 y = x; 6 } 7 8 int main(int argc, char** argv) 9 { 10 fun(1, 2, 3, 4); 11 fun(30,40); 12 13 return 0; 14 } The following is the WDB output snippet for this program: (gdb) b fun [0] cancel [1] all [2] fun< >(int,...) at my_variadic.
[2] fun< >(int,...) at my_variadic.cc:2 [3] fun< >(int,...) at my_variadic.cc:2 > 2 1 template void fun(int x, T ...args) 2 { 3 int a[] = {0, args..., 5}; 4 int y; 5 y = x; 6 } 7 8 int main(int argc, char** argv) 9 { 10 fun(1, 2, 3, 4); (gdb) q HP WDB commands or variables used in C++ application debugging Table 4 shows the summary of HP WDB commands to debug C++ applications.
Table 2 Summary of GDB Commands to Debug C++ Applications (continued) GDB Command Purpose Shows the current setting of overload resolution. show overload-resolution set print static-members [on | Prints static members when displaying a C++ object. The default is on. off] Do not print static members when displaying a C++ object. show print static-members set namespaces-enabled [on | off] Sets enabling of namespace support. The default value is on.
(docsfeedback@hp.com). Include the document title and part number, version number, or the URL when submitting your feedback.
Index A V Actions, 6 Actual name, 4 Anonymous unions, 32 Virtual function, 23 C C++ scope resolution operator, 9 C++ Templates, 28 D Debugging summary, 40 Display class information, 11 Display demangled names, 6 Display mangled names, 6 Display object information, 11 Display run-time information, 21 Display static information, 21 Dynamic memory issues, 33 E Exceptions, 26 Expressions, 6 H HP WDB scope resolution operator, 8 I Inherited classes, 14 Inline functions, 19 inline_bp_all option, 20 inline