1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- ADA.EXCEPTIONS.EXCEPTION_DATA --
9 -- Copyright (C) 1992-2009, 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 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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 with System
.Storage_Elements
; use System
.Storage_Elements
;
34 separate (Ada
.Exceptions
)
35 package body Exception_Data
is
37 -- This unit implements the Exception_Information related services for
38 -- both the Ada standard requirements and the GNAT.Exception_Traces
41 -- There are common parts between the contents of Exception_Information
42 -- (the regular Ada interface) and Tailored_Exception_Information (what
43 -- the automatic backtracing output includes). The overall structure is
47 -- Exception_Information
51 -- Basic_Exc_Info & Basic_Exc_Tback
55 -- (B_E_I) | Exception_Name: <exception name> (as in Exception_Name)
56 -- | Message: <message> (or a null line if no message)
57 -- | PID=nnnn (if != 0)
59 -- (B_E_TB) | Call stack traceback locations:
60 -- | <0xyyyyyyyy 0xyyyyyyyy ...>
63 -- Tailored_Exception_Information
65 -- +----------+----------+
67 -- Basic_Exc_Info & Tailored_Exc_Tback
69 -- +-----------+------------+
71 -- Basic_Exc_Tback Or Tback_Decorator
72 -- if no decorator set otherwise
74 -- Functions returning String imply secondary stack use, which is a heavy
75 -- mechanism requiring run-time support. Besides, some of the routines we
76 -- provide here are to be used by the default Last_Chance_Handler, at the
77 -- critical point where the runtime is about to be finalized. Since most
78 -- of the items we have at hand are of bounded length, we also provide a
79 -- procedural interface able to incrementally append the necessary bits to
80 -- a preallocated buffer or output them straight to stderr.
82 -- The procedural interface is composed of two major sections: a neutral
83 -- section for basic types like Address, Character, Natural or String, and
84 -- an exception oriented section for the e.g. Basic_Exception_Information.
85 -- This is the Append_Info family of procedures below.
87 -- Output to stderr is commanded by passing an empty buffer to update, and
88 -- care is taken not to overflow otherwise.
90 --------------------------------------------
91 -- Procedural Interface - Neutral section --
92 --------------------------------------------
94 procedure Append_Info_Address
97 Ptr
: in out Natural);
99 procedure Append_Info_Character
101 Info
: in out String;
102 Ptr
: in out Natural);
104 procedure Append_Info_Nat
106 Info
: in out String;
107 Ptr
: in out Natural);
109 procedure Append_Info_NL
110 (Info
: in out String;
111 Ptr
: in out Natural);
112 pragma Inline
(Append_Info_NL
);
114 procedure Append_Info_String
116 Info
: in out String;
117 Ptr
: in out Natural);
119 -------------------------------------------------------
120 -- Procedural Interface - Exception oriented section --
121 -------------------------------------------------------
123 procedure Append_Info_Exception_Name
125 Info
: in out String;
126 Ptr
: in out Natural);
128 procedure Append_Info_Exception_Name
129 (X
: Exception_Occurrence
;
130 Info
: in out String;
131 Ptr
: in out Natural);
133 procedure Append_Info_Exception_Message
134 (X
: Exception_Occurrence
;
135 Info
: in out String;
136 Ptr
: in out Natural);
138 procedure Append_Info_Basic_Exception_Information
139 (X
: Exception_Occurrence
;
140 Info
: in out String;
141 Ptr
: in out Natural);
143 procedure Append_Info_Basic_Exception_Traceback
144 (X
: Exception_Occurrence
;
145 Info
: in out String;
146 Ptr
: in out Natural);
148 procedure Append_Info_Exception_Information
149 (X
: Exception_Occurrence
;
150 Info
: in out String;
151 Ptr
: in out Natural);
153 -- The "functional" interface to the exception information not involving
154 -- a traceback decorator uses preallocated intermediate buffers to avoid
155 -- the use of secondary stack. Preallocation requires preliminary length
156 -- computation, for which a series of functions are introduced:
158 ---------------------------------
159 -- Length evaluation utilities --
160 ---------------------------------
162 function Basic_Exception_Info_Maxlength
163 (X
: Exception_Occurrence
) return Natural;
165 function Basic_Exception_Tback_Maxlength
166 (X
: Exception_Occurrence
) return Natural;
168 function Exception_Info_Maxlength
169 (X
: Exception_Occurrence
) return Natural;
171 function Exception_Name_Length
172 (Id
: Exception_Id
) return Natural;
174 function Exception_Name_Length
175 (X
: Exception_Occurrence
) return Natural;
177 function Exception_Message_Length
178 (X
: Exception_Occurrence
) return Natural;
180 --------------------------
181 -- Functional Interface --
182 --------------------------
184 function Basic_Exception_Traceback
185 (X
: Exception_Occurrence
) return String;
186 -- Returns an image of the complete call chain associated with an
187 -- exception occurrence in its most basic form, that is as a raw sequence
188 -- of hexadecimal binary addresses.
190 function Tailored_Exception_Traceback
191 (X
: Exception_Occurrence
) return String;
192 -- Returns an image of the complete call chain associated with an
193 -- exception occurrence, either in its basic form if no decorator is
194 -- in place, or as formatted by the decorator otherwise.
196 -----------------------------------------------------------------------
197 -- Services for the default Last_Chance_Handler and the task wrapper --
198 -----------------------------------------------------------------------
201 (Ada
, Append_Info_Exception_Message
, "__gnat_append_info_e_msg");
204 (Ada
, Append_Info_Exception_Information
, "__gnat_append_info_e_info");
207 (Ada
, Exception_Message_Length
, "__gnat_exception_msg_len");
209 -------------------------
210 -- Append_Info_Address --
211 -------------------------
213 procedure Append_Info_Address
215 Info
: in out String;
216 Ptr
: in out Natural)
218 S
: String (1 .. 18);
222 H
: constant array (Integer range 0 .. 15) of Character :=
228 S
(P
) := H
(Integer (N
mod 16));
237 Append_Info_String
(S
(P
- 1 .. S
'Last), Info
, Ptr
);
238 end Append_Info_Address
;
240 ---------------------------
241 -- Append_Info_Character --
242 ---------------------------
244 procedure Append_Info_Character
246 Info
: in out String;
247 Ptr
: in out Natural)
250 if Info
'Length = 0 then
252 elsif Ptr
< Info
'Last then
256 end Append_Info_Character
;
258 ---------------------
259 -- Append_Info_Nat --
260 ---------------------
262 procedure Append_Info_Nat
264 Info
: in out String;
265 Ptr
: in out Natural)
269 Append_Info_Nat
(N
/ 10, Info
, Ptr
);
272 Append_Info_Character
273 (Character'Val (Character'Pos ('0') + N
mod 10), Info
, Ptr
);
280 procedure Append_Info_NL
281 (Info
: in out String;
282 Ptr
: in out Natural)
285 Append_Info_Character
(ASCII
.LF
, Info
, Ptr
);
288 ------------------------
289 -- Append_Info_String --
290 ------------------------
292 procedure Append_Info_String
294 Info
: in out String;
295 Ptr
: in out Natural)
298 if Info
'Length = 0 then
302 Last
: constant Natural :=
303 Integer'Min (Ptr
+ S
'Length, Info
'Last);
305 Info
(Ptr
+ 1 .. Last
) := S
;
309 end Append_Info_String
;
311 ---------------------------------------------
312 -- Append_Info_Basic_Exception_Information --
313 ---------------------------------------------
315 -- To ease the maximum length computation, we define and pull out a couple
316 -- of string constants:
318 BEI_Name_Header
: constant String := "Exception name: ";
319 BEI_Msg_Header
: constant String := "Message: ";
320 BEI_PID_Header
: constant String := "PID: ";
322 procedure Append_Info_Basic_Exception_Information
323 (X
: Exception_Occurrence
;
324 Info
: in out String;
325 Ptr
: in out Natural)
327 Name
: String (1 .. Exception_Name_Length
(X
));
328 -- Buffer in which to fetch the exception name, in order to check
329 -- whether this is an internal _ABORT_SIGNAL or a regular occurrence.
331 Name_Ptr
: Natural := Name
'First - 1;
334 -- Output exception name and message except for _ABORT_SIGNAL, where
335 -- these two lines are omitted.
337 Append_Info_Exception_Name
(X
, Name
, Name_Ptr
);
339 if Name
(Name
'First) /= '_' then
340 Append_Info_String
(BEI_Name_Header
, Info
, Ptr
);
341 Append_Info_String
(Name
, Info
, Ptr
);
342 Append_Info_NL
(Info
, Ptr
);
344 if Exception_Message_Length
(X
) /= 0 then
345 Append_Info_String
(BEI_Msg_Header
, Info
, Ptr
);
346 Append_Info_Exception_Message
(X
, Info
, Ptr
);
347 Append_Info_NL
(Info
, Ptr
);
351 -- Output PID line if non-zero
354 Append_Info_String
(BEI_PID_Header
, Info
, Ptr
);
355 Append_Info_Nat
(X
.Pid
, Info
, Ptr
);
356 Append_Info_NL
(Info
, Ptr
);
358 end Append_Info_Basic_Exception_Information
;
360 -------------------------------------------
361 -- Basic_Exception_Information_Maxlength --
362 -------------------------------------------
364 function Basic_Exception_Info_Maxlength
365 (X
: Exception_Occurrence
) return Natural is
368 BEI_Name_Header
'Length + Exception_Name_Length
(X
) + 1
369 + BEI_Msg_Header
'Length + Exception_Message_Length
(X
) + 1
370 + BEI_PID_Header
'Length + 15;
371 end Basic_Exception_Info_Maxlength
;
373 -------------------------------------------
374 -- Append_Info_Basic_Exception_Traceback --
375 -------------------------------------------
377 -- As for Basic_Exception_Information:
379 BETB_Header
: constant String := "Call stack traceback locations:";
381 procedure Append_Info_Basic_Exception_Traceback
382 (X
: Exception_Occurrence
;
383 Info
: in out String;
384 Ptr
: in out Natural)
387 if X
.Num_Tracebacks
= 0 then
391 Append_Info_String
(BETB_Header
, Info
, Ptr
);
392 Append_Info_NL
(Info
, Ptr
);
394 for J
in 1 .. X
.Num_Tracebacks
loop
395 Append_Info_Address
(TBE
.PC_For
(X
.Tracebacks
(J
)), Info
, Ptr
);
396 exit when J
= X
.Num_Tracebacks
;
397 Append_Info_Character
(' ', Info
, Ptr
);
400 Append_Info_NL
(Info
, Ptr
);
401 end Append_Info_Basic_Exception_Traceback
;
403 -----------------------------------------
404 -- Basic_Exception_Traceback_Maxlength --
405 -----------------------------------------
407 function Basic_Exception_Tback_Maxlength
408 (X
: Exception_Occurrence
) return Natural
410 Space_Per_Traceback
: constant := 2 + 16 + 1;
411 -- Space for "0x" + HHHHHHHHHHHHHHHH + " "
413 return BETB_Header
'Length + 1 +
414 X
.Num_Tracebacks
* Space_Per_Traceback
+ 1;
415 end Basic_Exception_Tback_Maxlength
;
417 ---------------------------------------
418 -- Append_Info_Exception_Information --
419 ---------------------------------------
421 procedure Append_Info_Exception_Information
422 (X
: Exception_Occurrence
;
423 Info
: in out String;
424 Ptr
: in out Natural)
427 Append_Info_Basic_Exception_Information
(X
, Info
, Ptr
);
428 Append_Info_Basic_Exception_Traceback
(X
, Info
, Ptr
);
429 end Append_Info_Exception_Information
;
431 ------------------------------
432 -- Exception_Info_Maxlength --
433 ------------------------------
435 function Exception_Info_Maxlength
436 (X
: Exception_Occurrence
) return Natural is
439 Basic_Exception_Info_Maxlength
(X
)
440 + Basic_Exception_Tback_Maxlength
(X
);
441 end Exception_Info_Maxlength
;
443 -----------------------------------
444 -- Append_Info_Exception_Message --
445 -----------------------------------
447 procedure Append_Info_Exception_Message
448 (X
: Exception_Occurrence
;
449 Info
: in out String;
450 Ptr
: in out Natural) is
452 if X
.Id
= Null_Id
then
453 raise Constraint_Error
;
457 Len
: constant Natural := Exception_Message_Length
(X
);
458 Msg
: constant String (1 .. Len
) := X
.Msg
(1 .. Len
);
460 Append_Info_String
(Msg
, Info
, Ptr
);
462 end Append_Info_Exception_Message
;
464 --------------------------------
465 -- Append_Info_Exception_Name --
466 --------------------------------
468 procedure Append_Info_Exception_Name
470 Info
: in out String;
471 Ptr
: in out Natural)
475 raise Constraint_Error
;
479 Len
: constant Natural := Exception_Name_Length
(Id
);
480 Name
: constant String (1 .. Len
) := To_Ptr
(Id
.Full_Name
) (1 .. Len
);
482 Append_Info_String
(Name
, Info
, Ptr
);
484 end Append_Info_Exception_Name
;
486 procedure Append_Info_Exception_Name
487 (X
: Exception_Occurrence
;
488 Info
: in out String;
489 Ptr
: in out Natural)
492 Append_Info_Exception_Name
(X
.Id
, Info
, Ptr
);
493 end Append_Info_Exception_Name
;
495 ---------------------------
496 -- Exception_Name_Length --
497 ---------------------------
499 function Exception_Name_Length
500 (Id
: Exception_Id
) return Natural is
502 -- What is stored in the internal Name buffer includes a terminating
503 -- null character that we never care about.
505 return Id
.Name_Length
- 1;
506 end Exception_Name_Length
;
508 function Exception_Name_Length
509 (X
: Exception_Occurrence
) return Natural is
511 return Exception_Name_Length
(X
.Id
);
512 end Exception_Name_Length
;
514 ------------------------------
515 -- Exception_Message_Length --
516 ------------------------------
518 function Exception_Message_Length
519 (X
: Exception_Occurrence
) return Natural is
522 end Exception_Message_Length
;
524 -------------------------------
525 -- Basic_Exception_Traceback --
526 -------------------------------
528 function Basic_Exception_Traceback
529 (X
: Exception_Occurrence
) return String
531 Info
: aliased String (1 .. Basic_Exception_Tback_Maxlength
(X
));
532 Ptr
: Natural := Info
'First - 1;
535 Append_Info_Basic_Exception_Traceback
(X
, Info
, Ptr
);
536 return Info
(Info
'First .. Ptr
);
537 end Basic_Exception_Traceback
;
539 ---------------------------
540 -- Exception_Information --
541 ---------------------------
543 function Exception_Information
544 (X
: Exception_Occurrence
) return String
546 Info
: String (1 .. Exception_Info_Maxlength
(X
));
547 Ptr
: Natural := Info
'First - 1;
550 Append_Info_Exception_Information
(X
, Info
, Ptr
);
551 return Info
(Info
'First .. Ptr
);
552 end Exception_Information
;
554 -------------------------
555 -- Set_Exception_C_Msg --
556 -------------------------
558 procedure Set_Exception_C_Msg
560 Msg1
: System
.Address
;
562 Msg2
: System
.Address
:= System
.Null_Address
)
564 Excep
: constant EOA
:= Get_Current_Excep
.all;
565 Val
: Integer := Line
;
571 Exception_Propagation
.Setup_Exception
(Excep
, Excep
);
572 Excep
.Exception_Raised
:= False;
574 Excep
.Num_Tracebacks
:= 0;
575 Excep
.Pid
:= Local_Partition_ID
;
576 Excep
.Msg_Length
:= 0;
577 Excep
.Cleanup_Flag
:= False;
579 while To_Ptr
(Msg1
) (Excep
.Msg_Length
+ 1) /= ASCII
.NUL
580 and then Excep
.Msg_Length
< Exception_Msg_Max_Length
582 Excep
.Msg_Length
:= Excep
.Msg_Length
+ 1;
583 Excep
.Msg
(Excep
.Msg_Length
) := To_Ptr
(Msg1
) (Excep
.Msg_Length
);
586 -- Append line number if present
590 -- Compute the number of needed characters
597 -- If enough characters are available, put the line number
599 if Excep
.Msg_Length
<= Exception_Msg_Max_Length
- Size
then
600 Excep
.Msg
(Excep
.Msg_Length
+ 1) := ':';
601 Excep
.Msg_Length
:= Excep
.Msg_Length
+ Size
;
606 Remind
:= Val
rem 10;
608 Excep
.Msg
(Excep
.Msg_Length
- Size
) :=
609 Character'Val (Remind
+ Character'Pos ('0'));
615 -- Append second message if present
617 if Msg2
/= System
.Null_Address
618 and then Excep
.Msg_Length
+ 1 < Exception_Msg_Max_Length
620 Excep
.Msg_Length
:= Excep
.Msg_Length
+ 1;
621 Excep
.Msg
(Excep
.Msg_Length
) := ' ';
624 while To_Ptr
(Msg2
) (Ptr
) /= ASCII
.NUL
625 and then Excep
.Msg_Length
< Exception_Msg_Max_Length
627 Excep
.Msg_Length
:= Excep
.Msg_Length
+ 1;
628 Excep
.Msg
(Excep
.Msg_Length
) := To_Ptr
(Msg2
) (Ptr
);
632 end Set_Exception_C_Msg
;
634 -----------------------
635 -- Set_Exception_Msg --
636 -----------------------
638 procedure Set_Exception_Msg
642 Len
: constant Natural :=
643 Natural'Min (Message
'Length, Exception_Msg_Max_Length
);
644 First
: constant Integer := Message
'First;
645 Excep
: constant EOA
:= Get_Current_Excep
.all;
648 Exception_Propagation
.Setup_Exception
(Excep
, Excep
);
649 Excep
.Exception_Raised
:= False;
650 Excep
.Msg_Length
:= Len
;
651 Excep
.Msg
(1 .. Len
) := Message
(First
.. First
+ Len
- 1);
653 Excep
.Num_Tracebacks
:= 0;
654 Excep
.Pid
:= Local_Partition_ID
;
655 Excep
.Cleanup_Flag
:= False;
657 end Set_Exception_Msg
;
659 ----------------------------------
660 -- Tailored_Exception_Traceback --
661 ----------------------------------
663 function Tailored_Exception_Traceback
664 (X
: Exception_Occurrence
) return String
666 -- We reference the decorator *wrapper* here and not the decorator
667 -- itself. The purpose of the local variable Wrapper is to prevent a
668 -- potential race condition in the code below. The atomicity of this
669 -- assignment is enforced by pragma Atomic in System.Soft_Links.
671 -- The potential race condition here, if no local variable was used,
672 -- relates to the test upon the wrapper's value and the call, which
673 -- are not performed atomically. With the local variable, potential
674 -- changes of the wrapper's global value between the test and the
675 -- call become inoffensive.
677 Wrapper
: constant Traceback_Decorator_Wrapper_Call
:=
678 Traceback_Decorator_Wrapper
;
681 if Wrapper
= null then
682 return Basic_Exception_Traceback
(X
);
684 return Wrapper
.all (X
.Tracebacks
'Address, X
.Num_Tracebacks
);
686 end Tailored_Exception_Traceback
;
688 ------------------------------------
689 -- Tailored_Exception_Information --
690 ------------------------------------
692 function Tailored_Exception_Information
693 (X
: Exception_Occurrence
) return String
695 -- The tailored exception information is the basic information
696 -- associated with the tailored call chain backtrace.
698 Tback_Info
: constant String := Tailored_Exception_Traceback
(X
);
699 Tback_Len
: constant Natural := Tback_Info
'Length;
701 Info
: String (1 .. Basic_Exception_Info_Maxlength
(X
) + Tback_Len
);
702 Ptr
: Natural := Info
'First - 1;
705 Append_Info_Basic_Exception_Information
(X
, Info
, Ptr
);
706 Append_Info_String
(Tback_Info
, Info
, Ptr
);
707 return Info
(Info
'First .. Ptr
);
708 end Tailored_Exception_Information
;