config/sparc/sol2-bi.h: Revert previous delta.
[official-gcc.git] / gcc / ada / a-except.adb
blob979f11e41c916e9c339e17d0d06ab42ef0c09179
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- A D A . E X C E P T I O N S --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
29 -- --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 -- --
33 ------------------------------------------------------------------------------
35 pragma Polling (Off);
36 -- We must turn polling off for this unit, because otherwise we get
37 -- elaboration circularities with System.Exception_Tables.
39 with Ada.Unchecked_Deallocation;
41 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
43 with System; use System;
44 with System.Exception_Table; use System.Exception_Table;
45 with System.Exceptions; use System.Exceptions;
46 with System.Standard_Library; use System.Standard_Library;
47 with System.Storage_Elements; use System.Storage_Elements;
48 with System.Soft_Links; use System.Soft_Links;
49 with System.Machine_State_Operations; use System.Machine_State_Operations;
50 with System.Traceback;
52 with Unchecked_Conversion;
54 package body Ada.Exceptions is
56 procedure builtin_longjmp (buffer : Address; Flag : Integer);
57 pragma No_Return (builtin_longjmp);
58 pragma Import (C, builtin_longjmp, "_gnat_builtin_longjmp");
60 pragma Suppress (All_Checks);
61 -- We definitely do not want exceptions occurring within this unit, or
62 -- we are in big trouble. If an exceptional situation does occur, better
63 -- that it not be raised, since raising it can cause confusing chaos.
65 type Subprogram_Descriptor_List_Ptr is
66 access all Subprogram_Descriptor_List;
68 Subprogram_Descriptors : Subprogram_Descriptor_List_Ptr;
69 -- This location is initialized by Register_Exceptions to point to a
70 -- list of pointers to procedure descriptors, sorted into ascending
71 -- order of PC addresses.
73 -- Note that SDP_Table_Build is called *before* this unit (or any
74 -- other unit) is elaborated. That's important, because exceptions can
75 -- and do occur during elaboration of units, and must be handled during
76 -- elaboration. This means that we are counting on the fact that the
77 -- initialization of Subprogram_Descriptors to null is done by the
78 -- load process and NOT by an explicit assignment during elaboration.
80 Num_Subprogram_Descriptors : Natural;
81 -- Number of subprogram descriptors, the useful descriptors are stored
82 -- in Subprogram_Descriptors (1 .. Num_Subprogram_Descriptors). There
83 -- can be unused entries at the end of the array due to elimination of
84 -- duplicated entries (which can arise from use of pragma Import).
86 Exception_Tracebacks : Integer;
87 pragma Import (C, Exception_Tracebacks, "__gl_exception_tracebacks");
88 -- Boolean indicating whether tracebacks should be stored in exception
89 -- occurrences.
91 Zero_Cost_Exceptions : Integer;
92 pragma Import (C, Zero_Cost_Exceptions, "__gl_zero_cost_exceptions");
93 -- Boolean indicating if we are handling exceptions using a zero cost
94 -- mechanism.
96 -- ??? We currently have two alternatives for this scheme : one using
97 -- front-end tables and one using back-end tables. The former is known to
98 -- only work for GNAT3 and the latter is known to only work for GNAT5.
99 -- Both are present in this implementation and it would be good to have
100 -- separate bodies at some point.
102 -- Note that although we currently do not support it, the GCC3 back-end
103 -- tables are also potentially useable for setjmp/longjmp processing.
105 Nline : constant String := String' (1 => ASCII.LF);
106 -- Convenient shortcut
108 ------------------------------------------------
109 -- Entities to interface with the GCC runtime --
110 ------------------------------------------------
112 -- These come from "C++ ABI for Itanium : Exception handling", which is
113 -- the reference for GCC. They are used only when we are relying on
114 -- back-end tables for exception propagation, which in turn is currenly
115 -- only the case for Zero_Cost_Exceptions in GNAT5.
117 -- Return codes from the GCC runtime functions used to propagate
118 -- an exception.
120 type Unwind_Reason_Code is
121 (URC_NO_REASON,
122 URC_FOREIGN_EXCEPTION_CAUGHT,
123 URC_PHASE2_ERROR,
124 URC_PHASE1_ERROR,
125 URC_NORMAL_STOP,
126 URC_END_OF_STACK,
127 URC_HANDLER_FOUND,
128 URC_INSTALL_CONTEXT,
129 URC_CONTINUE_UNWIND);
131 -- ??? pragma Unreferenced is unknown until 3.15, so we need to disable
132 -- warnings around it to fix the bootstrap path.
134 pragma Warnings (Off);
135 pragma Unreferenced
136 (URC_NO_REASON,
137 URC_FOREIGN_EXCEPTION_CAUGHT,
138 URC_PHASE2_ERROR,
139 URC_PHASE1_ERROR,
140 URC_NORMAL_STOP,
141 URC_END_OF_STACK,
142 URC_HANDLER_FOUND,
143 URC_INSTALL_CONTEXT,
144 URC_CONTINUE_UNWIND);
145 pragma Warnings (On);
147 pragma Convention (C, Unwind_Reason_Code);
149 -- Mandatory common header for any exception object handled by the
150 -- GCC unwinding runtime.
152 subtype Exception_Class is String (1 .. 8);
154 GNAT_Exception_Class : constant Exception_Class
155 := "GNU" & ASCII.NUL & "Ada" & ASCII.NUL;
157 type Unwind_Exception is record
158 Class : Exception_Class := GNAT_Exception_Class;
159 Cleanup : System.Address := System.Null_Address;
160 Private1 : Integer;
161 Private2 : Integer;
162 end record;
164 pragma Convention (C, Unwind_Exception);
166 for Unwind_Exception'Alignment use Standard'Maximum_Alignment;
168 -- A GNAT exception object to be dealt with by the personality routine
169 -- called by the GCC unwinding runtime. This structure shall match the
170 -- one in raise.c and is currently experimental as it might be merged
171 -- with the GNAT runtime definition some day.
173 type GNAT_GCC_Exception is record
174 Header : Unwind_Exception;
175 -- Exception header first, as required by the ABI.
177 Id : Exception_Id;
178 -- Usual Exception identifier
180 Handled_By_Others : Boolean;
181 -- Is this exception handled by "when others" ?
183 Has_Cleanup : Boolean;
184 -- Did we see any at-end handler while walking up the stack
185 -- searching for a handler ? This is used to determine if we
186 -- start the propagation again after having tried once without
187 -- finding a true handler for the exception.
189 Select_Cleanups : Boolean;
190 -- Do we consider at-end handlers as legitimate handlers for the
191 -- exception ? This is used to control the propagation process
192 -- as described in Raise_Current_Excep.
193 end record;
195 pragma Convention (C, GNAT_GCC_Exception);
197 -- GCC runtime functions used
199 function Unwind_RaiseException
200 (E : access GNAT_GCC_Exception)
201 return Unwind_Reason_Code;
202 pragma Import (C, Unwind_RaiseException, "__gnat_Unwind_RaiseException");
204 -----------------------
205 -- Local Subprograms --
206 -----------------------
208 -- Note: the exported subprograms in this package body are called directly
209 -- from C clients using the given external name, even though they are not
210 -- technically visible in the Ada sense.
212 procedure AAA;
213 -- Mark start of procedures in this unit
215 procedure ZZZ;
216 -- Mark end of procedures in this package
218 function Address_Image (A : System.Address) return String;
219 -- Returns at string of the form 0xhhhhhhhhh for 32-bit addresses
220 -- or 0xhhhhhhhhhhhhhhhh for 64-bit addresses. Hex characters are
221 -- in lower case.
223 procedure Call_Chain (Excep : EOA);
224 -- Store up to Max_Tracebacks in Excep, corresponding to the current
225 -- call chain.
227 procedure Free
228 is new Ada.Unchecked_Deallocation
229 (Subprogram_Descriptor_List, Subprogram_Descriptor_List_Ptr);
231 procedure Process_Raise_Exception
232 (E : Exception_Id;
233 From_Signal_Handler : Boolean);
234 pragma Inline (Process_Raise_Exception);
235 pragma No_Return (Process_Raise_Exception);
236 -- This is the lowest level raise routine. It raises the exception
237 -- referenced by Current_Excep.all in the TSD, without deferring abort
238 -- (the caller must ensure that abort is deferred on entry).
240 -- This is actually the common implementation for Raise_Current_Excep and
241 -- Raise_From_Signal_Handler, with a couple of operations inhibited when
242 -- called from the latter. The origin of the call is indicated by the
243 -- From_Signal_Handler argument.
245 -- The Inline pragma is there for efficiency reasons.
247 procedure Propagate_Exception_With_FE_Support (Mstate : Machine_State);
248 pragma No_Return (Propagate_Exception_With_FE_Support);
249 -- This procedure propagates the exception represented by the occurrence
250 -- referenced by Current_Excep in the TSD for the current task. M is the
251 -- initial machine state, representing the site of the exception raise
252 -- operation.
254 -- The procedure searches the front end exception tables for an applicable
255 -- handler, calling Pop_Frame as needed. If and when it locates an
256 -- applicable handler, Enter_Handler is called to actually enter this
257 -- handler. If the search is unable to locate an applicable handler,
258 -- execution is terminated by calling Unhandled_Exception_Terminate.
260 procedure Propagate_Exception_With_GCC_Support (Mstate : Machine_State);
261 pragma No_Return (Propagate_Exception_With_GCC_Support);
262 -- This procedure propagates the exception represented by the occurrence
263 -- referenced by Current_Excep in the TSD for the current task. M is the
264 -- initial machine state, representing the site of the exception raise
265 -- operation. It is currently not used and is there for the purpose of
266 -- interface consistency against Propagate_Exception_With_FE_Support.
268 -- The procedure builds an object suitable for the libgcc processing and
269 -- calls Unwind_RaiseException to actually throw, taking care of handling
270 -- the two phase scheme it implements.
272 procedure Raise_Current_Excep (E : Exception_Id);
273 pragma No_Return (Raise_Current_Excep);
274 pragma Export (C, Raise_Current_Excep, "__gnat_raise_nodefer_with_msg");
275 -- This is a simple wrapper to Process_Raise_Exception setting the
276 -- From_Signal_Handler argument to False.
278 -- This external name for Raise_Current_Excep is historical, and probably
279 -- should be changed but for now we keep it, because gdb and gigi know
280 -- about it.
282 procedure Raise_Exception_No_Defer
283 (E : Exception_Id; Message : String := "");
284 pragma Export (Ada, Raise_Exception_No_Defer,
285 "ada__exceptions__raise_exception_no_defer");
286 pragma No_Return (Raise_Exception_No_Defer);
287 -- Similar to Raise_Exception, but with no abort deferral
289 procedure Raise_With_Msg (E : Exception_Id);
290 pragma No_Return (Raise_With_Msg);
291 pragma Export (C, Raise_With_Msg, "__gnat_raise_with_msg");
292 -- Raises an exception with given exception id value. A message
293 -- is associated with the raise, and has already been stored in the
294 -- exception occurrence referenced by the Current_Excep in the TSD.
295 -- Abort is deferred before the raise call.
297 procedure Raise_With_Location
298 (E : Exception_Id;
299 F : Big_String_Ptr;
300 L : Integer);
301 pragma No_Return (Raise_With_Location);
302 -- Raise an exception with given exception id value. A filename and line
303 -- number is associated with the raise and is stored in the exception
304 -- occurrence.
306 procedure Raise_With_Location_And_Msg
307 (E : Exception_Id;
308 F : Big_String_Ptr;
309 L : Integer;
310 M : Big_String_Ptr);
311 pragma No_Return (Raise_With_Location_And_Msg);
312 -- Raise an exception with given exception id value. A filename and line
313 -- number is associated with the raise and is stored in the exception
314 -- occurrence and in addition a string message M is appended to this.
316 procedure Raise_Constraint_Error
317 (File : Big_String_Ptr;
318 Line : Integer);
319 pragma No_Return (Raise_Constraint_Error);
320 pragma Export
321 (C, Raise_Constraint_Error, "__gnat_raise_constraint_error");
322 -- Raise constraint error with file:line information
324 procedure Raise_Constraint_Error_Msg
325 (File : Big_String_Ptr;
326 Line : Integer;
327 Msg : Big_String_Ptr);
328 pragma No_Return (Raise_Constraint_Error_Msg);
329 pragma Export
330 (C, Raise_Constraint_Error_Msg, "__gnat_raise_constraint_error_msg");
331 -- Raise constraint error with file:line + msg information
333 procedure Raise_Program_Error
334 (File : Big_String_Ptr;
335 Line : Integer);
336 pragma No_Return (Raise_Program_Error);
337 pragma Export
338 (C, Raise_Program_Error, "__gnat_raise_program_error");
339 -- Raise program error with file:line information
341 procedure Raise_Program_Error_Msg
342 (File : Big_String_Ptr;
343 Line : Integer;
344 Msg : Big_String_Ptr);
345 pragma No_Return (Raise_Program_Error_Msg);
346 pragma Export
347 (C, Raise_Program_Error_Msg, "__gnat_raise_program_error_msg");
348 -- Raise program error with file:line + msg information
350 procedure Raise_Storage_Error
351 (File : Big_String_Ptr;
352 Line : Integer);
353 pragma No_Return (Raise_Storage_Error);
354 pragma Export
355 (C, Raise_Storage_Error, "__gnat_raise_storage_error");
356 -- Raise storage error with file:line information
358 procedure Raise_Storage_Error_Msg
359 (File : Big_String_Ptr;
360 Line : Integer;
361 Msg : Big_String_Ptr);
362 pragma No_Return (Raise_Storage_Error_Msg);
363 pragma Export
364 (C, Raise_Storage_Error_Msg, "__gnat_raise_storage_error_msg");
365 -- Raise storage error with file:line + reason msg information
367 -- The exception raising process and the automatic tracing mechanism rely
368 -- on some careful use of flags attached to the exception occurrence. The
369 -- graph below illustrates the relations between the Raise_ subprograms
370 -- and identifies the points where basic flags such as Exception_Raised
371 -- are initialized.
373 -- (i) signs indicate the flags initialization points. R stands for Raise,
374 -- W for With, and E for Exception.
376 -- R_No_Msg R_E R_Pe R_Ce R_Se
377 -- | | | | |
378 -- +--+ +--+ +---+ | +---+
379 -- | | | | |
380 -- R_E_No_Defer(i) R_W_Msg(i) R_W_Loc R_W_C_Msg
381 -- | | | | | |
382 -- +------------+ | +-----------+ +--+ +--+ |
383 -- | | | | | |
384 -- | | | Set_E_C_Msg(i) |
385 -- | | | |
386 -- | | | +--------------------------+
387 -- | | | |
388 -- Raise_Current_Excep
390 procedure Reraise;
391 pragma No_Return (Reraise);
392 pragma Export (C, Reraise, "__gnat_reraise");
393 -- Reraises the exception referenced by the Current_Excep field of
394 -- the TSD (all fields of this exception occurrence are set). Abort
395 -- is deferred before the reraise operation.
397 function SDP_Table_Sort_Lt (Op1, Op2 : Natural) return Boolean;
398 -- Used in call to sort SDP table (SDP_Table_Build), compares two elements
400 procedure SDP_Table_Sort_Move (From : Natural; To : Natural);
401 -- Used in call to sort SDP table (SDP_Table_Build), moves one element
403 procedure Set_Exception_C_Msg
404 (Id : Exception_Id;
405 Msg1 : Big_String_Ptr;
406 Line : Integer := 0;
407 Msg2 : Big_String_Ptr := null);
408 -- This routine is called to setup the exception referenced by the
409 -- Current_Excep field in the TSD to contain the indicated Id value
410 -- and message. Msg1 is a null terminated string which is generated
411 -- as the exception message. If line is non-zero, then a colon and
412 -- the decimal representation of this integer is appended to the
413 -- message. When Msg2 is non-null, a space and this additional null
414 -- terminated string is added to the message.
416 procedure To_Stderr (S : String);
417 pragma Export (Ada, To_Stderr, "__gnat_to_stderr");
418 -- Little routine to output string to stderr that is also used
419 -- in the tasking run time.
421 procedure Unhandled_Exception_Terminate;
422 pragma No_Return (Unhandled_Exception_Terminate);
423 -- This procedure is called to terminate execution following an unhandled
424 -- exception. The exception information, including traceback if available
425 -- is output, and execution is then terminated. Note that at the point
426 -- where this routine is called, the stack has typically been destroyed
428 ---------------------------------
429 -- Debugger Interface Routines --
430 ---------------------------------
432 -- The routines here are null routines that normally have no effect.
433 -- they are provided for the debugger to place breakpoints on their
434 -- entry points to get control on an exception.
436 procedure Notify_Exception
437 (Id : Exception_Id;
438 Handler : Code_Loc;
439 Is_Others : Boolean);
440 pragma Export (C, Notify_Exception, "__gnat_notify_exception");
441 -- This routine is called whenever an exception is signalled. The Id
442 -- parameter is the Exception_Id of the exception being raised. The
443 -- second parameter Handler is Null_Loc if the exception is unhandled,
444 -- and is otherwise the entry point of the handler that will handle
445 -- the exception. Is_Others is True if the handler is an others handler
446 -- and False otherwise. In the unhandled exception case, if possible
447 -- (and certainly if zero cost exception handling is active), the
448 -- stack is still intact when this procedure is called. Note that this
449 -- routine is entered before any finalization handlers are entered if
450 -- the exception is unhandled by a "real" exception handler.
452 procedure Unhandled_Exception;
453 pragma Export (C, Unhandled_Exception, "__gnat_unhandled_exception");
454 -- This routine is called in addition to Notify_Exception in the
455 -- unhandled exception case. The fact that there are two routines
456 -- which are somewhat redundant is historical. Notify_Exception
457 -- certainly is complete enough, but GDB still uses this routine.
459 -----------------------------
460 -- Run-Time Check Routines --
461 -----------------------------
463 -- These routines are called from the runtime to raise a specific
464 -- exception with a reason message attached. The parameters are
465 -- the file name and line number in each case. The names are keyed
466 -- to the codes defined in Types.ads and a-types.h (for example,
467 -- the name Rcheck_05 refers to the Reason whose Pos code is 5).
469 procedure Rcheck_00 (File : Big_String_Ptr; Line : Integer);
470 procedure Rcheck_01 (File : Big_String_Ptr; Line : Integer);
471 procedure Rcheck_02 (File : Big_String_Ptr; Line : Integer);
472 procedure Rcheck_03 (File : Big_String_Ptr; Line : Integer);
473 procedure Rcheck_04 (File : Big_String_Ptr; Line : Integer);
474 procedure Rcheck_05 (File : Big_String_Ptr; Line : Integer);
475 procedure Rcheck_06 (File : Big_String_Ptr; Line : Integer);
476 procedure Rcheck_07 (File : Big_String_Ptr; Line : Integer);
477 procedure Rcheck_08 (File : Big_String_Ptr; Line : Integer);
478 procedure Rcheck_09 (File : Big_String_Ptr; Line : Integer);
479 procedure Rcheck_10 (File : Big_String_Ptr; Line : Integer);
480 procedure Rcheck_11 (File : Big_String_Ptr; Line : Integer);
481 procedure Rcheck_12 (File : Big_String_Ptr; Line : Integer);
482 procedure Rcheck_13 (File : Big_String_Ptr; Line : Integer);
483 procedure Rcheck_14 (File : Big_String_Ptr; Line : Integer);
484 procedure Rcheck_15 (File : Big_String_Ptr; Line : Integer);
485 procedure Rcheck_16 (File : Big_String_Ptr; Line : Integer);
486 procedure Rcheck_17 (File : Big_String_Ptr; Line : Integer);
487 procedure Rcheck_18 (File : Big_String_Ptr; Line : Integer);
488 procedure Rcheck_19 (File : Big_String_Ptr; Line : Integer);
489 procedure Rcheck_20 (File : Big_String_Ptr; Line : Integer);
490 procedure Rcheck_21 (File : Big_String_Ptr; Line : Integer);
491 procedure Rcheck_22 (File : Big_String_Ptr; Line : Integer);
492 procedure Rcheck_23 (File : Big_String_Ptr; Line : Integer);
493 procedure Rcheck_24 (File : Big_String_Ptr; Line : Integer);
494 procedure Rcheck_25 (File : Big_String_Ptr; Line : Integer);
495 procedure Rcheck_26 (File : Big_String_Ptr; Line : Integer);
496 procedure Rcheck_27 (File : Big_String_Ptr; Line : Integer);
497 procedure Rcheck_28 (File : Big_String_Ptr; Line : Integer);
499 pragma Export (C, Rcheck_00, "__gnat_rcheck_00");
500 pragma Export (C, Rcheck_01, "__gnat_rcheck_01");
501 pragma Export (C, Rcheck_02, "__gnat_rcheck_02");
502 pragma Export (C, Rcheck_03, "__gnat_rcheck_03");
503 pragma Export (C, Rcheck_04, "__gnat_rcheck_04");
504 pragma Export (C, Rcheck_05, "__gnat_rcheck_05");
505 pragma Export (C, Rcheck_06, "__gnat_rcheck_06");
506 pragma Export (C, Rcheck_07, "__gnat_rcheck_07");
507 pragma Export (C, Rcheck_08, "__gnat_rcheck_08");
508 pragma Export (C, Rcheck_09, "__gnat_rcheck_09");
509 pragma Export (C, Rcheck_10, "__gnat_rcheck_10");
510 pragma Export (C, Rcheck_11, "__gnat_rcheck_11");
511 pragma Export (C, Rcheck_12, "__gnat_rcheck_12");
512 pragma Export (C, Rcheck_13, "__gnat_rcheck_13");
513 pragma Export (C, Rcheck_14, "__gnat_rcheck_14");
514 pragma Export (C, Rcheck_15, "__gnat_rcheck_15");
515 pragma Export (C, Rcheck_16, "__gnat_rcheck_16");
516 pragma Export (C, Rcheck_17, "__gnat_rcheck_17");
517 pragma Export (C, Rcheck_18, "__gnat_rcheck_18");
518 pragma Export (C, Rcheck_19, "__gnat_rcheck_19");
519 pragma Export (C, Rcheck_20, "__gnat_rcheck_20");
520 pragma Export (C, Rcheck_21, "__gnat_rcheck_21");
521 pragma Export (C, Rcheck_22, "__gnat_rcheck_22");
522 pragma Export (C, Rcheck_23, "__gnat_rcheck_23");
523 pragma Export (C, Rcheck_24, "__gnat_rcheck_24");
524 pragma Export (C, Rcheck_25, "__gnat_rcheck_25");
525 pragma Export (C, Rcheck_26, "__gnat_rcheck_26");
526 pragma Export (C, Rcheck_27, "__gnat_rcheck_27");
527 pragma Export (C, Rcheck_28, "__gnat_rcheck_28");
529 ---------------------------------------------
530 -- Reason Strings for Run-Time Check Calls --
531 ---------------------------------------------
533 -- These strings are null-terminated and are used by Rcheck_nn. The
534 -- strings correspond to the definitions for Types.RT_Exception_Code.
536 use ASCII;
538 Rmsg_00 : constant String := "access check failed" & NUL;
539 Rmsg_01 : constant String := "access parameter is null" & NUL;
540 Rmsg_02 : constant String := "discriminant check failed" & NUL;
541 Rmsg_03 : constant String := "divide by zero" & NUL;
542 Rmsg_04 : constant String := "explicit raise" & NUL;
543 Rmsg_05 : constant String := "index check failed" & NUL;
544 Rmsg_06 : constant String := "invalid data" & NUL;
545 Rmsg_07 : constant String := "length check failed" & NUL;
546 Rmsg_08 : constant String := "overflow check failed" & NUL;
547 Rmsg_09 : constant String := "partition check failed" & NUL;
548 Rmsg_10 : constant String := "range check failed" & NUL;
549 Rmsg_11 : constant String := "tag check failed" & NUL;
550 Rmsg_12 : constant String := "access before elaboration" & NUL;
551 Rmsg_13 : constant String := "accessibility check failed" & NUL;
552 Rmsg_14 : constant String := "all guards closed" & NUL;
553 Rmsg_15 : constant String := "duplicated entry address" & NUL;
554 Rmsg_16 : constant String := "explicit raise" & NUL;
555 Rmsg_17 : constant String := "finalize raised exception" & NUL;
556 Rmsg_18 : constant String := "invalid data" & NUL;
557 Rmsg_19 : constant String := "misaligned address value" & NUL;
558 Rmsg_20 : constant String := "missing return" & NUL;
559 Rmsg_21 : constant String := "potentially blocking operation" & NUL;
560 Rmsg_22 : constant String := "stubbed subprogram called" & NUL;
561 Rmsg_23 : constant String := "unchecked union restriction" & NUL;
562 Rmsg_24 : constant String := "empty storage pool" & NUL;
563 Rmsg_25 : constant String := "explicit raise" & NUL;
564 Rmsg_26 : constant String := "infinite recursion" & NUL;
565 Rmsg_27 : constant String := "object too large" & NUL;
566 Rmsg_28 : constant String := "restriction violation" & NUL;
568 --------------------------------------
569 -- Calls to Run-Time Check Routines --
570 --------------------------------------
572 procedure Rcheck_00 (File : Big_String_Ptr; Line : Integer) is
573 begin
574 Raise_Constraint_Error_Msg (File, Line, To_Ptr (Rmsg_00'Address));
575 end Rcheck_00;
577 procedure Rcheck_01 (File : Big_String_Ptr; Line : Integer) is
578 begin
579 Raise_Constraint_Error_Msg (File, Line, To_Ptr (Rmsg_01'Address));
580 end Rcheck_01;
582 procedure Rcheck_02 (File : Big_String_Ptr; Line : Integer) is
583 begin
584 Raise_Constraint_Error_Msg (File, Line, To_Ptr (Rmsg_02'Address));
585 end Rcheck_02;
587 procedure Rcheck_03 (File : Big_String_Ptr; Line : Integer) is
588 begin
589 Raise_Constraint_Error_Msg (File, Line, To_Ptr (Rmsg_03'Address));
590 end Rcheck_03;
592 procedure Rcheck_04 (File : Big_String_Ptr; Line : Integer) is
593 begin
594 Raise_Constraint_Error_Msg (File, Line, To_Ptr (Rmsg_04'Address));
595 end Rcheck_04;
597 procedure Rcheck_05 (File : Big_String_Ptr; Line : Integer) is
598 begin
599 Raise_Constraint_Error_Msg (File, Line, To_Ptr (Rmsg_05'Address));
600 end Rcheck_05;
602 procedure Rcheck_06 (File : Big_String_Ptr; Line : Integer) is
603 begin
604 Raise_Constraint_Error_Msg (File, Line, To_Ptr (Rmsg_06'Address));
605 end Rcheck_06;
607 procedure Rcheck_07 (File : Big_String_Ptr; Line : Integer) is
608 begin
609 Raise_Constraint_Error_Msg (File, Line, To_Ptr (Rmsg_07'Address));
610 end Rcheck_07;
612 procedure Rcheck_08 (File : Big_String_Ptr; Line : Integer) is
613 begin
614 Raise_Constraint_Error_Msg (File, Line, To_Ptr (Rmsg_08'Address));
615 end Rcheck_08;
617 procedure Rcheck_09 (File : Big_String_Ptr; Line : Integer) is
618 begin
619 Raise_Constraint_Error_Msg (File, Line, To_Ptr (Rmsg_09'Address));
620 end Rcheck_09;
622 procedure Rcheck_10 (File : Big_String_Ptr; Line : Integer) is
623 begin
624 Raise_Constraint_Error_Msg (File, Line, To_Ptr (Rmsg_10'Address));
625 end Rcheck_10;
627 procedure Rcheck_11 (File : Big_String_Ptr; Line : Integer) is
628 begin
629 Raise_Constraint_Error_Msg (File, Line, To_Ptr (Rmsg_11'Address));
630 end Rcheck_11;
632 procedure Rcheck_12 (File : Big_String_Ptr; Line : Integer) is
633 begin
634 Raise_Program_Error_Msg (File, Line, To_Ptr (Rmsg_12'Address));
635 end Rcheck_12;
637 procedure Rcheck_13 (File : Big_String_Ptr; Line : Integer) is
638 begin
639 Raise_Program_Error_Msg (File, Line, To_Ptr (Rmsg_13'Address));
640 end Rcheck_13;
642 procedure Rcheck_14 (File : Big_String_Ptr; Line : Integer) is
643 begin
644 Raise_Program_Error_Msg (File, Line, To_Ptr (Rmsg_14'Address));
645 end Rcheck_14;
647 procedure Rcheck_15 (File : Big_String_Ptr; Line : Integer) is
648 begin
649 Raise_Program_Error_Msg (File, Line, To_Ptr (Rmsg_15'Address));
650 end Rcheck_15;
652 procedure Rcheck_16 (File : Big_String_Ptr; Line : Integer) is
653 begin
654 Raise_Program_Error_Msg (File, Line, To_Ptr (Rmsg_16'Address));
655 end Rcheck_16;
657 procedure Rcheck_17 (File : Big_String_Ptr; Line : Integer) is
658 begin
659 Raise_Program_Error_Msg (File, Line, To_Ptr (Rmsg_17'Address));
660 end Rcheck_17;
662 procedure Rcheck_18 (File : Big_String_Ptr; Line : Integer) is
663 begin
664 Raise_Program_Error_Msg (File, Line, To_Ptr (Rmsg_18'Address));
665 end Rcheck_18;
667 procedure Rcheck_19 (File : Big_String_Ptr; Line : Integer) is
668 begin
669 Raise_Program_Error_Msg (File, Line, To_Ptr (Rmsg_19'Address));
670 end Rcheck_19;
672 procedure Rcheck_20 (File : Big_String_Ptr; Line : Integer) is
673 begin
674 Raise_Program_Error_Msg (File, Line, To_Ptr (Rmsg_20'Address));
675 end Rcheck_20;
677 procedure Rcheck_21 (File : Big_String_Ptr; Line : Integer) is
678 begin
679 Raise_Program_Error_Msg (File, Line, To_Ptr (Rmsg_21'Address));
680 end Rcheck_21;
682 procedure Rcheck_22 (File : Big_String_Ptr; Line : Integer) is
683 begin
684 Raise_Program_Error_Msg (File, Line, To_Ptr (Rmsg_22'Address));
685 end Rcheck_22;
687 procedure Rcheck_23 (File : Big_String_Ptr; Line : Integer) is
688 begin
689 Raise_Storage_Error_Msg (File, Line, To_Ptr (Rmsg_23'Address));
690 end Rcheck_23;
692 procedure Rcheck_24 (File : Big_String_Ptr; Line : Integer) is
693 begin
694 Raise_Storage_Error_Msg (File, Line, To_Ptr (Rmsg_24'Address));
695 end Rcheck_24;
697 procedure Rcheck_25 (File : Big_String_Ptr; Line : Integer) is
698 begin
699 Raise_Storage_Error_Msg (File, Line, To_Ptr (Rmsg_25'Address));
700 end Rcheck_25;
702 procedure Rcheck_26 (File : Big_String_Ptr; Line : Integer) is
703 begin
704 Raise_Storage_Error_Msg (File, Line, To_Ptr (Rmsg_26'Address));
705 end Rcheck_26;
707 procedure Rcheck_27 (File : Big_String_Ptr; Line : Integer) is
708 begin
709 Raise_Storage_Error_Msg (File, Line, To_Ptr (Rmsg_27'Address));
710 end Rcheck_27;
712 procedure Rcheck_28 (File : Big_String_Ptr; Line : Integer) is
713 begin
714 Raise_Storage_Error_Msg (File, Line, To_Ptr (Rmsg_28'Address));
715 end Rcheck_28;
717 ---------------------------------------
718 -- Exception backtracing subprograms --
719 ---------------------------------------
721 -- What is automatically output when exception tracing is on basically
722 -- corresponds to the usual exception information, but with the call
723 -- chain backtrace possibly tailored by a backtrace decorator. Modifying
724 -- Exception_Information itself is not a good idea because the decorated
725 -- output is completely out of control and would break all our code
726 -- related to the streaming of exceptions.
728 -- We then provide an alternative function to Exception_Information to
729 -- compute the possibly tailored output, which is equivalent if no
730 -- decorator is currently set :
732 function Tailored_Exception_Information
733 (X : Exception_Occurrence)
734 return String;
735 -- Exception information to be output in the case of automatic tracing
736 -- requested through GNAT.Exception_Traces.
738 -- This is the same as Exception_Information if no backtrace decorator
739 -- is currently in place. Otherwise, this is Exception_Information with
740 -- the call chain raw addresses replaced by the result of a call to the
741 -- current decorator provided with the call chain addresses.
743 pragma Export
744 (Ada, Tailored_Exception_Information,
745 "__gnat_tailored_exception_information");
746 -- This function is used within this package but also from within
747 -- System.Tasking.Stages.
749 -- The output of Exception_Information and Tailored_Exception_Information
750 -- share a common part which was formerly built using local procedures
751 -- within Exception_Information. These procedures have been extracted from
752 -- their original place to be available to Tailored_Exception_Information
753 -- also.
755 -- Each of these procedures appends some input to an information string
756 -- currently being built. The Ptr argument represents the last position
757 -- in this string at which a character has been written.
759 procedure Append_Info_Nat
760 (N : Natural;
761 Info : in out String;
762 Ptr : in out Natural);
763 -- Append the image of N at the end of the provided information string
765 procedure Append_Info_NL
766 (Info : in out String;
767 Ptr : in out Natural);
768 -- Append a LF at the end of the provided information string
770 procedure Append_Info_String
771 (S : String;
772 Info : in out String;
773 Ptr : in out Natural);
774 -- Append a string at the end of the provided information string
776 -- To build Exception_Information and Tailored_Exception_Information,
777 -- we then use three intermediate functions :
779 function Basic_Exception_Information
780 (X : Exception_Occurrence)
781 return String;
782 -- Returns the basic exception information string associated with a
783 -- given exception occurrence. This is the common part shared by both
784 -- Exception_Information and Tailored_Exception_Infomation.
786 function Basic_Exception_Traceback
787 (X : Exception_Occurrence)
788 return String;
789 -- Returns an image of the complete call chain associated with an
790 -- exception occurrence in its most basic form, that is as a raw sequence
791 -- of hexadecimal binary addresses.
793 function Tailored_Exception_Traceback
794 (X : Exception_Occurrence)
795 return String;
796 -- Returns an image of the complete call chain associated with an
797 -- exception occurrence, either in its basic form if no decorator is
798 -- in place, or as formatted by the decorator otherwise.
800 -- The overall organization of the exception information related code
801 -- is summarized below :
803 -- Exception_Information
804 -- |
805 -- +-------+--------+
806 -- | |
807 -- Basic_Exc_Info & Basic_Exc_Tback
810 -- Tailored_Exception_Information
811 -- |
812 -- +----------+----------+
813 -- | |
814 -- Basic_Exc_Info & Tailored_Exc_Tback
815 -- |
816 -- +-----------+------------+
817 -- | |
818 -- Basic_Exc_Tback Or Tback_Decorator
819 -- if no decorator set otherwise
821 ----------------------------------------------
822 -- Run-Time Exception Notification Routines --
823 ----------------------------------------------
825 -- The notification routines described above are low level "handles" for
826 -- the debugger but what needs to be done at the notification points
827 -- always involves more than just calling one of these routines. The
828 -- routines below provide a common run-time interface for this purpose,
829 -- with variations depending on the handled/not handled status of the
830 -- occurrence. They are exported to be usable by the Ada exception
831 -- handling personality routine when the GCC 3 mechanism is used.
833 procedure Notify_Handled_Exception
834 (Handler : Code_Loc;
835 Is_Others : Boolean;
836 Low_Notify : Boolean);
837 pragma Export (C, Notify_Handled_Exception,
838 "__gnat_notify_handled_exception");
839 -- Routine to call when a handled occurrence is about to be propagated.
840 -- Low_Notify might be set to false to skip the low level debugger
841 -- notification, which is useful when the information it requires is
842 -- not available, like in the SJLJ case.
844 procedure Notify_Unhandled_Exception (Id : Exception_Id);
845 pragma Export (C, Notify_Unhandled_Exception,
846 "__gnat_notify_unhandled_exception");
847 -- Routine to call when an unhandled occurrence is about to be propagated.
849 --------------------------------
850 -- Import Run-Time C Routines --
851 --------------------------------
853 -- The purpose of the following pragma Imports is to ensure that we
854 -- generate appropriate subprogram descriptors for all C routines in
855 -- the standard GNAT library that can raise exceptions. This ensures
856 -- that the exception propagation can properly find these routines
858 pragma Warnings (Off); -- so old compiler does not complain
859 pragma Propagate_Exceptions;
861 procedure Unhandled_Terminate;
862 pragma Import (C, Unhandled_Terminate, "__gnat_unhandled_terminate");
864 -----------------------
865 -- Polling Interface --
866 -----------------------
868 type Unsigned is mod 2 ** 32;
870 Counter : Unsigned := 0;
871 -- This counter is provided for convenience. It can be used in Poll to
872 -- perform periodic but not systematic operations.
874 procedure Poll is separate;
875 -- The actual polling routine is separate, so that it can easily
876 -- be replaced with a target dependent version.
878 ---------
879 -- AAA --
880 ---------
882 -- This dummy procedure gives us the start of the PC range for addresses
883 -- within the exception unit itself. We hope that gigi/gcc keep all the
884 -- procedures in their original order!
886 procedure AAA is
887 begin
888 null;
889 end AAA;
891 -------------------
892 -- Address_Image --
893 -------------------
895 function Address_Image (A : Address) return String is
896 S : String (1 .. 18);
897 P : Natural;
898 N : Integer_Address;
900 H : constant array (Integer range 0 .. 15) of Character :=
901 "0123456789abcdef";
902 begin
903 P := S'Last;
904 N := To_Integer (A);
905 while N /= 0 loop
906 S (P) := H (Integer (N mod 16));
907 P := P - 1;
908 N := N / 16;
909 end loop;
911 S (P - 1) := '0';
912 S (P) := 'x';
913 return S (P - 1 .. S'Last);
914 end Address_Image;
916 ---------------------
917 -- Append_Info_Nat --
918 ---------------------
920 procedure Append_Info_Nat
921 (N : Natural;
922 Info : in out String;
923 Ptr : in out Natural)
925 begin
926 if N > 9 then
927 Append_Info_Nat (N / 10, Info, Ptr);
928 end if;
930 Ptr := Ptr + 1;
931 Info (Ptr) := Character'Val (Character'Pos ('0') + N mod 10);
932 end Append_Info_Nat;
934 --------------------
935 -- Append_Info_NL --
936 --------------------
938 procedure Append_Info_NL
939 (Info : in out String;
940 Ptr : in out Natural)
942 begin
943 Ptr := Ptr + 1;
944 Info (Ptr) := ASCII.LF;
945 end Append_Info_NL;
947 ------------------------
948 -- Append_Info_String --
949 ------------------------
951 procedure Append_Info_String
952 (S : String;
953 Info : in out String;
954 Ptr : in out Natural)
956 begin
957 Info (Ptr + 1 .. Ptr + S'Length) := S;
958 Ptr := Ptr + S'Length;
959 end Append_Info_String;
961 ---------------------------------
962 -- Basic_Exception_Information --
963 ---------------------------------
965 function Basic_Exception_Information
966 (X : Exception_Occurrence)
967 return String
969 Name : constant String := Exception_Name (X);
970 Msg : constant String := Exception_Message (X);
971 -- Exception name and message that are going to be included in the
972 -- information to return, if not empty.
974 Name_Len : constant Natural := Name'Length;
975 Msg_Len : constant Natural := Msg'Length;
976 -- Length of these strings, useful to compute the size of the string
977 -- we have to allocate for the complete result as well as in the body
978 -- of this procedure.
980 Info_Maxlen : constant Natural := 50 + Name_Len + Msg_Len;
981 -- Maximum length of the information string we will build, with :
983 -- 50 = 16 + 2 for the text associated with the name
984 -- + 9 + 2 for the text associated with the message
985 -- + 5 + 2 for the text associated with the pid
986 -- + 14 for the text image of the pid itself and a margin.
988 -- This is indeed a maximum since some data may not appear at all if
989 -- not relevant. For example, nothing related to the exception message
990 -- will be there if this message is empty.
992 -- WARNING : Do not forget to update these numbers if anything
993 -- involved in the computation changes.
995 Info : String (1 .. Info_Maxlen);
996 -- Information string we are going to build, containing the common
997 -- part shared by Exc_Info and Tailored_Exc_Info.
999 Ptr : Natural := 0;
1001 begin
1002 -- Output exception name and message except for _ABORT_SIGNAL, where
1003 -- these two lines are omitted (see discussion above).
1005 if Name (1) /= '_' then
1006 Append_Info_String ("Exception name: ", Info, Ptr);
1007 Append_Info_String (Name, Info, Ptr);
1008 Append_Info_NL (Info, Ptr);
1010 if Msg_Len /= 0 then
1011 Append_Info_String ("Message: ", Info, Ptr);
1012 Append_Info_String (Msg, Info, Ptr);
1013 Append_Info_NL (Info, Ptr);
1014 end if;
1015 end if;
1017 -- Output PID line if non-zero
1019 if X.Pid /= 0 then
1020 Append_Info_String ("PID: ", Info, Ptr);
1021 Append_Info_Nat (X.Pid, Info, Ptr);
1022 Append_Info_NL (Info, Ptr);
1023 end if;
1025 return Info (1 .. Ptr);
1026 end Basic_Exception_Information;
1028 -------------------------------
1029 -- Basic_Exception_Traceback --
1030 -------------------------------
1032 function Basic_Exception_Traceback
1033 (X : Exception_Occurrence)
1034 return String
1036 Info_Maxlen : constant Natural := 35 + X.Num_Tracebacks * 19;
1037 -- Maximum length of the information string we are building, with :
1038 -- 33 = 31 + 4 for the text before and after the traceback, and
1039 -- 19 = 2 + 16 + 1 for each address ("0x" + HHHH + " ")
1041 -- WARNING : Do not forget to update these numbers if anything
1042 -- involved in the computation changes.
1044 Info : String (1 .. Info_Maxlen);
1045 -- Information string we are going to build, containing an image
1046 -- of the call chain associated with the exception occurrence in its
1047 -- most basic form, that is as a sequence of binary addresses.
1049 Ptr : Natural := 0;
1051 begin
1052 if X.Num_Tracebacks > 0 then
1053 Append_Info_String ("Call stack traceback locations:", Info, Ptr);
1054 Append_Info_NL (Info, Ptr);
1056 for J in 1 .. X.Num_Tracebacks loop
1057 Append_Info_String (Address_Image (X.Tracebacks (J)), Info, Ptr);
1058 exit when J = X.Num_Tracebacks;
1059 Append_Info_String (" ", Info, Ptr);
1060 end loop;
1062 Append_Info_NL (Info, Ptr);
1063 end if;
1065 return Info (1 .. Ptr);
1066 end Basic_Exception_Traceback;
1068 -----------------
1069 -- Break_Start --
1070 -----------------
1072 procedure Break_Start is
1073 begin
1074 null;
1075 end Break_Start;
1077 ----------------
1078 -- Call_Chain --
1079 ----------------
1081 procedure Call_Chain (Excep : EOA) is
1082 begin
1083 if Excep.Num_Tracebacks /= 0 then
1084 -- This is a reraise, no need to store a new (wrong) chain.
1085 return;
1086 end if;
1088 System.Traceback.Call_Chain
1089 (Excep.Tracebacks'Address,
1090 Max_Tracebacks,
1091 Excep.Num_Tracebacks,
1092 AAA'Address,
1093 ZZZ'Address);
1094 end Call_Chain;
1096 ------------------------------
1097 -- Current_Target_Exception --
1098 ------------------------------
1100 function Current_Target_Exception return Exception_Occurrence is
1101 begin
1102 return Null_Occurrence;
1103 end Current_Target_Exception;
1105 -------------------
1106 -- EId_To_String --
1107 -------------------
1109 function EId_To_String (X : Exception_Id) return String is
1110 begin
1111 if X = Null_Id then
1112 return "";
1113 else
1114 return Exception_Name (X);
1115 end if;
1116 end EId_To_String;
1118 ------------------
1119 -- EO_To_String --
1120 ------------------
1122 -- We use the null string to represent the null occurrence, otherwise
1123 -- we output the Exception_Information string for the occurrence.
1125 function EO_To_String (X : Exception_Occurrence) return String is
1126 begin
1127 if X.Id = Null_Id then
1128 return "";
1129 else
1130 return Exception_Information (X);
1131 end if;
1132 end EO_To_String;
1134 ------------------------
1135 -- Exception_Identity --
1136 ------------------------
1138 function Exception_Identity
1139 (X : Exception_Occurrence)
1140 return Exception_Id
1142 begin
1143 if X.Id = Null_Id then
1144 raise Constraint_Error;
1145 else
1146 return X.Id;
1147 end if;
1148 end Exception_Identity;
1150 ---------------------------
1151 -- Exception_Information --
1152 ---------------------------
1154 -- The format of the string is:
1156 -- Exception_Name: nnnnn
1157 -- Message: mmmmm
1158 -- PID: ppp
1159 -- Call stack traceback locations:
1160 -- 0xhhhh 0xhhhh 0xhhhh ... 0xhhh
1162 -- where
1164 -- nnnn is the fully qualified name of the exception in all upper
1165 -- case letters. This line is always present.
1167 -- mmmm is the message (this line present only if message is non-null)
1169 -- ppp is the Process Id value as a decimal integer (this line is
1170 -- present only if the Process Id is non-zero). Currently we are
1171 -- not making use of this field.
1173 -- The Call stack traceback locations line and the following values
1174 -- are present only if at least one traceback location was recorded.
1175 -- the values are given in C style format, with lower case letters
1176 -- for a-f, and only as many digits present as are necessary.
1178 -- The line terminator sequence at the end of each line, including the
1179 -- last line is a CR-LF sequence (16#0D# followed by 16#0A#).
1181 -- The Exception_Name and Message lines are omitted in the abort
1182 -- signal case, since this is not really an exception, and the only
1183 -- use of this routine is internal for printing termination output.
1185 -- WARNING: if the format of the generated string is changed, please note
1186 -- that an equivalent modification to the routine String_To_EO must be
1187 -- made to preserve proper functioning of the stream attributes.
1189 function Exception_Information (X : Exception_Occurrence) return String is
1191 -- This information is now built using the circuitry introduced in
1192 -- association with the support of traceback decorators, as the
1193 -- catenation of the exception basic information and the call chain
1194 -- backtrace in its basic form.
1196 Basic_Info : constant String := Basic_Exception_Information (X);
1197 Tback_Info : constant String := Basic_Exception_Traceback (X);
1199 Basic_Len : constant Natural := Basic_Info'Length;
1200 Tback_Len : constant Natural := Tback_Info'Length;
1202 Info : String (1 .. Basic_Len + Tback_Len);
1203 Ptr : Natural := 0;
1205 begin
1206 Append_Info_String (Basic_Info, Info, Ptr);
1207 Append_Info_String (Tback_Info, Info, Ptr);
1209 return Info;
1210 end Exception_Information;
1212 -----------------------
1213 -- Exception_Message --
1214 -----------------------
1216 function Exception_Message (X : Exception_Occurrence) return String is
1217 begin
1218 if X.Id = Null_Id then
1219 raise Constraint_Error;
1220 end if;
1222 return X.Msg (1 .. X.Msg_Length);
1223 end Exception_Message;
1225 --------------------
1226 -- Exception_Name --
1227 --------------------
1229 function Exception_Name (Id : Exception_Id) return String is
1230 begin
1231 if Id = null then
1232 raise Constraint_Error;
1233 end if;
1235 return Id.Full_Name.all (1 .. Id.Name_Length - 1);
1236 end Exception_Name;
1238 function Exception_Name (X : Exception_Occurrence) return String is
1239 begin
1240 return Exception_Name (X.Id);
1241 end Exception_Name;
1243 ---------------------------
1244 -- Exception_Name_Simple --
1245 ---------------------------
1247 function Exception_Name_Simple (X : Exception_Occurrence) return String is
1248 Name : constant String := Exception_Name (X);
1249 P : Natural;
1251 begin
1252 P := Name'Length;
1253 while P > 1 loop
1254 exit when Name (P - 1) = '.';
1255 P := P - 1;
1256 end loop;
1258 return Name (P .. Name'Length);
1259 end Exception_Name_Simple;
1261 -----------------------------
1262 -- Process_Raise_Exception --
1263 -----------------------------
1265 procedure Process_Raise_Exception
1266 (E : Exception_Id;
1267 From_Signal_Handler : Boolean)
1269 pragma Inspection_Point (E);
1270 -- This is so the debugger can reliably inspect the parameter
1272 Jumpbuf_Ptr : constant Address := Get_Jmpbuf_Address.all;
1273 Mstate_Ptr : constant Machine_State :=
1274 Machine_State (Get_Machine_State_Addr.all);
1275 Excep : EOA := Get_Current_Excep.all;
1277 begin
1278 -- WARNING : There should be no exception handler for this body
1279 -- because this would cause gigi to prepend a setup for a new
1280 -- jmpbuf to the sequence of statements. We would then always get
1281 -- this new buf in Jumpbuf_Ptr instead of the one for the exception
1282 -- we are handling, which would completely break the whole design
1283 -- of this procedure.
1285 -- Processing varies between zero cost and setjmp/lonjmp processing.
1287 if Zero_Cost_Exceptions /= 0 then
1289 -- Use the front-end tables to propagate if we have them, otherwise
1290 -- resort to the GCC back-end alternative. The backtrace for the
1291 -- occurrence is stored while walking up the stack, and thus stops
1292 -- in the handler's frame if there is one. Notifications are also
1293 -- not performed here since it is not yet known if the exception is
1294 -- handled.
1296 -- Set the machine state unless we are raising from a signal handler
1297 -- since it has already been set properly in that case.
1299 if not From_Signal_Handler then
1300 Set_Machine_State (Mstate_Ptr);
1301 end if;
1303 if Subprogram_Descriptors /= null then
1304 Propagate_Exception_With_FE_Support (Mstate_Ptr);
1305 else
1306 Propagate_Exception_With_GCC_Support (Mstate_Ptr);
1307 end if;
1309 else
1311 -- Compute the backtrace for this occurrence if the corresponding
1312 -- binder option has been set and we are not raising from a signal
1313 -- handler. Call_Chain takes care of the reraise case.
1315 if not From_Signal_Handler
1316 and then Exception_Tracebacks /= 0
1317 then
1318 Call_Chain (Excep);
1319 end if;
1321 -- If the jump buffer pointer is non-null, transfer control using
1322 -- it. Otherwise announce an unhandled exception (note that this
1323 -- means that we have no finalizations to do other than at the outer
1324 -- level). Perform the necessary notification tasks in both cases.
1326 if Jumpbuf_Ptr /= Null_Address then
1328 if not Excep.Exception_Raised then
1329 Excep.Exception_Raised := True;
1330 Notify_Handled_Exception (Null_Loc, False, False);
1332 -- The low level debugger notification is skipped from the
1333 -- call above because we do not have the necessary information
1334 -- to "feed" it properly.
1336 end if;
1338 builtin_longjmp (Jumpbuf_Ptr, 1);
1340 else
1341 Notify_Unhandled_Exception (E);
1342 Unhandled_Exception_Terminate;
1343 end if;
1344 end if;
1346 end Process_Raise_Exception;
1348 -----------------------------------------
1349 -- Propagate_Exception_With_FE_Support --
1350 -----------------------------------------
1352 procedure Propagate_Exception_With_FE_Support (Mstate : Machine_State) is
1353 Excep : constant EOA := Get_Current_Excep.all;
1354 Loc : Code_Loc;
1355 Lo, Hi : Natural;
1356 Pdesc : Natural;
1357 Hrec : Handler_Record_Ptr;
1358 Info : Subprogram_Info_Type;
1360 type Machine_State_Record is
1361 new Storage_Array (1 .. Machine_State_Length);
1362 for Machine_State_Record'Alignment use Standard'Maximum_Alignment;
1364 procedure Duplicate_Machine_State (Dest, Src : Machine_State);
1365 -- Copy Src into Dest, assuming that a Machine_State is pointing to
1366 -- an area of Machine_State_Length bytes.
1368 procedure Duplicate_Machine_State (Dest, Src : Machine_State) is
1369 type Machine_State_Record_Access is access Machine_State_Record;
1370 function To_MSR is new Unchecked_Conversion
1371 (Machine_State, Machine_State_Record_Access);
1373 begin
1374 To_MSR (Dest).all := To_MSR (Src).all;
1375 end Duplicate_Machine_State;
1377 -- Data for handling the finalization handler case. A simple approach
1378 -- in this routine would simply to unwind stack frames till we find a
1379 -- handler and then enter it. But this is undesirable in the case where
1380 -- we have only finalization handlers, and no "real" handler, i.e. a
1381 -- case where we have an unhandled exception.
1383 -- In this case we prefer to signal unhandled exception with the stack
1384 -- intact, and entering finalization handlers would destroy the stack
1385 -- state. To deal with this, as we unwind the stack, we note the first
1386 -- finalization handler, and remember it in the following variables.
1387 -- We then continue to unwind. If and when we find a "real", i.e. non-
1388 -- finalization handler, then we use these variables to pass control to
1389 -- the finalization handler.
1391 FH_Found : Boolean := False;
1392 -- Set when a finalization handler is found
1394 FH_Mstate : aliased Machine_State_Record;
1395 -- Records the machine state for the finalization handler
1397 FH_Handler : Code_Loc := Null_Address;
1398 -- Record handler address for finalization handler
1400 FH_Num_Trb : Natural := 0;
1401 -- Save number of tracebacks for finalization handler
1403 begin
1404 -- Loop through stack frames as exception propagates
1406 Main_Loop : loop
1407 Loc := Get_Code_Loc (Mstate);
1408 exit Main_Loop when Loc = Null_Loc;
1410 -- Record location unless it is inside this unit. Note: this
1411 -- test should really say Code_Address, but Address is the same
1412 -- as Code_Address for unnested subprograms, and Code_Address
1413 -- would cause a bootstrap problem
1415 if Loc < AAA'Address or else Loc > ZZZ'Address then
1417 -- Record location unless we already recorded max tracebacks
1419 if Excep.Num_Tracebacks /= Max_Tracebacks then
1421 -- Do not record location if it is the return point from
1422 -- a reraise call from within a cleanup handler
1424 if not Excep.Cleanup_Flag then
1425 Excep.Num_Tracebacks := Excep.Num_Tracebacks + 1;
1426 Excep.Tracebacks (Excep.Num_Tracebacks) := Loc;
1428 -- For reraise call from cleanup handler, skip entry and
1429 -- clear the flag so that we will start to record again
1431 else
1432 Excep.Cleanup_Flag := False;
1433 end if;
1434 end if;
1435 end if;
1437 -- Do binary search on procedure table
1439 Lo := 1;
1440 Hi := Num_Subprogram_Descriptors;
1442 -- Binary search loop
1444 loop
1445 Pdesc := (Lo + Hi) / 2;
1447 -- Note that Loc is expected to be the procedure's call point
1448 -- and not the return point.
1450 if Loc < Subprogram_Descriptors (Pdesc).Code then
1451 Hi := Pdesc - 1;
1453 elsif Pdesc < Num_Subprogram_Descriptors
1454 and then Loc > Subprogram_Descriptors (Pdesc + 1).Code
1455 then
1456 Lo := Pdesc + 1;
1458 else
1459 exit;
1460 end if;
1462 -- This happens when the current Loc is completely outside of
1463 -- the range of the program, which usually means that we reached
1464 -- the top level frame (e.g __start). In this case we have an
1465 -- unhandled exception.
1467 exit Main_Loop when Hi < Lo;
1468 end loop;
1470 -- Come here with Subprogram_Descriptors (Pdesc) referencing the
1471 -- procedure descriptor that applies to this PC value. Now do a
1472 -- serial search to see if any handler is applicable to this PC
1473 -- value, and to the exception that we are propagating
1475 for J in 1 .. Subprogram_Descriptors (Pdesc).Num_Handlers loop
1476 Hrec := Subprogram_Descriptors (Pdesc).Handler_Records (J);
1478 if Loc >= Hrec.Lo and then Loc < Hrec.Hi then
1480 -- PC range is applicable, see if handler is for this exception
1482 -- First test for case of "all others" (finalization) handler.
1483 -- We do not enter such a handler until we are sure there is
1484 -- a real handler further up the stack.
1486 if Hrec.Id = All_Others_Id then
1488 -- If this is the first finalization handler, then
1489 -- save the machine state so we can enter it later
1490 -- without having to repeat the search.
1492 if not FH_Found then
1493 FH_Found := True;
1494 Duplicate_Machine_State
1495 (Machine_State (FH_Mstate'Address), Mstate);
1496 FH_Handler := Hrec.Handler;
1497 FH_Num_Trb := Excep.Num_Tracebacks;
1498 end if;
1500 -- Normal (non-finalization exception with matching Id)
1502 elsif Excep.Id = Hrec.Id
1503 or else (Hrec.Id = Others_Id
1504 and not Excep.Id.Not_Handled_By_Others)
1505 then
1506 -- Perform the necessary notification tasks.
1508 Notify_Handled_Exception
1509 (Hrec.Handler, Hrec.Id = Others_Id, True);
1511 -- If we already encountered a finalization handler, then
1512 -- reset the context to that handler, and enter it.
1514 if FH_Found then
1515 Excep.Num_Tracebacks := FH_Num_Trb;
1516 Excep.Cleanup_Flag := True;
1518 Enter_Handler
1519 (Machine_State (FH_Mstate'Address), FH_Handler);
1521 -- If we have not encountered a finalization handler,
1522 -- then enter the current handler.
1524 else
1525 Enter_Handler (Mstate, Hrec.Handler);
1526 end if;
1527 end if;
1528 end if;
1529 end loop;
1531 Info := Subprogram_Descriptors (Pdesc).Subprogram_Info;
1532 exit Main_Loop when Info = No_Info;
1533 Pop_Frame (Mstate, Info);
1534 end loop Main_Loop;
1536 -- Fall through if no "real" exception handler found. First thing is to
1537 -- perform the necessary notification tasks with the stack intact.
1539 Notify_Unhandled_Exception (Excep.Id);
1541 -- If there were finalization handlers, then enter the top one.
1542 -- Just because there is no handler does not mean we don't have
1543 -- to still execute all finalizations and cleanups before
1544 -- terminating. Note that the process of calling cleanups
1545 -- does not disturb the back trace stack, since he same
1546 -- exception occurrence gets reraised, and new traceback
1547 -- entries added as we go along.
1549 if FH_Found then
1550 Excep.Num_Tracebacks := FH_Num_Trb;
1551 Excep.Cleanup_Flag := True;
1552 Enter_Handler (Machine_State (FH_Mstate'Address), FH_Handler);
1553 end if;
1555 -- If no cleanups, then this is the real unhandled termination
1557 Unhandled_Exception_Terminate;
1559 end Propagate_Exception_With_FE_Support;
1561 ------------------------------------------
1562 -- Propagate_Exception_With_GCC_Support --
1563 ------------------------------------------
1565 procedure Propagate_Exception_With_GCC_Support (Mstate : Machine_State) is
1566 Excep : EOA := Get_Current_Excep.all;
1567 This_Exception : aliased GNAT_GCC_Exception;
1568 Status : Unwind_Reason_Code;
1570 begin
1571 -- ??? Nothing is currently done for backtracing purposes. We could
1572 -- have used the personality routine to record the addresses while
1573 -- walking up the stack, but this method has two drawbacks : 1/ the
1574 -- trace is incomplete if the exception is handled since we don't walk
1575 -- up the frame with the handler, and 2/ we will miss frames if the
1576 -- exception propagates through frames for which our personality
1577 -- routine is not called (e.g. if C or C++ frames are on the way).
1579 -- Fill in the useful flags for the personality routine called for each
1580 -- frame via the call to Unwind_RaiseException below.
1582 This_Exception.Id := Excep.Id;
1583 This_Exception.Handled_By_Others := not Excep.Id.Not_Handled_By_Others;
1584 This_Exception.Has_Cleanup := False;
1586 -- We are looking for a regular handler first. If there is one, either
1587 -- it or the first at-end handler before it will be entered. If there
1588 -- is none, control will normally get back to after the call, with
1589 -- Has_Cleanup set to true if at least one at-end handler has been
1590 -- found while walking up the stack.
1592 This_Exception.Select_Cleanups := False;
1594 Status := Unwind_RaiseException (This_Exception'Access);
1596 -- If we get here we know the exception is not handled, as otherwise
1597 -- Unwind_RaiseException arranges for a handler to be entered. We might
1598 -- have met cleanups handlers, though, requiring to start again with
1599 -- the Select_Cleanups flag set to True.
1601 -- Before restarting for cleanups, take the necessary steps to enable
1602 -- the debugger to gain control while the stack is still intact. Flag
1603 -- the occurrence as raised to avoid notifying again in case cleanup
1604 -- handlers are entered later.
1606 if not Excep.Exception_Raised then
1607 Excep.Exception_Raised := True;
1608 Notify_Unhandled_Exception (Excep.Id);
1609 end if;
1611 -- Now raise again selecting cleanups as true handlers. Only do this if
1612 -- we know at least one such handler exists since otherwise we would
1613 -- perform a complete stack upwalk for nothing.
1615 if This_Exception.Has_Cleanup then
1616 This_Exception.Select_Cleanups := True;
1617 Status := Unwind_RaiseException (This_Exception'Access);
1619 -- The first cleanup found is entered. It performs its job, raises
1620 -- the initial exception again, and the flow goes back to the first
1621 -- step above with the stack in a different state.
1622 end if;
1624 -- We get here when there is no handler to be run at all. The debugger
1625 -- has been notified before the second step above.
1627 Unhandled_Exception_Terminate;
1629 end Propagate_Exception_With_GCC_Support;
1631 ----------------------------
1632 -- Raise_Constraint_Error --
1633 ----------------------------
1635 procedure Raise_Constraint_Error
1636 (File : Big_String_Ptr;
1637 Line : Integer)
1639 begin
1640 Raise_With_Location (Constraint_Error_Def'Access, File, Line);
1641 end Raise_Constraint_Error;
1643 --------------------------------
1644 -- Raise_Constraint_Error_Msg --
1645 --------------------------------
1647 procedure Raise_Constraint_Error_Msg
1648 (File : Big_String_Ptr;
1649 Line : Integer;
1650 Msg : Big_String_Ptr)
1652 begin
1653 Raise_With_Location_And_Msg
1654 (Constraint_Error_Def'Access, File, Line, Msg);
1655 end Raise_Constraint_Error_Msg;
1657 -------------------------
1658 -- Raise_Current_Excep --
1659 -------------------------
1661 procedure Raise_Current_Excep (E : Exception_Id) is
1662 begin
1663 Process_Raise_Exception (E => E, From_Signal_Handler => False);
1664 end Raise_Current_Excep;
1666 ---------------------
1667 -- Raise_Exception --
1668 ---------------------
1670 procedure Raise_Exception
1671 (E : Exception_Id;
1672 Message : String := "")
1674 Len : constant Natural :=
1675 Natural'Min (Message'Length, Exception_Msg_Max_Length);
1676 Excep : constant EOA := Get_Current_Excep.all;
1678 begin
1679 if E /= null then
1680 Excep.Msg_Length := Len;
1681 Excep.Msg (1 .. Len) := Message (1 .. Len);
1682 Raise_With_Msg (E);
1683 end if;
1684 end Raise_Exception;
1686 ----------------------------
1687 -- Raise_Exception_Always --
1688 ----------------------------
1690 procedure Raise_Exception_Always
1691 (E : Exception_Id;
1692 Message : String := "")
1694 Len : constant Natural :=
1695 Natural'Min (Message'Length, Exception_Msg_Max_Length);
1697 Excep : constant EOA := Get_Current_Excep.all;
1699 begin
1700 Excep.Msg_Length := Len;
1701 Excep.Msg (1 .. Len) := Message (1 .. Len);
1702 Raise_With_Msg (E);
1703 end Raise_Exception_Always;
1705 -------------------------------
1706 -- Raise_From_Signal_Handler --
1707 -------------------------------
1709 procedure Raise_From_Signal_Handler
1710 (E : Exception_Id;
1711 M : Big_String_Ptr)
1713 begin
1714 Set_Exception_C_Msg (E, M);
1715 Abort_Defer.all;
1716 Process_Raise_Exception (E => E, From_Signal_Handler => True);
1717 end Raise_From_Signal_Handler;
1719 ------------------
1720 -- Raise_No_Msg --
1721 ------------------
1723 procedure Raise_No_Msg (E : Exception_Id) is
1724 Excep : constant EOA := Get_Current_Excep.all;
1726 begin
1727 Excep.Msg_Length := 0;
1728 Raise_With_Msg (E);
1729 end Raise_No_Msg;
1731 -------------------------
1732 -- Raise_Program_Error --
1733 -------------------------
1735 procedure Raise_Program_Error
1736 (File : Big_String_Ptr;
1737 Line : Integer)
1739 begin
1740 Raise_With_Location (Program_Error_Def'Access, File, Line);
1741 end Raise_Program_Error;
1743 -----------------------------
1744 -- Raise_Program_Error_Msg --
1745 -----------------------------
1747 procedure Raise_Program_Error_Msg
1748 (File : Big_String_Ptr;
1749 Line : Integer;
1750 Msg : Big_String_Ptr)
1752 begin
1753 Raise_With_Location_And_Msg
1754 (Program_Error_Def'Access, File, Line, Msg);
1755 end Raise_Program_Error_Msg;
1757 -------------------------
1758 -- Raise_Storage_Error --
1759 -------------------------
1761 procedure Raise_Storage_Error
1762 (File : Big_String_Ptr;
1763 Line : Integer)
1765 begin
1766 Raise_With_Location (Storage_Error_Def'Access, File, Line);
1767 end Raise_Storage_Error;
1769 -----------------------------
1770 -- Raise_Storage_Error_Msg --
1771 -----------------------------
1773 procedure Raise_Storage_Error_Msg
1774 (File : Big_String_Ptr;
1775 Line : Integer;
1776 Msg : Big_String_Ptr)
1778 begin
1779 Raise_With_Location_And_Msg
1780 (Storage_Error_Def'Access, File, Line, Msg);
1781 end Raise_Storage_Error_Msg;
1783 ----------------------
1784 -- Raise_With_C_Msg --
1785 ----------------------
1787 procedure Raise_With_C_Msg
1788 (E : Exception_Id;
1789 M : Big_String_Ptr)
1791 begin
1792 Set_Exception_C_Msg (E, M);
1793 Abort_Defer.all;
1794 Raise_Current_Excep (E);
1795 end Raise_With_C_Msg;
1797 -------------------------
1798 -- Raise_With_Location --
1799 -------------------------
1801 procedure Raise_With_Location
1802 (E : Exception_Id;
1803 F : Big_String_Ptr;
1804 L : Integer)
1806 begin
1807 Set_Exception_C_Msg (E, F, L);
1808 Abort_Defer.all;
1809 Raise_Current_Excep (E);
1810 end Raise_With_Location;
1812 ---------------------------------
1813 -- Raise_With_Location_And_Msg --
1814 ---------------------------------
1816 procedure Raise_With_Location_And_Msg
1817 (E : Exception_Id;
1818 F : Big_String_Ptr;
1819 L : Integer;
1820 M : Big_String_Ptr)
1822 begin
1823 Set_Exception_C_Msg (E, F, L, M);
1824 Abort_Defer.all;
1825 Raise_Current_Excep (E);
1826 end Raise_With_Location_And_Msg;
1828 --------------------
1829 -- Raise_With_Msg --
1830 --------------------
1832 procedure Raise_With_Msg (E : Exception_Id) is
1833 Excep : constant EOA := Get_Current_Excep.all;
1835 begin
1836 Excep.Exception_Raised := False;
1837 Excep.Id := E;
1838 Excep.Num_Tracebacks := 0;
1839 Excep.Cleanup_Flag := False;
1840 Excep.Pid := Local_Partition_ID;
1841 Abort_Defer.all;
1842 Raise_Current_Excep (E);
1843 end Raise_With_Msg;
1845 -------------
1846 -- Reraise --
1847 -------------
1849 procedure Reraise is
1850 Excep : constant EOA := Get_Current_Excep.all;
1852 begin
1853 Abort_Defer.all;
1854 Raise_Current_Excep (Excep.Id);
1855 end Reraise;
1857 ------------------------
1858 -- Reraise_Occurrence --
1859 ------------------------
1861 procedure Reraise_Occurrence (X : Exception_Occurrence) is
1862 begin
1863 if X.Id /= null then
1864 Abort_Defer.all;
1865 Save_Occurrence (Get_Current_Excep.all.all, X);
1866 Raise_Current_Excep (X.Id);
1867 end if;
1868 end Reraise_Occurrence;
1870 -------------------------------
1871 -- Reraise_Occurrence_Always --
1872 -------------------------------
1874 procedure Reraise_Occurrence_Always (X : Exception_Occurrence) is
1875 begin
1876 Abort_Defer.all;
1877 Save_Occurrence (Get_Current_Excep.all.all, X);
1878 Raise_Current_Excep (X.Id);
1879 end Reraise_Occurrence_Always;
1881 ---------------------------------
1882 -- Reraise_Occurrence_No_Defer --
1883 ---------------------------------
1885 procedure Reraise_Occurrence_No_Defer (X : Exception_Occurrence) is
1886 begin
1887 Save_Occurrence (Get_Current_Excep.all.all, X);
1888 Raise_Current_Excep (X.Id);
1889 end Reraise_Occurrence_No_Defer;
1891 ---------------------
1892 -- Save_Occurrence --
1893 ---------------------
1895 procedure Save_Occurrence
1896 (Target : out Exception_Occurrence;
1897 Source : Exception_Occurrence)
1899 begin
1900 Target.Id := Source.Id;
1901 Target.Msg_Length := Source.Msg_Length;
1902 Target.Num_Tracebacks := Source.Num_Tracebacks;
1903 Target.Pid := Source.Pid;
1904 Target.Cleanup_Flag := Source.Cleanup_Flag;
1906 Target.Msg (1 .. Target.Msg_Length) :=
1907 Source.Msg (1 .. Target.Msg_Length);
1909 Target.Tracebacks (1 .. Target.Num_Tracebacks) :=
1910 Source.Tracebacks (1 .. Target.Num_Tracebacks);
1911 end Save_Occurrence;
1913 function Save_Occurrence
1914 (Source : Exception_Occurrence)
1915 return EOA
1917 Target : EOA := new Exception_Occurrence;
1919 begin
1920 Save_Occurrence (Target.all, Source);
1921 return Target;
1922 end Save_Occurrence;
1924 ---------------------
1925 -- SDP_Table_Build --
1926 ---------------------
1928 procedure SDP_Table_Build
1929 (SDP_Addresses : System.Address;
1930 SDP_Count : Natural;
1931 Elab_Addresses : System.Address;
1932 Elab_Addr_Count : Natural)
1934 type SDLP_Array is array (1 .. SDP_Count) of Subprogram_Descriptors_Ptr;
1935 type SDLP_Array_Ptr is access all SDLP_Array;
1937 function To_SDLP_Array_Ptr is new Unchecked_Conversion
1938 (System.Address, SDLP_Array_Ptr);
1940 T : constant SDLP_Array_Ptr := To_SDLP_Array_Ptr (SDP_Addresses);
1942 type Elab_Array is array (1 .. Elab_Addr_Count) of Code_Loc;
1943 type Elab_Array_Ptr is access all Elab_Array;
1945 function To_Elab_Array_Ptr is new Unchecked_Conversion
1946 (System.Address, Elab_Array_Ptr);
1948 EA : constant Elab_Array_Ptr := To_Elab_Array_Ptr (Elab_Addresses);
1950 Ndes : Natural;
1951 Previous_Subprogram_Descriptors : Subprogram_Descriptor_List_Ptr;
1953 begin
1954 -- If first call, then initialize count of subprogram descriptors
1956 if Subprogram_Descriptors = null then
1957 Num_Subprogram_Descriptors := 0;
1958 end if;
1960 -- First count number of subprogram descriptors. This count includes
1961 -- entries with duplicated code addresses (resulting from Import).
1963 Ndes := Num_Subprogram_Descriptors + Elab_Addr_Count;
1964 for J in T'Range loop
1965 Ndes := Ndes + T (J).Count;
1966 end loop;
1968 -- Now, allocate the new table (extra zero'th element is for sort call)
1969 -- after having saved the previous one
1971 Previous_Subprogram_Descriptors := Subprogram_Descriptors;
1972 Subprogram_Descriptors := new Subprogram_Descriptor_List (0 .. Ndes);
1974 -- If there was a previous Subprogram_Descriptors table, copy it back
1975 -- into the new one being built. Then free the memory used for the
1976 -- previous table.
1978 for J in 1 .. Num_Subprogram_Descriptors loop
1979 Subprogram_Descriptors (J) := Previous_Subprogram_Descriptors (J);
1980 end loop;
1982 Free (Previous_Subprogram_Descriptors);
1984 -- Next, append the elaboration routine addresses, building dummy
1985 -- SDP's for them as we go through the list.
1987 Ndes := Num_Subprogram_Descriptors;
1988 for J in EA'Range loop
1989 Ndes := Ndes + 1;
1990 Subprogram_Descriptors (Ndes) := new Subprogram_Descriptor_0;
1992 Subprogram_Descriptors (Ndes).all :=
1993 Subprogram_Descriptor'
1994 (Num_Handlers => 0,
1995 Code => Fetch_Code (EA (J)),
1996 Subprogram_Info => EA (J),
1997 Handler_Records => (1 .. 0 => null));
1998 end loop;
2000 -- Now copy in pointers to SDP addresses of application subprograms
2002 for J in T'Range loop
2003 for K in 1 .. T (J).Count loop
2004 Ndes := Ndes + 1;
2005 Subprogram_Descriptors (Ndes) := T (J).SDesc (K);
2006 Subprogram_Descriptors (Ndes).Code :=
2007 Fetch_Code (T (J).SDesc (K).Code);
2008 end loop;
2009 end loop;
2011 -- Now we need to sort the table into ascending PC order
2013 Sort (Ndes, SDP_Table_Sort_Move'Access, SDP_Table_Sort_Lt'Access);
2015 -- Now eliminate duplicate entries. Note that in the case where
2016 -- entries have duplicate code addresses, the code for the Lt
2017 -- routine ensures that the interesting one (i.e. the one with
2018 -- handler entries if there are any) comes first.
2020 Num_Subprogram_Descriptors := 1;
2022 for J in 2 .. Ndes loop
2023 if Subprogram_Descriptors (J).Code /=
2024 Subprogram_Descriptors (Num_Subprogram_Descriptors).Code
2025 then
2026 Num_Subprogram_Descriptors := Num_Subprogram_Descriptors + 1;
2027 Subprogram_Descriptors (Num_Subprogram_Descriptors) :=
2028 Subprogram_Descriptors (J);
2029 end if;
2030 end loop;
2032 end SDP_Table_Build;
2034 -----------------------
2035 -- SDP_Table_Sort_Lt --
2036 -----------------------
2038 function SDP_Table_Sort_Lt (Op1, Op2 : Natural) return Boolean is
2039 SDC1 : constant Code_Loc := Subprogram_Descriptors (Op1).Code;
2040 SDC2 : constant Code_Loc := Subprogram_Descriptors (Op2).Code;
2042 begin
2043 if SDC1 < SDC2 then
2044 return True;
2046 elsif SDC1 > SDC2 then
2047 return False;
2049 -- For two descriptors for the same procedure, we want the more
2050 -- interesting one first. A descriptor with an exception handler
2051 -- is more interesting than one without. This happens if the less
2052 -- interesting one came from a pragma Import.
2054 else
2055 return Subprogram_Descriptors (Op1).Num_Handlers /= 0
2056 and then Subprogram_Descriptors (Op2).Num_Handlers = 0;
2057 end if;
2058 end SDP_Table_Sort_Lt;
2060 --------------------------
2061 -- SDP_Table_Sort_Move --
2062 --------------------------
2064 procedure SDP_Table_Sort_Move (From : Natural; To : Natural) is
2065 begin
2066 Subprogram_Descriptors (To) := Subprogram_Descriptors (From);
2067 end SDP_Table_Sort_Move;
2069 -------------------------
2070 -- Set_Exception_C_Msg --
2071 -------------------------
2073 procedure Set_Exception_C_Msg
2074 (Id : Exception_Id;
2075 Msg1 : Big_String_Ptr;
2076 Line : Integer := 0;
2077 Msg2 : Big_String_Ptr := null)
2079 Excep : constant EOA := Get_Current_Excep.all;
2080 Val : Integer := Line;
2081 Remind : Integer;
2082 Size : Integer := 1;
2083 Ptr : Natural;
2085 begin
2086 Excep.Exception_Raised := False;
2087 Excep.Id := Id;
2088 Excep.Num_Tracebacks := 0;
2089 Excep.Pid := Local_Partition_ID;
2090 Excep.Msg_Length := 0;
2091 Excep.Cleanup_Flag := False;
2093 while Msg1 (Excep.Msg_Length + 1) /= ASCII.NUL
2094 and then Excep.Msg_Length < Exception_Msg_Max_Length
2095 loop
2096 Excep.Msg_Length := Excep.Msg_Length + 1;
2097 Excep.Msg (Excep.Msg_Length) := Msg1 (Excep.Msg_Length);
2098 end loop;
2100 -- Append line number if present
2102 if Line > 0 then
2104 -- Compute the number of needed characters
2106 while Val > 0 loop
2107 Val := Val / 10;
2108 Size := Size + 1;
2109 end loop;
2111 -- If enough characters are available, put the line number
2113 if Excep.Msg_Length <= Exception_Msg_Max_Length - Size then
2114 Excep.Msg (Excep.Msg_Length + 1) := ':';
2115 Excep.Msg_Length := Excep.Msg_Length + Size;
2116 Val := Line;
2117 Size := 0;
2119 while Val > 0 loop
2120 Remind := Val rem 10;
2121 Val := Val / 10;
2122 Excep.Msg (Excep.Msg_Length - Size) :=
2123 Character'Val (Remind + Character'Pos ('0'));
2124 Size := Size + 1;
2125 end loop;
2126 end if;
2127 end if;
2129 -- Append second message if present
2131 if Msg2 /= null
2132 and then Excep.Msg_Length + 1 < Exception_Msg_Max_Length
2133 then
2134 Excep.Msg_Length := Excep.Msg_Length + 1;
2135 Excep.Msg (Excep.Msg_Length) := ' ';
2137 Ptr := 1;
2138 while Msg2 (Ptr) /= ASCII.NUL
2139 and then Excep.Msg_Length < Exception_Msg_Max_Length
2140 loop
2141 Excep.Msg_Length := Excep.Msg_Length + 1;
2142 Excep.Msg (Excep.Msg_Length) := Msg2 (Ptr);
2143 Ptr := Ptr + 1;
2144 end loop;
2145 end if;
2146 end Set_Exception_C_Msg;
2148 -------------------
2149 -- String_To_EId --
2150 -------------------
2152 function String_To_EId (S : String) return Exception_Id is
2153 begin
2154 if S = "" then
2155 return Null_Id;
2156 else
2157 return Exception_Id (Internal_Exception (S));
2158 end if;
2159 end String_To_EId;
2161 ------------------
2162 -- String_To_EO --
2163 ------------------
2165 function String_To_EO (S : String) return Exception_Occurrence is
2166 From : Natural;
2167 To : Integer;
2169 X : Exception_Occurrence;
2170 -- This is the exception occurrence we will create
2172 procedure Bad_EO;
2173 pragma No_Return (Bad_EO);
2174 -- Signal bad exception occurrence string
2176 procedure Next_String;
2177 -- On entry, To points to last character of previous line of the
2178 -- message, terminated by LF. On return, From .. To are set to
2179 -- specify the next string, or From > To if there are no more lines.
2181 procedure Bad_EO is
2182 begin
2183 Raise_Exception
2184 (Program_Error'Identity,
2185 "bad exception occurrence in stream input");
2186 end Bad_EO;
2188 procedure Next_String is
2189 begin
2190 From := To + 2;
2192 if From < S'Last then
2193 To := From + 1;
2195 while To < S'Last - 1 loop
2196 if To >= S'Last then
2197 Bad_EO;
2198 elsif S (To + 1) = ASCII.LF then
2199 exit;
2200 else
2201 To := To + 1;
2202 end if;
2203 end loop;
2204 end if;
2205 end Next_String;
2207 -- Start of processing for String_To_EO
2209 begin
2210 if S = "" then
2211 return Null_Occurrence;
2213 else
2214 X.Cleanup_Flag := False;
2216 To := S'First - 2;
2217 Next_String;
2219 if S (From .. From + 15) /= "Exception name: " then
2220 Bad_EO;
2221 end if;
2223 X.Id := Exception_Id (Internal_Exception (S (From + 16 .. To)));
2225 Next_String;
2227 if From <= To and then S (From) = 'M' then
2228 if S (From .. From + 8) /= "Message: " then
2229 Bad_EO;
2230 end if;
2232 X.Msg_Length := To - From - 8;
2233 X.Msg (1 .. X.Msg_Length) := S (From + 9 .. To);
2234 Next_String;
2236 else
2237 X.Msg_Length := 0;
2238 end if;
2240 X.Pid := 0;
2242 if From <= To and then S (From) = 'P' then
2243 if S (From .. From + 3) /= "PID:" then
2244 Bad_EO;
2245 end if;
2247 From := From + 5; -- skip past PID: space
2249 while From <= To loop
2250 X.Pid := X.Pid * 10 +
2251 (Character'Pos (S (From)) - Character'Pos ('0'));
2252 From := From + 1;
2253 end loop;
2255 Next_String;
2256 end if;
2258 X.Num_Tracebacks := 0;
2260 if From <= To then
2261 if S (From .. To) /= "Call stack traceback locations:" then
2262 Bad_EO;
2263 end if;
2265 Next_String;
2266 loop
2267 exit when From > To;
2269 declare
2270 Ch : Character;
2271 C : Integer_Address;
2272 N : Integer_Address;
2274 begin
2275 if S (From) /= '0'
2276 or else S (From + 1) /= 'x'
2277 then
2278 Bad_EO;
2279 else
2280 From := From + 2;
2281 end if;
2283 C := 0;
2284 while From <= To loop
2285 Ch := S (From);
2287 if Ch in '0' .. '9' then
2288 N :=
2289 Character'Pos (S (From)) - Character'Pos ('0');
2291 elsif Ch in 'a' .. 'f' then
2292 N :=
2293 Character'Pos (S (From)) - Character'Pos ('a') + 10;
2295 elsif Ch = ' ' then
2296 From := From + 1;
2297 exit;
2299 else
2300 Bad_EO;
2301 end if;
2303 C := C * 16 + N;
2305 From := From + 1;
2306 end loop;
2308 if X.Num_Tracebacks = Max_Tracebacks then
2309 Bad_EO;
2310 end if;
2312 X.Num_Tracebacks := X.Num_Tracebacks + 1;
2313 X.Tracebacks (X.Num_Tracebacks) := To_Address (C);
2314 end;
2315 end loop;
2316 end if;
2318 -- If an exception was converted to a string, it must have
2319 -- already been raised, so flag it accordingly and we are done.
2321 X.Exception_Raised := True;
2322 return X;
2323 end if;
2324 end String_To_EO;
2326 ----------------------------------
2327 -- Tailored_Exception_Traceback --
2328 ----------------------------------
2330 function Tailored_Exception_Traceback
2331 (X : Exception_Occurrence)
2332 return String
2334 -- We indeed reference the decorator *wrapper* from here and not the
2335 -- decorator itself. The purpose of the local variable Wrapper is to
2336 -- prevent a potential crash by race condition in the code below. The
2337 -- atomicity of this assignment is enforced by pragma Atomic in
2338 -- System.Soft_Links.
2340 -- The potential race condition here, if no local variable was used,
2341 -- relates to the test upon the wrapper's value and the call, which
2342 -- are not performed atomically. With the local variable, potential
2343 -- changes of the wrapper's global value between the test and the
2344 -- call become inoffensive.
2346 Wrapper : constant Traceback_Decorator_Wrapper_Call :=
2347 Traceback_Decorator_Wrapper;
2349 begin
2350 if Wrapper = null then
2351 return Basic_Exception_Traceback (X);
2352 else
2353 return Wrapper.all (X.Tracebacks'Address, X.Num_Tracebacks);
2354 end if;
2355 end Tailored_Exception_Traceback;
2357 ------------------------------------
2358 -- Tailored_Exception_Information --
2359 ------------------------------------
2361 function Tailored_Exception_Information
2362 (X : Exception_Occurrence)
2363 return String
2365 -- The tailored exception information is simply the basic information
2366 -- associated with the tailored call chain backtrace.
2368 Basic_Info : constant String := Basic_Exception_Information (X);
2369 Tback_Info : constant String := Tailored_Exception_Traceback (X);
2371 Basic_Len : constant Natural := Basic_Info'Length;
2372 Tback_Len : constant Natural := Tback_Info'Length;
2374 Info : String (1 .. Basic_Len + Tback_Len);
2375 Ptr : Natural := 0;
2377 begin
2378 Append_Info_String (Basic_Info, Info, Ptr);
2379 Append_Info_String (Tback_Info, Info, Ptr);
2381 return Info;
2382 end Tailored_Exception_Information;
2384 -------------------------
2385 -- Unhandled_Exception --
2386 -------------------------
2388 procedure Unhandled_Exception is
2389 begin
2390 null;
2391 end Unhandled_Exception;
2393 ----------------------
2394 -- Notify_Exception --
2395 ----------------------
2397 procedure Notify_Exception
2398 (Id : Exception_Id;
2399 Handler : Code_Loc;
2400 Is_Others : Boolean)
2402 begin
2403 null;
2404 end Notify_Exception;
2406 ------------------------------
2407 -- Notify_Handled_Exception --
2408 ------------------------------
2410 procedure Notify_Handled_Exception
2411 (Handler : Code_Loc;
2412 Is_Others : Boolean;
2413 Low_Notify : Boolean)
2415 Excep : constant EOA := Get_Current_Excep.all;
2417 begin
2418 -- Notify the debugger that we have found a handler and are about to
2419 -- propagate an exception, but only if specifically told to do so.
2421 if Low_Notify then
2422 Notify_Exception (Excep.Id, Handler, Is_Others);
2423 end if;
2425 -- Output some exception information if necessary, as specified by
2426 -- GNAT.Exception_Traces. Take care not to output information about
2427 -- internal exceptions.
2429 -- ??? In the ZCX case, the traceback entries we have at this point
2430 -- only include the ones we stored while walking up the stack *up to
2431 -- the handler*. All the frames above the subprogram in which the
2432 -- handler is found are missing.
2434 if Exception_Trace = Every_Raise
2435 and then not Excep.Id.Not_Handled_By_Others
2436 then
2437 To_Stderr (Nline);
2438 To_Stderr ("Exception raised");
2439 To_Stderr (Nline);
2440 To_Stderr (Tailored_Exception_Information (Excep.all));
2441 end if;
2443 end Notify_Handled_Exception;
2445 ------------------------------
2446 -- Notify_Handled_Exception --
2447 ------------------------------
2449 procedure Notify_Unhandled_Exception (Id : Exception_Id) is
2450 begin
2451 -- Simply perform the two necessary low level notification calls.
2453 Unhandled_Exception;
2454 Notify_Exception (Id, Null_Loc, False);
2456 end Notify_Unhandled_Exception;
2458 -----------------------------------
2459 -- Unhandled_Exception_Terminate --
2460 -----------------------------------
2462 adafinal_Called : Boolean := False;
2463 -- Used to prevent recursive call to adafinal in the event that
2464 -- adafinal processing itself raises an unhandled exception.
2466 type FILEs is new System.Address;
2467 type int is new Integer;
2469 procedure Unhandled_Exception_Terminate is
2471 Excep : constant EOA := Save_Occurrence (Get_Current_Excep.all.all);
2472 -- This occurrence will be used to display a message after finalization.
2473 -- It is necessary to save a copy here, or else the designated value
2474 -- could be overwritten if an exception is raised during finalization
2475 -- (even if that exception is caught).
2477 Msg : constant String := Exception_Message (Excep.all);
2479 -- Start of processing for Unhandled_Exception_Terminate
2481 begin
2482 -- First call adafinal
2484 if not adafinal_Called then
2485 adafinal_Called := True;
2486 System.Soft_Links.Adafinal.all;
2487 end if;
2489 -- Check for special case of raising _ABORT_SIGNAL, which is not
2490 -- really an exception at all. We recognize this by the fact that
2491 -- it is the only exception whose name starts with underscore.
2493 if Exception_Name (Excep.all) (1) = '_' then
2494 To_Stderr (Nline);
2495 To_Stderr ("Execution terminated by abort of environment task");
2496 To_Stderr (Nline);
2498 -- If no tracebacks, we print the unhandled exception in the old style
2499 -- (i.e. the style used before ZCX was implemented). We do this to
2500 -- retain compatibility, especially with the nightly scripts, but
2501 -- this can be removed at some point ???
2503 elsif Excep.Num_Tracebacks = 0 then
2504 To_Stderr (Nline);
2505 To_Stderr ("raised ");
2506 To_Stderr (Exception_Name (Excep.all));
2508 if Msg'Length /= 0 then
2509 To_Stderr (" : ");
2510 To_Stderr (Msg);
2511 end if;
2513 To_Stderr (Nline);
2515 -- New style, zero cost exception case
2517 else
2518 -- Tailored_Exception_Information is also called here so that the
2519 -- backtrace decorator gets called if it has been set. This is
2520 -- currently required because some paths in Raise_Current_Excep
2521 -- do not go through the calls that display this information.
2523 -- Note also that with the current scheme in Raise_Current_Excep
2524 -- we can have this whole information output twice, typically when
2525 -- some handler is found on the call chain but none deals with the
2526 -- occurrence or if this occurrence gets reraised up to here.
2528 To_Stderr (Nline);
2529 To_Stderr ("Execution terminated by unhandled exception");
2530 To_Stderr (Nline);
2531 To_Stderr (Tailored_Exception_Information (Excep.all));
2532 end if;
2534 -- Perform system dependent shutdown code
2536 declare
2537 procedure Unhandled_Terminate;
2538 pragma No_Return (Unhandled_Terminate);
2539 pragma Import
2540 (C, Unhandled_Terminate, "__gnat_unhandled_terminate");
2542 begin
2543 Unhandled_Terminate;
2544 end;
2546 end Unhandled_Exception_Terminate;
2548 ------------------------------
2549 -- Raise_Exception_No_Defer --
2550 ------------------------------
2552 procedure Raise_Exception_No_Defer
2553 (E : Exception_Id;
2554 Message : String := "")
2556 Len : constant Natural :=
2557 Natural'Min (Message'Length, Exception_Msg_Max_Length);
2559 Excep : constant EOA := Get_Current_Excep.all;
2561 begin
2562 Excep.Exception_Raised := False;
2563 Excep.Msg_Length := Len;
2564 Excep.Msg (1 .. Len) := Message (1 .. Len);
2565 Excep.Id := E;
2566 Excep.Num_Tracebacks := 0;
2567 Excep.Cleanup_Flag := False;
2568 Excep.Pid := Local_Partition_ID;
2570 -- DO NOT CALL Abort_Defer.all; !!!!
2572 Raise_Current_Excep (E);
2573 end Raise_Exception_No_Defer;
2575 ---------------
2576 -- To_Stderr --
2577 ---------------
2579 procedure To_Stderr (S : String) is
2580 procedure put_char_stderr (C : int);
2581 pragma Import (C, put_char_stderr, "put_char_stderr");
2583 begin
2584 for J in 1 .. S'Length loop
2585 if S (J) /= ASCII.CR then
2586 put_char_stderr (Character'Pos (S (J)));
2587 end if;
2588 end loop;
2589 end To_Stderr;
2591 ---------
2592 -- ZZZ --
2593 ---------
2595 -- This dummy procedure gives us the end of the PC range for addresses
2596 -- within the exception unit itself. We hope that gigi/gcc keeps all the
2597 -- procedures in their original order!
2599 procedure ZZZ is
2600 begin
2601 null;
2602 end ZZZ;
2604 begin
2605 -- Allocate the Non-Tasking Machine_State
2607 Set_Machine_State_Addr_NT (System.Address (Allocate_Machine_State));
2608 end Ada.Exceptions;