C H A P T E R 15 |
Debugging C++ With dbx |
This chapter describes how dbx handles 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.
Although this chapter concentrates on two specific aspects of debugging C++, dbx allows you full functionality when debugging your C++ programs. You can:
A 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, dbx enables you to:
If you give a step command 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 you step out of a destructor executed during stack unwinding, control is returned at the start of the next destructor. When all destructors have been executed, a step command brings you to the catch block handling the throwing of the exception
Use the exception command to display an exception's type at any time during debugging. If you use the exception command without an option, the type shown is determined by the setting of the dbx environment variable output_dynamic_type:
Specifying the -d or +d option overrides the setting of the environment variable:
For more information, see exception Command.
You can intercept, or catch, exceptions of a specific type before the stack has been unwound. Use the intercept command with no arguments to list the types that are being intercepted. Use -a to intercept all exceptions. Use typename to add a type to the intercept list. Use -x to exclude a particular type from being intercepted.
For example, to intercept all types except int, you could type:
For more information, see intercept Command.
Use the unintercept command to remove exception types from the intercept list. Use the command with no arguments to list the types that are being intercepted (same as the intercept command). Use -a to remove all intercepted types from the list. Use typename to remove a type from the intercept list. Use -x to stop excluding a particular type from being intercepted.
For more information, see unintercept Command.
The whocatches command 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. The command returns "type is unhandled" if the catch point is in the same function that is doing the throw.
For more information, see whocatches Command.
This example demonstrates how exception handling is done in dbx using a sample program containing exceptions. An exception of type int is thrown in the function bar and is caught in the following catch block.
The following transcript from the example program shows the exception handling features in dbx.
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:
The following code example shows the class template Array and its instantiations and the function template square and its instantiations.
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.
Use the whereis command to print a list of all occurrences of function or class instantiations for a function or class 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.
Use the whatis command to print the definitions of function and class templates and instantiated functions and classes.
(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 class template instantiation:
For a function template instantiation:
For more information, see whatis Command.
To stop in all member functions of a template class:
Use the stop inclass command to set breakpoints at all member functions of a particular template class:
For more information, see stop Command and inclass classname [-recurse | -norecurse].
Use the stop infunction command to set breakpoints at all instances of the specified function template:
For more information, see stop Command and infunction function.
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:
For more information, stop Command and in function.
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.
For more information, see call Command.
Use the print command to evaluate a function instantiation or a member function of a class template:.
Use print to evaluate the this pointer.
For more information, see print Command.
Use the list command to print the source listing for the specified function instantiation.
For more information, see list Command.
Copyright © 2004, Sun Microsystems, Inc. All Rights Reserved.