Sun Studio 12 Update 1:使用 dbx 调试程序

使用 C++ 模板调试

dbx 支持 C++ 模板。可将包含类和函数模板的程序装入 dbx 中,并对要用于类或函数的模板调用任何 dbx 命令,例如:

模板示例

以下代码示例中显示了类模板 Array 及其实例以及函数模板 square 及其实例。


 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        }

该示例中:

C++ 模板的命令

在模板和模板实例中使用这些命令。知道类或类型定义后,便可以打印值、显示源码列表或设置断点。

whereis name 命令

使用 whereis 命令可打印函数模板或类模板的函数或类实例的所有具体值列表。

对于类模板:


(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

对于函数模板:


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

__type_0 参数引用 0 号模板参数。__type_1 引用下一个模板参数。

有关更多信息,请参见whereis 命令

whatis name 命令

使用 whatis 命令可打印函数模板和类模板的定义以及实例化的函数和类。

对于类模板:


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

对于类模板的构造函数:


(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);

对于函数模板:


(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);

对于类模板实例化:


(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;
};

对于函数模板实例化:


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

有关更多信息,请参见whatis 命令

stop inclass classname 命令

要在模板类的所有成员函数中停止


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

可使用 stop inclass 命令在特定模板类的所有成员函数中设置断点:


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

有关更多信息,请参见stop 命令inclass classname [-recurse | -norecurse]

stop infunction name 命令

可使用 stop infunction 命令在指定函数模板的所有实例中设置断点


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

有关更多信息,请参见stop 命令infunction function

stop in function 命令

可使用 stop in 命令在模板类的成员函数或在模板函数中设置断点。

对于类模板实例化的成员:


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

对于函数实例化:


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

有关更多信息,请参见stop 命令in function

call function_name( parameters) 命令

可使用 call 命令在作用域中停止时显式调用函数实例或类模板的成员函数。如果 dbx 无法确定正确的实例,它会显示一个带编号的实例列表,您可以从列表中进行选择。


(dbx) call square(j,&i)

有关更多信息,请参见call 命令

print 表达式

可使用 print 命令对函数实例或类模板的成员函数求值:


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

使用 printthis 指针求值。


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

有关更多信息,请参见print 命令

list 表达式

可使用 list 命令打印指定函数实例的源码列表。


(dbx) list square(int, int*)

有关更多信息,请参见list 命令