2005-12-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / ada / a-exextr.adb
blobbb7e5ad616c8f20606cf310f3bc7834d60ff2dfa
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- ADA.EXCEPTIONS.EXCEPTION_TRACES --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2005, 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 ------------------------------------------------------------------------------
34 with Unchecked_Conversion;
36 pragma Warnings (Off);
37 with Ada.Exceptions.Last_Chance_Handler;
38 pragma Warnings (On);
39 -- Bring last chance handler into closure
41 separate (Ada.Exceptions)
42 package body Exception_Traces is
44 Nline : constant String := String'(1 => ASCII.LF);
45 -- Convenient shortcut
47 type Exception_Action is access procedure (E : Exception_Occurrence);
48 Global_Action : Exception_Action := null;
49 pragma Export
50 (Ada, Global_Action, "__gnat_exception_actions_global_action");
51 -- Global action, executed whenever an exception is raised. Changing the
52 -- export name must be coordinated with code in g-excact.adb.
54 Raise_Hook_Initialized : Boolean := False;
55 pragma Export
56 (Ada, Raise_Hook_Initialized, "__gnat_exception_actions_initialized");
58 procedure Last_Chance_Handler
59 (Except : Exception_Occurrence);
60 pragma Import (C, Last_Chance_Handler, "__gnat_last_chance_handler");
61 pragma No_Return (Last_Chance_Handler);
62 -- Users can replace the default version of this routine,
63 -- Ada.Exceptions.Last_Chance_Handler.
65 function To_Action is new Unchecked_Conversion
66 (Raise_Action, Exception_Action);
68 -----------------------
69 -- Local Subprograms --
70 -----------------------
72 procedure Notify_Exception (Excep : EOA; Is_Unhandled : Boolean);
73 -- Factorizes the common processing for Notify_Handled_Exception and
74 -- Notify_Unhandled_Exception. Is_Unhandled is set to True only in the
75 -- latter case because Notify_Handled_Exception may be called for an
76 -- actually unhandled occurrence in the Front-End-SJLJ case.
78 ---------------------------------
79 -- Debugger Interface Routines --
80 ---------------------------------
82 -- The routines here are null routines that normally have no effect.
83 -- They are provided for the debugger to place breakpoints on their
84 -- entry points to get control on an exception.
86 procedure Unhandled_Exception;
87 pragma Export (C, Unhandled_Exception, "__gnat_unhandled_exception");
88 -- Hook for GDB to support "break exception unhandled"
90 -- For "break exception", GDB uses __gnat_raise_nodefer_with_msg, which
91 -- is not in this section because it functions as more than simply a
92 -- debugger interface.
94 --------------------------------
95 -- Import Run-Time C Routines --
96 --------------------------------
98 -- The purpose of the following pragma Import is to ensure that we
99 -- generate appropriate subprogram descriptors for all C routines in
100 -- the standard GNAT library that can raise exceptions. This ensures
101 -- that the exception propagation can properly find these routines
103 pragma Propagate_Exceptions;
105 ----------------------
106 -- Notify_Exception --
107 ----------------------
109 procedure Notify_Exception (Excep : EOA; Is_Unhandled : Boolean) is
110 begin
111 -- Output the exception information required by the Exception_Trace
112 -- configuration. Take care not to output information about internal
113 -- exceptions.
115 -- ??? In the Front-End ZCX case, the traceback entries we have at this
116 -- point only include the ones we stored while walking up the stack *up
117 -- to the handler*. All the frames above the subprogram in which the
118 -- handler is found are missing.
120 if not Excep.Id.Not_Handled_By_Others
121 and then
122 (Exception_Trace = Every_Raise
123 or else (Exception_Trace = Unhandled_Raise and then Is_Unhandled))
124 then
125 To_Stderr (Nline);
127 if Is_Unhandled then
128 To_Stderr ("Unhandled ");
129 end if;
131 To_Stderr ("Exception raised");
132 To_Stderr (Nline);
133 To_Stderr (Tailored_Exception_Information (Excep.all));
134 end if;
136 -- Call the user-specific actions
137 -- ??? We should presumably look at the reraise status here.
139 if Raise_Hook_Initialized
140 and then Exception_Data_Ptr (Excep.Id).Raise_Hook /= null
141 then
142 To_Action (Exception_Data_Ptr (Excep.Id).Raise_Hook) (Excep.all);
143 end if;
145 if Global_Action /= null then
146 Global_Action (Excep.all);
147 end if;
148 end Notify_Exception;
150 ------------------------------
151 -- Notify_Handled_Exception --
152 ------------------------------
154 procedure Notify_Handled_Exception is
155 begin
156 Notify_Exception (Get_Current_Excep.all, Is_Unhandled => False);
157 end Notify_Handled_Exception;
159 --------------------------------
160 -- Notify_Unhandled_Exception --
161 --------------------------------
163 procedure Notify_Unhandled_Exception is
164 Excep : constant EOA := Get_Current_Excep.all;
166 begin
167 -- Check whether there is any termination handler to be executed for
168 -- the environment task, and execute it if needed. Here we handle both
169 -- the Abnormal and Unhandled_Exception task termination. Normal
170 -- task termination routine is executed elsewhere (either in the
171 -- Task_Wrapper or in the Adafinal routine for the environment task).
173 Task_Termination_Handler.all (Excep.all);
175 Notify_Exception (Excep, Is_Unhandled => True);
176 Unhandled_Exception;
177 end Notify_Unhandled_Exception;
179 -------------------------
180 -- Unhandled_Exception --
181 -------------------------
183 procedure Unhandled_Exception is
184 begin
185 null;
186 end Unhandled_Exception;
188 -----------------------------------
189 -- Unhandled_Exception_Terminate --
190 -----------------------------------
192 procedure Unhandled_Exception_Terminate is
193 Excep : constant EOA := Save_Occurrence (Get_Current_Excep.all.all);
194 -- This occurrence will be used to display a message after finalization.
195 -- It is necessary to save a copy here, or else the designated value
196 -- could be overwritten if an exception is raised during finalization
197 -- (even if that exception is caught).
199 begin
200 Last_Chance_Handler (Excep.all);
201 end Unhandled_Exception_Terminate;
203 ------------------------------------
204 -- Handling GNAT.Exception_Traces --
205 ------------------------------------
207 -- The bulk of exception traces output is centralized in Notify_Exception,
208 -- for both the Handled and Unhandled cases. Extra task specific output is
209 -- triggered in the task wrapper for unhandled occurrences in tasks. It is
210 -- not performed in this unit to avoid dragging dependencies against the
211 -- tasking units here.
213 -- We used to rely on the output performed by Unhanded_Exception_Terminate
214 -- for the case of an unhandled occurrence in the environment thread, and
215 -- the task wrapper was responsible for the whole output in the tasking
216 -- case.
218 -- This initial scheme had a drawback: the output from Terminate only
219 -- occurs after finalization is done, which means possibly never if some
220 -- tasks keep hanging around.
222 -- The first "presumably obvious" fix consists in moving the Terminate
223 -- output before the finalization. It has not been retained because it
224 -- introduces annoying changes in output orders when the finalization
225 -- itself issues outputs, this also in "regular" cases not resorting to
226 -- Exception_Traces.
228 -- Today's solution has the advantage of simplicity and better isolates
229 -- the Exception_Traces machinery.
231 -- It currently outputs the information about unhandled exceptions twice
232 -- in the environment thread, once in the notification routine and once in
233 -- the termination routine. Avoiding the second output is possible but so
234 -- far has been considered undesirable. It would mean changing the order
235 -- of outputs between the two runs with or without exception traces, while
236 -- it seems preferrable to only have additional outputs in the former
237 -- case.
239 end Exception_Traces;