File : a-except-2005.ads


   1 ------------------------------------------------------------------------------
   2 --                                                                          --
   3 --                         GNAT RUN-TIME COMPONENTS                         --
   4 --                                                                          --
   5 --                       A D A . E X C E P T I O N S                        --
   6 --                                                                          --
   7 --                                 S p e c                                  --
   8 --                                                                          --
   9 --          Copyright (C) 1992-2015, Free Software Foundation, Inc.         --
  10 --                                                                          --
  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. --
  14 --                                                                          --
  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.                                     --
  21 --                                                                          --
  22 --                                                                          --
  23 --                                                                          --
  24 --                                                                          --
  25 --                                                                          --
  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/>.                                          --
  30 --                                                                          --
  31 -- GNAT was originally developed  by the GNAT team at  New York University. --
  32 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
  33 --                                                                          --
  34 ------------------------------------------------------------------------------
  35 
  36 --  This version of Ada.Exceptions fully supports Ada 95 and later language
  37 --  versions.  It is used in all situations except for the build of the
  38 --  compiler and other basic tools. For these latter builds, we use an
  39 --  Ada 95-only version.
  40 
  41 --  The reason for this splitting off of a separate version is to support
  42 --  older bootstrap compilers that do not support Ada 2005 features, and
  43 --  Ada.Exceptions is part of the compiler sources.
  44 
  45 pragma Polling (Off);
  46 --  We must turn polling off for this unit, because otherwise we get
  47 --  elaboration circularities with ourself.
  48 
  49 with System;
  50 with System.Parameters;
  51 with System.Standard_Library;
  52 with System.Traceback_Entries;
  53 
  54 package Ada.Exceptions is
  55    pragma Preelaborate;
  56    --  In accordance with Ada 2005 AI-362.
  57 
  58    type Exception_Id is private;
  59    pragma Preelaborable_Initialization (Exception_Id);
  60 
  61    Null_Id : constant Exception_Id;
  62 
  63    type Exception_Occurrence is limited private;
  64    pragma Preelaborable_Initialization (Exception_Occurrence);
  65 
  66    type Exception_Occurrence_Access is access all Exception_Occurrence;
  67 
  68    Null_Occurrence : constant Exception_Occurrence;
  69 
  70    function Exception_Name (Id : Exception_Id) return String;
  71 
  72    function Exception_Name (X : Exception_Occurrence) return String;
  73 
  74    function Wide_Exception_Name
  75      (Id : Exception_Id) return Wide_String;
  76    pragma Ada_05 (Wide_Exception_Name);
  77 
  78    function Wide_Exception_Name
  79      (X : Exception_Occurrence) return Wide_String;
  80    pragma Ada_05 (Wide_Exception_Name);
  81 
  82    function Wide_Wide_Exception_Name
  83      (Id : Exception_Id) return Wide_Wide_String;
  84    pragma Ada_05 (Wide_Wide_Exception_Name);
  85 
  86    function Wide_Wide_Exception_Name
  87      (X : Exception_Occurrence) return Wide_Wide_String;
  88    pragma Ada_05 (Wide_Wide_Exception_Name);
  89 
  90    procedure Raise_Exception (E : Exception_Id; Message : String := "");
  91    pragma No_Return (Raise_Exception);
  92    --  Note: In accordance with AI-466, CE is raised if E = Null_Id
  93 
  94    function Exception_Message (X : Exception_Occurrence) return String;
  95 
  96    procedure Reraise_Occurrence (X : Exception_Occurrence);
  97    --  Note: it would be really nice to give a pragma No_Return for this
  98    --  procedure, but it would be wrong, since Reraise_Occurrence does return
  99    --  if the argument is the null exception occurrence. See also procedure
 100    --  Reraise_Occurrence_Always in the private part of this package.
 101 
 102    function Exception_Identity (X : Exception_Occurrence) return Exception_Id;
 103 
 104    function Exception_Information (X : Exception_Occurrence) return String;
 105    --  The format of the exception information is as follows:
 106    --
 107    --    exception name (as in Exception_Name)
 108    --    message (or a null line if no message)
 109    --    PID=nnnn
 110    --    0xyyyyyyyy 0xyyyyyyyy ...
 111    --
 112    --  The lines are separated by a ASCII.LF character
 113    --
 114    --  The nnnn is the partition Id given as decimal digits
 115    --
 116    --  The 0x... line represents traceback program counter locations,
 117    --  in order with the first one being the exception location.
 118 
 119    --  Note on ordering: the compiler uses the Save_Occurrence procedure, but
 120    --  not the function from Rtsfind, so it is important that the procedure
 121    --  come first, since Rtsfind finds the first matching entity.
 122 
 123    procedure Save_Occurrence
 124      (Target : out Exception_Occurrence;
 125       Source : Exception_Occurrence);
 126 
 127    function Save_Occurrence
 128      (Source : Exception_Occurrence)
 129       return   Exception_Occurrence_Access;
 130 
 131    --  Ada 2005 (AI-438): The language revision introduces the following
 132    --  subprograms and attribute definitions. We do not provide them
 133    --  explicitly. instead, the corresponding stream attributes are made
 134    --  available through a pragma Stream_Convert in the private part.
 135 
 136    --  procedure Read_Exception_Occurrence
 137    --    (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
 138    --     Item   : out Exception_Occurrence);
 139 
 140    --  procedure Write_Exception_Occurrence
 141    --    (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
 142    --     Item   : Exception_Occurrence);
 143 
 144    --  for Exception_Occurrence'Read use Read_Exception_Occurrence;
 145    --  for Exception_Occurrence'Write use Write_Exception_Occurrence;
 146 
 147 private
 148    package SSL renames System.Standard_Library;
 149    package SP renames System.Parameters;
 150 
 151    subtype EOA is Exception_Occurrence_Access;
 152 
 153    Exception_Msg_Max_Length : constant := SP.Default_Exception_Msg_Max_Length;
 154 
 155    ------------------
 156    -- Exception_Id --
 157    ------------------
 158 
 159    subtype Code_Loc is System.Address;
 160    --  Code location used in building exception tables and for call addresses
 161    --  when propagating an exception. Values of this type are created by using
 162    --  Label'Address or extracted from machine states using Get_Code_Loc.
 163 
 164    Null_Loc : constant Code_Loc := System.Null_Address;
 165    --  Null code location, used to flag outer level frame
 166 
 167    type Exception_Id is new SSL.Exception_Data_Ptr;
 168 
 169    function EId_To_String (X : Exception_Id) return String;
 170    function String_To_EId (S : String) return Exception_Id;
 171    pragma Stream_Convert (Exception_Id, String_To_EId, EId_To_String);
 172    --  Functions for implementing Exception_Id stream attributes
 173 
 174    Null_Id : constant Exception_Id := null;
 175 
 176    -------------------------
 177    -- Private Subprograms --
 178    -------------------------
 179 
 180    function Exception_Name_Simple (X : Exception_Occurrence) return String;
 181    --  Like Exception_Name, but returns the simple non-qualified name of the
 182    --  exception. This is used to implement the Exception_Name function in
 183    --  Current_Exceptions (the DEC compatible unit). It is called from the
 184    --  compiler generated code (using Rtsfind, which does not respect the
 185    --  private barrier, so we can place this function in the private part
 186    --  where the compiler can find it, but the spec is unchanged.)
 187 
 188    procedure Raise_Exception_Always (E : Exception_Id; Message : String := "");
 189    pragma No_Return (Raise_Exception_Always);
 190    pragma Export (Ada, Raise_Exception_Always, "__gnat_raise_exception");
 191    --  This differs from Raise_Exception only in that the caller has determined
 192    --  that for sure the parameter E is not null, and that therefore no check
 193    --  for Null_Id is required. The expander converts Raise_Exception calls to
 194    --  Raise_Exception_Always if it can determine this is the case. The Export
 195    --  allows this routine to be accessed from Pure units.
 196 
 197    procedure Raise_From_Signal_Handler
 198      (E : Exception_Id;
 199       M : System.Address);
 200    pragma Export
 201      (Ada, Raise_From_Signal_Handler,
 202            "ada__exceptions__raise_from_signal_handler");
 203    pragma No_Return (Raise_From_Signal_Handler);
 204    --  This routine is used to raise an exception from a signal handler. The
 205    --  signal handler has already stored the machine state (i.e. the state that
 206    --  corresponds to the location at which the signal was raised). E is the
 207    --  Exception_Id specifying what exception is being raised, and M is a
 208    --  pointer to a null-terminated string which is the message to be raised.
 209    --  Note that this routine never returns, so it is permissible to simply
 210    --  jump to this routine, rather than call it. This may be appropriate for
 211    --  systems where the right way to get out of signal handler is to alter the
 212    --  PC value in the machine state or in some other way ask the operating
 213    --  system to return here rather than to the original location.
 214 
 215    procedure Raise_From_Controlled_Operation
 216      (X : Ada.Exceptions.Exception_Occurrence);
 217    pragma No_Return (Raise_From_Controlled_Operation);
 218    pragma Export
 219      (Ada, Raise_From_Controlled_Operation,
 220            "__gnat_raise_from_controlled_operation");
 221    --  Raise Program_Error, providing information about X (an exception raised
 222    --  during a controlled operation) in the exception message.
 223 
 224    procedure Reraise_Library_Exception_If_Any;
 225    pragma Export
 226      (Ada, Reraise_Library_Exception_If_Any,
 227            "__gnat_reraise_library_exception_if_any");
 228    --  If there was an exception raised during library-level finalization,
 229    --  reraise the exception.
 230 
 231    procedure Reraise_Occurrence_Always (X : Exception_Occurrence);
 232    pragma No_Return (Reraise_Occurrence_Always);
 233    --  This differs from Raise_Occurrence only in that the caller guarantees
 234    --  that for sure the parameter X is not the null occurrence, and that
 235    --  therefore this procedure cannot return. The expander uses this routine
 236    --  in the translation of a raise statement with no parameter (reraise).
 237 
 238    procedure Reraise_Occurrence_No_Defer (X : Exception_Occurrence);
 239    pragma No_Return (Reraise_Occurrence_No_Defer);
 240    --  Exactly like Reraise_Occurrence, except that abort is not deferred
 241    --  before the call and the parameter X is known not to be the null
 242    --  occurrence. This is used in generated code when it is known that abort
 243    --  is already deferred.
 244 
 245    function Triggered_By_Abort return Boolean;
 246    --  Determine whether the current exception (if it exists) is an instance of
 247    --  Standard'Abort_Signal.
 248 
 249    -----------------------
 250    -- Polling Interface --
 251    -----------------------
 252 
 253    --  The GNAT compiler has an option to generate polling calls to the Poll
 254    --  routine in this package. Specifying the -gnatP option for a compilation
 255    --  causes a call to Ada.Exceptions.Poll to be generated on every subprogram
 256    --  entry and on every iteration of a loop, thus avoiding the possibility of
 257    --  a case of unbounded time between calls.
 258 
 259    --  This polling interface may be used for instrumentation or debugging
 260    --  purposes (e.g. implementing watchpoints in software or in the debugger).
 261 
 262    --  In the GNAT technology itself, this interface is used to implement
 263    --  immediate asynchronous transfer of control and immediate abort on
 264    --  targets which do not provide for one thread interrupting another.
 265 
 266    --  Note: this used to be in a separate unit called System.Poll, but that
 267    --  caused horrible circular elaboration problems between System.Poll and
 268    --  Ada.Exceptions.
 269 
 270    procedure Poll;
 271    --  Check for asynchronous abort. Note that we do not inline the body.
 272    --  This makes the interface more useful for debugging purposes.
 273 
 274    --------------------------
 275    -- Exception_Occurrence --
 276    --------------------------
 277 
 278    package TBE renames System.Traceback_Entries;
 279 
 280    Max_Tracebacks : constant := 50;
 281    --  Maximum number of trace backs stored in exception occurrence
 282 
 283    subtype Tracebacks_Array is TBE.Tracebacks_Array (1 .. Max_Tracebacks);
 284    --  Traceback array stored in exception occurrence
 285 
 286    type Exception_Occurrence is record
 287       Id : Exception_Id;
 288       --  Exception_Identity for this exception occurrence
 289 
 290       Machine_Occurrence : System.Address;
 291       --  The underlying machine occurrence. For GCC, this corresponds to the
 292       --  _Unwind_Exception structure address.
 293 
 294       Msg_Length : Natural := 0;
 295       --  Length of message (zero = no message)
 296 
 297       Msg : String (1 .. Exception_Msg_Max_Length);
 298       --  Characters of message
 299 
 300       Exception_Raised : Boolean := False;
 301       --  Set to true to indicate that this exception occurrence has actually
 302       --  been raised. When an exception occurrence is first created, this is
 303       --  set to False, then when it is processed by Raise_Current_Exception,
 304       --  it is set to True. If Raise_Current_Exception is used to raise an
 305       --  exception for which this flag is already True, then it knows that
 306       --  it is dealing with the reraise case (which is useful to distinguish
 307       --  for exception tracing purposes).
 308 
 309       Pid : Natural := 0;
 310       --  Partition_Id for partition raising exception
 311 
 312       Num_Tracebacks : Natural range 0 .. Max_Tracebacks := 0;
 313       --  Number of traceback entries stored
 314 
 315       Tracebacks : Tracebacks_Array;
 316       --  Stored tracebacks (in Tracebacks (1 .. Num_Tracebacks))
 317    end record;
 318 
 319    function "=" (Left, Right : Exception_Occurrence) return Boolean
 320      is abstract;
 321    --  Don't allow comparison on exception occurrences, we should not need
 322    --  this, and it would not work right, because of the Msg and Tracebacks
 323    --  fields which have unused entries not copied by Save_Occurrence.
 324 
 325    function Get_Exception_Machine_Occurrence
 326      (X : Exception_Occurrence) return System.Address;
 327    pragma Export (Ada, Get_Exception_Machine_Occurrence,
 328                   "__gnat_get_exception_machine_occurrence");
 329    --  Get the machine occurrence corresponding to an exception occurrence.
 330    --  It is Null_Address if there is no machine occurrence (in runtimes that
 331    --  doesn't use GCC mechanism) or if it has been lost (Save_Occurrence
 332    --  doesn't save the machine occurrence).
 333 
 334    function EO_To_String (X : Exception_Occurrence) return String;
 335    function String_To_EO (S : String) return Exception_Occurrence;
 336    pragma Stream_Convert (Exception_Occurrence, String_To_EO, EO_To_String);
 337    --  Functions for implementing Exception_Occurrence stream attributes
 338 
 339    Null_Occurrence : constant Exception_Occurrence := (
 340      Id                 => null,
 341      Machine_Occurrence => System.Null_Address,
 342      Msg_Length         => 0,
 343      Msg                => (others => ' '),
 344      Exception_Raised   => False,
 345      Pid                => 0,
 346      Num_Tracebacks     => 0,
 347      Tracebacks         => (others => TBE.Null_TB_Entry));
 348 
 349 end Ada.Exceptions;