1 /* fileline.c -- Get file and line number information in a backtrace.
2 Copyright (C) 2012-2023 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 #if defined (HAVE_KERN_PROC_ARGS) || defined (HAVE_KERN_PROC)
43 #include <sys/sysctl.h>
46 #ifdef HAVE_MACH_O_DYLD_H
47 #include <mach-o/dyld.h>
50 #include "backtrace.h"
53 #ifndef HAVE_GETEXECNAME
54 #define getexecname() NULL
57 #if !defined (HAVE_KERN_PROC_ARGS) && !defined (HAVE_KERN_PROC)
59 #define sysctl_exec_name1(state, error_callback, data) NULL
60 #define sysctl_exec_name2(state, error_callback, data) NULL
62 #else /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */
65 sysctl_exec_name (struct backtrace_state
*state
,
66 int mib0
, int mib1
, int mib2
, int mib3
,
67 backtrace_error_callback error_callback
, void *data
)
79 if (sysctl (mib
, 4, NULL
, &len
, NULL
, 0) < 0)
81 name
= (char *) backtrace_alloc (state
, len
, error_callback
, data
);
85 if (sysctl (mib
, 4, name
, &rlen
, NULL
, 0) < 0)
87 backtrace_free (state
, name
, len
, error_callback
, data
);
93 #ifdef HAVE_KERN_PROC_ARGS
96 sysctl_exec_name1 (struct backtrace_state
*state
,
97 backtrace_error_callback error_callback
, void *data
)
99 /* This variant is used on NetBSD. */
100 return sysctl_exec_name (state
, CTL_KERN
, KERN_PROC_ARGS
, -1,
101 KERN_PROC_PATHNAME
, error_callback
, data
);
106 #define sysctl_exec_name1(state, error_callback, data) NULL
110 #ifdef HAVE_KERN_PROC
113 sysctl_exec_name2 (struct backtrace_state
*state
,
114 backtrace_error_callback error_callback
, void *data
)
116 /* This variant is used on FreeBSD. */
117 return sysctl_exec_name (state
, CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1,
118 error_callback
, data
);
123 #define sysctl_exec_name2(state, error_callback, data) NULL
127 #endif /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */
129 #ifdef HAVE_MACH_O_DYLD_H
132 macho_get_executable_path (struct backtrace_state
*state
,
133 backtrace_error_callback error_callback
, void *data
)
139 if (_NSGetExecutablePath (NULL
, &len
) == 0)
141 name
= (char *) backtrace_alloc (state
, len
, error_callback
, data
);
144 if (_NSGetExecutablePath (name
, &len
) != 0)
146 backtrace_free (state
, name
, len
, error_callback
, data
);
152 #else /* !defined (HAVE_MACH_O_DYLD_H) */
154 #define macho_get_executable_path(state, error_callback, data) NULL
156 #endif /* !defined (HAVE_MACH_O_DYLD_H) */
158 /* Initialize the fileline information from the executable. Returns 1
159 on success, 0 on failure. */
162 fileline_initialize (struct backtrace_state
*state
,
163 backtrace_error_callback error_callback
, void *data
)
166 fileline fileline_fn
;
168 int called_error_callback
;
170 const char *filename
;
173 if (!state
->threaded
)
174 failed
= state
->fileline_initialization_failed
;
176 failed
= backtrace_atomic_load_int (&state
->fileline_initialization_failed
);
180 error_callback (data
, "failed to read executable information", -1);
184 if (!state
->threaded
)
185 fileline_fn
= state
->fileline_fn
;
187 fileline_fn
= backtrace_atomic_load_pointer (&state
->fileline_fn
);
188 if (fileline_fn
!= NULL
)
191 /* We have not initialized the information. Do it now. */
194 called_error_callback
= 0;
195 for (pass
= 0; pass
< 8; ++pass
)
202 filename
= state
->filename
;
205 filename
= getexecname ();
208 filename
= "/proc/self/exe";
211 filename
= "/proc/curproc/file";
214 snprintf (buf
, sizeof (buf
), "/proc/%ld/object/a.out",
219 filename
= sysctl_exec_name1 (state
, error_callback
, data
);
222 filename
= sysctl_exec_name2 (state
, error_callback
, data
);
225 filename
= macho_get_executable_path (state
, error_callback
, data
);
231 if (filename
== NULL
)
234 descriptor
= backtrace_open (filename
, error_callback
, data
,
236 if (descriptor
< 0 && !does_not_exist
)
238 called_error_callback
= 1;
247 if (!called_error_callback
)
249 if (state
->filename
!= NULL
)
250 error_callback (data
, state
->filename
, ENOENT
);
252 error_callback (data
,
253 "libbacktrace could not find executable to open",
261 if (!backtrace_initialize (state
, filename
, descriptor
, error_callback
,
268 if (!state
->threaded
)
269 state
->fileline_initialization_failed
= 1;
271 backtrace_atomic_store_int (&state
->fileline_initialization_failed
, 1);
275 if (!state
->threaded
)
276 state
->fileline_fn
= fileline_fn
;
279 backtrace_atomic_store_pointer (&state
->fileline_fn
, fileline_fn
);
281 /* Note that if two threads initialize at once, one of the data
282 sets may be leaked. */
288 /* Given a PC, find the file name, line number, and function name. */
291 backtrace_pcinfo (struct backtrace_state
*state
, uintptr_t pc
,
292 backtrace_full_callback callback
,
293 backtrace_error_callback error_callback
, void *data
)
295 if (!fileline_initialize (state
, error_callback
, data
))
298 if (state
->fileline_initialization_failed
)
301 return state
->fileline_fn (state
, pc
, callback
, error_callback
, data
);
304 /* Given a PC, find the symbol for it, and its value. */
307 backtrace_syminfo (struct backtrace_state
*state
, uintptr_t pc
,
308 backtrace_syminfo_callback callback
,
309 backtrace_error_callback error_callback
, void *data
)
311 if (!fileline_initialize (state
, error_callback
, data
))
314 if (state
->fileline_initialization_failed
)
317 state
->syminfo_fn (state
, pc
, callback
, error_callback
, data
);
321 /* A backtrace_syminfo_callback that can call into a
322 backtrace_full_callback, used when we have a symbol table but no
326 backtrace_syminfo_to_full_callback (void *data
, uintptr_t pc
,
328 uintptr_t symval ATTRIBUTE_UNUSED
,
329 uintptr_t symsize ATTRIBUTE_UNUSED
)
331 struct backtrace_call_full
*bdata
= (struct backtrace_call_full
*) data
;
333 bdata
->ret
= bdata
->full_callback (bdata
->full_data
, pc
, NULL
, 0, symname
);
336 /* An error callback that corresponds to
337 backtrace_syminfo_to_full_callback. */
340 backtrace_syminfo_to_full_error_callback (void *data
, const char *msg
,
343 struct backtrace_call_full
*bdata
= (struct backtrace_call_full
*) data
;
345 bdata
->full_error_callback (bdata
->full_data
, msg
, errnum
);