summaryrefslogtreecommitdiff
path: root/gcc/ada/g-trasym-vms-ia64.adb
blob: 897e2ebb24ae9170913907e585120b78e8b5a8c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT RUN-TIME COMPONENTS                         --
--                                                                          --
--             G N A T . T R A C E B A C K . S Y M B O L I C                --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--         Copyright (C) 2005-2010, Free Software Foundation, Inc.          --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

--  Run-time symbolic traceback support for IA64/VMS

with Ada.Exceptions.Traceback; use Ada.Exceptions.Traceback;
with System;
with System.Aux_DEC;
with System.Soft_Links;
with System.Traceback_Entries;

package body GNAT.Traceback.Symbolic is

   use System;
   use System.Aux_DEC;
   use System.Traceback_Entries;

   subtype Var_String_Buf is String (1 .. 254);

   type Var_String is record
      Curlen : Unsigned_Word := 0;
      Buf    : Var_String_Buf;
   end record;
   pragma Convention (C, Var_String);
   for Var_String'Size use 8 * 256;

   type Descriptor64 is record
      Mbo       : Unsigned_Word;
      Dtype     : Unsigned_Byte;
      Class     : Unsigned_Byte;
      Mbmo      : Unsigned_Longword;
      Maxstrlen : Integer_64;
      Pointer   : Address;
   end record;
   pragma Convention (C, Descriptor64);

   subtype Cond_Value_Type is Unsigned_Longword;

   --  TBK_API_PARAM as defined in TBKDEF

   type Tbk_Api_Param is record
      Length              : Unsigned_Word;
      T_Type              : Unsigned_Byte;
      Version             : Unsigned_Byte;
      Reserveda           : Unsigned_Longword;
      Faulting_Pc         : Address;
      Faulting_Fp         : Address;
      Filename_Desc       : Address;
      Library_Module_Desc : Address;
      Record_Number       : Address;
      Image_Desc          : Address;
      Module_Desc         : Address;
      Routine_Desc        : Address;
      Listing_Lineno      : Address;
      Rel_Pc              : Address;
      Image_Base_Addr     : Address;
      Module_Base_Addr    : Address;
      Malloc_Rtn          : Address;
      Free_Rtn            : Address;
      Symbolize_Flags     : Address;
      Reserved0           : Unsigned_Quadword;
      Reserved1           : Unsigned_Quadword;
      Reserved2           : Unsigned_Quadword;
   end record;
   pragma Convention (C, Tbk_Api_Param);

   K_Version : constant Unsigned_Byte := 1;
   --  Current API version

   K_Length : constant Unsigned_Word := 152;
   --  Length of the parameter

   pragma Compile_Time_Error (Tbk_Api_Param'Size = K_Length * 8,
                              "Bad length for tbk_api_param");
   --  Sanity check

   function Symbolize (Param : Address) return Cond_Value_Type;
   pragma Import (C, Symbolize, "TBK$I64_SYMBOLIZE");

   function Decode_Ada_Name (Encoded_Name : String) return String;
   --  Decodes an Ada identifier name. Removes leading "_ada_" and trailing
   --  __{DIGIT}+ or ${DIGIT}+, converts other "__" to '.'

   procedure Setup_Descriptor64_Vs (Desc : out Descriptor64; Var : Address);
   --  Setup descriptor Desc for address Var

   ---------------------
   -- Decode_Ada_Name --
   ---------------------

   function Decode_Ada_Name (Encoded_Name : String) return String is
      Decoded_Name : String (1 .. Encoded_Name'Length);
      Pos          : Integer := Encoded_Name'First;
      Last         : Integer := Encoded_Name'Last;
      DPos         : Integer := 1;

   begin
      if Pos > Last then
         return "";
      end if;

      --  Skip leading _ada_

      if Encoded_Name'Length > 4
        and then Encoded_Name (Pos .. Pos + 4) = "_ada_"
      then
         Pos := Pos + 5;
      end if;

      --  Skip trailing __{DIGIT}+ or ${DIGIT}+

      if Encoded_Name (Last) in '0' .. '9' then
         for J in reverse Pos + 2 .. Last - 1 loop
            case Encoded_Name (J) is
               when '0' .. '9' =>
                  null;

               when '$' =>
                  Last := J - 1;
                  exit;

               when '_' =>
                  if Encoded_Name (J - 1) = '_' then
                     Last := J - 2;
                  end if;
                  exit;

               when others =>
                  exit;
            end case;
         end loop;
      end if;

      --  Now just copy encoded name to decoded name, converting "__" to '.'

      while Pos <= Last loop
         if Encoded_Name (Pos) = '_' and then Encoded_Name (Pos + 1) = '_'
           and then Pos /= Encoded_Name'First
         then
            Decoded_Name (DPos) := '.';
            Pos := Pos + 2;
         else
            Decoded_Name (DPos) := Encoded_Name (Pos);
            Pos := Pos + 1;
         end if;

         DPos := DPos + 1;
      end loop;

      return Decoded_Name (1 .. DPos - 1);
   end Decode_Ada_Name;

   ---------------------------
   -- Setup_Descriptor64_Vs --
   ---------------------------

   procedure Setup_Descriptor64_Vs (Desc : out Descriptor64; Var : Address) is
      K_Dtype_Vt : constant Unsigned_Byte := 37;
      K_Class_Vs : constant Unsigned_Byte := 11;
   begin
      Desc.Mbo := 1;
      Desc.Dtype := K_Dtype_Vt;
      Desc.Class := K_Class_Vs;
      Desc.Mbmo := -1;
      Desc.Maxstrlen := Integer_64 (Var_String_Buf'Length);
      Desc.Pointer := Var;
   end Setup_Descriptor64_Vs;

   ------------------------
   -- Symbolic_Traceback --
   ------------------------

   function Symbolic_Traceback (Traceback : Tracebacks_Array) return String is
      Param         : Tbk_Api_Param;
      Status        : Cond_Value_Type;
      Record_Number : Unsigned_Longword;
      Image_Name    : Var_String;
      Image_Dsc     : Descriptor64;
      Module_Name   : Var_String;
      Module_Dsc    : Descriptor64;
      Routine_Name  : Var_String;
      Routine_Dsc   : Descriptor64;
      Line_Number   : Unsigned_Longword;
      Res           : String (1 .. 256 * Traceback'Length);
      Len           : Integer;

   begin
      if Traceback'Length = 0 then
         return "";
      end if;

      Len := 0;

      --  Since image computation is not thread-safe we need task lockout

      System.Soft_Links.Lock_Task.all;

      --  Initialize descriptors

      Setup_Descriptor64_Vs (Image_Dsc, Image_Name'Address);
      Setup_Descriptor64_Vs (Module_Dsc, Module_Name'Address);
      Setup_Descriptor64_Vs (Routine_Dsc, Routine_Name'Address);

      for J in Traceback'Range loop
         --  Initialize fields in case they are not written

         Record_Number := 0;
         Line_Number := 0;
         Image_Name.Curlen := 0;
         Module_Name.Curlen := 0;
         Routine_Name.Curlen := 0;

         --  Symbolize

         Param := (Length              => K_Length,
                   T_Type              => 0,
                   Version             => K_Version,
                   Reserveda           => 0,
                   Faulting_Pc         => PC_For (Traceback (J)),
                   Faulting_Fp         => 0,
                   Filename_Desc       => Null_Address,
                   Library_Module_Desc => Null_Address,
                   Record_Number       => Record_Number'Address,
                   Image_Desc          => Image_Dsc'Address,
                   Module_Desc         => Module_Dsc'Address,
                   Routine_Desc        => Routine_Dsc'Address,
                   Listing_Lineno      => Line_Number'Address,
                   Rel_Pc              => Null_Address,
                   Image_Base_Addr     => Null_Address,
                   Module_Base_Addr    => Null_Address,
                   Malloc_Rtn          => Null_Address,
                   Free_Rtn            => Null_Address,
                   Symbolize_Flags     => Null_Address,
                   Reserved0           => (0, 0),
                   Reserved1           => (0, 0),
                   Reserved2           => (0, 0));

         Status := Symbolize (Param'Address);

         --  Check for success (marked by bit 0)

         if (Status rem 2) = 1 then

            --  Success

            if Line_Number = 0 then

               --  As GCC doesn't emit source file correlation, use record
               --  number of line number is not set

               Line_Number := Record_Number;
            end if;

            declare
               First : constant Integer := Len + 1;
               Last  : Integer := First + 80 - 1;
               Pos   : Integer;

               Routine_Name_D : constant String :=
                                  Decode_Ada_Name
                                    (Routine_Name.Buf
                                      (1 .. Natural (Routine_Name.Curlen)));

               Lineno : constant String :=
                          Unsigned_Longword'Image (Line_Number);

            begin
               Res (First .. Last) := (others => ' ');

               Res (First .. First + Natural (Image_Name.Curlen) - 1) :=
                 Image_Name.Buf (1 .. Natural (Image_Name.Curlen));

               Res (First + 10 ..
                      First + 10 + Natural (Module_Name.Curlen) - 1) :=
                 Module_Name.Buf (1 .. Natural (Module_Name.Curlen));

               Res (First + 30 ..
                      First + 30 + Routine_Name_D'Length - 1) :=
                 Routine_Name_D;

               --  If routine name doesn't fit 20 characters, output the line
               --  number on next line at 50th position.

               if Routine_Name_D'Length > 20 then
                  Pos := First + 30 + Routine_Name_D'Length;
                  Res (Pos) := ASCII.LF;
                  Last := Pos + 80;
                  Res (Pos + 1 .. Last) := (others => ' ');
                  Pos := Pos + 51;
               else
                  Pos := First + 50;
               end if;

               Res (Pos .. Pos + Lineno'Length - 1) := Lineno;

               Res (Last) := ASCII.LF;
               Len := Last;
            end;

         --  Failure (bit 0 clear)

         else
            Res (Len + 1 .. Len + 6) := "ERROR" & ASCII.LF;
            Len := Len + 6;
         end if;
      end loop;

      System.Soft_Links.Unlock_Task.all;
      return Res (1 .. Len);
   end Symbolic_Traceback;

   function Symbolic_Traceback (E : Exception_Occurrence) return String is
   begin
      return Symbolic_Traceback (Tracebacks (E));
   end Symbolic_Traceback;

end GNAT.Traceback.Symbolic;