2011-03-24 Paolo Bonzini <bonzini@gnu.org>
[official-gcc.git] / gcc / ada / g-trasym.adb
blob105001ddc1d2c13d4754fabc8755fe91d52c4fb7
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- G N A T . T R A C E B A C K . S Y M B O L I C --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1999-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 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- Run-time symbolic traceback support
36 with System.Soft_Links;
37 with Ada.Exceptions.Traceback; use Ada.Exceptions.Traceback;
39 package body GNAT.Traceback.Symbolic is
41 pragma Linker_Options ("-laddr2line");
42 pragma Linker_Options ("-lbfd");
43 pragma Linker_Options ("-liberty");
45 package TSL renames System.Soft_Links;
47 -- To perform the raw addresses to symbolic form translation we rely on a
48 -- libaddr2line symbolizer which examines debug info from a provided
49 -- executable file name, and an absolute path is needed to ensure the file
50 -- is always found. This is "__gnat_locate_exec_on_path (gnat_argv [0])"
51 -- for our executable file, a fairly heavy operation so we cache the
52 -- result.
54 Exename : System.Address;
55 -- Pointer to the name of the executable file to be used on all
56 -- invocations of the libaddr2line symbolization service.
58 Exename_Resolved : Boolean := False;
59 -- Flag to indicate whether we have performed the executable file name
60 -- resolution already. Relying on a not null Exename for this purpose
61 -- would be potentially inefficient as this is what we will get if the
62 -- resolution attempt fails.
64 ------------------------
65 -- Symbolic_Traceback --
66 ------------------------
68 function Symbolic_Traceback (Traceback : Tracebacks_Array) return String is
70 procedure convert_addresses
71 (filename : System.Address;
72 addrs : System.Address;
73 n_addrs : Integer;
74 buf : System.Address;
75 len : System.Address);
76 pragma Import (C, convert_addresses, "convert_addresses");
77 -- This is the procedure version of the Ada-aware addr2line. It places
78 -- in BUF a string representing the symbolic translation of the N_ADDRS
79 -- raw addresses provided in ADDRS, looked up in debug information from
80 -- FILENAME. LEN points to an integer which contains the size of the
81 -- BUF buffer at input and the result length at output.
83 -- This procedure is provided by libaddr2line on targets that support
84 -- it. A dummy version is in adaint.c for other targets so that build
85 -- of shared libraries doesn't generate unresolved symbols.
87 -- Note that this procedure is *not* thread-safe.
89 type Argv_Array is array (0 .. 0) of System.Address;
90 gnat_argv : access Argv_Array;
91 pragma Import (C, gnat_argv, "gnat_argv");
93 function locate_exec_on_path
94 (c_exename : System.Address) return System.Address;
95 pragma Import (C, locate_exec_on_path, "__gnat_locate_exec_on_path");
97 Res : String (1 .. 256 * Traceback'Length);
98 Len : Integer;
100 use type System.Address;
102 begin
103 -- The symbolic translation of an empty set of addresses is an empty
104 -- string.
106 if Traceback'Length = 0 then
107 return "";
108 end if;
110 -- If our input set of raw addresses is not empty, resort to the
111 -- libaddr2line service to symbolize it all.
113 -- Compute, cache and provide the absolute path to our executable file
114 -- name as the binary file where the relevant debug information is to be
115 -- found. If the executable file name resolution fails, we have no
116 -- sensible basis to invoke the symbolizer at all.
118 -- Protect all this against concurrent accesses explicitly, as the
119 -- underlying services are potentially thread unsafe.
121 TSL.Lock_Task.all;
123 if not Exename_Resolved then
124 Exename := locate_exec_on_path (gnat_argv (0));
125 Exename_Resolved := True;
126 end if;
128 if Exename /= System.Null_Address then
129 Len := Res'Length;
130 convert_addresses
131 (Exename, Traceback'Address, Traceback'Length,
132 Res (1)'Address, Len'Address);
133 end if;
135 TSL.Unlock_Task.all;
137 -- Return what the addr2line symbolizer has produced if we have called
138 -- it (the executable name resolution succeeded), or an empty string
139 -- otherwise.
141 if Exename /= System.Null_Address then
142 return Res (1 .. Len);
143 else
144 return "";
145 end if;
147 end Symbolic_Traceback;
149 function Symbolic_Traceback (E : Exception_Occurrence) return String is
150 begin
151 return Symbolic_Traceback (Tracebacks (E));
152 end Symbolic_Traceback;
154 end GNAT.Traceback.Symbolic;