1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . E X C E P T I O N S --
9 -- Copyright (C) 1992-2009, 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 Warnings (Off);
55 pragma Preelaborate_05;
57 -- In accordance with Ada 2005 AI-362. The warnings pragmas are so that we
58 -- can compile this using older compiler versions, which will ignore the
59 -- pragma, which is fine for the bootstrap.
61 type Exception_Id is private;
62 pragma Preelaborable_Initialization (Exception_Id);
64 Null_Id : constant Exception_Id;
66 type Exception_Occurrence is limited private;
67 pragma Preelaborable_Initialization (Exception_Occurrence);
69 type Exception_Occurrence_Access is access all Exception_Occurrence;
71 Null_Occurrence : constant Exception_Occurrence;
73 function Exception_Name (Id : Exception_Id) return String;
75 function Exception_Name (X : Exception_Occurrence) return String;
77 function Wide_Exception_Name
78 (Id : Exception_Id) return Wide_String;
79 pragma Ada_05 (Wide_Exception_Name);
81 function Wide_Exception_Name
82 (X : Exception_Occurrence) return Wide_String;
83 pragma Ada_05 (Wide_Exception_Name);
85 function Wide_Wide_Exception_Name
86 (Id : Exception_Id) return Wide_Wide_String;
87 pragma Ada_05 (Wide_Wide_Exception_Name);
89 function Wide_Wide_Exception_Name
90 (X : Exception_Occurrence) return Wide_Wide_String;
91 pragma Ada_05 (Wide_Wide_Exception_Name);
93 procedure Raise_Exception (E : Exception_Id; Message : String := "");
94 pragma No_Return (Raise_Exception);
95 -- Note: In accordance with AI-466, CE is raised if E = Null_Id
97 function Exception_Message (X : Exception_Occurrence) return String;
99 procedure Reraise_Occurrence (X : Exception_Occurrence);
100 -- Note: it would be really nice to give a pragma No_Return for this
101 -- procedure, but it would be wrong, since Reraise_Occurrence does return
102 -- if the argument is the null exception occurrence. See also procedure
103 -- Reraise_Occurrence_Always in the private part of this package.
105 function Exception_Identity (X : Exception_Occurrence) return Exception_Id;
107 function Exception_Information (X : Exception_Occurrence) return String;
108 -- The format of the exception information is as follows:
110 -- exception name (as in Exception_Name)
111 -- message (or a null line if no message)
113 -- 0xyyyyyyyy 0xyyyyyyyy ...
115 -- The lines are separated by a ASCII.LF character
116 -- The nnnn is the partition Id given as decimal digits.
117 -- The 0x... line represents traceback program counter locations,
118 -- in order with the first one being the exception location.
120 -- Note on ordering: the compiler uses the Save_Occurrence procedure, but
121 -- not the function from Rtsfind, so it is important that the procedure
122 -- come first, since Rtsfind finds the first matching entity.
124 procedure Save_Occurrence
125 (Target : out Exception_Occurrence;
126 Source : Exception_Occurrence);
128 function Save_Occurrence
129 (Source : Exception_Occurrence)
130 return Exception_Occurrence_Access;
132 -- Ada 2005 (AI-438): The language revision introduces the following
133 -- subprograms and attribute definitions. We do not provide them
134 -- explicitly. instead, the corresponding stream attributes are made
135 -- available through a pragma Stream_Convert in the private part.
137 -- procedure Read_Exception_Occurrence
138 -- (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
139 -- Item : out Exception_Occurrence);
141 -- procedure Write_Exception_Occurrence
142 -- (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
143 -- Item : Exception_Occurrence);
145 -- for Exception_Occurrence'Read use Read_Exception_Occurrence;
146 -- for Exception_Occurrence'Write use Write_Exception_Occurrence;
149 package SSL renames System.Standard_Library;
150 package SP renames System.Parameters;
152 subtype EOA is Exception_Occurrence_Access;
154 Exception_Msg_Max_Length : constant := SP.Default_Exception_Msg_Max_Length;
160 subtype Code_Loc is System.Address;
161 -- Code location used in building exception tables and for call addresses
162 -- when propagating an exception. Values of this type are created by using
163 -- Label'Address or extracted from machine states using Get_Code_Loc.
165 Null_Loc : constant Code_Loc := System.Null_Address;
166 -- Null code location, used to flag outer level frame
168 type Exception_Id is new SSL.Exception_Data_Ptr;
170 function EId_To_String (X : Exception_Id) return String;
171 function String_To_EId (S : String) return Exception_Id;
172 pragma Stream_Convert (Exception_Id, String_To_EId, EId_To_String);
173 -- Functions for implementing Exception_Id stream attributes
175 Null_Id : constant Exception_Id := null;
177 -------------------------
178 -- Private Subprograms --
179 -------------------------
181 function Current_Target_Exception return Exception_Occurrence;
183 (Ada, Current_Target_Exception,
184 "__gnat_current_target_exception");
185 -- This routine should return the current raised exception on targets
186 -- which have built-in exception handling such as the Java Virtual
187 -- Machine. For other targets this routine is simply ignored. Currently,
188 -- only JGNAT uses this. See 4jexcept.ads for details. The pragma Export
189 -- allows this routine to be accessed elsewhere in the run-time, even
190 -- though it is in the private part of this package (it is not allowed
191 -- to be in the visible part, since this is set by the reference manual).
193 function Exception_Name_Simple (X : Exception_Occurrence) return String;
194 -- Like Exception_Name, but returns the simple non-qualified name of the
195 -- exception. This is used to implement the Exception_Name function in
196 -- Current_Exceptions (the DEC compatible unit). It is called from the
197 -- compiler generated code (using Rtsfind, which does not respect the
198 -- private barrier, so we can place this function in the private part
199 -- where the compiler can find it, but the spec is unchanged.)
201 procedure Raise_Exception_Always (E : Exception_Id; Message : String := "");
202 pragma No_Return (Raise_Exception_Always);
203 pragma Export (Ada, Raise_Exception_Always, "__gnat_raise_exception");
204 -- This differs from Raise_Exception only in that the caller has determined
205 -- that for sure the parameter E is not null, and that therefore no check
206 -- for Null_Id is required. The expander converts Raise_Exception calls to
207 -- Raise_Exception_Always if it can determine this is the case. The Export
208 -- allows this routine to be accessed from Pure units.
210 procedure Raise_From_Signal_Handler
214 (Ada, Raise_From_Signal_Handler,
215 "ada__exceptions__raise_from_signal_handler");
216 pragma No_Return (Raise_From_Signal_Handler);
217 -- This routine is used to raise an exception from a signal handler. The
218 -- signal handler has already stored the machine state (i.e. the state that
219 -- corresponds to the location at which the signal was raised). E is the
220 -- Exception_Id specifying what exception is being raised, and M is a
221 -- pointer to a null-terminated string which is the message to be raised.
222 -- Note that this routine never returns, so it is permissible to simply
223 -- jump to this routine, rather than call it. This may be appropriate for
224 -- systems where the right way to get out of signal handler is to alter the
225 -- PC value in the machine state or in some other way ask the operating
226 -- system to return here rather than to the original location.
228 procedure Raise_From_Controlled_Operation
229 (X : Ada.Exceptions.Exception_Occurrence);
230 pragma No_Return (Raise_From_Controlled_Operation);
231 -- Raise Program_Error, providing information about X (an exception
232 -- raised during a controlled operation) in the exception message.
234 procedure Reraise_Occurrence_Always (X : Exception_Occurrence);
235 pragma No_Return (Reraise_Occurrence_Always);
236 -- This differs from Raise_Occurrence only in that the caller guarantees
237 -- that for sure the parameter X is not the null occurrence, and that
238 -- therefore this procedure cannot return. The expander uses this routine
239 -- in the translation of a raise statement with no parameter (reraise).
241 procedure Reraise_Occurrence_No_Defer (X : Exception_Occurrence);
242 pragma No_Return (Reraise_Occurrence_No_Defer);
243 -- Exactly like Reraise_Occurrence, except that abort is not deferred
244 -- before the call and the parameter X is known not to be the null
245 -- occurrence. This is used in generated code when it is known that
246 -- abort is already deferred.
248 -----------------------
249 -- Polling Interface --
250 -----------------------
252 -- The GNAT compiler has an option to generate polling calls to the Poll
253 -- routine in this package. Specifying the -gnatP option for a compilation
254 -- causes a call to Ada.Exceptions.Poll to be generated on every subprogram
255 -- entry and on every iteration of a loop, thus avoiding the possibility of
256 -- a case of unbounded time between calls.
258 -- This polling interface may be used for instrumentation or debugging
259 -- purposes (e.g. implementing watchpoints in software or in the debugger).
261 -- In the GNAT technology itself, this interface is used to implement
262 -- immediate asynchronous transfer of control and immediate abort on
263 -- targets which do not provide for one thread interrupting another.
265 -- Note: this used to be in a separate unit called System.Poll, but that
266 -- caused horrible circular elaboration problems between System.Poll and
267 -- Ada.Exceptions. One way of solving such circularities is unification!
270 -- Check for asynchronous abort. Note that we do not inline the body.
271 -- This makes the interface more useful for debugging purposes.
273 --------------------------
274 -- Exception_Occurrence --
275 --------------------------
277 package TBE renames System.Traceback_Entries;
279 Max_Tracebacks : constant := 50;
280 -- Maximum number of trace backs stored in exception occurrence
282 type Tracebacks_Array is array (1 .. Max_Tracebacks) of TBE.Traceback_Entry;
283 -- Traceback array stored in exception occurrence
285 type Exception_Occurrence is record
287 -- Exception_Identity for this exception occurrence
288 -- WARNING System.System.Finalization_Implementation.Finalize_List
289 -- relies on the fact that this field is always first in the exception
292 Msg_Length : Natural := 0;
293 -- Length of message (zero = no message)
295 Msg : String (1 .. Exception_Msg_Max_Length);
296 -- Characters of message
298 Cleanup_Flag : Boolean := False;
299 -- The cleanup flag is normally False, it is set True for an exception
300 -- occurrence passed to a cleanup routine, and will still be set True
301 -- when the cleanup routine does a Reraise_Occurrence call using this
302 -- exception occurrence. This is used to avoid recording a bogus trace
303 -- back entry from this reraise call.
305 Exception_Raised : Boolean := False;
306 -- Set to true to indicate that this exception occurrence has actually
307 -- been raised. When an exception occurrence is first created, this is
308 -- set to False, then when it is processed by Raise_Current_Exception,
309 -- it is set to True. If Raise_Current_Exception is used to raise an
310 -- exception for which this flag is already True, then it knows that
311 -- it is dealing with the reraise case (which is useful to distinguish
312 -- for exception tracing purposes).
315 -- Partition_Id for partition raising exception
317 Num_Tracebacks : Natural range 0 .. Max_Tracebacks := 0;
318 -- Number of traceback entries stored
320 Tracebacks : Tracebacks_Array;
321 -- Stored tracebacks (in Tracebacks (1 .. Num_Tracebacks))
323 Private_Data : System.Address := System.Null_Address;
324 -- Field used by low level exception mechanism to store specific data.
325 -- Currently used by the GCC exception mechanism to store a pointer to
326 -- a GNAT_GCC_Exception.
329 function "=" (Left, Right : Exception_Occurrence) return Boolean
331 -- Don't allow comparison on exception occurrences, we should not need
332 -- this, and it would not work right, because of the Msg and Tracebacks
333 -- fields which have unused entries not copied by Save_Occurrence.
335 function EO_To_String (X : Exception_Occurrence) return String;
336 function String_To_EO (S : String) return Exception_Occurrence;
337 pragma Stream_Convert (Exception_Occurrence, String_To_EO, EO_To_String);
338 -- Functions for implementing Exception_Occurrence stream attributes
340 Null_Occurrence : constant Exception_Occurrence := (
343 Msg => (others => ' '),
344 Cleanup_Flag => False,
345 Exception_Raised => False,
348 Tracebacks => (others => TBE.Null_TB_Entry),
349 Private_Data => System.Null_Address);