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