| Debugging a Program With dbx |
Debugging C++
This chapter describes how
dbxhandles C++ exceptions and debugging C++ templates, including a summary of commands used when completing these tasks and examples with code samples.This chapter is organized into the following sections:
For information on compiling C++ programs, see Debugging Optimized Code.
Using
dbxwith C++Although this chapter concentrates on two specific aspects of debugging C++,
dbxallows you full functionality when debugging your C++ programs. You can:
- Find out about class and type definitions (see Looking Up Definitions of Types and Classes)
- Print or display inherited data members (see Printing C++)
- Find out dynamic information about an object pointer (see Printing C++)
- Debug virtual functions (see Calling a Function)
- Using runtime type information (see Printing the Value of a Variable or an Expression)
- Set breakpoints on all member functions of a class (see Setting Breakpoints in Member Functions of the Same Class)
- Set breakpoints on all overloaded member functions (see Setting Breakpoints in Member Functions of Different Classes)
- Set breakpoints on all overloaded nonmember functions (see Setting Multiple Breakpoints in Nonmember Functions)
- Set breakpoints on all member functions of a particular object (see Setting Breakpoints in Objects)
- Deal with overloaded functions/data members (see Setting a stop Breakpoint in a Function)
Exception Handling in
dbxA program stops running if an exception occurs. Exceptions signal programming anomalies, such as division by zero or array overflow. You can set up blocks to catch exceptions raised by expressions elsewhere in the code.
While debugging a program,
dbxenables you to:
- Catch unhandled exceptions before stack unwinding
- Catch unexpected exceptions
- Catch specific exceptions whether handled or not before stack unwinding
- Determine where a specific exception would be caught if it occurred at a particular point in the program
If you give a
stepcommand after stopping at a point where an exception is thrown, control is returned at the start of the first destructor executed during stack unwinding. If youstepout of a destructor executed during stack unwinding, control is returned at the start of the next destructor. When all destructors have been executed, astepcommand brings you to the catch block handling the throwing of the exceptionCommands for Handling Exceptions
exception [-d | +d]CommandUse the
exceptioncommand to display an exception's type at any time during debugging. If you use theexceptioncommand without an option, the type shown is determined by the setting of thedbxenvironment variableoutput_dynamic_type:
- If it is set to
on, the derived type is shown.- If it is set to
off(the default), the static type is shown.Specifying the
-dor+doption overrides the setting of the environment variable:
- If you specify
-d, the derived type is shown.- If you specify
+d, the static type is shown.For more information, see "exception Command" in the Using dbx Commands section of the Sun WorkShop online help.
intercept [-a | -x |typename]CommandYou can intercept, or catch, exceptions of a specific type before the stack has been unwound. Use the
interceptcommand with no arguments to list the types that are being intercepted. Use-ato intercept all exceptions. Use typename to add a type to the intercept list. Use-xto exclude a particular type from being intercepted.For example, to intercept all types except
int, you could type:
(dbx)intercept -a(dbx)intercept -x intFor more information, see "intercept Command" in the Using dbx Commands section of the Sun WorkShop online help.
unintercept [-a | -x |typename]CommandUse the
uninterceptcommand to remove exception types from the intercept list. Use the command with no arguments to list the types that are being intercepted (same as theinterceptcommand). Use-ato remove all intercepted types from the list. Use typename to remove a type from the intercept list. Use-xto stop excluding a particular type from being intercepted.For more information, see "unintercept Command" in the Using dbx Commands section of the Sun WorkShop online help.
whocatchestypename CommandThe
whocatchescommand reports where an exception of typename would be caught if thrown at the current point of execution. Use this command to find out what would happen if an exception were thrown from the top frame of the stack.The line number, function name, and frame number of the catch clause that would catch typename are displayed.
Examples of Exception Handling
This example demonstrates how exception handling is done in
dbxusing a sample program containing exceptions. An exception of typeintis thrown in the functionbarand is caught in the following catch block.
1 #include <stdio.h>23 class c {4 int x;5 public:6 c(int i) { x = i; }7 ~c() {8 printf("destructor for c(%d)\n", x);9 }10 };
1112 void bar() {13 c c1(3);14 throw(99);15 }1617 int main() {18 try {19 c c2(5);20 bar();21 return 0;22 }23 catch (int i) {24 printf("caught exception %d\n", i);25 }26 }The following transcript from the example program shows the exception handling features in
dbx.
Debugging With C++ Templates
dbxsupports C++ templates. You can load programs containing class and function templates intodbxand invoke any of thedbxcommands on a template that you would use on a class or function, such as:
- Setting breakpoints at class or function template instantiations (see stop inclass classname Command, stop infunction name Command, and stop in function Command)
- Printing a list of all class and function template instantiations (see whereis name Command)
- Displaying the definitions of templates and instances (see whatis name Command)
- Calling member template functions and function template instantiations (see call function_name (parameters) Command)
- Printing values of function template instantiations (print Expressions)
- Displaying the source code for function template instantiations (see list Expressions)
Template Example
The following code example shows the class template
Arrayand its instantiations and the function template square and its instantiations.
Arrayis a class templatesquareis a function templateArray<int>is a class template instantiation (template class)Array<int>::getlengthis a member function of a template classsquare(int, int*)andsquare(double, double*)are function template instantiations (template functions)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.
whereisname CommandUse the
whereiscommand to print a list of all occurrences of function or class instantiations for a function or class template.For a class template:
For a function template:
(dbx)whereis squarefunction template instance: `square<int>(__type_0,__type_0*)function template instance: `square<double>(__type_0,__type_0*)function template: `a.out`squareThe
__type_0parameter refers to the 0th template parameter. A__type_1would refer to the next template parameter.For more information, see "whereis Command" in the Using dbx Commands section of the Sun WorkShop online help.
whatisname CommandUse the
whatiscommand to print the definitions of function and class templates and instantiated functions and classes.For a class template:
(dbx)whatis -t Arraytemplate<class T> class ArrayTo get the full template declaration, try `whatis -t Array<int>';For the class template's constructors:
(dbx)whatis ArrayMore than one identifier 'Array'.Select one of the following:0) Cancel1) Array<int>::Array(int)2) Array<double>::Array(int>>1Array<int>::Array(int 1);For a function template:
For a class template instantiation:
For a function template instantiation:
(dbx)whatis square(int, int*)void square(int num, int *result);For more information, see "whatis Command" and "whatis Command Used With C++" in the Using dbx Commands section of the Sun WorkShop online help.
stop inclassclassname CommandTo stop in all member functions of a template class:
(dbx)stop inclass Array(2) stop inclass ArrayUse the
stopinclasscommand 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" in the Using dbx Commands section of the Sun WorkShop online help.
stop infunctionname CommandUse the
stopinfunctioncommand to set breakpoints at all instances of the specified function template:
(dbx)stop infunction square(9) stop infunction squareFor more information, see "stop Command" in the Using dbx Commands section of the Sun WorkShop online help.
stop infunction CommandUse the
stopincommand 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, see "stop Command" in the Using dbx Commands section of the Sun WorkShop online help.
callfunction_name (parameters) CommandUse the
callcommand to explicitly call a function instantiation or a member function of a class template when you are stopped in scope. Ifdbxis unable to choose the correct instance, a menu lets you choose it.
(dbx)call square(j,&i)For more information, see "call Command" in the Using dbx Commands section of the Sun WorkShop online help.
Use the
(dbx)print iarray.getlength()iarray.getlength() = 5Use
thispointer.
(dbx)whatis thisclass Array<int> *this;(dbx)print *this*this = {length = 5array = 0x21608}For more information, see "print Command" in the Using dbx Commands section of the Sun WorkShop online help.
listExpressionsUse the
listcommand to print the source listing for the specified function instantiation.
(dbx)list square(int, int*)For more information, see "list Command" in the Using dbx Commands section of the Sun WorkShop online help.
|
Sun Microsystems, Inc. Copyright information. All rights reserved. Feedback |
Library | Contents | Previous | Next | Index |