]> git.cworth.org Git - vogl/blob - src/libbacktrace/fileline.c
Initial vogl checkin
[vogl] / src / libbacktrace / fileline.c
1 /* fileline.c -- Get file and line number information in a backtrace.
2    Copyright (C) 2012-2014 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor, Google.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9     (1) Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer. 
11
12     (2) Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions and the following disclaimer in
14     the documentation and/or other materials provided with the
15     distribution.  
16     
17     (3) The name of the author may not be used to
18     endorse or promote products derived from this software without
19     specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.  */
32
33 #include "config.h"
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <unistd.h> // For access()
41
42 #include "backtrace.h"
43 #include "internal.h"
44
45 #ifndef HAVE_GETEXECNAME
46 #define getexecname() NULL
47 #endif
48
49 /* Initialize the fileline information from the executable.  Returns 1
50    on success, 0 on failure.  */
51
52 static int
53 fileline_initialize (struct backtrace_state *state,
54                      backtrace_error_callback error_callback, void *data)
55 {
56   int failed;
57   fileline fileline_fn;
58   int pass;
59
60   if (!state->threaded)
61     failed = state->fileline_initialization_failed;
62   else
63     failed = backtrace_atomic_load_int (&state->fileline_initialization_failed);
64
65   if (failed)
66     {
67       error_callback (data, "failed to read executable information", -1);
68       return 0;
69     }
70
71   if (!state->threaded)
72     fileline_fn = state->fileline_fn;
73   else
74     fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
75   if (fileline_fn != NULL)
76     return 1;
77
78   /* We have not initialized the information.  Do it now.  */
79
80   const char *filename;
81   for (pass = 0; pass < 4; ++pass)
82     {
83       switch (pass)
84         {
85         case 0:
86           filename = state->filename;
87           break;
88         case 1:
89           filename = getexecname ();
90           break;
91         case 2:
92           filename = "/proc/self/exe";
93           break;
94         case 3:
95           filename = "/proc/curproc/file";
96           break;
97         default:
98           abort ();
99         }
100
101       /* If we've got a filename and the file exists, break. */
102       if (filename && filename[0] && (access(filename, F_OK) == 0))
103         break;
104
105       filename = NULL;
106     }
107
108   if (!filename)
109     {
110       if (state->filename != NULL)
111         error_callback (data, state->filename, ENOENT);
112       else
113         error_callback (data,
114                         "libbacktrace could not find executable to open",
115                         0);
116       failed = 1;
117     }
118
119   if (!failed)
120     {
121       if (!backtrace_initialize (state, filename, 0, 1, error_callback, data,
122                                  &fileline_fn))
123         failed = 1;
124     }
125
126   if (failed)
127     {
128       if (!state->threaded)
129         state->fileline_initialization_failed = 1;
130       else
131         backtrace_atomic_store_int (&state->fileline_initialization_failed, 1);
132       return 0;
133     }
134
135   if (!state->threaded)
136     state->fileline_fn = fileline_fn;
137   else
138     {
139       backtrace_atomic_store_pointer (&state->fileline_fn, fileline_fn);
140
141       /* Note that if two threads initialize at once, one of the data
142          sets may be leaked.  */
143     }
144
145   return 1;
146 }
147
148 /* Return the debug filename with dwarf debug information (or NULL if not found). */
149
150 const char *
151 backtrace_get_debug_filename (struct backtrace_state *state)
152 {
153     return state ? state->debug_filename : NULL;
154 }
155
156 /* Initialize the fileline information from a user specified filename.  Returns 1
157    on success, 0 on failure.  */
158
159 int
160 backtrace_fileline_initialize (struct backtrace_state *state, uintptr_t base_address, int is_exe,
161                                backtrace_error_callback error_callback, void *data)
162 {
163   int failed;
164   fileline fileline_fn;
165
166   if (!state->threaded)
167     failed = state->fileline_initialization_failed;
168   else
169     failed = backtrace_atomic_load_int (&state->fileline_initialization_failed);
170
171   if (failed)
172     {
173       error_callback (data, "failed to read executable information", -1);
174       return 0;
175     }
176
177   if (!state->threaded)
178     fileline_fn = state->fileline_fn;
179   else
180     fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
181   if (fileline_fn != NULL)
182     return 1;
183
184   /* We have not initialized information. Do it now.  */
185   /* For this function, we need a filename and base address. */
186
187   if (!state->filename || !base_address)
188     {
189       error_callback (data, state->filename, ENOENT);
190       failed = 1;
191     }
192   else
193     {
194       if (!backtrace_initialize (state, state->filename, base_address, is_exe,
195                                  error_callback, data, &fileline_fn))
196         failed = 1;
197     }
198
199   if (failed)
200     {
201       if (!state->threaded)
202         state->fileline_initialization_failed = 1;
203       else
204         backtrace_atomic_store_int (&state->fileline_initialization_failed, 1);
205       return 0;
206     }
207
208   if (!state->threaded)
209     state->fileline_fn = fileline_fn;
210   else
211     {
212       backtrace_atomic_store_pointer (&state->fileline_fn, fileline_fn);
213
214       /* Note that if two threads initialize at once, one of the data
215          sets may be leaked.  */
216     }
217
218   return 1;
219 }
220 /* Given a PC, find the file name, line number, and function name.  */
221
222 int
223 backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
224                   backtrace_full_callback callback,
225                   backtrace_error_callback error_callback, void *data)
226 {
227   if (!fileline_initialize (state, error_callback, data))
228     return 0;
229
230   if (state->fileline_initialization_failed)
231     return 0;
232
233   return state->fileline_fn (state, pc, callback, error_callback, data);
234 }
235
236 /* Given a PC, find the symbol for it, and its value.  */
237
238 int
239 backtrace_syminfo (struct backtrace_state *state, uintptr_t pc,
240                    backtrace_syminfo_callback callback,
241                    backtrace_error_callback error_callback, void *data)
242 {
243   if (!fileline_initialize (state, error_callback, data))
244     return 0;
245
246   if (state->fileline_initialization_failed)
247     return 0;
248
249   state->syminfo_fn (state, pc, callback, error_callback, data);
250   return 1;
251 }