PR go/81548
[official-gcc.git] / libbacktrace / fileline.c
blob303e4dce83a737b3b441dba873a75a5020b6ab3a
1 /* fileline.c -- Get file and line number information in a backtrace.
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
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.
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.
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. */
33 #include "config.h"
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>
42 #include "backtrace.h"
43 #include "internal.h"
45 #ifndef HAVE_GETEXECNAME
46 #define getexecname() NULL
47 #endif
49 /* Initialize the fileline information from the executable. Returns 1
50 on success, 0 on failure. */
52 static int
53 fileline_initialize (struct backtrace_state *state,
54 backtrace_error_callback error_callback, void *data)
56 int failed;
57 fileline fileline_fn;
58 int pass;
59 int called_error_callback;
60 int descriptor;
61 char buf[64];
63 if (!state->threaded)
64 failed = state->fileline_initialization_failed;
65 else
66 failed = backtrace_atomic_load_int (&state->fileline_initialization_failed);
68 if (failed)
70 error_callback (data, "failed to read executable information", -1);
71 return 0;
74 if (!state->threaded)
75 fileline_fn = state->fileline_fn;
76 else
77 fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn);
78 if (fileline_fn != NULL)
79 return 1;
81 /* We have not initialized the information. Do it now. */
83 descriptor = -1;
84 called_error_callback = 0;
85 for (pass = 0; pass < 5; ++pass)
87 const char *filename;
88 int does_not_exist;
90 switch (pass)
92 case 0:
93 filename = state->filename;
94 break;
95 case 1:
96 filename = getexecname ();
97 break;
98 case 2:
99 filename = "/proc/self/exe";
100 break;
101 case 3:
102 filename = "/proc/curproc/file";
103 break;
104 case 4:
105 snprintf (buf, sizeof (buf), "/proc/%d/object/a.out", getpid ());
106 filename = buf;
107 break;
108 default:
109 abort ();
112 if (filename == NULL)
113 continue;
115 descriptor = backtrace_open (filename, error_callback, data,
116 &does_not_exist);
117 if (descriptor < 0 && !does_not_exist)
119 called_error_callback = 1;
120 break;
122 if (descriptor >= 0)
123 break;
126 if (descriptor < 0)
128 if (!called_error_callback)
130 if (state->filename != NULL)
131 error_callback (data, state->filename, ENOENT);
132 else
133 error_callback (data,
134 "libbacktrace could not find executable to open",
137 failed = 1;
140 if (!failed)
142 if (!backtrace_initialize (state, descriptor, error_callback, data,
143 &fileline_fn))
144 failed = 1;
147 if (failed)
149 if (!state->threaded)
150 state->fileline_initialization_failed = 1;
151 else
152 backtrace_atomic_store_int (&state->fileline_initialization_failed, 1);
153 return 0;
156 if (!state->threaded)
157 state->fileline_fn = fileline_fn;
158 else
160 backtrace_atomic_store_pointer (&state->fileline_fn, fileline_fn);
162 /* Note that if two threads initialize at once, one of the data
163 sets may be leaked. */
166 return 1;
169 /* Given a PC, find the file name, line number, and function name. */
172 backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc,
173 backtrace_full_callback callback,
174 backtrace_error_callback error_callback, void *data)
176 if (!fileline_initialize (state, error_callback, data))
177 return 0;
179 if (state->fileline_initialization_failed)
180 return 0;
182 return state->fileline_fn (state, pc, callback, error_callback, data);
185 /* Given a PC, find the symbol for it, and its value. */
188 backtrace_syminfo (struct backtrace_state *state, uintptr_t pc,
189 backtrace_syminfo_callback callback,
190 backtrace_error_callback error_callback, void *data)
192 if (!fileline_initialize (state, error_callback, data))
193 return 0;
195 if (state->fileline_initialization_failed)
196 return 0;
198 state->syminfo_fn (state, pc, callback, error_callback, data);
199 return 1;