1 /* fileline.c -- Get file and line number information in a backtrace.
2 Copyright (C) 2012-2020 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 #include "backtrace.h"
49 #ifndef HAVE_GETEXECNAME
50 #define getexecname() NULL
53 #if !defined (HAVE_KERN_PROC_ARGS) && !defined (HAVE_KERN_PROC)
55 #define sysctl_exec_name1(state, error_callback, data) NULL
56 #define sysctl_exec_name2(state, error_callback, data) NULL
58 #else /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */
61 sysctl_exec_name (struct backtrace_state
*state
,
62 int mib0
, int mib1
, int mib2
, int mib3
,
63 backtrace_error_callback error_callback
, void *data
)
75 if (sysctl (mib
, 4, NULL
, &len
, NULL
, 0) < 0)
77 name
= (char *) backtrace_alloc (state
, len
, error_callback
, data
);
81 if (sysctl (mib
, 4, name
, &rlen
, NULL
, 0) < 0)
83 backtrace_free (state
, name
, len
, error_callback
, data
);
89 #ifdef HAVE_KERN_PROC_ARGS
92 sysctl_exec_name1 (struct backtrace_state
*state
,
93 backtrace_error_callback error_callback
, void *data
)
95 /* This variant is used on NetBSD. */
96 return sysctl_exec_name (state
, CTL_KERN
, KERN_PROC_ARGS
, -1,
97 KERN_PROC_PATHNAME
, error_callback
, data
);
102 #define sysctl_exec_name1(state, error_callback, data) NULL
106 #ifdef HAVE_KERN_PROC
109 sysctl_exec_name2 (struct backtrace_state
*state
,
110 backtrace_error_callback error_callback
, void *data
)
112 /* This variant is used on FreeBSD. */
113 return sysctl_exec_name (state
, CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1,
114 error_callback
, data
);
119 #define sysctl_exec_name2(state, error_callback, data) NULL
123 #endif /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */
125 /* Initialize the fileline information from the executable. Returns 1
126 on success, 0 on failure. */
129 fileline_initialize (struct backtrace_state
*state
,
130 backtrace_error_callback error_callback
, void *data
)
133 fileline fileline_fn
;
135 int called_error_callback
;
137 const char *filename
;
140 if (!state
->threaded
)
141 failed
= state
->fileline_initialization_failed
;
143 failed
= backtrace_atomic_load_int (&state
->fileline_initialization_failed
);
147 error_callback (data
, "failed to read executable information", -1);
151 if (!state
->threaded
)
152 fileline_fn
= state
->fileline_fn
;
154 fileline_fn
= backtrace_atomic_load_pointer (&state
->fileline_fn
);
155 if (fileline_fn
!= NULL
)
158 /* We have not initialized the information. Do it now. */
161 called_error_callback
= 0;
162 for (pass
= 0; pass
< 7; ++pass
)
169 filename
= state
->filename
;
172 filename
= getexecname ();
175 filename
= "/proc/self/exe";
178 filename
= "/proc/curproc/file";
181 snprintf (buf
, sizeof (buf
), "/proc/%ld/object/a.out",
186 filename
= sysctl_exec_name1 (state
, error_callback
, data
);
189 filename
= sysctl_exec_name2 (state
, error_callback
, data
);
195 if (filename
== NULL
)
198 descriptor
= backtrace_open (filename
, error_callback
, data
,
200 if (descriptor
< 0 && !does_not_exist
)
202 called_error_callback
= 1;
211 if (!called_error_callback
)
213 if (state
->filename
!= NULL
)
214 error_callback (data
, state
->filename
, ENOENT
);
216 error_callback (data
,
217 "libbacktrace could not find executable to open",
225 if (!backtrace_initialize (state
, filename
, descriptor
, error_callback
,
232 if (!state
->threaded
)
233 state
->fileline_initialization_failed
= 1;
235 backtrace_atomic_store_int (&state
->fileline_initialization_failed
, 1);
239 if (!state
->threaded
)
240 state
->fileline_fn
= fileline_fn
;
243 backtrace_atomic_store_pointer (&state
->fileline_fn
, fileline_fn
);
245 /* Note that if two threads initialize at once, one of the data
246 sets may be leaked. */
252 /* Given a PC, find the file name, line number, and function name. */
255 backtrace_pcinfo (struct backtrace_state
*state
, uintptr_t pc
,
256 backtrace_full_callback callback
,
257 backtrace_error_callback error_callback
, void *data
)
259 if (!fileline_initialize (state
, error_callback
, data
))
262 if (state
->fileline_initialization_failed
)
265 return state
->fileline_fn (state
, pc
, callback
, error_callback
, data
);
268 /* Given a PC, find the symbol for it, and its value. */
271 backtrace_syminfo (struct backtrace_state
*state
, uintptr_t pc
,
272 backtrace_syminfo_callback callback
,
273 backtrace_error_callback error_callback
, void *data
)
275 if (!fileline_initialize (state
, error_callback
, data
))
278 if (state
->fileline_initialization_failed
)
281 state
->syminfo_fn (state
, pc
, callback
, error_callback
, data
);