OSDN Git Service

2010-10-26 Vincent Celier <celier@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-exextr.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                      ADA.EXCEPTIONS.EXCEPTION_TRACES                     --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2010, 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 3,  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.                                     --
17 --                                                                          --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception,   --
20 -- version 3.1, as published by the Free Software Foundation.               --
21 --                                                                          --
22 -- You should have received a copy of the GNU General Public License and    --
23 -- a copy of the GCC Runtime Library Exception along with this program;     --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25 -- <http://www.gnu.org/licenses/>.                                          --
26 --                                                                          --
27 -- GNAT was originally developed  by the GNAT team at  New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 with Ada.Unchecked_Conversion;
33
34 pragma Warnings (Off);
35 with Ada.Exceptions.Last_Chance_Handler;
36 pragma Warnings (On);
37 --  Bring last chance handler into closure
38
39 separate (Ada.Exceptions)
40 package body Exception_Traces is
41
42    Nline : constant String := String'(1 => ASCII.LF);
43    --  Convenient shortcut
44
45    type Exception_Action is access procedure (E : Exception_Occurrence);
46    Global_Action : Exception_Action := null;
47    pragma Export
48      (Ada, Global_Action, "__gnat_exception_actions_global_action");
49    --  Global action, executed whenever an exception is raised.  Changing the
50    --  export name must be coordinated with code in g-excact.adb.
51
52    Raise_Hook_Initialized : Boolean := False;
53    pragma Export
54      (Ada, Raise_Hook_Initialized, "__gnat_exception_actions_initialized");
55
56    procedure Last_Chance_Handler (Except : Exception_Occurrence);
57    pragma Import (C, Last_Chance_Handler, "__gnat_last_chance_handler");
58    pragma No_Return (Last_Chance_Handler);
59    --  Users can replace the default version of this routine,
60    --  Ada.Exceptions.Last_Chance_Handler.
61
62    function To_Action is new Ada.Unchecked_Conversion
63      (Raise_Action, Exception_Action);
64
65    -----------------------
66    -- Local Subprograms --
67    -----------------------
68
69    procedure Notify_Exception (Excep : EOA; Is_Unhandled : Boolean);
70    --  Factorizes the common processing for Notify_Handled_Exception and
71    --  Notify_Unhandled_Exception. Is_Unhandled is set to True only in the
72    --  latter case because Notify_Handled_Exception may be called for an
73    --  actually unhandled occurrence in the Front-End-SJLJ case.
74
75    --------------------------------
76    -- Import Run-Time C Routines --
77    --------------------------------
78
79    --  The purpose of the following pragma Import is to ensure that we
80    --  generate appropriate subprogram descriptors for all C routines in
81    --  the standard GNAT library that can raise exceptions. This ensures
82    --  that the exception propagation can properly find these routines
83
84    pragma Propagate_Exceptions;
85
86    ----------------------
87    -- Notify_Exception --
88    ----------------------
89
90    procedure Notify_Exception (Excep : EOA; Is_Unhandled : Boolean) is
91    begin
92       --  Output the exception information required by the Exception_Trace
93       --  configuration. Take care not to output information about internal
94       --  exceptions.
95
96       --  ??? In the Front-End ZCX case, the traceback entries we have at this
97       --  point only include the ones we stored while walking up the stack *up
98       --  to the handler*. All the frames above the subprogram in which the
99       --  handler is found are missing.
100
101       if not Excep.Id.Not_Handled_By_Others
102         and then
103           (Exception_Trace = Every_Raise
104             or else (Exception_Trace = Unhandled_Raise and then Is_Unhandled))
105       then
106          --  Exception trace messages need to be protected when several tasks
107          --  can issue them at the same time.
108
109          Lock_Task.all;
110          To_Stderr (Nline);
111
112          if Is_Unhandled then
113             To_Stderr ("Unhandled ");
114          end if;
115
116          To_Stderr ("Exception raised");
117          To_Stderr (Nline);
118          To_Stderr (Tailored_Exception_Information (Excep.all));
119          Unlock_Task.all;
120       end if;
121
122       --  Call the user-specific actions
123       --  ??? We should presumably look at the reraise status here.
124
125       if Raise_Hook_Initialized
126         and then Exception_Data_Ptr (Excep.Id).Raise_Hook /= null
127       then
128          To_Action (Exception_Data_Ptr (Excep.Id).Raise_Hook) (Excep.all);
129       end if;
130
131       if Global_Action /= null then
132          Global_Action (Excep.all);
133       end if;
134    end Notify_Exception;
135
136    ------------------------------
137    -- Notify_Handled_Exception --
138    ------------------------------
139
140    procedure Notify_Handled_Exception is
141    begin
142       Notify_Exception (Get_Current_Excep.all, Is_Unhandled => False);
143    end Notify_Handled_Exception;
144
145    --------------------------------
146    -- Notify_Unhandled_Exception --
147    --------------------------------
148
149    procedure Notify_Unhandled_Exception is
150       Excep : constant EOA := Get_Current_Excep.all;
151
152    begin
153       --  Check whether there is any termination handler to be executed for
154       --  the environment task, and execute it if needed. Here we handle both
155       --  the Abnormal and Unhandled_Exception task termination. Normal
156       --  task termination routine is executed elsewhere (either in the
157       --  Task_Wrapper or in the Adafinal routine for the environment task).
158
159       Task_Termination_Handler.all (Excep.all);
160
161       Notify_Exception (Excep, Is_Unhandled => True);
162       Debug_Unhandled_Exception (SSL.Exception_Data_Ptr (Excep.Id));
163    end Notify_Unhandled_Exception;
164
165    -----------------------------------
166    -- Unhandled_Exception_Terminate --
167    -----------------------------------
168
169    procedure Unhandled_Exception_Terminate is
170       Excep : constant EOA := Save_Occurrence (Get_Current_Excep.all.all);
171       --  This occurrence will be used to display a message after finalization.
172       --  It is necessary to save a copy here, or else the designated value
173       --  could be overwritten if an exception is raised during finalization
174       --  (even if that exception is caught).
175
176    begin
177       Last_Chance_Handler (Excep.all);
178    end Unhandled_Exception_Terminate;
179
180    ------------------------------------
181    -- Handling GNAT.Exception_Traces --
182    ------------------------------------
183
184    --  The bulk of exception traces output is centralized in Notify_Exception,
185    --  for both the Handled and Unhandled cases. Extra task specific output is
186    --  triggered in the task wrapper for unhandled occurrences in tasks. It is
187    --  not performed in this unit to avoid dragging dependencies against the
188    --  tasking units here.
189
190    --  We used to rely on the output performed by Unhanded_Exception_Terminate
191    --  for the case of an unhandled occurrence in the environment thread, and
192    --  the task wrapper was responsible for the whole output in the tasking
193    --  case.
194
195    --  This initial scheme had a drawback: the output from Terminate only
196    --  occurs after finalization is done, which means possibly never if some
197    --  tasks keep hanging around.
198
199    --  The first "presumably obvious" fix consists in moving the Terminate
200    --  output before the finalization. It has not been retained because it
201    --  introduces annoying changes in output orders when the finalization
202    --  itself issues outputs, this also in "regular" cases not resorting to
203    --  Exception_Traces.
204
205    --  Today's solution has the advantage of simplicity and better isolates
206    --  the Exception_Traces machinery.
207
208    --  It currently outputs the information about unhandled exceptions twice
209    --  in the environment thread, once in the notification routine and once in
210    --  the termination routine. Avoiding the second output is possible but so
211    --  far has been considered undesirable. It would mean changing the order
212    --  of outputs between the two runs with or without exception traces, while
213    --  it seems preferable to only have additional outputs in the former
214    --  case.
215
216 end Exception_Traces;