OSDN Git Service

* env.c [__alpha__ && __osf__] (AES_SOURCE): Define.
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-traceb-hpux.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                     S Y S T E M . T R A C E B A C K                      --
6 --                             (HP/UX Version)                              --
7 --                                                                          --
8 --                                 B o d y                                  --
9 --                                                                          --
10 --                     Copyright (C) 1999-2006, AdaCore                     --
11 --            Copyright (C) 2008, Free Software Foundation, Inc.            --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
22 -- Boston, MA 02110-1301, USA.                                              --
23 --                                                                          --
24 -- As a special exception,  if other files  instantiate  generics from this --
25 -- unit, or you link  this unit with other files  to produce an executable, --
26 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
27 -- covered  by the  GNU  General  Public  License.  This exception does not --
28 -- however invalidate  any other reasons why  the executable file  might be --
29 -- covered by the  GNU Public License.                                      --
30 --                                                                          --
31 -- GNAT was originally developed  by the GNAT team at  New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
33 --                                                                          --
34 ------------------------------------------------------------------------------
35
36 with Ada.Unchecked_Conversion;
37
38 package body System.Traceback is
39
40    --  This package implements the backtracing facility by way of a dedicated
41    --  HP library for stack unwinding described in the "Runtime Architecture
42    --  Document".
43
44    pragma Linker_Options ("/usr/lib/libcl.a");
45
46    --  The library basically offers services to fetch information about a
47    --  "previous" frame based on information about a "current" one.
48
49    type Current_Frame_Descriptor is record
50       cur_fsz : Address;  --  Frame size of current routine.
51       cur_sp  : Address;  --  The current value of stack pointer.
52       cur_rls : Address;  --  PC-space of the caller.
53       cur_rlo : Address;  --  PC-offset of the caller.
54       cur_dp  : Address;  --  Data Pointer of the current routine.
55       top_rp  : Address;  --  Initial value of RP.
56       top_mrp : Address;  --  Initial value of MRP.
57       top_sr0 : Address;  --  Initial value of sr0.
58       top_sr4 : Address;  --  Initial value of sr4.
59       top_r3  : Address;  --  Initial value of gr3.
60       cur_r19 : Address;  --  GR19 value of the calling routine.
61       top_r4  : Address;  --  Initial value of gr4.
62       dummy   : Address;  --  Reserved.
63       out_rlo : Address;  --  PC-offset of the caller after get_previous.
64    end record;
65
66    type Previous_Frame_Descriptor is record
67       prev_fsz : Address;  --  frame size of calling routine.
68       prev_sp  : Address;  --  SP of calling routine.
69       prev_rls : Address;  --  PC_space of calling routine's caller.
70       prev_rlo : Address;  --  PC_offset of calling routine's caller.
71       prev_dp  : Address;  --  DP of calling routine.
72       udescr0  : Address;  --  low word of calling routine's unwind desc.
73       udescr1  : Address;  --  high word of calling routine's unwind desc.
74       ustart   : Address;  --  start of the unwind region.
75       uend     : Address;  --  end of the unwind region.
76       uw_index : Address;  --  index into the unwind table.
77       prev_r19 : Address;  --  GR19 value of the caller's caller.
78       top_r3   : Address;  --  Caller's initial gr3.
79       top_r4   : Address;  --  Caller's initial gr4.
80    end record;
81
82    --  Provide useful shortcuts for the names
83
84    subtype CFD is Current_Frame_Descriptor;
85    subtype PFD is Previous_Frame_Descriptor;
86
87    --  Frames with dynamic stack allocation are handled using the associated
88    --  frame pointer, but HP compilers and GCC setup this pointer differently.
89    --  HP compilers set it to point at the top (highest address) of the static
90    --  part of the frame, whereas GCC sets it to point at the bottom of this
91    --  region. We have to fake the unwinder to compensate for this difference,
92    --  for which we'll need to access some subprograms unwind descriptors.
93
94    type Bits_2_Value is mod 2 ** 2;
95    for Bits_2_Value'Size use 2;
96
97    type Bits_4_Value  is mod 2 ** 4;
98    for Bits_4_Value'Size use 4;
99
100    type Bits_5_Value  is mod 2 ** 5;
101    for Bits_5_Value'Size use 5;
102
103    type Bits_27_Value is mod 2 ** 27;
104    for Bits_27_Value'Size use 27;
105
106    type Unwind_Descriptor is record
107       cannot_unwind         : Boolean;
108       mcode                 : Boolean;
109       mcode_save_restore    : Boolean;
110       region_desc           : Bits_2_Value;
111       reserved0             : Boolean;
112       entry_sr              : Boolean;
113       entry_fr              : Bits_4_Value;
114       entry_gr              : Bits_5_Value;
115
116       args_stored           : Boolean;
117       variable_frame        : Boolean;
118       separate_package_body : Boolean;
119       frame_extension_mcode : Boolean;
120
121       stack_overflow_check  : Boolean;
122       two_steps_sp_adjust   : Boolean;
123       sr4_export            : Boolean;
124       cxx_info              : Boolean;
125
126       cxx_try_catch         : Boolean;
127       sched_entry_seq       : Boolean;
128       reserved1             : Boolean;
129       save_sp               : Boolean;
130
131       save_rp               : Boolean;
132       save_mrp              : Boolean;
133       save_r19              : Boolean;
134       cleanups              : Boolean;
135
136       hpe_interrupt_marker  : Boolean;
137       hpux_interrupt_marker : Boolean;
138       large_frame           : Boolean;
139       alloca_frame          : Boolean;
140
141       reserved2             : Boolean;
142       frame_size            : Bits_27_Value;
143    end record;
144
145    for Unwind_Descriptor'Size use 64;
146
147    for Unwind_Descriptor use record
148       cannot_unwind         at 0 range 0 .. 0;
149       mcode                 at 0 range 1 .. 1;
150       mcode_save_restore    at 0 range 2 .. 2;
151       region_desc           at 0 range 3 .. 4;
152       reserved0             at 0 range 5 .. 5;
153       entry_sr              at 0 range 6 .. 6;
154       entry_fr              at 0 range 7 .. 10;
155
156       entry_gr              at 1 range 3 .. 7;
157
158       args_stored           at 2 range 0 .. 0;
159       variable_frame        at 2 range 1 .. 1;
160       separate_package_body at 2 range 2 .. 2;
161       frame_extension_mcode at 2 range 3 .. 3;
162       stack_overflow_check  at 2 range 4 .. 4;
163       two_steps_sp_adjust   at 2 range 5 .. 5;
164       sr4_export            at 2 range 6 .. 6;
165       cxx_info              at 2 range 7 .. 7;
166
167       cxx_try_catch         at 3 range 0 .. 0;
168       sched_entry_seq       at 3 range 1 .. 1;
169       reserved1             at 3 range 2 .. 2;
170       save_sp               at 3 range 3 .. 3;
171       save_rp               at 3 range 4 .. 4;
172       save_mrp              at 3 range 5 .. 5;
173       save_r19              at 3 range 6 .. 6;
174       cleanups              at 3 range 7 .. 7;
175
176       hpe_interrupt_marker  at 4 range 0 .. 0;
177       hpux_interrupt_marker at 4 range 1 .. 1;
178       large_frame           at 4 range 2 .. 2;
179       alloca_frame          at 4 range 3 .. 3;
180
181       reserved2             at 4 range 4 .. 4;
182       frame_size            at 4 range 5 .. 31;
183    end record;
184
185    subtype UWD is Unwind_Descriptor;
186    type UWD_Ptr is access all UWD;
187
188    function To_UWD_Access is new Ada.Unchecked_Conversion (Address, UWD_Ptr);
189
190    --  The descriptor associated with a given code location is retrieved
191    --  using functions imported from the HP library, requiring the definition
192    --  of additional structures.
193
194    type Unwind_Table_Region is record
195       Table_Start : Address;
196       Table_End   : Address;
197    end record;
198    --  An Unwind Table region, which is a memory area containing Unwind
199    --  Descriptors.
200
201    subtype UWT is Unwind_Table_Region;
202
203    --  The subprograms imported below are provided by the HP library
204
205    function U_get_unwind_table return UWT;
206    pragma Import (C, U_get_unwind_table, "U_get_unwind_table");
207    --  Get the unwind table region associated with the current executable.
208    --  This function is actually documented as having an argument, but which
209    --  is only used for the MPE/iX targets.
210
211    function U_get_shLib_unwind_table (r19 : Address) return UWT;
212    pragma Import (C, U_get_shLib_unwind_table, "U_get_shLib_unw_tbl");
213    --  Return the unwind table region associated with a possible shared
214    --  library, as determined by the provided r19 value.
215
216    function U_get_shLib_text_addr (r19 : Address) return Address;
217    pragma Import (C, U_get_shLib_text_addr, "U_get_shLib_text_addr");
218    --  Return the address at which the code for a shared library begins, or
219    --  -1 if the value provided for r19 does not identify shared library code.
220
221    function U_get_unwind_entry
222      (Pc          : Address;
223       Space       : Address;
224       Table_Start : Address;
225       Table_End   : Address) return Address;
226    pragma Import (C, U_get_unwind_entry, "U_get_unwind_entry");
227    --  Given the bounds of an unwind table, return the address of the
228    --  unwind descriptor associated with a code location/space. In the case
229    --  of shared library code, the offset from the beginning of the library
230    --  is expected as Pc.
231
232    procedure U_init_frame_record (Frame : not null access CFD);
233    pragma Import (C, U_init_frame_record, "U_init_frame_record");
234
235    procedure U_prep_frame_rec_for_unwind (Frame : not null access CFD);
236    pragma Import (C, U_prep_frame_rec_for_unwind,
237                     "U_prep_frame_rec_for_unwind");
238
239    --  Fetch the description data of the frame in which these two procedures
240    --  are called.
241
242    function U_get_u_rlo
243      (Cur : not null access CFD; Prev : not null access PFD) return Integer;
244    pragma Import (C, U_get_u_rlo, "U_IS_STUB_OR_CALLX");
245    --  From a complete current frame with a return location possibly located
246    --  into a linker generated stub, and basic information about the previous
247    --  frame, place the first non stub return location into the current frame.
248    --  Return -1 if something went wrong during the computation.
249
250    function U_is_shared_pc (rlo : Address; r19 : Address) return Address;
251    pragma Import (C, U_is_shared_pc, "U_is_shared_pc");
252    --  Return 0 if the provided return location does not correspond to code
253    --  in a shared library, or something non null otherwise.
254
255    function U_get_previous_frame_x
256      (current_frame  : not null access CFD;
257       previous_frame : not null access PFD;
258       previous_size  : Integer) return Integer;
259    pragma Import (C, U_get_previous_frame_x, "U_get_previous_frame_x");
260    --  Fetch the data describing the "previous" frame relatively to the
261    --  "current" one. "previous_size" should be the size of the "previous"
262    --  frame descriptor provided.
263    --
264    --  The library provides a simpler interface without the size parameter
265    --  but it is not usable when frames with dynamically allocated space are
266    --  on the way.
267
268    ------------------
269    -- C_Call_Chain --
270    ------------------
271
272    function C_Call_Chain
273      (Traceback : System.Address;
274       Max_Len   : Natural) return Natural
275    is
276       Val : Natural;
277
278    begin
279       Call_Chain (Traceback, Max_Len, Val);
280       return Val;
281    end C_Call_Chain;
282
283    ----------------
284    -- Call_Chain --
285    ----------------
286
287    procedure Call_Chain
288      (Traceback   : System.Address;
289       Max_Len     : Natural;
290       Len         : out Natural;
291       Exclude_Min : System.Address := System.Null_Address;
292       Exclude_Max : System.Address := System.Null_Address;
293       Skip_Frames : Natural := 1)
294    is
295       type Tracebacks_Array is array (1 .. Max_Len) of System.Address;
296       pragma Suppress_Initialization (Tracebacks_Array);
297
298       --  The code location returned by the unwinder is a return location but
299       --  what we need is a call point. Under HP-UX call instructions are 4
300       --  bytes long and the return point they specify is 4 bytes beyond the
301       --  next instruction because of the delay slot.
302
303       Call_Size  : constant := 4;
304       DSlot_Size : constant := 4;
305       Rlo_Offset : constant := Call_Size + DSlot_Size;
306
307       --  Moreover, the return point is passed via a register which two least
308       --  significant bits specify a privilege level that we will have to mask.
309
310       Priv_Mask  : constant := 16#00000003#;
311
312       Frame       : aliased CFD;
313       Code        : System.Address;
314       J           : Natural := 1;
315       Pop_Success : Boolean;
316       Trace       : Tracebacks_Array;
317       for Trace'Address use Traceback;
318
319       --  The backtracing process needs a set of subprograms :
320
321       function UWD_For_RLO_Of (Frame : not null access CFD) return UWD_Ptr;
322       --  Return an access to the unwind descriptor for the caller of
323       --  a given frame, using only the provided return location.
324
325       function UWD_For_Caller_Of (Frame : not null access CFD) return UWD_Ptr;
326       --  Return an access to the unwind descriptor for the user code caller
327       --  of a given frame, or null if the information is not available.
328
329       function Pop_Frame (Frame : not null access CFD) return Boolean;
330       --  Update the provided machine state structure so that it reflects
331       --  the state one call frame "above" the initial one.
332       --
333       --  Return True if the operation has been successful, False otherwise.
334       --  Failure typically occurs when the top of the call stack has been
335       --  reached.
336
337       function Prepare_For_Unwind_Of
338         (Frame : not null access CFD) return Boolean;
339       --  Perform the necessary adaptations to the machine state before
340       --  calling the unwinder. Currently used for the specific case of
341       --  dynamically sized previous frames.
342       --
343       --  Return True if everything went fine, or False otherwise.
344
345       Program_UWT : constant UWT := U_get_unwind_table;
346
347       ---------------
348       -- Pop_Frame --
349       ---------------
350
351       function Pop_Frame (Frame : not null access CFD) return Boolean is
352          Up_Frame    : aliased PFD;
353          State_Ready : Boolean;
354
355       begin
356          --  Check/adapt the state before calling the unwinder and return
357          --  if anything went wrong.
358
359          State_Ready := Prepare_For_Unwind_Of (Frame);
360
361          if not State_Ready then
362             return False;
363          end if;
364
365          --  Now, safely call the unwinder and use the results
366
367          if U_get_previous_frame_x (Frame,
368                                     Up_Frame'Access,
369                                     Up_Frame'Size) /= 0
370          then
371             return False;
372          end if;
373
374          --  In case a stub is on the way, the usual previous return location
375          --  (the one in prev_rlo) is the one in the stub and the "real" one
376          --  is placed in the "current" record, so let's take this one into
377          --  account.
378
379          Frame.out_rlo := Frame.cur_rlo;
380
381          Frame.cur_fsz := Up_Frame.prev_fsz;
382          Frame.cur_sp  := Up_Frame.prev_sp;
383          Frame.cur_rls := Up_Frame.prev_rls;
384          Frame.cur_rlo := Up_Frame.prev_rlo;
385          Frame.cur_dp  := Up_Frame.prev_dp;
386          Frame.cur_r19 := Up_Frame.prev_r19;
387          Frame.top_r3  := Up_Frame.top_r3;
388          Frame.top_r4  := Up_Frame.top_r4;
389
390          return True;
391       end Pop_Frame;
392
393       ---------------------------------
394       -- Prepare_State_For_Unwind_Of --
395       ---------------------------------
396
397       function Prepare_For_Unwind_Of
398         (Frame : not null access CFD) return Boolean
399       is
400          Caller_UWD    : UWD_Ptr;
401          FP_Adjustment : Integer;
402
403       begin
404          --  No need to bother doing anything if the stack is already fully
405          --  unwound.
406
407          if Frame.cur_rlo = 0 then
408             return False;
409          end if;
410
411          --  When ALLOCA_FRAME is set in an unwind descriptor, the unwinder
412          --  uses the value provided in current.top_r3 or current.top_r4 as
413          --  a frame pointer to compute the size of the frame. What decides
414          --  between r3 or r4 is the unwind descriptor LARGE_FRAME bit, with
415          --  r4 chosen if the bit is set.
416
417          --  The size computed by the unwinder is STATIC_PART + (SP - FP),
418          --  which is correct with HP's frame pointer convention, but not
419          --  with GCC's one since we end up with the static part accounted
420          --  for twice.
421
422          --  We have to compute r4 when it is required because the unwinder
423          --  has looked for it at a place where it was not if we went through
424          --  GCC frames.
425
426          --  The size of the static part of a frame can be found in the
427          --  associated unwind descriptor.
428
429          Caller_UWD := UWD_For_Caller_Of (Frame);
430
431          --  If we cannot get it, we are unable to compute the potentially
432          --  necessary adjustments. We'd better not try to go on then.
433
434          if Caller_UWD = null then
435             return False;
436          end if;
437
438          --  If the caller frame is a GCC one, r3 is its frame pointer and
439          --  points to the bottom of the frame. The value to provide for r4
440          --  can then be computed directly from the one of r3, compensating
441          --  for the static part of the frame.
442
443          --  If the caller frame is an HP one, r3 is used to locate the
444          --  previous frame marker, that is it also points to the bottom of
445          --  the frame (this is why r3 cannot be used as the frame pointer in
446          --  the HP sense for large frames). The value to provide for r4 can
447          --  then also be computed from the one of r3 with the compensation
448          --  for the static part of the frame.
449
450          FP_Adjustment := Integer (Caller_UWD.frame_size * 8);
451          Frame.top_r4  := Address (Integer (Frame.top_r3) + FP_Adjustment);
452
453          return True;
454       end Prepare_For_Unwind_Of;
455
456       -----------------------
457       -- UWD_For_Caller_Of --
458       -----------------------
459
460       function UWD_For_Caller_Of (Frame : not null access CFD) return UWD_Ptr
461       is
462          UWD_Access : UWD_Ptr;
463
464       begin
465          --  First try the most direct path, using the return location data
466          --  associated with the frame.
467
468          UWD_Access := UWD_For_RLO_Of (Frame);
469
470          if UWD_Access /= null then
471             return UWD_Access;
472          end if;
473
474          --  If we did not get a result, we might face an in-stub return
475          --  address. In this case U_get_previous_frame can tell us what the
476          --  first not-in-stub return point is. We cannot call it directly,
477          --  though, because we haven't computed the potentially necessary
478          --  frame pointer adjustments, which might lead to SEGV in some
479          --  circumstances. Instead, we directly call the libcl routine which
480          --  is called by U_get_previous_frame and which only requires few
481          --  information. Take care, however, that the information is provided
482          --  in the "current" argument, so we need to work on a copy to avoid
483          --  disturbing our caller.
484
485          declare
486             U_Current  : aliased CFD := Frame.all;
487             U_Previous : aliased PFD;
488
489          begin
490             U_Previous.prev_dp  := U_Current.cur_dp;
491             U_Previous.prev_rls := U_Current.cur_rls;
492             U_Previous.prev_sp  := U_Current.cur_sp - U_Current.cur_fsz;
493
494             if U_get_u_rlo (U_Current'Access, U_Previous'Access) /= -1 then
495                UWD_Access := UWD_For_RLO_Of (U_Current'Access);
496             end if;
497          end;
498
499          return UWD_Access;
500       end UWD_For_Caller_Of;
501
502       --------------------
503       -- UWD_For_RLO_Of --
504       --------------------
505
506       function UWD_For_RLO_Of (Frame : not null access CFD) return UWD_Ptr
507       is
508          UWD_Address : Address;
509
510          --  The addresses returned by the library point to full descriptors
511          --  including the frame information bits but also the applicable PC
512          --  range. We need to account for this.
513
514          Frame_Info_Offset  : constant := 8;
515
516       begin
517          --  First try to locate the descriptor in the program's unwind table
518
519          UWD_Address := U_get_unwind_entry (Frame.cur_rlo,
520                                             Frame.cur_rls,
521                                             Program_UWT.Table_Start,
522                                             Program_UWT.Table_End);
523
524          --  If we did not get it, we might have a frame from code in a
525          --  stub or shared library. For code in stub we would have to
526          --  compute the first non-stub return location but this is not
527          --  the role of this subprogram, so let's just try to see if we
528          --  can get a result from the tables in shared libraries.
529
530          if UWD_Address = -1
531            and then U_is_shared_pc (Frame.cur_rlo, Frame.cur_r19) /= 0
532          then
533             declare
534                Shlib_UWT   : constant UWT     :=
535                                U_get_shLib_unwind_table (Frame.cur_r19);
536                Shlib_Start : constant Address :=
537                                U_get_shLib_text_addr (Frame.cur_r19);
538                Rlo_Offset  : constant Address :=
539                                Frame.cur_rlo - Shlib_Start;
540             begin
541                UWD_Address := U_get_unwind_entry (Rlo_Offset,
542                                                   Frame.cur_rls,
543                                                   Shlib_UWT.Table_Start,
544                                                   Shlib_UWT.Table_End);
545             end;
546          end if;
547
548          if UWD_Address /= -1 then
549             return To_UWD_Access (UWD_Address + Frame_Info_Offset);
550          else
551             return null;
552          end if;
553       end UWD_For_RLO_Of;
554
555    --  Start of processing for Call_Chain
556
557    begin
558       --  Fetch the state for this subprogram's frame and pop it so that we
559       --  start with an initial out_rlo "here".
560
561       U_init_frame_record (Frame'Access);
562       Frame.top_sr0 := 0;
563       Frame.top_sr4 := 0;
564
565       U_prep_frame_rec_for_unwind (Frame'Access);
566
567       Pop_Success := Pop_Frame (Frame'Access);
568
569       --  Skip the requested number of frames
570
571       for I in 1 .. Skip_Frames loop
572          Pop_Success := Pop_Frame (Frame'Access);
573       end loop;
574
575       --  Loop popping frames and storing locations until either a problem
576       --  occurs, or the top of the call chain is reached, or the provided
577       --  array is full.
578
579       loop
580          --  We have to test some conditions against the return location
581          --  as it is returned, so get it as is first.
582
583          Code := Frame.out_rlo;
584
585          exit when not Pop_Success or else Code = 0 or else J = Max_Len + 1;
586
587          --  Compute the call point from the retrieved return location :
588          --  Mask the privilege bits and account for the delta between the
589          --  call site and the return point.
590
591          Code := (Code and not Priv_Mask) - Rlo_Offset;
592
593          if Code < Exclude_Min or else Code > Exclude_Max then
594             Trace (J) := Code;
595             J := J + 1;
596          end if;
597
598          Pop_Success := Pop_Frame (Frame'Access);
599       end loop;
600
601       Len := J - 1;
602    end Call_Chain;
603
604 end System.Traceback;