1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7 *
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 */
11
12
13 /*! \file */
14
15 #include <config.h>
16
17 #include <string.h>
18 #include <stdlib.h>
19 #ifdef HAVE_LIBCTRACE
20 #include <execinfo.h>
21 #endif
22
23 #include <isc/backtrace.h>
24 #include <isc/result.h>
25 #include <isc/util.h>
26
27 #ifdef ISC_PLATFORM_USEBACKTRACE
28 /*
29 * Getting a back trace of a running process is tricky and highly platform
30 * dependent. Our current approach is as follows:
31 * 1. If the system library supports the "backtrace()" function, use it.
32 * 2. Otherwise, if the compiler is gcc and the architecture is x86_64 or IA64,
33 * then use gcc's (hidden) Unwind_Backtrace() function. Note that this
34 * function doesn't work for C programs on many other architectures.
35 * 3. Otherwise, if the architecture x86 or x86_64, try to unwind the stack
36 * frame following frame pointers. This assumes the executable binary
37 * compiled with frame pointers; this is not always true for x86_64 (rather,
38 * compiler optimizations often disable frame pointers). The validation
39 * checks in getnextframeptr() hopefully rejects bogus values stored in
40 * the RBP register in such a case. If the backtrace function itself crashes
41 * due to this problem, the whole package should be rebuilt with
42 * --disable-backtrace.
43 */
44 #ifdef HAVE_LIBCTRACE
45 #define BACKTRACE_LIBC
46 #elif defined(HAVE_UNWIND_BACKTRACE)
47 #define BACKTRACE_GCC
48 #elif defined(WIN32)
49 #define BACKTRACE_WIN32
50 #elif defined(__x86_64__) || defined(__i386__)
51 #define BACKTRACE_X86STACK
52 #else
53 #define BACKTRACE_DISABLED
54 #endif /* HAVE_LIBCTRACE */
55 #else /* !ISC_PLATFORM_USEBACKTRACE */
56 #define BACKTRACE_DISABLED
57 #endif /* ISC_PLATFORM_USEBACKTRACE */
58
59 #ifdef BACKTRACE_LIBC
60 isc_result_t
isc_backtrace_gettrace(void ** addrs,int maxaddrs,int * nframes)61 isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
62 int n;
63
64 /*
65 * Validate the arguments: intentionally avoid using REQUIRE().
66 * See notes in backtrace.h.
67 */
68 if (addrs == NULL || nframes == NULL)
69 return (ISC_R_FAILURE);
70
71 /*
72 * backtrace(3) includes this function itself in the address array,
73 * which should be eliminated from the returned sequence.
74 */
75 n = backtrace(addrs, maxaddrs);
76 if (n < 2)
77 return (ISC_R_NOTFOUND);
78 n--;
79 memmove(addrs, &addrs[1], sizeof(void *) * n);
80 *nframes = n;
81 return (ISC_R_SUCCESS);
82 }
83 #elif defined(BACKTRACE_GCC)
84 extern int _Unwind_Backtrace(void* fn, void* a);
85 extern void* _Unwind_GetIP(void* ctx);
86
87 typedef struct {
88 void **result;
89 int max_depth;
90 int skip_count;
91 int count;
92 } trace_arg_t;
93
94 static int
btcallback(void * uc,void * opq)95 btcallback(void *uc, void *opq) {
96 trace_arg_t *arg = (trace_arg_t *)opq;
97
98 if (arg->skip_count > 0)
99 arg->skip_count--;
100 else
101 arg->result[arg->count++] = (void *)_Unwind_GetIP(uc);
102 if (arg->count == arg->max_depth)
103 return (5); /* _URC_END_OF_STACK */
104
105 return (0); /* _URC_NO_REASON */
106 }
107
108 isc_result_t
isc_backtrace_gettrace(void ** addrs,int maxaddrs,int * nframes)109 isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
110 trace_arg_t arg;
111
112 /* Argument validation: see above. */
113 if (addrs == NULL || nframes == NULL)
114 return (ISC_R_FAILURE);
115
116 arg.skip_count = 1;
117 arg.result = addrs;
118 arg.max_depth = maxaddrs;
119 arg.count = 0;
120 _Unwind_Backtrace(btcallback, &arg);
121
122 *nframes = arg.count;
123
124 return (ISC_R_SUCCESS);
125 }
126 #elif defined(BACKTRACE_WIN32)
127 isc_result_t
isc_backtrace_gettrace(void ** addrs,int maxaddrs,int * nframes)128 isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
129 unsigned long ftc = (unsigned long)maxaddrs;
130
131 *nframes = (int)CaptureStackBackTrace(1, ftc, addrs, NULL);
132 return ISC_R_SUCCESS;
133 }
134 #elif defined(BACKTRACE_X86STACK)
135 #ifdef __x86_64__
136 static unsigned long
getrbp(void)137 getrbp(void) {
138 unsigned long rbp;
139 __asm("movq %%rbp, %0\n" : "=r"(rbp));
140 return rbp;
141 }
142 #endif
143
144 static void **
getnextframeptr(void ** sp)145 getnextframeptr(void **sp) {
146 void **newsp = (void **)*sp;
147
148 /*
149 * Perform sanity check for the new frame pointer, derived from
150 * google glog. This can actually be bogus depending on compiler.
151 */
152
153 /* prohibit the stack frames from growing downwards */
154 if (newsp <= sp)
155 return (NULL);
156
157 /* A heuristics to reject "too large" frame: this actually happened. */
158 if ((char *)newsp - (char *)sp > 100000)
159 return (NULL);
160
161 /*
162 * Not sure if other checks used in glog are needed at this moment.
163 * For our purposes we don't have to consider non-contiguous frames,
164 * for example.
165 */
166
167 return (newsp);
168 }
169
170 isc_result_t
isc_backtrace_gettrace(void ** addrs,int maxaddrs,int * nframes)171 isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
172 int i = 0;
173 void **sp;
174
175 /* Argument validation: see above. */
176 if (addrs == NULL || nframes == NULL)
177 return (ISC_R_FAILURE);
178
179 #ifdef __x86_64__
180 sp = (void **)getrbp();
181 if (sp == NULL)
182 return (ISC_R_NOTFOUND);
183 /*
184 * sp is the frame ptr of this function itself due to the call to
185 * getrbp(), so need to unwind one frame for consistency.
186 */
187 sp = getnextframeptr(sp);
188 #else
189 /*
190 * i386: the frame pointer is stored 2 words below the address for the
191 * first argument. Note that the body of this function cannot be
192 * inlined since it depends on the address of the function argument.
193 */
194 sp = (void **)&addrs - 2;
195 #endif
196
197 while (sp != NULL && i < maxaddrs) {
198 addrs[i++] = *(sp + 1);
199 sp = getnextframeptr(sp);
200 }
201
202 *nframes = i;
203
204 return (ISC_R_SUCCESS);
205 }
206 #elif defined(BACKTRACE_DISABLED)
207 isc_result_t
isc_backtrace_gettrace(void ** addrs,int maxaddrs,int * nframes)208 isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
209 /* Argument validation: see above. */
210 if (addrs == NULL || nframes == NULL)
211 return (ISC_R_FAILURE);
212
213 UNUSED(maxaddrs);
214
215 return (ISC_R_NOTIMPLEMENTED);
216 }
217 #endif
218
219 isc_result_t
isc_backtrace_getsymbolfromindex(int idx,const void ** addrp,const char ** symbolp)220 isc_backtrace_getsymbolfromindex(int idx, const void **addrp,
221 const char **symbolp)
222 {
223 REQUIRE(addrp != NULL && *addrp == NULL);
224 REQUIRE(symbolp != NULL && *symbolp == NULL);
225
226 if (idx < 0 || idx >= isc__backtrace_nsymbols)
227 return (ISC_R_RANGE);
228
229 *addrp = isc__backtrace_symtable[idx].addr;
230 *symbolp = isc__backtrace_symtable[idx].symbol;
231 return (ISC_R_SUCCESS);
232 }
233
234 static int
symtbl_compare(const void * addr,const void * entryarg)235 symtbl_compare(const void *addr, const void *entryarg) {
236 const isc_backtrace_symmap_t *entry = entryarg;
237 const isc_backtrace_symmap_t *end =
238 &isc__backtrace_symtable[isc__backtrace_nsymbols - 1];
239
240 if (isc__backtrace_nsymbols == 1 || entry == end) {
241 if (addr >= entry->addr) {
242 /*
243 * If addr is equal to or larger than that of the last
244 * entry of the table, we cannot be sure if this is
245 * within a valid range so we consider it valid.
246 */
247 return (0);
248 }
249 return (-1);
250 }
251
252 /* entry + 1 is a valid entry from now on. */
253 if (addr < entry->addr)
254 return (-1);
255 else if (addr >= (entry + 1)->addr)
256 return (1);
257 return (0);
258 }
259
260 isc_result_t
isc_backtrace_getsymbol(const void * addr,const char ** symbolp,unsigned long * offsetp)261 isc_backtrace_getsymbol(const void *addr, const char **symbolp,
262 unsigned long *offsetp)
263 {
264 isc_result_t result = ISC_R_SUCCESS;
265 isc_backtrace_symmap_t *found;
266
267 /*
268 * Validate the arguments: intentionally avoid using REQUIRE().
269 * See notes in backtrace.h.
270 */
271 if (symbolp == NULL || *symbolp != NULL || offsetp == NULL)
272 return (ISC_R_FAILURE);
273
274 if (isc__backtrace_nsymbols < 1)
275 return (ISC_R_NOTFOUND);
276
277 /*
278 * Search the table for the entry that meets:
279 * entry.addr <= addr < next_entry.addr.
280 */
281 found = bsearch(addr, isc__backtrace_symtable, isc__backtrace_nsymbols,
282 sizeof(isc__backtrace_symtable[0]), symtbl_compare);
283 if (found == NULL)
284 result = ISC_R_NOTFOUND;
285 else {
286 *symbolp = found->symbol;
287 *offsetp = (unsigned long) ((const char *)addr -
288 (char *)found->addr);
289 }
290
291 return (result);
292 }
293