1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . E X C E P T I O N S --
9 -- Copyright (C) 1992-2013, Free Software Foundation, Inc. --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 3, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. --
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
23 -- additional permissions described in the GCC Runtime Library Exception, --
24 -- version 3.1, as published by the Free Software Foundation. --
26 -- You should have received a copy of the GNU General Public License and --
27 -- a copy of the GCC Runtime Library Exception along with this program; --
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
29 -- <http://www.gnu.org/licenses/>. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
34 ------------------------------------------------------------------------------
36 -- This version of Ada.Exceptions fully supports both Ada 95 and Ada 2005.
37 -- It is used in all situations except for the build of the compiler and
38 -- other basic tools. For these latter builds, we use an Ada 95-only version.
40 -- The reason for this splitting off of a separate version is that bootstrap
41 -- compilers often will be used that do not support Ada 2005 features, and
42 -- Ada.Exceptions is part of the compiler sources.
45 -- We must turn polling off for this unit, because otherwise we get
46 -- elaboration circularities with ourself.
49 with System
.Parameters
;
50 with System
.Standard_Library
;
51 with System
.Traceback_Entries
;
53 package Ada
.Exceptions
is
54 pragma Preelaborate_05
;
55 -- In accordance with Ada 2005 AI-362.
57 type Exception_Id
is private;
58 pragma Preelaborable_Initialization
(Exception_Id
);
60 Null_Id
: constant Exception_Id
;
62 type Exception_Occurrence
is limited private;
63 pragma Preelaborable_Initialization
(Exception_Occurrence
);
65 type Exception_Occurrence_Access
is access all Exception_Occurrence
;
67 Null_Occurrence
: constant Exception_Occurrence
;
69 function Exception_Name
(Id
: Exception_Id
) return String;
71 function Exception_Name
(X
: Exception_Occurrence
) return String;
73 function Wide_Exception_Name
74 (Id
: Exception_Id
) return Wide_String;
75 pragma Ada_05
(Wide_Exception_Name
);
77 function Wide_Exception_Name
78 (X
: Exception_Occurrence
) return Wide_String;
79 pragma Ada_05
(Wide_Exception_Name
);
81 function Wide_Wide_Exception_Name
82 (Id
: Exception_Id
) return Wide_Wide_String
;
83 pragma Ada_05
(Wide_Wide_Exception_Name
);
85 function Wide_Wide_Exception_Name
86 (X
: Exception_Occurrence
) return Wide_Wide_String
;
87 pragma Ada_05
(Wide_Wide_Exception_Name
);
89 procedure Raise_Exception
(E
: Exception_Id
; Message
: String := "");
90 pragma No_Return
(Raise_Exception
);
91 -- Note: In accordance with AI-466, CE is raised if E = Null_Id
93 function Exception_Message
(X
: Exception_Occurrence
) return String;
95 procedure Reraise_Occurrence
(X
: Exception_Occurrence
);
96 -- Note: it would be really nice to give a pragma No_Return for this
97 -- procedure, but it would be wrong, since Reraise_Occurrence does return
98 -- if the argument is the null exception occurrence. See also procedure
99 -- Reraise_Occurrence_Always in the private part of this package.
101 function Exception_Identity
(X
: Exception_Occurrence
) return Exception_Id
;
103 function Exception_Information
(X
: Exception_Occurrence
) return String;
104 -- The format of the exception information is as follows:
106 -- exception name (as in Exception_Name)
107 -- message (or a null line if no message)
109 -- 0xyyyyyyyy 0xyyyyyyyy ...
111 -- The lines are separated by a ASCII.LF character
113 -- The nnnn is the partition Id given as decimal digits
115 -- The 0x... line represents traceback program counter locations,
116 -- in order with the first one being the exception location.
118 -- Note on ordering: the compiler uses the Save_Occurrence procedure, but
119 -- not the function from Rtsfind, so it is important that the procedure
120 -- come first, since Rtsfind finds the first matching entity.
122 procedure Save_Occurrence
123 (Target
: out Exception_Occurrence
;
124 Source
: Exception_Occurrence
);
126 function Save_Occurrence
127 (Source
: Exception_Occurrence
)
128 return Exception_Occurrence_Access
;
130 -- Ada 2005 (AI-438): The language revision introduces the following
131 -- subprograms and attribute definitions. We do not provide them
132 -- explicitly. instead, the corresponding stream attributes are made
133 -- available through a pragma Stream_Convert in the private part.
135 -- procedure Read_Exception_Occurrence
136 -- (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
137 -- Item : out Exception_Occurrence);
139 -- procedure Write_Exception_Occurrence
140 -- (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
141 -- Item : Exception_Occurrence);
143 -- for Exception_Occurrence'Read use Read_Exception_Occurrence;
144 -- for Exception_Occurrence'Write use Write_Exception_Occurrence;
147 package SSL
renames System
.Standard_Library
;
148 package SP
renames System
.Parameters
;
150 subtype EOA
is Exception_Occurrence_Access
;
152 Exception_Msg_Max_Length
: constant := SP
.Default_Exception_Msg_Max_Length
;
158 subtype Code_Loc
is System
.Address
;
159 -- Code location used in building exception tables and for call addresses
160 -- when propagating an exception. Values of this type are created by using
161 -- Label'Address or extracted from machine states using Get_Code_Loc.
163 Null_Loc
: constant Code_Loc
:= System
.Null_Address
;
164 -- Null code location, used to flag outer level frame
166 type Exception_Id
is new SSL
.Exception_Data_Ptr
;
168 function EId_To_String
(X
: Exception_Id
) return String;
169 function String_To_EId
(S
: String) return Exception_Id
;
170 pragma Stream_Convert
(Exception_Id
, String_To_EId
, EId_To_String
);
171 -- Functions for implementing Exception_Id stream attributes
173 Null_Id
: constant Exception_Id
:= null;
175 -------------------------
176 -- Private Subprograms --
177 -------------------------
179 function Current_Target_Exception
return Exception_Occurrence
;
181 (Ada
, Current_Target_Exception
,
182 "__gnat_current_target_exception");
183 -- This routine should return the current raised exception on targets which
184 -- have built-in exception handling such as the Java Virtual Machine. For
185 -- other targets this routine is simply ignored. Currently, only JGNAT
186 -- uses this. See 4jexcept.ads for details. The pragma Export allows this
187 -- routine to be accessed elsewhere in the run-time, even though it is in
188 -- the private part of this package (it is not allowed to be in the visible
189 -- part, since this is set by the reference manual).
191 function Exception_Name_Simple
(X
: Exception_Occurrence
) return String;
192 -- Like Exception_Name, but returns the simple non-qualified name of the
193 -- exception. This is used to implement the Exception_Name function in
194 -- Current_Exceptions (the DEC compatible unit). It is called from the
195 -- compiler generated code (using Rtsfind, which does not respect the
196 -- private barrier, so we can place this function in the private part
197 -- where the compiler can find it, but the spec is unchanged.)
199 procedure Raise_Exception_Always
(E
: Exception_Id
; Message
: String := "");
200 pragma No_Return
(Raise_Exception_Always
);
201 pragma Export
(Ada
, Raise_Exception_Always
, "__gnat_raise_exception");
202 -- This differs from Raise_Exception only in that the caller has determined
203 -- that for sure the parameter E is not null, and that therefore no check
204 -- for Null_Id is required. The expander converts Raise_Exception calls to
205 -- Raise_Exception_Always if it can determine this is the case. The Export
206 -- allows this routine to be accessed from Pure units.
208 procedure Raise_From_Signal_Handler
212 (Ada
, Raise_From_Signal_Handler
,
213 "ada__exceptions__raise_from_signal_handler");
214 pragma No_Return
(Raise_From_Signal_Handler
);
215 -- This routine is used to raise an exception from a signal handler. The
216 -- signal handler has already stored the machine state (i.e. the state that
217 -- corresponds to the location at which the signal was raised). E is the
218 -- Exception_Id specifying what exception is being raised, and M is a
219 -- pointer to a null-terminated string which is the message to be raised.
220 -- Note that this routine never returns, so it is permissible to simply
221 -- jump to this routine, rather than call it. This may be appropriate for
222 -- systems where the right way to get out of signal handler is to alter the
223 -- PC value in the machine state or in some other way ask the operating
224 -- system to return here rather than to the original location.
226 procedure Raise_From_Controlled_Operation
227 (X
: Ada
.Exceptions
.Exception_Occurrence
);
228 pragma No_Return
(Raise_From_Controlled_Operation
);
230 (Ada
, Raise_From_Controlled_Operation
,
231 "__gnat_raise_from_controlled_operation");
232 -- Raise Program_Error, providing information about X (an exception raised
233 -- during a controlled operation) in the exception message.
235 procedure Reraise_Library_Exception_If_Any
;
237 (Ada
, Reraise_Library_Exception_If_Any
,
238 "__gnat_reraise_library_exception_if_any");
239 -- If there was an exception raised during library-level finalization,
240 -- reraise the exception.
242 procedure Reraise_Occurrence_Always
(X
: Exception_Occurrence
);
243 pragma No_Return
(Reraise_Occurrence_Always
);
244 -- This differs from Raise_Occurrence only in that the caller guarantees
245 -- that for sure the parameter X is not the null occurrence, and that
246 -- therefore this procedure cannot return. The expander uses this routine
247 -- in the translation of a raise statement with no parameter (reraise).
249 procedure Reraise_Occurrence_No_Defer
(X
: Exception_Occurrence
);
250 pragma No_Return
(Reraise_Occurrence_No_Defer
);
251 -- Exactly like Reraise_Occurrence, except that abort is not deferred
252 -- before the call and the parameter X is known not to be the null
253 -- occurrence. This is used in generated code when it is known that abort
254 -- is already deferred.
256 function Triggered_By_Abort
return Boolean;
257 -- Determine whether the current exception (if it exists) is an instance of
258 -- Standard'Abort_Signal.
260 -----------------------
261 -- Polling Interface --
262 -----------------------
264 -- The GNAT compiler has an option to generate polling calls to the Poll
265 -- routine in this package. Specifying the -gnatP option for a compilation
266 -- causes a call to Ada.Exceptions.Poll to be generated on every subprogram
267 -- entry and on every iteration of a loop, thus avoiding the possibility of
268 -- a case of unbounded time between calls.
270 -- This polling interface may be used for instrumentation or debugging
271 -- purposes (e.g. implementing watchpoints in software or in the debugger).
273 -- In the GNAT technology itself, this interface is used to implement
274 -- immediate asynchronous transfer of control and immediate abort on
275 -- targets which do not provide for one thread interrupting another.
277 -- Note: this used to be in a separate unit called System.Poll, but that
278 -- caused horrible circular elaboration problems between System.Poll and
279 -- Ada.Exceptions. One way of solving such circularities is unification!
282 -- Check for asynchronous abort. Note that we do not inline the body.
283 -- This makes the interface more useful for debugging purposes.
285 --------------------------
286 -- Exception_Occurrence --
287 --------------------------
289 package TBE
renames System
.Traceback_Entries
;
291 Max_Tracebacks
: constant := 50;
292 -- Maximum number of trace backs stored in exception occurrence
294 type Tracebacks_Array
is array (1 .. Max_Tracebacks
) of TBE
.Traceback_Entry
;
295 -- Traceback array stored in exception occurrence
297 type Exception_Occurrence
is record
299 -- Exception_Identity for this exception occurrence
301 Machine_Occurrence
: System
.Address
;
302 -- The underlying machine occurrence. For GCC, this corresponds to the
303 -- _Unwind_Exception structure address.
305 Msg_Length
: Natural := 0;
306 -- Length of message (zero = no message)
308 Msg
: String (1 .. Exception_Msg_Max_Length
);
309 -- Characters of message
311 Exception_Raised
: Boolean := False;
312 -- Set to true to indicate that this exception occurrence has actually
313 -- been raised. When an exception occurrence is first created, this is
314 -- set to False, then when it is processed by Raise_Current_Exception,
315 -- it is set to True. If Raise_Current_Exception is used to raise an
316 -- exception for which this flag is already True, then it knows that
317 -- it is dealing with the reraise case (which is useful to distinguish
318 -- for exception tracing purposes).
321 -- Partition_Id for partition raising exception
323 Num_Tracebacks
: Natural range 0 .. Max_Tracebacks
:= 0;
324 -- Number of traceback entries stored
326 Tracebacks
: Tracebacks_Array
;
327 -- Stored tracebacks (in Tracebacks (1 .. Num_Tracebacks))
330 function "=" (Left
, Right
: Exception_Occurrence
) return Boolean
332 -- Don't allow comparison on exception occurrences, we should not need
333 -- this, and it would not work right, because of the Msg and Tracebacks
334 -- fields which have unused entries not copied by Save_Occurrence.
336 function Get_Exception_Machine_Occurrence
337 (X
: Exception_Occurrence
) return System
.Address
;
338 pragma Export
(Ada
, Get_Exception_Machine_Occurrence
,
339 "__gnat_get_exception_machine_occurrence");
340 -- Get the machine occurrence corresponding to an exception occurrence.
341 -- It is Null_Address if there is no machine occurrence (in runtimes that
342 -- doesn't use GCC mechanism) or if it has been lost (Save_Occurrence
343 -- doesn't save the machine occurrence).
345 function EO_To_String
(X
: Exception_Occurrence
) return String;
346 function String_To_EO
(S
: String) return Exception_Occurrence
;
347 pragma Stream_Convert
(Exception_Occurrence
, String_To_EO
, EO_To_String
);
348 -- Functions for implementing Exception_Occurrence stream attributes
350 Null_Occurrence
: constant Exception_Occurrence
:= (
352 Machine_Occurrence
=> System
.Null_Address
,
354 Msg
=> (others => ' '),
355 Exception_Raised
=> False,
358 Tracebacks
=> (others => TBE
.Null_TB_Entry
));