OSDN Git Service

PR 33870
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-elchha.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --    A D A . E X C E P T I O N S . L A S T _ C H A N C E _ H A N D L E R   --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 2003-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  Default version for most targets
35
36 pragma Warnings (Off);
37 pragma Compiler_Unit;
38 pragma Warnings (On);
39
40 with System.Standard_Library; use System.Standard_Library;
41 --  Used for Adafinal
42
43 with System.Soft_Links;
44 --  Used for Task_Termination_Handler
45 --           Task_Termination_NT
46
47 procedure Ada.Exceptions.Last_Chance_Handler
48   (Except : Exception_Occurrence)
49 is
50    procedure Unhandled_Terminate;
51    pragma No_Return (Unhandled_Terminate);
52    pragma Import (C, Unhandled_Terminate, "__gnat_unhandled_terminate");
53    --  Perform system dependent shutdown code
54
55    function Exception_Message_Length
56      (X : Exception_Occurrence) return Natural;
57    pragma Import (Ada, Exception_Message_Length, "__gnat_exception_msg_len");
58
59    procedure Append_Info_Exception_Message
60      (X : Exception_Occurrence; Info : in out String; Ptr : in out Natural);
61    pragma Import
62      (Ada, Append_Info_Exception_Message, "__gnat_append_info_e_msg");
63
64    procedure Append_Info_Exception_Information
65      (X : Exception_Occurrence; Info : in out String; Ptr : in out Natural);
66    pragma Import
67      (Ada, Append_Info_Exception_Information, "__gnat_append_info_e_info");
68
69    procedure To_Stderr (S : String);
70    pragma Import (Ada, To_Stderr, "__gnat_to_stderr");
71    --  Little routine to output string to stderr
72
73    Ptr   : Natural := 0;
74    Nobuf : String (1 .. 0);
75
76    Nline : constant String := String'(1 => ASCII.LF);
77    --  Convenient shortcut
78
79 begin
80    --  Do not execute any task termination code when shutting down the system.
81    --  The Adafinal procedure would execute the task termination routine for
82    --  normal termination, but we have already executed the task termination
83    --  procedure because of an unhandled exception.
84
85    System.Soft_Links.Task_Termination_Handler :=
86      System.Soft_Links.Task_Termination_NT'Access;
87
88    --  Let's shutdown the runtime now. The rest of the procedure needs to be
89    --  careful not to use anything that would require runtime support. In
90    --  particular, functions returning strings are banned since the sec stack
91    --  is no longer functional. This is particularly important to note for the
92    --  Exception_Information output. We used to allow the tailored version to
93    --  show up here, which turned out to be a bad idea as it might involve a
94    --  traceback decorator the length of which we don't control. Potentially
95    --  heavy primary/secondary stack use or dynamic allocations right before
96    --  this point are not welcome, moving the output before the finalization
97    --  raises order of outputs concerns, and decorators are intended to only
98    --  be used with exception traces, which should have been issued already.
99
100    System.Standard_Library.Adafinal;
101
102    --  Check for special case of raising _ABORT_SIGNAL, which is not
103    --  really an exception at all. We recognize this by the fact that
104    --  it is the only exception whose name starts with underscore.
105
106    if To_Ptr (Except.Id.Full_Name) (1) = '_' then
107       To_Stderr (Nline);
108       To_Stderr ("Execution terminated by abort of environment task");
109       To_Stderr (Nline);
110
111    --  If no tracebacks, we print the unhandled exception in the old style
112    --  (i.e. the style used before ZCX was implemented). We do this to
113    --  retain compatibility.
114
115    elsif Except.Num_Tracebacks = 0 then
116       To_Stderr (Nline);
117       To_Stderr ("raised ");
118       To_Stderr
119         (To_Ptr (Except.Id.Full_Name) (1 .. Except.Id.Name_Length - 1));
120
121       if Exception_Message_Length (Except) /= 0 then
122          To_Stderr (" : ");
123          Append_Info_Exception_Message (Except, Nobuf, Ptr);
124       end if;
125
126       To_Stderr (Nline);
127
128    --  Traceback exists
129
130    else
131       To_Stderr (Nline);
132       To_Stderr ("Execution terminated by unhandled exception");
133       To_Stderr (Nline);
134
135       Append_Info_Exception_Information (Except, Nobuf, Ptr);
136    end if;
137
138    Unhandled_Terminate;
139 end Ada.Exceptions.Last_Chance_Handler;