OSDN Git Service

* sysdep.c: Problem discovered during IA64 VMS port.
[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-2003 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,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, 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 with Unchecked_Conversion;
35
36 separate (Ada.Exceptions)
37 package body Exception_Traces is
38
39    Nline : constant String := String'(1 => ASCII.LF);
40    --  Convenient shortcut
41
42    type Exception_Action is access procedure (E : Exception_Occurrence);
43    Global_Action : Exception_Action := null;
44    pragma Export
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.
48
49    Raise_Hook_Initialized : Boolean := False;
50    pragma Export
51      (Ada, Raise_Hook_Initialized, "__gnat_exception_actions_initialized");
52
53    function To_Action is new Unchecked_Conversion
54      (Raise_Action, Exception_Action);
55
56    -----------------------
57    -- Local Subprograms --
58    -----------------------
59
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.
65
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.
70
71    ---------------------------------
72    -- Debugger Interface Routines --
73    ---------------------------------
74
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.
78
79    procedure Unhandled_Exception;
80    pragma Export (C, Unhandled_Exception, "__gnat_unhandled_exception");
81    --  Hook for GDB to support "break exception unhandled".
82
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.
86
87    --------------------------------
88    -- Import Run-Time C Routines --
89    --------------------------------
90
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
95
96    pragma Propagate_Exceptions;
97
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
102
103    ----------------------
104    -- Notify_Exception --
105    ----------------------
106
107    procedure Notify_Exception (Excep : EOA; Is_Unhandled : Boolean) is
108    begin
109       --  Output the exception information required by the Exception_Trace
110       --  configuration. Take care not to output information about internal
111       --  exceptions.
112
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.
117
118       if not Excep.Id.Not_Handled_By_Others
119         and then
120         (Exception_Trace = Every_Raise
121          or else (Exception_Trace = Unhandled_Raise and then Is_Unhandled))
122       then
123          To_Stderr (Nline);
124
125          if Is_Unhandled then
126             To_Stderr ("Unhandled ");
127          end if;
128
129          To_Stderr ("Exception raised");
130          To_Stderr (Nline);
131          To_Stderr (Tailored_Exception_Information (Excep.all));
132       end if;
133
134       --  Call the user-specific actions
135       --  ??? We should presumably look at the reraise status here.
136
137       if Raise_Hook_Initialized
138         and then Exception_Data_Ptr (Excep.Id).Raise_Hook /= null
139       then
140          To_Action (Exception_Data_Ptr (Excep.Id).Raise_Hook) (Excep.all);
141       end if;
142
143       if Global_Action /= null then
144          Global_Action (Excep.all);
145       end if;
146    end Notify_Exception;
147
148    ------------------------------
149    -- Notify_Handled_Exception --
150    ------------------------------
151
152    procedure Notify_Handled_Exception is
153    begin
154       Notify_Exception (Get_Current_Excep.all, Is_Unhandled => False);
155    end Notify_Handled_Exception;
156
157    --------------------------------
158    -- Notify_Unhandled_Exception --
159    --------------------------------
160
161    procedure Notify_Unhandled_Exception is
162    begin
163       Notify_Exception (Get_Current_Excep.all, Is_Unhandled => True);
164       Unhandled_Exception;
165    end Notify_Unhandled_Exception;
166
167    -------------------------
168    -- Unhandled_Exception --
169    -------------------------
170
171    procedure Unhandled_Exception is
172    begin
173       null;
174    end Unhandled_Exception;
175
176    -----------------------------------
177    -- Unhandled_Exception_Terminate --
178    -----------------------------------
179
180    type int is new Integer;
181
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).
188
189       Msg : constant String := Excep.Msg (1 .. Excep.Msg_Length);
190
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 ???
198
199       subtype Exc_Info_Type is String (1 .. Max_Static_Exc_Info);
200       type Str_Ptr is access Exc_Info_Type;
201       Exc_Info : Str_Ptr;
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.
208
209    --  Start of processing for Unhandled_Exception_Terminate
210
211    begin
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.
215
216       if Excep.Id.Full_Name.all (1) /= '_'
217         and then Excep.Num_Tracebacks /= 0
218       then
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);
223          end if;
224       end if;
225
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
230
231       System.Standard_Library.Adafinal;
232
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.
236
237       if Excep.Id.Full_Name.all (1) = '_' then
238          To_Stderr (Nline);
239          To_Stderr ("Execution terminated by abort of environment task");
240          To_Stderr (Nline);
241
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 ???
246
247       elsif Excep.Num_Tracebacks = 0 then
248          To_Stderr (Nline);
249          To_Stderr ("raised ");
250          To_Stderr (Excep.Id.Full_Name.all (1 .. Excep.Id.Name_Length - 1));
251
252          if Msg'Length /= 0 then
253             To_Stderr (" : ");
254             To_Stderr (Msg);
255          end if;
256
257          To_Stderr (Nline);
258
259       else
260          --  Traceback exists
261
262          --  Note we can have this whole information output twice if
263          --  this occurrence gets reraised up to here.
264
265          To_Stderr (Nline);
266          To_Stderr ("Execution terminated by unhandled exception");
267          To_Stderr (Nline);
268          To_Stderr (Exc_Info (1 .. Exc_Info_Last));
269       end if;
270
271       Unhandled_Terminate;
272    end Unhandled_Exception_Terminate;
273
274    ---------------
275    -- To_Stderr --
276    ---------------
277
278    procedure To_Stderr (S : String) is
279       procedure put_char_stderr (C : int);
280       pragma Import (C, put_char_stderr, "put_char_stderr");
281
282    begin
283       for J in 1 .. S'Length loop
284          if S (J) /= ASCII.CR then
285             put_char_stderr (Character'Pos (S (J)));
286          end if;
287       end loop;
288    end To_Stderr;
289
290
291    ------------------------------------
292    -- Handling GNAT.Exception_Traces --
293    ------------------------------------
294
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.
300
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
304    --  case.
305
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.
309
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
314    --  Exception_Traces.
315
316    --  Today's solution has the advantage of simplicity and better isolates
317    --  the Exception_Traces machinery.
318
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
325    --  case.
326
327 end Exception_Traces;