OSDN Git Service

2007-04-20 Ed Schonberg <schonberg@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-2006, 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 ------------------------------------------------------------------------------
33
34 --  Run-time symbolic traceback support
35
36 with System.Soft_Links;
37 with Ada.Exceptions.Traceback; use Ada.Exceptions.Traceback;
38
39 package body GNAT.Traceback.Symbolic is
40
41    pragma Linker_Options ("-laddr2line");
42    pragma Linker_Options ("-lbfd");
43    pragma Linker_Options ("-liberty");
44
45    package TSL renames System.Soft_Links;
46
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.
53
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.
57
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.
63
64    ------------------------
65    -- Symbolic_Traceback --
66    ------------------------
67
68    function Symbolic_Traceback (Traceback : Tracebacks_Array) return String is
69
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 is filled with the result length.
81       --
82       --  This procedure is provided by libaddr2line on targets that support
83       --  it. A dummy version is in adaint.c for other targets so that build
84       --  of shared libraries doesn't generate unresolved symbols.
85       --
86       --  Note that this procedure is *not* thread-safe.
87
88       type Argv_Array is array (0 .. 0) of System.Address;
89       gnat_argv : access Argv_Array;
90       pragma Import (C, gnat_argv, "gnat_argv");
91
92       function locate_exec_on_path
93         (c_exename : System.Address) return System.Address;
94       pragma Import (C, locate_exec_on_path, "__gnat_locate_exec_on_path");
95
96       Res : String (1 .. 256 * Traceback'Length);
97       Len : Integer;
98
99       use type System.Address;
100
101    begin
102       --  The symbolic translation of an empty set of addresses is the
103       --  the empty string.
104
105       if Traceback'Length <= 0 then
106          return "";
107       end if;
108
109       --  If our input set of raw addresses is not empty, resort to the
110       --  libaddr2line service to symbolize it all.
111
112       --  Compute, cache and provide the absolute path to our executable file
113       --  name as the binary file where the relevant debug information is to
114       --  be found. If the executable file name resolution fails, we have no
115       --  sensible basis to invoke the symbolizer at all.
116
117       --  Protect all this against concurrent accesses explicitely, as the
118       --  underlying services are potentially thread unsafe.
119
120       TSL.Lock_Task.all;
121
122       if not Exename_Resolved then
123          Exename := locate_exec_on_path (gnat_argv (0));
124          Exename_Resolved := True;
125       end if;
126
127       if Exename /= System.Null_Address then
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;