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
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
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. */
35 #include <sys/types.h>
42 #include "backtrace.h"
45 #ifndef HAVE_GETEXECNAME
46 #define getexecname() NULL
49 /* Initialize the fileline information from the executable. Returns 1
50 on success, 0 on failure. */
53 fileline_initialize (struct backtrace_state
*state
,
54 backtrace_error_callback error_callback
, void *data
)
59 int called_error_callback
;
64 failed
= state
->fileline_initialization_failed
;
66 failed
= backtrace_atomic_load_int (&state
->fileline_initialization_failed
);
70 error_callback (data
, "failed to read executable information", -1);
75 fileline_fn
= state
->fileline_fn
;
77 fileline_fn
= backtrace_atomic_load_pointer (&state
->fileline_fn
);
78 if (fileline_fn
!= NULL
)
81 /* We have not initialized the information. Do it now. */
84 called_error_callback
= 0;
85 for (pass
= 0; pass
< 5; ++pass
)
93 filename
= state
->filename
;
96 filename
= getexecname ();
99 filename
= "/proc/self/exe";
102 filename
= "/proc/curproc/file";
105 snprintf (buf
, sizeof (buf
), "/proc/%ld/object/a.out",
113 if (filename
== NULL
)
116 descriptor
= backtrace_open (filename
, error_callback
, data
,
118 if (descriptor
< 0 && !does_not_exist
)
120 called_error_callback
= 1;
129 if (!called_error_callback
)
131 if (state
->filename
!= NULL
)
132 error_callback (data
, state
->filename
, ENOENT
);
134 error_callback (data
,
135 "libbacktrace could not find executable to open",
143 if (!backtrace_initialize (state
, descriptor
, error_callback
, data
,
150 if (!state
->threaded
)
151 state
->fileline_initialization_failed
= 1;
153 backtrace_atomic_store_int (&state
->fileline_initialization_failed
, 1);
157 if (!state
->threaded
)
158 state
->fileline_fn
= fileline_fn
;
161 backtrace_atomic_store_pointer (&state
->fileline_fn
, fileline_fn
);
163 /* Note that if two threads initialize at once, one of the data
164 sets may be leaked. */
170 /* Given a PC, find the file name, line number, and function name. */
173 backtrace_pcinfo (struct backtrace_state
*state
, uintptr_t pc
,
174 backtrace_full_callback callback
,
175 backtrace_error_callback error_callback
, void *data
)
177 if (!fileline_initialize (state
, error_callback
, data
))
180 if (state
->fileline_initialization_failed
)
183 return state
->fileline_fn (state
, pc
, callback
, error_callback
, data
);
186 /* Given a PC, find the symbol for it, and its value. */
189 backtrace_syminfo (struct backtrace_state
*state
, uintptr_t pc
,
190 backtrace_syminfo_callback callback
,
191 backtrace_error_callback error_callback
, void *data
)
193 if (!fileline_initialize (state
, error_callback
, data
))
196 if (state
->fileline_initialization_failed
)
199 state
->syminfo_fn (state
, pc
, callback
, error_callback
, data
);