OSDN Git Service

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