Debugging a Program With dbx

Exception Handling in dbx

A program stops running if an exception occurs. Exceptions signal programming anomalies, such as division by zero or array overflow. To deal with exceptions, 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 step after stopping at a throw point, 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, step brings you to the catch block handling the throw.

Commands for Handling Exceptions

exception [-d | +d]

Use this command to display an exception's type. This variable can be used at any time during debugging. With --d, the derived type is shown; otherwise, the static type is shown. This command overwrites the dbxenv variable output_dynamic_type setting.

intercept [-a | -x | typename]

You can intercept, or catch, exceptions of a specific type before the stack has been unwound. Use this command with no arguments to list the types that are being intercepted. Use --a to intercepted all throws. 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 enter:


(dbx) intercept -a
(dbx) intercept -x int

unintercept [-a | -x | typename]

Use this command to remove exception types from the intercept list. Use this command with no arguments to list the types that are being intercepted (same as intercept). 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.

whocatches typename

This command reports where, if at all, 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 is displayed.

Examples of Exception Handling

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.


1  #include <stdio.h>
2
3  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  };


11
12  void bar() {
13      c c1(3);
14      throw(99);
15  }
16
17  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.


(dbx) intercept
(dbx) intercept int
int   
(dbx) stop in bar
(2) stop in bar()
(dbx)run
Running: a.out 
(process id 304)
Stopped in bar at line 13 in file "foo.cc"
   13       c c1(3);
(dbx) whocatches int
int is caught at line 24, in function
main (frame number 2)
(dbx) whocatches c
dbx: no runtime type info for class c (never thrown or caught)
(dbx) contException of type int is caught at line 24,
in function main (frame number 4)
stopped in _exdbg_notify_of_throw at 0xef731494
0xef731494: _exdbg_notify_of_throw          :        jmp     %o7 + 0x8
Current function is bar
   14        throw(99);


(dbx) step
stopped in c::~c at line 8 in file "foo.cc"
    8         printf("destructor
for c(%d)\n", x); 
(dbx) step
destructor for c(3)
stopped in c::~c at line 9 in file "foo.cc"
    9       }
(dbx) step
stopped in c::~c at line 8 in file "foo.cc"
    8        printf("destructor for c(%d)\n", x); 
(dbx) step
destructor for c(5)
stopped in c::~c at line 9 in file "foo.cc"
    9       )
(dbx) step
stopped in main at line 24 in file "foo.cc"
   24           printf("caught exception %d\n", i);
(dbx) step
caught exception 99
stopped in main at line 26 in file "foo.cc"
   26   }