Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / ada / vxaddr2line.adb
blob6d79cfc5c4833213e24b61cd032db3846c1dd6dd
1 ------------------------------------------------------------------------------
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-2004 Ada Core Technologies, Inc. --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 -- This program is meant to be used with vxworks to compute symbolic
28 -- backtraces on the host from non-symbolic backtraces obtained on the target.
30 -- The basic idea is to automate the computation of the necessary address
31 -- adjustments prior to calling addr2line when the application has only been
32 -- partially linked on the host.
34 -- Variants for various targets are supported, and the command line should
35 -- be like :
37 -- <target>-addr2line [-a <target_arch>] <exe_file> <ref_address>
38 -- <backtrace addresses>
40 -- Where:
41 -- <target_arch> :
42 -- selects the target architecture. In the absence of this parameter the
43 -- default variant is chosen based on the Detect_Arch result. Generally,
44 -- this parameter will only be used if vxaddr2line is recompiled manually.
45 -- Otherwise, the command name will always be of the form
46 -- <target>-vxaddr2line where there is no ambiguity on the target's
47 -- architecture.
49 -- <exe_file> :
50 -- The name of the partially linked binary file for the application.
52 -- <ref_address> :
53 -- Runtime address (on the target) of a reference symbol you choose,
54 -- which name shall match the value of the Ref_Symbol variable declared
55 -- below. A symbol with a small offset from the beginning of the text
56 -- segment is better, so "adainit" is a good choice.
58 -- <backtrace addresses> :
59 -- The call chain addresses you obtained at run time on the target and
60 -- for which you want a symbolic association.
62 -- TO ADD A NEW ARCHITECTURE add an appropriate value to Architecture type
63 -- (in a format <host>_<target>), and then an appropriate value to Config_List
64 -- array
66 with Text_IO; use Text_IO;
67 with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
68 with Ada.Command_Line; use Ada.Command_Line;
69 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
71 with GNAT.OS_Lib; use GNAT.OS_Lib;
72 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
73 with GNAT.Expect; use GNAT.Expect;
74 with GNAT.Regpat; use GNAT.Regpat;
76 procedure VxAddr2Line is
78 Ref_Symbol : constant String := "adainit";
79 -- This is the name of the reference symbol which runtime address shall
80 -- be provided as the <ref_address> argument.
82 -- All supported architectures
83 type Architecture is
84 (SOLARIS_I586,
85 WINDOWS_POWERPC,
86 WINDOWS_M68K,
87 SOLARIS_POWERPC,
88 DEC_ALPHA);
90 type Arch_Record is record
91 Addr2line_Binary : String_Access;
92 -- Name of the addr2line utility to use.
94 Nm_Binary : String_Access;
95 -- Name of the host nm utility, which will be used to find out the
96 -- offset of the reference symbol in the text segment of the partially
97 -- linked executable.
99 Addr_Digits_To_Skip : Integer;
100 -- When addresses such as 0xfffffc0001dfed50 are provided, for instance
101 -- on ALPHA, indicate the number of leading digits that can be ignored,
102 -- which will avoid computational overflows. Typically only useful when
103 -- 64bit addresses are provided.
105 Bt_Offset_From_Call : Integer;
106 -- Offset from a backtrace address to the address of the corresponding
107 -- call instruction. This should always be 0, except on platforms where
108 -- the backtrace addresses actually correspond to return and not call
109 -- points. In such cases, a negative value is most likely.
110 end record;
112 -- Configuration for each of the architectures
113 Arch_List : array (Architecture'Range) of Arch_Record :=
114 (WINDOWS_POWERPC =>
115 (Addr2line_Binary => null,
116 Nm_Binary => null,
117 Addr_Digits_To_Skip => 0,
118 Bt_Offset_From_Call => -4),
119 WINDOWS_M68K =>
120 (Addr2line_Binary => null,
121 Nm_Binary => null,
122 Addr_Digits_To_Skip => 0,
123 Bt_Offset_From_Call => -4),
124 SOLARIS_POWERPC =>
125 (Addr2line_Binary => null,
126 Nm_Binary => null,
127 Addr_Digits_To_Skip => 0,
128 Bt_Offset_From_Call => 0),
129 SOLARIS_I586 =>
130 (Addr2line_Binary => null,
131 Nm_Binary => null,
132 Addr_Digits_To_Skip => 0,
133 Bt_Offset_From_Call => -2),
134 DEC_ALPHA =>
135 (Addr2line_Binary => null,
136 Nm_Binary => null,
137 Addr_Digits_To_Skip => 8,
138 Bt_Offset_From_Call => 0)
141 -- Current architecture
142 Cur_Arch : Architecture;
144 -- State of architecture detection
145 Detect_Success : Boolean := False;
147 -----------------------
148 -- Local subprograms --
149 -----------------------
151 procedure Error (Msg : String);
152 pragma No_Return (Error);
153 -- Prints the message and then terminates the program
155 procedure Usage;
156 -- Displays the short help message and then terminates the program
158 function Get_Reference_Offset return Integer;
159 -- Computes the static offset of the reference symbol by calling nm
161 function Get_Value_From_Hex_Arg (Arg : Natural) return Integer;
162 -- Threats the argument number Arg as a C-style hexadecimal literal
163 -- and returns its integer value
165 function Hex_Image (Value : Integer) return String_Access;
166 -- Returns access to a string that contains hexadecimal image of Value
168 -- Separate functions that provide build-time customization:
170 procedure Detect_Arch;
171 -- Saves in Cur_Arch the current architecture, based on the name of
172 -- vxaddr2line instance and properties of the host. Detect_Success is False
173 -- if detection fails
175 -----------------
176 -- Detect_Arch --
177 -----------------
179 procedure Detect_Arch is
180 Name : constant String := Base_Name (Command_Name);
181 Proc : constant String :=
182 Name (Name'First .. Index (Name, "-") - 1);
183 Target : constant String :=
184 Name (Name'First .. Index (Name, "vxaddr2line") - 1);
186 begin
187 Detect_Success := False;
189 if Proc = "" then
190 return;
191 end if;
193 if Proc = "alpha" then
194 Cur_Arch := DEC_ALPHA;
195 else
196 -- Let's detect the host.
197 -- ??? A naive implementation that can't distinguish between Unixes
198 if Directory_Separator = '/' then
199 Cur_Arch := Architecture'Value ("solaris_" & Proc);
200 else
201 Cur_Arch := Architecture'Value ("windows_" & Proc);
202 end if;
203 end if;
205 if Arch_List (Cur_Arch).Addr2line_Binary = null then
206 Arch_List (Cur_Arch).Addr2line_Binary := new String'
207 (Target & "addr2line");
208 end if;
209 if Arch_List (Cur_Arch).Nm_Binary = null then
210 Arch_List (Cur_Arch).Nm_Binary := new String'
211 (Target & "nm");
212 end if;
214 Detect_Success := True;
216 exception
217 when others =>
218 return;
219 end Detect_Arch;
221 -----------
222 -- Error --
223 -----------
225 procedure Error (Msg : String) is
226 begin
227 Put_Line (Msg);
228 OS_Exit (1);
229 raise Program_Error;
230 end Error;
232 --------------------------
233 -- Get_Reference_Offset --
234 --------------------------
236 function Get_Reference_Offset return Integer is
237 Nm_Cmd : constant String_Access :=
238 Locate_Exec_On_Path (Arch_List (Cur_Arch).Nm_Binary.all);
240 Nm_Args : constant Argument_List :=
241 (new String'("-P"),
242 new String'(Argument (1)));
244 Forever : aliased String := "^@@@@";
245 Reference : aliased String := Ref_Symbol & "\s+\S\s+([\da-fA-F]+)";
247 Pd : Process_Descriptor;
248 Result : Expect_Match;
250 begin
251 -- If Nm is not found, abort
253 if Nm_Cmd = null then
254 Error ("Couldn't find " & Arch_List (Cur_Arch).Nm_Binary.all);
255 end if;
257 Non_Blocking_Spawn
258 (Pd, Nm_Cmd.all, Nm_Args, Buffer_Size => 0, Err_To_Out => True);
260 -- Expect a string containing the reference symbol
262 Expect (Pd, Result,
263 Regexp_Array'(1 => Reference'Unchecked_Access),
264 Timeout => -1);
266 -- If we are here, the pattern was matched successfully
268 declare
269 Match_String : constant String := Expect_Out_Match (Pd);
270 Matches : Match_Array (0 .. 1);
271 Value : Integer;
273 begin
274 Match (Reference, Match_String, Matches);
275 Value := Integer'Value
276 ("16#"
277 & Match_String (Matches (1).First .. Matches (1).Last) & "#");
279 -- Expect a string that will never be emitted, so that the
280 -- process can be correctly terminated (with Process_Died)
282 Expect (Pd, Result,
283 Regexp_Array'(1 => Forever'Unchecked_Access),
284 Timeout => -1);
286 exception
287 when Process_Died =>
288 return Value;
289 end;
291 -- We can not get here
293 raise Program_Error;
295 exception
296 when Invalid_Process =>
297 Error ("Could not spawn a process " & Nm_Cmd.all);
299 when others =>
301 -- The process died without matching the reference symbol or the
302 -- format wasn't recognized.
304 Error ("Unexpected output from " & Nm_Cmd.all);
305 end Get_Reference_Offset;
307 ----------------------------
308 -- Get_Value_From_Hex_Arg --
309 ----------------------------
311 function Get_Value_From_Hex_Arg (Arg : Natural) return Integer is
312 Cur_Arg : constant String := Argument (Arg);
313 Offset : Natural;
315 begin
316 -- Skip "0x" prefix if present
318 if Cur_Arg'Length > 2 and then Cur_Arg (1 .. 2) = "0x" then
319 Offset := 3;
320 else
321 Offset := 1;
322 end if;
324 -- Add architecture-specific offset
326 Offset := Offset + Arch_List (Cur_Arch).Addr_Digits_To_Skip;
328 -- Convert to value
330 return Integer'Value ("16#" & Cur_Arg (Offset .. Cur_Arg'Last) & "#");
331 end Get_Value_From_Hex_Arg;
333 ---------------
334 -- Hex_Image --
335 ---------------
337 function Hex_Image (Value : Integer) return String_Access is
338 Result : String (1 .. 20);
339 Start_Pos : Natural;
341 begin
342 Put (Result, Value, 16);
343 Start_Pos := Index (Result, "16#") + 3;
344 return new String'(Result (Start_Pos .. Result'Last - 1));
345 end Hex_Image;
347 -----------
348 -- Usage --
349 -----------
351 procedure Usage is
352 begin
353 Put_Line ("Usage : " & Base_Name (Command_Name)
354 & " <executable> <"
355 & Ref_Symbol & " offset on target> <addr1> ...");
357 OS_Exit (1);
358 end Usage;
360 Ref_Static_Offset, Ref_Runtime_Address, Bt_Address : Integer;
362 Addr2line_Cmd : String_Access;
364 Addr2line_Args : Argument_List (1 .. 501);
365 -- We expect that there won't be more than 500 backtrace frames
367 Addr2line_Args_Count : Natural;
369 Success : Boolean;
371 -- Start of processing for VxAddr2Line
373 begin
375 Detect_Arch;
377 -- There should be at least two arguments
379 if Argument_Count < 2 then
380 Usage;
381 end if;
383 -- ??? HARD LIMIT! There should be at most 501 arguments
385 if Argument_Count > 501 then
386 Error ("Too many backtrace frames");
387 end if;
389 -- Do we have a valid architecture?
391 if not Detect_Success then
392 Put_Line ("Couldn't detect the architecture");
393 return;
394 end if;
396 Addr2line_Cmd :=
397 Locate_Exec_On_Path (Arch_List (Cur_Arch).Addr2line_Binary.all);
399 -- If Addr2line is not found, abort
401 if Addr2line_Cmd = null then
402 Error ("Couldn't find " & Arch_List (Cur_Arch).Addr2line_Binary.all);
403 end if;
405 -- The first argument specifies the image file. Check if it exists.
407 if not Is_Regular_File (Argument (1)) then
408 Error ("Couldn't find the executable " & Argument (1));
409 end if;
411 -- The second argument specifies the reference symbol runtime address.
412 -- Let's parse and store it
414 Ref_Runtime_Address := Get_Value_From_Hex_Arg (2);
416 -- Run nm command to get the reference symbol static offset
418 Ref_Static_Offset := Get_Reference_Offset;
420 -- Build addr2line parameters. First, the standard part
422 Addr2line_Args (1) := new String'("--exe=" & Argument (1));
423 Addr2line_Args_Count := 1;
425 -- Now, append to this the adjusted backtraces in arguments 4 and further
427 for J in 3 .. Argument_Count loop
429 -- Basically, for each address in the runtime backtrace ...
431 -- o We compute its offset relatively to the runtime address of the
432 -- reference symbol,
434 -- and then ...
436 -- o We add this offset to the static one for the reference symbol in
437 -- the executable to find the executable offset corresponding to the
438 -- backtrace address.
440 Bt_Address := Get_Value_From_Hex_Arg (J);
442 Bt_Address :=
443 Bt_Address - Ref_Runtime_Address
444 + Ref_Static_Offset
445 + Arch_List (Cur_Arch).Bt_Offset_From_Call;
447 Addr2line_Args_Count := Addr2line_Args_Count + 1;
448 Addr2line_Args (Addr2line_Args_Count) := Hex_Image (Bt_Address);
449 end loop;
451 -- Run the resulting command
453 Spawn (Addr2line_Cmd.all,
454 Addr2line_Args (1 .. Addr2line_Args_Count), Success);
456 exception
457 when others =>
459 -- Mask all exceptions
461 return;
462 end VxAddr2Line;