xref: /netbsd-src/external/gpl3/gcc/dist/libbacktrace/fileline.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /* fileline.c -- Get file and line number information in a backtrace.
2    Copyright (C) 2012-2013 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 
41 #include "backtrace.h"
42 #include "internal.h"
43 
44 #ifndef HAVE_GETEXECNAME
45 #define getexecname() NULL
46 #endif
47 
48 /* Initialize the fileline information from the executable.  Returns 1
49    on success, 0 on failure.  */
50 
51 static int
52 fileline_initialize (struct backtrace_state *state,
53 		     backtrace_error_callback error_callback, void *data)
54 {
55   int failed;
56   fileline fileline_fn;
57   int pass;
58   int called_error_callback;
59   int descriptor;
60 
61   failed = state->fileline_initialization_failed;
62 
63   if (state->threaded)
64     {
65       /* Use __sync_bool_compare_and_swap to do an atomic load.  */
66       while (!__sync_bool_compare_and_swap
67 	     (&state->fileline_initialization_failed, failed, failed))
68 	failed = state->fileline_initialization_failed;
69     }
70 
71   if (failed)
72     {
73       error_callback (data, "failed to read executable information", -1);
74       return 0;
75     }
76 
77   fileline_fn = state->fileline_fn;
78   if (state->threaded)
79     {
80       while (!__sync_bool_compare_and_swap (&state->fileline_fn, fileline_fn,
81 					    fileline_fn))
82 	fileline_fn = state->fileline_fn;
83     }
84   if (fileline_fn != NULL)
85     return 1;
86 
87   /* We have not initialized the information.  Do it now.  */
88 
89   descriptor = -1;
90   called_error_callback = 0;
91   for (pass = 0; pass < 4; ++pass)
92     {
93       const char *filename;
94       int does_not_exist;
95 
96       switch (pass)
97 	{
98 	case 0:
99 	  filename = state->filename;
100 	  break;
101 	case 1:
102 	  filename = getexecname ();
103 	  break;
104 	case 2:
105 	  filename = "/proc/self/exe";
106 	  break;
107 	case 3:
108 	  filename = "/proc/curproc/file";
109 	  break;
110 	default:
111 	  abort ();
112 	}
113 
114       if (filename == NULL)
115 	continue;
116 
117       descriptor = backtrace_open (filename, error_callback, data,
118 				   &does_not_exist);
119       if (descriptor < 0 && !does_not_exist)
120 	{
121 	  called_error_callback = 1;
122 	  break;
123 	}
124       if (descriptor >= 0)
125 	break;
126     }
127 
128   if (descriptor < 0)
129     {
130       if (!called_error_callback)
131 	{
132 	  if (state->filename != NULL)
133 	    error_callback (data, state->filename, ENOENT);
134 	  else
135 	    error_callback (data,
136 			    "libbacktrace could not find executable to open",
137 			    0);
138 	}
139       failed = 1;
140     }
141 
142   if (!failed)
143     {
144       if (!backtrace_initialize (state, descriptor, error_callback, data,
145 				 &fileline_fn))
146 	failed = 1;
147     }
148 
149   if (failed)
150     {
151       if (!state->threaded)
152 	state->fileline_initialization_failed = 1;
153       else
154 	__sync_bool_compare_and_swap (&state->fileline_initialization_failed,
155 				      0, failed);
156       return 0;
157     }
158 
159   if (!state->threaded)
160     state->fileline_fn = fileline_fn;
161   else
162     {
163       __sync_bool_compare_and_swap (&state->fileline_fn, NULL, fileline_fn);
164 
165       /* At this point we know that state->fileline_fn is not NULL.
166 	 Either we stored our value, or some other thread stored its
167 	 value.  If some other thread stored its value, we leak the
168 	 one we just initialized.  Either way, state->fileline_fn is
169 	 initialized.  The compare_and_swap is a full memory barrier,
170 	 so we should have full access to that value even if it was
171 	 created by another thread.  */
172     }
173 
174   return 1;
175 }
176 
177 /* Given a PC, find the file name, line number, and function name.  */
178 
179 int
180 backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
181 		  backtrace_full_callback callback,
182 		  backtrace_error_callback error_callback, void *data)
183 {
184   if (!fileline_initialize (state, error_callback, data))
185     return 0;
186 
187   if (state->fileline_initialization_failed)
188     return 0;
189 
190   return state->fileline_fn (state, pc, callback, error_callback, data);
191 }
192 
193 /* Given a PC, find the symbol for it, and its value.  */
194 
195 int
196 backtrace_syminfo (struct backtrace_state *state, uintptr_t pc,
197 		   backtrace_syminfo_callback callback,
198 		   backtrace_error_callback error_callback, void *data)
199 {
200   if (!fileline_initialize (state, error_callback, data))
201     return 0;
202 
203   if (state->fileline_initialization_failed)
204     return 0;
205 
206   state->syminfo_fn (state, pc, callback, error_callback, data);
207   return 1;
208 }
209