OSDN Git Service

2011-08-01 Arnaud Charlet <charlet@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-trasym.adb
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-2010, 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.                                     --
17 --                                                                          --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception,   --
20 -- version 3.1, as published by the Free Software Foundation.               --
21 --                                                                          --
22 -- You should have received a copy of the GNU General Public License and    --
23 -- a copy of the GCC Runtime Library Exception along with this program;     --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25 -- <http://www.gnu.org/licenses/>.                                          --
26 --                                                                          --
27 -- GNAT was originally developed  by the GNAT team at  New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 --  Run-time symbolic traceback support
33
34 with System.Soft_Links;
35 with Ada.Exceptions.Traceback; use Ada.Exceptions.Traceback;
36
37 package body GNAT.Traceback.Symbolic is
38
39    pragma Linker_Options ("-laddr2line");
40    pragma Linker_Options ("-lbfd");
41    pragma Linker_Options ("-liberty");
42
43    package TSL renames System.Soft_Links;
44
45    --  To perform the raw addresses to symbolic form translation we rely on a
46    --  libaddr2line symbolizer which examines debug info from a provided
47    --  executable file name, and an absolute path is needed to ensure the file
48    --  is always found. This is "__gnat_locate_exec_on_path (gnat_argv [0])"
49    --  for our executable file, a fairly heavy operation so we cache the
50    --  result.
51
52    Exename : System.Address;
53    --  Pointer to the name of the executable file to be used on all
54    --  invocations of the libaddr2line symbolization service.
55
56    Exename_Resolved : Boolean := False;
57    --  Flag to indicate whether we have performed the executable file name
58    --  resolution already. Relying on a not null Exename for this purpose
59    --  would be potentially inefficient as this is what we will get if the
60    --  resolution attempt fails.
61
62    ------------------------
63    -- Symbolic_Traceback --
64    ------------------------
65
66    function Symbolic_Traceback (Traceback : Tracebacks_Array) return String is
67
68       procedure convert_addresses
69         (filename : System.Address;
70          addrs    : System.Address;
71          n_addrs  : Integer;
72          buf      : System.Address;
73          len      : System.Address);
74       pragma Import (C, convert_addresses, "convert_addresses");
75       --  This is the procedure version of the Ada-aware addr2line. It places
76       --  in BUF a string representing the symbolic translation of the N_ADDRS
77       --  raw addresses provided in ADDRS, looked up in debug information from
78       --  FILENAME. LEN points to an integer which contains the size of the
79       --  BUF buffer at input and the result length at output.
80       --
81       --  This procedure is provided by libaddr2line on targets that support
82       --  it. A dummy version is in adaint.c for other targets so that build
83       --  of shared libraries doesn't generate unresolved symbols.
84       --
85       --  Note that this procedure is *not* thread-safe.
86
87       type Argv_Array is array (0 .. 0) of System.Address;
88       gnat_argv : access Argv_Array;
89       pragma Import (C, gnat_argv, "gnat_argv");
90
91       function locate_exec_on_path
92         (c_exename : System.Address) return System.Address;
93       pragma Import (C, locate_exec_on_path, "__gnat_locate_exec_on_path");
94
95       Res : String (1 .. 256 * Traceback'Length);
96       Len : Integer;
97
98       use type System.Address;
99
100    begin
101       --  The symbolic translation of an empty set of addresses is an empty
102       --  string.
103
104       if Traceback'Length = 0 then
105          return "";
106       end if;
107
108       --  If our input set of raw addresses is not empty, resort to the
109       --  libaddr2line service to symbolize it all.
110
111       --  Compute, cache and provide the absolute path to our executable file
112       --  name as the binary file where the relevant debug information is to be
113       --  found. If the executable file name resolution fails, we have no
114       --  sensible basis to invoke the symbolizer at all.
115
116       --  Protect all this against concurrent accesses explicitly, as the
117       --  underlying services are potentially thread unsafe.
118
119       TSL.Lock_Task.all;
120
121       if not Exename_Resolved then
122          Exename := locate_exec_on_path (gnat_argv (0));
123          Exename_Resolved := True;
124       end if;
125
126       if Exename /= System.Null_Address then
127          Len := Res'Length;
128          convert_addresses
129            (Exename, Traceback'Address, Traceback'Length,
130             Res (1)'Address, Len'Address);
131       end if;
132
133       TSL.Unlock_Task.all;
134
135       --  Return what the addr2line symbolizer has produced if we have called
136       --  it (the executable name resolution succeeded), or an empty string
137       --  otherwise.
138
139       if Exename /= System.Null_Address then
140          return Res (1 .. Len);
141       else
142          return "";
143       end if;
144
145    end Symbolic_Traceback;
146
147    function Symbolic_Traceback (E : Exception_Occurrence) return String is
148    begin
149       return Symbolic_Traceback (Tracebacks (E));
150    end Symbolic_Traceback;
151
152 end GNAT.Traceback.Symbolic;