Next: , Previous: Debugging Control, Up: Switches for gcc


3.2.16 Exception Handling Control

GNAT uses two methods for handling exceptions at run-time. The longjmp/setjmp method saves the context when entering a frame with an exception handler. Then when an exception is raised, the context can be restored immediately, without the need for tracing stack frames. This method provides very fast exception propagation, but introduces significant overhead for the use of exception handlers, even if no exception is raised.

The other approach is called “zero cost” exception handling. With this method, the compiler builds static tables to describe the exception ranges. No dynamic code is required when entering a frame containing an exception handler. When an exception is raised, the tables are used to control a back trace of the subprogram invocation stack to locate the required exception handler. This method has considerably poorer performance for the propagation of exceptions, but there is no overhead for exception handlers if no exception is raised. Note that in this mode and in the context of mixed Ada and C/C++ programming, to propagate an exception through a C/C++ code, the C/C++ code must be compiled with the -funwind-tables GCC's option.

The following switches can be used to control which of the two exception handling methods is used.

-gnatL
This switch causes the longjmp/setjmp approach to be used for exception handling. If this is the default mechanism for the target (see below), then this has no effect. If the default mechanism for the target is zero cost exceptions, then this switch can be used to modify this default, but it must be used for all units in the partition, including all run-time library units. One way to achieve this is to use the -a and -f switches for gnatmake. This option is rarely used. One case in which it may be advantageous is if you have an application where exception raising is common and the overall performance of the application is improved by favoring exception propagation.
-gnatZ
This switch causes the zero cost approach to be sed for exception handling. If this is the default mechanism for the target (see below), then this has no effect. If the default mechanism for the target is longjmp/setjmp exceptions, then this switch can be used to modify this default, but it must be used for all units in the partition, including all run-time library units. One way to achieve this is to use the -a and -f switches for gnatmake. This option can only be used if the zero cost approach is available for the target in use (see below).

The longjmp/setjmp approach is available on all targets, but the zero cost approach is only available on selected targets. To determine whether zero cost exceptions can be used for a particular target, look at the private part of the file system.ads. Either GCC_ZCX_Support or Front_End_ZCX_Support must be True to use the zero cost approach. If both of these switches are set to False, this means that zero cost exception handling is not yet available for that target. The switch ZCX_By_Default indicates the default approach. If this switch is set to True, then the zero cost approach is used by default.