Sun Studio 12 Update 1: Debugging a Program With dbx

Debugging With C++ Templates

dbx supports C++ templates. You can load programs containing class and function templates into dbx and invoke any of the dbx commands on a template that you would use on a class or function, such as:

Template Example

The following code example shows the class template Array and its instantiations and the function template square and its instantiations.


 1        template<class C> void square(C num, C *result)
 2        {
 3             *result = num * num;
 4        }
 5    
 6        template<class T> class Array
 7        {
 8        public:
 9              int getlength(void)
10             {
11                    return length;
12              }
13    
14              T & operator[](int i)
15              {
16                    return array[i];
17             }
18    
19              Array(int l)
20              {
21                    length = l;
22                    array = new T[length];
23              }
24    
25              ~Array(void)
26              {
27                    delete [] array;
28              }
29    
30        private:
31              int length;
32              T *array;
33        };
34    
35        int main(void)
36        {
37              int i, j = 3;
38             square(j, &i);
39    
40             double d, e = 4.1;
41              square(e, &d);
42    
43              Array<int> iarray(5);
44              for (i = 0; i < iarray.getlength(); ++i)
45              {
46                   iarray[i] = i;
47             }
48    
49              Array<double> darray(5);
50              for (i = 0; i < darray.getlength(); ++i)
51             {
52                    darray[i] = i * 2.1;
53              }
54    
55              return 0;
56        }

In the example:

Commands for C++ Templates

Use these commands on templates and template instantiations. Once you know the class or type definitions, you can print values, display source listings, or set breakpoints.

whereis name Command

Use the whereis command to print a list of all occurrences of function or class instantiations for a function or class template.

For a class template:


(dbx) whereis Array
member function: `Array<int>::Array(int)
member function: `Array<double>::Array(int)
class template instance: `Array<int>
class template instance: `Array<double>
class template: `a.out`template_doc_2.cc`Array

For a function template:


(dbx) whereis square
function template instance: `square<int>(__type_0,__type_0*)
function template instance: `square<double>(__type_0,__type_0*)

The __type_0 parameter refers to the 0th template parameter. A __type_1 would refer to the next template parameter.

For more information, see whereis Command.

whatis name Command

Use the whatis command to print the definitions of function and class templates and instantiated functions and classes.

For a class template:


(dbx) whatis -t Array
template<class T> class Array
To get the full template declaration, try `whatis -t Array<int>’;

For the class template’s constructors:


(dbx) whatis Array
More than one identifier ’Array’.
Select one of the following:
 0) Cancel
 1) Array<int>::Array(int)
 2) Array<double>::Array(int>
> 1
Array<int>::Array(int 1);

For a function template:


(dbx) whatis square
More than one identifier ’square’.
Select one of the following:
 0) Cancel
 1) square<int(__type_0,__type_0*)
 2) square<double>(__type_0,__type_0*)
> 2
void square<double>(double num, double *result);

For a class template instantiation:


(dbx) whatis -t Array<double>
class Array<double>; {
public:
    int Array<double>::getlength()
    double &Array<double>::operator [](int i);
    Array<double>::Array<double>(int l);
    Array<double>::~Array<double>();
private:
    int length;
    double *array;
};

For a function template instantiation:


(dbx) whatis square(int, int*)
void square(int num, int *result);

For more information, see whatis Command.

stop inclass classname Command

To stop in all member functions of a template class:


(dbx)stop inclass Array
(2) stop inclass Array

Use the stop inclass command to set breakpoints at all member functions of a particular template class:


(dbx) stop inclass Array<int>
(2) stop inclass Array<int>

For more information, see stop Command and inclass classname [-recurse | -norecurse].

stop infunction name Command

Use the stop infunction command to set breakpoints at all instances of the specified function template:


(dbx) stop infunction square
(9) stop infunction square

For more information, see stop Command and infunction function.

stop in function Command

Use the stop in command to set a breakpoint at a member function of a template class or at a template function.

For a member of a class template instantiation:


(dbx) stop in Array<int>::Array(int l)
(2) stop in Array<int>::Array(int)

For a function instantiation:


(dbx) stop in square(double, double*)
(6) stop in square(double, double*)

For more information, stop Command and in function.

call function_name(parameters) Command

Use the call command to explicitly call a function instantiation or a member function of a class template when you are stopped in scope. If dbx is unable to determine the correct instance, it displays a numbered list of instances from which you can choose.


(dbx) call square(j,&i)

For more information, see call Command.

print Expressions

Use the print command to evaluate a function instantiation or a member function of a class template:.


(dbx) print iarray.getlength()
iarray.getlength() = 5

Use print to evaluate the this pointer.


(dbx) whatis this
class Array<int> *this;
(dbx) print *this
*this = {
    length = 5
    array   = 0x21608
}

For more information, see print Command.

list Expressions

Use the list command to print the source listing for the specified function instantiation.


(dbx) list square(int, int*)

For more information, see list Command.