1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . E X C E P T I O N S --
9 -- Copyright (C) 1992-2015, 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 is used only for building the compiler
37 -- and certain basic tools. The "real" version of Ada.Exceptions is in
38 -- a-except-2005.ads/adb, and is used for all other builds where full Ada
39 -- functionality is required. In particular, it is used for building run
40 -- times on all targets.
42 -- This version is limited to Ada 95 features. It omits Ada 2005 features
43 -- such as the additional definitions of Exception_Name returning
44 -- Wide_[Wide_]String. It differs from the version specified in the Ada 95 RM
45 -- only in that it is declared Preelaborate (see declaration below for why
48 -- The reason for this splitting off of a separate version is to support
49 -- older bootstrap compilers that do not support Ada 2005 features, and
50 -- Ada.Exceptions is part of the compiler sources.
52 pragma Compiler_Unit_Warning
;
55 -- We must turn polling off for this unit, because otherwise we get
56 -- elaboration circularities with ourself.
59 with System
.Parameters
;
60 with System
.Standard_Library
;
61 with System
.Traceback_Entries
;
63 package Ada
.Exceptions
is
65 -- We make this preelaborable. If we did not do this, then run time units
66 -- used by the compiler (e.g. s-soflin.ads) would run into trouble.
67 -- Conformance with Ada 95 is not an issue, since this version is used
68 -- only by the compiler.
70 type Exception_Id
is private;
72 Null_Id
: constant Exception_Id
;
74 type Exception_Occurrence
is limited private;
76 type Exception_Occurrence_Access
is access all Exception_Occurrence
;
78 Null_Occurrence
: constant Exception_Occurrence
;
80 function Exception_Name
(X
: Exception_Occurrence
) return String;
81 -- Same as Exception_Name (Exception_Identity (X))
83 function Exception_Name
(Id
: Exception_Id
) return String;
85 procedure Raise_Exception
(E
: Exception_Id
; Message
: String := "");
86 pragma No_Return
(Raise_Exception
);
87 -- Note: In accordance with AI-466, CE is raised if E = Null_Id
89 function Exception_Message
(X
: Exception_Occurrence
) return String;
91 procedure Reraise_Occurrence
(X
: Exception_Occurrence
);
92 -- Note: it would be really nice to give a pragma No_Return for this
93 -- procedure, but it would be wrong, since Reraise_Occurrence does return
94 -- if the argument is the null exception occurrence. See also procedure
95 -- Reraise_Occurrence_Always in the private part of this package.
97 function Exception_Identity
(X
: Exception_Occurrence
) return Exception_Id
;
99 function Exception_Information
(X
: Exception_Occurrence
) return String;
100 -- The format of the exception information is as follows:
102 -- exception name (as in Exception_Name)
103 -- message (or a null line if no message)
105 -- 0xyyyyyyyy 0xyyyyyyyy ...
107 -- The lines are separated by a ASCII.LF character
108 -- The nnnn is the partition Id given as decimal digits.
109 -- The 0x... line represents traceback program counter locations,
110 -- in order with the first one being the exception location.
112 -- Note on ordering: the compiler uses the Save_Occurrence procedure, but
113 -- not the function from Rtsfind, so it is important that the procedure
114 -- come first, since Rtsfind finds the first matching entity.
116 procedure Save_Occurrence
117 (Target
: out Exception_Occurrence
;
118 Source
: Exception_Occurrence
);
120 function Save_Occurrence
121 (Source
: Exception_Occurrence
)
122 return Exception_Occurrence_Access
;
125 package SSL
renames System
.Standard_Library
;
126 package SP
renames System
.Parameters
;
128 subtype EOA
is Exception_Occurrence_Access
;
130 Exception_Msg_Max_Length
: constant := SP
.Default_Exception_Msg_Max_Length
;
136 subtype Code_Loc
is System
.Address
;
137 -- Code location used in building exception tables and for call addresses
138 -- when propagating an exception. Values of this type are created by using
139 -- Label'Address or extracted from machine states using Get_Code_Loc.
141 Null_Loc
: constant Code_Loc
:= System
.Null_Address
;
142 -- Null code location, used to flag outer level frame
144 type Exception_Id
is new SSL
.Exception_Data_Ptr
;
146 function EId_To_String
(X
: Exception_Id
) return String;
147 function String_To_EId
(S
: String) return Exception_Id
;
148 pragma Stream_Convert
(Exception_Id
, String_To_EId
, EId_To_String
);
149 -- Functions for implementing Exception_Id stream attributes
151 Null_Id
: constant Exception_Id
:= null;
153 -------------------------
154 -- Private Subprograms --
155 -------------------------
157 function Current_Target_Exception
return Exception_Occurrence
;
159 (Ada
, Current_Target_Exception
,
160 "__gnat_current_target_exception");
161 -- This routine should return the current raised exception on targets
162 -- which have built-in exception handling such as the Java Virtual
163 -- Machine. For other targets this routine is simply ignored. Currently,
164 -- only JGNAT uses this. See 4jexcept.ads for details. The pragma Export
165 -- allows this routine to be accessed elsewhere in the run-time, even
166 -- though it is in the private part of this package (it is not allowed
167 -- to be in the visible part, since this is set by the reference manual).
169 function Exception_Name_Simple
(X
: Exception_Occurrence
) return String;
170 -- Like Exception_Name, but returns the simple non-qualified name of the
171 -- exception. This is used to implement the Exception_Name function in
172 -- Current_Exceptions (the DEC compatible unit). It is called from the
173 -- compiler generated code (using Rtsfind, which does not respect the
174 -- private barrier, so we can place this function in the private part
175 -- where the compiler can find it, but the spec is unchanged.)
177 procedure Raise_Exception_Always
(E
: Exception_Id
; Message
: String := "");
178 pragma No_Return
(Raise_Exception_Always
);
179 pragma Export
(Ada
, Raise_Exception_Always
, "__gnat_raise_exception");
180 -- This differs from Raise_Exception only in that the caller has determined
181 -- that for sure the parameter E is not null, and that therefore no check
182 -- for Null_Id is required. The expander converts Raise_Exception calls to
183 -- Raise_Exception_Always if it can determine this is the case. The Export
184 -- allows this routine to be accessed from Pure units.
186 procedure Raise_From_Signal_Handler
190 (Ada
, Raise_From_Signal_Handler
,
191 "ada__exceptions__raise_from_signal_handler");
192 pragma No_Return
(Raise_From_Signal_Handler
);
193 -- This routine is used to raise an exception from a signal handler. The
194 -- signal handler has already stored the machine state (i.e. the state that
195 -- corresponds to the location at which the signal was raised). E is the
196 -- Exception_Id specifying what exception is being raised, and M is a
197 -- pointer to a null-terminated string which is the message to be raised.
198 -- Note that this routine never returns, so it is permissible to simply
199 -- jump to this routine, rather than call it. This may be appropriate for
200 -- systems where the right way to get out of signal handler is to alter the
201 -- PC value in the machine state or in some other way ask the operating
202 -- system to return here rather than to the original location.
204 procedure Raise_From_Controlled_Operation
205 (X
: Ada
.Exceptions
.Exception_Occurrence
);
206 pragma No_Return
(Raise_From_Controlled_Operation
);
208 (Ada
, Raise_From_Controlled_Operation
,
209 "__gnat_raise_from_controlled_operation");
210 -- Raise Program_Error, providing information about X (an exception raised
211 -- during a controlled operation) in the exception message.
213 procedure Reraise_Library_Exception_If_Any
;
215 (Ada
, Reraise_Library_Exception_If_Any
,
216 "__gnat_reraise_library_exception_if_any");
217 -- If there was an exception raised during library-level finalization,
218 -- reraise the exception.
220 procedure Reraise_Occurrence_Always
(X
: Exception_Occurrence
);
221 pragma No_Return
(Reraise_Occurrence_Always
);
222 -- This differs from Raise_Occurrence only in that the caller guarantees
223 -- that for sure the parameter X is not the null occurrence, and that
224 -- therefore this procedure cannot return. The expander uses this routine
225 -- in the translation of a raise statement with no parameter (reraise).
227 procedure Reraise_Occurrence_No_Defer
(X
: Exception_Occurrence
);
228 pragma No_Return
(Reraise_Occurrence_No_Defer
);
229 -- Exactly like Reraise_Occurrence, except that abort is not deferred
230 -- before the call and the parameter X is known not to be the null
231 -- occurrence. This is used in generated code when it is known that
232 -- abort is already deferred.
234 function Triggered_By_Abort
return Boolean;
235 -- Determine whether the current exception (if it exists) is an instance of
236 -- Standard'Abort_Signal.
238 -----------------------
239 -- Polling Interface --
240 -----------------------
242 -- The GNAT compiler has an option to generate polling calls to the Poll
243 -- routine in this package. Specifying the -gnatP option for a compilation
244 -- causes a call to Ada.Exceptions.Poll to be generated on every subprogram
245 -- entry and on every iteration of a loop, thus avoiding the possibility of
246 -- a case of unbounded time between calls.
248 -- This polling interface may be used for instrumentation or debugging
249 -- purposes (e.g. implementing watchpoints in software or in the debugger).
251 -- In the GNAT technology itself, this interface is used to implement
252 -- immediate asynchronous transfer of control and immediate abort on
253 -- targets which do not provide for one thread interrupting another.
255 -- Note: this used to be in a separate unit called System.Poll, but that
256 -- caused horrible circular elaboration problems between System.Poll and
260 -- Check for asynchronous abort. Note that we do not inline the body.
261 -- This makes the interface more useful for debugging purposes.
263 --------------------------
264 -- Exception_Occurrence --
265 --------------------------
267 package TBE
renames System
.Traceback_Entries
;
269 Max_Tracebacks
: constant := 50;
270 -- Maximum number of trace backs stored in exception occurrence
272 subtype Tracebacks_Array
is TBE
.Tracebacks_Array
(1 .. Max_Tracebacks
);
273 -- Traceback array stored in exception occurrence
275 type Exception_Occurrence
is record
277 -- Exception_Identity for this exception occurrence
279 Msg_Length
: Natural := 0;
280 -- Length of message (zero = no message)
282 Msg
: String (1 .. Exception_Msg_Max_Length
);
283 -- Characters of message
285 Exception_Raised
: Boolean := False;
286 -- Set to true to indicate that this exception occurrence has actually
287 -- been raised. When an exception occurrence is first created, this is
288 -- set to False, then when it is processed by Raise_Current_Exception,
289 -- it is set to True. If Raise_Current_Exception is used to raise an
290 -- exception for which this flag is already True, then it knows that
291 -- it is dealing with the reraise case (which is useful to distinguish
292 -- for exception tracing purposes).
295 -- Partition_Id for partition raising exception
297 Num_Tracebacks
: Natural range 0 .. Max_Tracebacks
:= 0;
298 -- Number of traceback entries stored
300 Tracebacks
: Tracebacks_Array
;
301 -- Stored tracebacks (in Tracebacks (1 .. Num_Tracebacks))
304 function "=" (Left
, Right
: Exception_Occurrence
) return Boolean
306 -- Don't allow comparison on exception occurrences, we should not need
307 -- this, and it would not work right, because of the Msg and Tracebacks
308 -- fields which have unused entries not copied by Save_Occurrence.
310 function EO_To_String
(X
: Exception_Occurrence
) return String;
311 function String_To_EO
(S
: String) return Exception_Occurrence
;
312 pragma Stream_Convert
(Exception_Occurrence
, String_To_EO
, EO_To_String
);
313 -- Functions for implementing Exception_Occurrence stream attributes
315 Null_Occurrence
: constant Exception_Occurrence
:= (
318 Msg
=> (others => ' '),
319 Exception_Raised
=> False,
322 Tracebacks
=> (others => TBE
.Null_TB_Entry
));