OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [gcc/] [ada/] [vxaddr2line.adb] - Blame information for rev 404

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 281 jeremybenn
------------------------------------------------------------------------------
2
--                                                                          --
3
--                         GNAT COMPILER COMPONENTS                         --
4
--                                                                          --
5
--                           V X A D D R 2 L I N E                          --
6
--                                                                          --
7
--                                 B o d y                                  --
8
--                                                                          --
9
--                     Copyright (C) 2002-2009, AdaCore                     --
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 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.  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 COPYING3.  If not, go to --
19
-- http://www.gnu.org/licenses for a complete copy of the license.          --
20
--                                                                          --
21
-- GNAT was originally developed  by the GNAT team at  New York University. --
22
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
23
--                                                                          --
24
------------------------------------------------------------------------------
25
 
26
--  This program is meant to be used with vxworks to compute symbolic
27
--  backtraces on the host from non-symbolic backtraces obtained on the target.
28
 
29
--  The basic idea is to automate the computation of the necessary address
30
--  adjustments prior to calling addr2line when the application has only been
31
--  partially linked on the host.
32
 
33
--  Variants for various targets are supported, and the command line should
34
--  be like :
35
 
36
--  <target>-addr2line [-a <target_arch>] <exe_file> <ref_address>
37
--                     <backtrace addresses>
38
 
39
--  Where:
40
--  <target_arch> :
41
--    selects the target architecture. In the absence of this parameter the
42
--    default variant is chosen based on the Detect_Arch result. Generally,
43
--    this parameter will only be used if vxaddr2line is recompiled manually.
44
--    Otherwise, the command name will always be of the form
45
--    <target>-vxaddr2line where there is no ambiguity on the target's
46
--    architecture.
47
 
48
--  <exe_file> :
49
--    The name of the partially linked binary file for the application.
50
 
51
--  <ref_address> :
52
--    Runtime address (on the target) of a reference symbol you choose,
53
--    which name shall match the value of the Ref_Symbol variable declared
54
--    below. A symbol with a small offset from the beginning of the text
55
--    segment is better, so "adainit" is a good choice.
56
 
57
--  <backtrace addresses> :
58
--    The call chain addresses you obtained at run time on the target and
59
--    for which you want a symbolic association.
60
 
61
--  TO ADD A NEW ARCHITECTURE add an appropriate value to Architecture type
62
--  (in a format <host>_<target>), and then an appropriate value to Config_List
63
--  array
64
 
65
with Ada.Text_IO;       use Ada.Text_IO;
66
with Ada.Command_Line;  use Ada.Command_Line;
67
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
68
with Interfaces;        use Interfaces;
69
 
70
with GNAT.OS_Lib;               use GNAT.OS_Lib;
71
with GNAT.Directory_Operations; use GNAT.Directory_Operations;
72
with GNAT.Expect;               use GNAT.Expect;
73
with GNAT.Regpat;               use GNAT.Regpat;
74
 
75
procedure VxAddr2Line is
76
 
77
   package Unsigned_32_IO is new Modular_IO (Unsigned_32);
78
   --  Instantiate Modular_IO to have Put
79
 
80
   Ref_Symbol : constant String := "adainit";
81
   --  This is the name of the reference symbol which runtime address shall
82
   --  be provided as the <ref_address> argument.
83
 
84
   --  All supported architectures
85
   type Architecture is
86
     (SOLARIS_I586,
87
      WINDOWS_POWERPC,
88
      WINDOWS_I586,
89
      WINDOWS_M68K,
90
      SOLARIS_POWERPC,
91
      DEC_ALPHA);
92
 
93
   type Arch_Record is record
94
      Addr2line_Binary : String_Access;
95
      --  Name of the addr2line utility to use
96
 
97
      Nm_Binary : String_Access;
98
      --  Name of the host nm utility, which will be used to find out the
99
      --  offset of the reference symbol in the text segment of the partially
100
      --  linked executable.
101
 
102
      Addr_Digits_To_Skip : Integer;
103
      --  When addresses such as 0xfffffc0001dfed50 are provided, for instance
104
      --  on ALPHA, indicate the number of leading digits that can be ignored,
105
      --  which will avoid computational overflows. Typically only useful when
106
      --  64bit addresses are provided.
107
 
108
      Bt_Offset_From_Call : Unsigned_32;
109
      --  Offset from a backtrace address to the address of the corresponding
110
      --  call instruction. This should always be 0, except on platforms where
111
      --  the backtrace addresses actually correspond to return and not call
112
      --  points. In such cases, a negative value is most likely.
113
   end record;
114
 
115
   --  Configuration for each of the architectures
116
   Arch_List : array (Architecture'Range) of Arch_Record :=
117
     (WINDOWS_POWERPC =>
118
        (Addr2line_Binary    => null,
119
         Nm_Binary           => null,
120
         Addr_Digits_To_Skip => 0,
121
         Bt_Offset_From_Call => -4),
122
      WINDOWS_M68K =>
123
        (Addr2line_Binary    => null,
124
         Nm_Binary           => null,
125
         Addr_Digits_To_Skip => 0,
126
         Bt_Offset_From_Call => -4),
127
      WINDOWS_I586 =>
128
        (Addr2line_Binary    => null,
129
         Nm_Binary           => null,
130
         Addr_Digits_To_Skip => 0,
131
         Bt_Offset_From_Call => -2),
132
      SOLARIS_POWERPC =>
133
        (Addr2line_Binary    => null,
134
         Nm_Binary           => null,
135
         Addr_Digits_To_Skip => 0,
136
         Bt_Offset_From_Call => 0),
137
      SOLARIS_I586 =>
138
        (Addr2line_Binary    => null,
139
         Nm_Binary           => null,
140
         Addr_Digits_To_Skip => 0,
141
         Bt_Offset_From_Call => -2),
142
      DEC_ALPHA =>
143
        (Addr2line_Binary    => null,
144
         Nm_Binary           => null,
145
         Addr_Digits_To_Skip => 8,
146
         Bt_Offset_From_Call => 0)
147
     );
148
 
149
   --  Current architecture
150
   Cur_Arch : Architecture;
151
 
152
   --  State of architecture detection
153
   Detect_Success : Boolean := False;
154
 
155
   -----------------------
156
   -- Local subprograms --
157
   -----------------------
158
 
159
   procedure Error (Msg : String);
160
   pragma No_Return (Error);
161
   --  Prints the message and then terminates the program
162
 
163
   procedure Usage;
164
   --  Displays the short help message and then terminates the program
165
 
166
   function Get_Reference_Offset return Unsigned_32;
167
   --  Computes the static offset of the reference symbol by calling nm
168
 
169
   function Get_Value_From_Hex_Arg (Arg : Natural) return Unsigned_32;
170
   --  Threats the argument number Arg as a C-style hexadecimal literal
171
   --  and returns its integer value
172
 
173
   function Hex_Image (Value : Unsigned_32) return String_Access;
174
   --  Returns access to a string that contains hexadecimal image of Value
175
 
176
   --  Separate functions that provide build-time customization:
177
 
178
   procedure Detect_Arch;
179
   --  Saves in Cur_Arch the current architecture, based on the name of
180
   --  vxaddr2line instance and properties of the host. Detect_Success is False
181
   --  if detection fails
182
 
183
   -----------------
184
   -- Detect_Arch --
185
   -----------------
186
 
187
   procedure Detect_Arch is
188
      Name   : constant String := Base_Name (Command_Name);
189
      Proc   : constant String :=
190
                 Name (Name'First .. Index (Name, "-") - 1);
191
      Target : constant String :=
192
                 Name (Name'First .. Index (Name, "vxaddr2line") - 1);
193
 
194
   begin
195
      Detect_Success := False;
196
 
197
      if Proc = "" then
198
         return;
199
      end if;
200
 
201
      if Proc = "alpha" then
202
         Cur_Arch := DEC_ALPHA;
203
      else
204
         --  Let's detect the host.
205
         --  ??? A naive implementation that can't distinguish between Unixes
206
         if Directory_Separator = '/' then
207
            Cur_Arch := Architecture'Value ("solaris_" & Proc);
208
         else
209
            Cur_Arch := Architecture'Value ("windows_" & Proc);
210
         end if;
211
      end if;
212
 
213
      if Arch_List (Cur_Arch).Addr2line_Binary = null then
214
         Arch_List (Cur_Arch).Addr2line_Binary := new String'
215
           (Target & "addr2line");
216
      end if;
217
      if Arch_List (Cur_Arch).Nm_Binary = null then
218
         Arch_List (Cur_Arch).Nm_Binary := new String'
219
           (Target & "nm");
220
      end if;
221
 
222
      Detect_Success := True;
223
 
224
   exception
225
      when others =>
226
         return;
227
   end Detect_Arch;
228
 
229
   -----------
230
   -- Error --
231
   -----------
232
 
233
   procedure Error (Msg : String) is
234
   begin
235
      Put_Line (Msg);
236
      OS_Exit (1);
237
      raise Program_Error;
238
   end Error;
239
 
240
   --------------------------
241
   -- Get_Reference_Offset --
242
   --------------------------
243
 
244
   function Get_Reference_Offset return Unsigned_32 is
245
      Nm_Cmd  : constant String_Access :=
246
                  Locate_Exec_On_Path (Arch_List (Cur_Arch).Nm_Binary.all);
247
 
248
      Nm_Args : constant Argument_List :=
249
                  (new String'("-P"),
250
                   new String'(Argument (1)));
251
 
252
      Forever   : aliased String := "^@@@@";
253
      Reference : aliased String := Ref_Symbol & "\s+\S\s+([\da-fA-F]+)";
254
 
255
      Pd     : Process_Descriptor;
256
      Result : Expect_Match;
257
 
258
   begin
259
      --  If Nm is not found, abort
260
 
261
      if Nm_Cmd = null then
262
         Error ("Couldn't find " & Arch_List (Cur_Arch).Nm_Binary.all);
263
      end if;
264
 
265
      Non_Blocking_Spawn
266
        (Pd, Nm_Cmd.all, Nm_Args, Buffer_Size => 0, Err_To_Out => True);
267
 
268
      --  Expect a string containing the reference symbol
269
 
270
      Expect (Pd, Result,
271
              Regexp_Array'(1 => Reference'Unchecked_Access),
272
              Timeout => -1);
273
 
274
      --  If we are here, the pattern was matched successfully
275
 
276
      declare
277
         Match_String : constant String := Expect_Out_Match (Pd);
278
         Matches      : Match_Array (0 .. 1);
279
         Value        : Unsigned_32;
280
 
281
      begin
282
         Match (Reference, Match_String, Matches);
283
         Value := Unsigned_32'Value
284
           ("16#"
285
            & Match_String (Matches (1).First .. Matches (1).Last) & "#");
286
 
287
         --  Expect a string that will never be emitted, so that the
288
         --  process can be correctly terminated (with Process_Died)
289
 
290
         Expect (Pd, Result,
291
                 Regexp_Array'(1 => Forever'Unchecked_Access),
292
                 Timeout => -1);
293
 
294
      exception
295
         when Process_Died =>
296
            return Value;
297
      end;
298
 
299
      --  We cannot get here
300
 
301
      raise Program_Error;
302
 
303
   exception
304
      when Invalid_Process =>
305
         Error ("Could not spawn a process " & Nm_Cmd.all);
306
 
307
      when others    =>
308
 
309
         --  The process died without matching the reference symbol or the
310
         --  format wasn't recognized.
311
 
312
         Error ("Unexpected output from " & Nm_Cmd.all);
313
   end Get_Reference_Offset;
314
 
315
   ----------------------------
316
   -- Get_Value_From_Hex_Arg --
317
   ----------------------------
318
 
319
   function Get_Value_From_Hex_Arg (Arg : Natural) return Unsigned_32 is
320
      Cur_Arg : constant String := Argument (Arg);
321
      Offset  : Natural;
322
 
323
   begin
324
      --  Skip "0x" prefix if present
325
 
326
      if Cur_Arg'Length > 2 and then Cur_Arg (1 .. 2) = "0x" then
327
         Offset := 3;
328
      else
329
         Offset := 1;
330
      end if;
331
 
332
      --  Add architecture-specific offset
333
 
334
      Offset := Offset + Arch_List (Cur_Arch).Addr_Digits_To_Skip;
335
 
336
      --  Convert to value
337
 
338
      return Unsigned_32'Value
339
        ("16#" & Cur_Arg (Offset .. Cur_Arg'Last) & "#");
340
 
341
   exception
342
      when Constraint_Error =>
343
 
344
         Error ("Can't parse backtrace address '" & Cur_Arg & "'");
345
         raise;
346
   end Get_Value_From_Hex_Arg;
347
 
348
   ---------------
349
   -- Hex_Image --
350
   ---------------
351
 
352
   function Hex_Image (Value : Unsigned_32) return String_Access is
353
      Result    : String (1 .. 20);
354
      Start_Pos : Natural;
355
 
356
   begin
357
      Unsigned_32_IO.Put (Result, Value, 16);
358
      Start_Pos := Index (Result, "16#") + 3;
359
      return new String'(Result (Start_Pos .. Result'Last - 1));
360
   end Hex_Image;
361
 
362
   -----------
363
   -- Usage --
364
   -----------
365
 
366
   procedure Usage is
367
   begin
368
      Put_Line ("Usage : " & Base_Name (Command_Name)
369
                & " <executable> <"
370
                & Ref_Symbol & " offset on target> <addr1> ...");
371
 
372
      OS_Exit (1);
373
   end Usage;
374
 
375
   Ref_Static_Offset, Ref_Runtime_Address, Bt_Address : Unsigned_32;
376
 
377
   Addr2line_Cmd : String_Access;
378
 
379
   Addr2line_Args : Argument_List (1 .. 501);
380
   --  We expect that there won't be more than 500 backtrace frames
381
 
382
   Addr2line_Args_Count : Natural;
383
 
384
   Success : Boolean;
385
 
386
--  Start of processing for VxAddr2Line
387
 
388
begin
389
 
390
   Detect_Arch;
391
 
392
   --  There should be at least two arguments
393
 
394
   if Argument_Count < 2 then
395
      Usage;
396
   end if;
397
 
398
   --  ??? HARD LIMIT! There should be at most 501 arguments
399
 
400
   if Argument_Count > 501 then
401
      Error ("Too many backtrace frames");
402
   end if;
403
 
404
   --  Do we have a valid architecture?
405
 
406
   if not Detect_Success then
407
      Put_Line ("Couldn't detect the architecture");
408
      return;
409
   end if;
410
 
411
   Addr2line_Cmd :=
412
     Locate_Exec_On_Path (Arch_List (Cur_Arch).Addr2line_Binary.all);
413
 
414
   --  If Addr2line is not found, abort
415
 
416
   if Addr2line_Cmd = null then
417
      Error ("Couldn't find " & Arch_List (Cur_Arch).Addr2line_Binary.all);
418
   end if;
419
 
420
   --  The first argument specifies the image file. Check if it exists
421
 
422
   if not Is_Regular_File (Argument (1)) then
423
      Error ("Couldn't find the executable " & Argument (1));
424
   end if;
425
 
426
   --  The second argument specifies the reference symbol runtime address.
427
   --  Let's parse and store it
428
 
429
   Ref_Runtime_Address := Get_Value_From_Hex_Arg (2);
430
 
431
   --  Run nm command to get the reference symbol static offset
432
 
433
   Ref_Static_Offset := Get_Reference_Offset;
434
 
435
   --  Build addr2line parameters. First, the standard part
436
 
437
   Addr2line_Args (1) := new String'("--exe=" & Argument (1));
438
   Addr2line_Args_Count := 1;
439
 
440
   --  Now, append to this the adjusted backtraces in arguments 4 and further
441
 
442
   for J in 3 .. Argument_Count loop
443
 
444
      --  Basically, for each address in the runtime backtrace ...
445
 
446
      --  o We compute its offset relatively to the runtime address of the
447
      --    reference symbol,
448
 
449
      --  and then ...
450
 
451
      --  o We add this offset to the static one for the reference symbol in
452
      --    the executable to find the executable offset corresponding to the
453
      --    backtrace address.
454
 
455
      Bt_Address := Get_Value_From_Hex_Arg (J);
456
 
457
      Bt_Address :=
458
        Bt_Address - Ref_Runtime_Address
459
                   + Ref_Static_Offset
460
                   + Arch_List (Cur_Arch).Bt_Offset_From_Call;
461
 
462
      Addr2line_Args_Count := Addr2line_Args_Count + 1;
463
      Addr2line_Args (Addr2line_Args_Count) := Hex_Image (Bt_Address);
464
   end loop;
465
 
466
   --  Run the resulting command
467
 
468
   Spawn (Addr2line_Cmd.all,
469
          Addr2line_Args (1 .. Addr2line_Args_Count), Success);
470
 
471
   if not Success then
472
      Error ("Couldn't spawn " & Addr2line_Cmd.all);
473
   end if;
474
 
475
exception
476
   when others =>
477
 
478
      --  Mask all exceptions
479
 
480
      return;
481
end VxAddr2Line;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.