1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . T R A C E B A C K . S Y M B O L I C --
9 -- Copyright (C) 1999-2010, AdaCore --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- Run-time symbolic traceback support
34 with System.Soft_Links;
35 with Ada.Exceptions.Traceback; use Ada.Exceptions.Traceback;
37 package body GNAT.Traceback.Symbolic is
39 pragma Linker_Options ("-laddr2line");
40 pragma Linker_Options ("-lbfd");
41 pragma Linker_Options ("-liberty");
43 package TSL renames System.Soft_Links;
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
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.
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.
62 ------------------------
63 -- Symbolic_Traceback --
64 ------------------------
66 function Symbolic_Traceback (Traceback : Tracebacks_Array) return String is
68 procedure convert_addresses
69 (filename : System.Address;
70 addrs : 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.
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.
85 -- Note that this procedure is *not* thread-safe.
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");
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");
95 Res : String (1 .. 256 * Traceback'Length);
98 use type System.Address;
101 -- The symbolic translation of an empty set of addresses is an empty
104 if Traceback'Length = 0 then
108 -- If our input set of raw addresses is not empty, resort to the
109 -- libaddr2line service to symbolize it all.
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.
116 -- Protect all this against concurrent accesses explicitly, as the
117 -- underlying services are potentially thread unsafe.
121 if not Exename_Resolved then
122 Exename := locate_exec_on_path (gnat_argv (0));
123 Exename_Resolved := True;
126 if Exename /= System.Null_Address then
129 (Exename, Traceback'Address, Traceback'Length,
130 Res (1)'Address, Len'Address);
135 -- Return what the addr2line symbolizer has produced if we have called
136 -- it (the executable name resolution succeeded), or an empty string
139 if Exename /= System.Null_Address then
140 return Res (1 .. Len);
145 end Symbolic_Traceback;
147 function Symbolic_Traceback (E : Exception_Occurrence) return String is
149 return Symbolic_Traceback (Tracebacks (E));
150 end Symbolic_Traceback;
152 end GNAT.Traceback.Symbolic;