1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- ADA.EXCEPTIONS.EXCEPTION_TRACES --
9 -- Copyright (C) 1992-2003 Free Software Foundation, Inc. --
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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
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. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Unchecked_Conversion;
36 separate (Ada.Exceptions)
37 package body Exception_Traces is
39 Nline : constant String := String'(1 => ASCII.LF);
40 -- Convenient shortcut
42 type Exception_Action is access procedure (E : Exception_Occurrence);
43 Global_Action : Exception_Action := null;
45 (Ada, Global_Action, "__gnat_exception_actions_global_action");
46 -- Global action, executed whenever an exception is raised. Changing the
47 -- export name must be coordinated with code in g-excact.adb.
49 Raise_Hook_Initialized : Boolean := False;
51 (Ada, Raise_Hook_Initialized, "__gnat_exception_actions_initialized");
53 function To_Action is new Unchecked_Conversion
54 (Raise_Action, Exception_Action);
56 -----------------------
57 -- Local Subprograms --
58 -----------------------
60 procedure Notify_Exception (Excep : EOA; Is_Unhandled : Boolean);
61 -- Factorizes the common processing for Notify_Handled_Exception and
62 -- Notify_Unhandled_Exception. Is_Unhandled is set to True only in the
63 -- latter case because Notify_Handled_Exception may be called for an
64 -- actually unhandled occurrence in the Front-End-SJLJ case.
66 procedure To_Stderr (S : String);
67 pragma Export (Ada, To_Stderr, "__gnat_to_stderr");
68 -- Little routine to output string to stderr that is also used
69 -- in the tasking run time.
71 ---------------------------------
72 -- Debugger Interface Routines --
73 ---------------------------------
75 -- The routines here are null routines that normally have no effect.
76 -- They are provided for the debugger to place breakpoints on their
77 -- entry points to get control on an exception.
79 procedure Unhandled_Exception;
80 pragma Export (C, Unhandled_Exception, "__gnat_unhandled_exception");
81 -- Hook for GDB to support "break exception unhandled".
83 -- For "break exception", GDB uses __gnat_raise_nodefer_with_msg, which
84 -- is not in this section because it fullfills other purposes than a mere
85 -- debugger interface.
87 --------------------------------
88 -- Import Run-Time C Routines --
89 --------------------------------
91 -- The purpose of the following pragma Import is to ensure that we
92 -- generate appropriate subprogram descriptors for all C routines in
93 -- the standard GNAT library that can raise exceptions. This ensures
94 -- that the exception propagation can properly find these routines
96 pragma Propagate_Exceptions;
98 procedure Unhandled_Terminate;
99 pragma No_Return (Unhandled_Terminate);
100 pragma Import (C, Unhandled_Terminate, "__gnat_unhandled_terminate");
101 -- Perform system dependent shutdown code
103 ----------------------
104 -- Notify_Exception --
105 ----------------------
107 procedure Notify_Exception (Excep : EOA; Is_Unhandled : Boolean) is
109 -- Output the exception information required by the Exception_Trace
110 -- configuration. Take care not to output information about internal
113 -- ??? In the Front-End ZCX case, the traceback entries we have at this
114 -- point only include the ones we stored while walking up the stack *up
115 -- to the handler*. All the frames above the subprogram in which the
116 -- handler is found are missing.
118 if not Excep.Id.Not_Handled_By_Others
120 (Exception_Trace = Every_Raise
121 or else (Exception_Trace = Unhandled_Raise and then Is_Unhandled))
126 To_Stderr ("Unhandled ");
129 To_Stderr ("Exception raised");
131 To_Stderr (Tailored_Exception_Information (Excep.all));
134 -- Call the user-specific actions
135 -- ??? We should presumably look at the reraise status here.
137 if Raise_Hook_Initialized
138 and then Exception_Data_Ptr (Excep.Id).Raise_Hook /= null
140 To_Action (Exception_Data_Ptr (Excep.Id).Raise_Hook) (Excep.all);
143 if Global_Action /= null then
144 Global_Action (Excep.all);
146 end Notify_Exception;
148 ------------------------------
149 -- Notify_Handled_Exception --
150 ------------------------------
152 procedure Notify_Handled_Exception is
154 Notify_Exception (Get_Current_Excep.all, Is_Unhandled => False);
155 end Notify_Handled_Exception;
157 --------------------------------
158 -- Notify_Unhandled_Exception --
159 --------------------------------
161 procedure Notify_Unhandled_Exception is
163 Notify_Exception (Get_Current_Excep.all, Is_Unhandled => True);
165 end Notify_Unhandled_Exception;
167 -------------------------
168 -- Unhandled_Exception --
169 -------------------------
171 procedure Unhandled_Exception is
174 end Unhandled_Exception;
176 -----------------------------------
177 -- Unhandled_Exception_Terminate --
178 -----------------------------------
180 type int is new Integer;
182 procedure Unhandled_Exception_Terminate is
183 Excep : constant EOA := Save_Occurrence (Get_Current_Excep.all.all);
184 -- This occurrence will be used to display a message after finalization.
185 -- It is necessary to save a copy here, or else the designated value
186 -- could be overwritten if an exception is raised during finalization
187 -- (even if that exception is caught).
189 Msg : constant String := Excep.Msg (1 .. Excep.Msg_Length);
191 Max_Static_Exc_Info : constant := 1024;
192 -- That should be enough for most exception information cases
193 -- eventhough tailorising introduces some uncertainty. the
194 -- name+message should not exceed 320 chars, so that leaves at
195 -- least 35 backtrace slots (each slot needs 19 chars for
196 -- representing a 64 bit address).
197 -- And what happens on overflow ???
199 subtype Exc_Info_Type is String (1 .. Max_Static_Exc_Info);
200 type Str_Ptr is access Exc_Info_Type;
202 Exc_Info_Last : Natural := 0;
203 -- Buffer that is allocated to store the tailored exception
204 -- information while Adafinal is run. This buffer is allocated
205 -- on the heap only when it is needed. It is better to allocate
206 -- on the heap than on the stack since stack overflows are more
207 -- common that heap overflows.
209 -- Start of processing for Unhandled_Exception_Terminate
212 -- First allocate & store the exception info in a buffer when
213 -- we know it will be needed. This needs to be done before
214 -- Adafinal because it implicitly uses the secondary stack.
216 if Excep.Id.Full_Name.all (1) /= '_'
217 and then Excep.Num_Tracebacks /= 0
219 Exc_Info := new Exc_Info_Type;
220 if Exc_Info /= null then
221 Tailored_Exception_Information
222 (Excep.all, Exc_Info.all, Exc_Info_Last);
226 -- Let's shutdown the runtime now. The rest of the procedure
227 -- needs to be careful not to use anything that would require
228 -- runtime support. In particular, function returing strings
229 -- are banned since the sec stack is not functional anymore
231 System.Standard_Library.Adafinal;
233 -- Check for special case of raising _ABORT_SIGNAL, which is not
234 -- really an exception at all. We recognize this by the fact that
235 -- it is the only exception whose name starts with underscore.
237 if Excep.Id.Full_Name.all (1) = '_' then
239 To_Stderr ("Execution terminated by abort of environment task");
242 -- If no tracebacks, we print the unhandled exception in the old style
243 -- (i.e. the style used before ZCX was implemented). We do this to
244 -- retain compatibility, especially with the nightly scripts, but
245 -- this can be removed at some point ???
247 elsif Excep.Num_Tracebacks = 0 then
249 To_Stderr ("raised ");
250 To_Stderr (Excep.Id.Full_Name.all (1 .. Excep.Id.Name_Length - 1));
252 if Msg'Length /= 0 then
262 -- Note we can have this whole information output twice if
263 -- this occurrence gets reraised up to here.
266 To_Stderr ("Execution terminated by unhandled exception");
268 To_Stderr (Exc_Info (1 .. Exc_Info_Last));
272 end Unhandled_Exception_Terminate;
278 procedure To_Stderr (S : String) is
279 procedure put_char_stderr (C : int);
280 pragma Import (C, put_char_stderr, "put_char_stderr");
283 for J in 1 .. S'Length loop
284 if S (J) /= ASCII.CR then
285 put_char_stderr (Character'Pos (S (J)));
291 ------------------------------------
292 -- Handling GNAT.Exception_Traces --
293 ------------------------------------
295 -- The bulk of exception traces output is centralized in Notify_Exception,
296 -- for both the Handled and Unhandled cases. Extra task specific output is
297 -- triggered in the task wrapper for unhandled occurrences in tasks. It is
298 -- not performed in this unit to avoid dragging dependencies against the
299 -- tasking units here.
301 -- We used to rely on the output performed by Unhanded_Exception_Terminate
302 -- for the case of an unhandled occurrence in the environment thread, and
303 -- the task wrapper was responsible for the whole output in the tasking
306 -- This initial scheme had a drawback: the output from Terminate only
307 -- occurs after finalization is done, which means possibly never if some
308 -- tasks keep hanging around.
310 -- The first "presumably obvious" fix consists in moving the Terminate
311 -- output before the finalization. It has not been retained because it
312 -- introduces annoying changes in output orders when the finalization
313 -- itself issues outputs, this also in "regular" cases not resorting to
316 -- Today's solution has the advantage of simplicity and better isolates
317 -- the Exception_Traces machinery.
319 -- It currently outputs the information about unhandled exceptions twice
320 -- in the environment thread, once in the notification routine and once in
321 -- the termination routine. Avoiding the second output is possible but so
322 -- far has been considered undesirable. It would mean changing the order
323 -- of outputs between the two runs with or without exception traces, while
324 -- it seems preferrable to only have additional outputs in the former
327 end Exception_Traces;