1 /* $NetBSD: backtrace.c,v 1.7 2014/12/10 04:37:59 christos Exp $ */
4 * Copyright (C) 2009, 2013, 2014 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 /* Id: backtrace.c,v 1.3 2009/09/02 23:48:02 tbox Exp */
31 #include <isc/backtrace.h>
32 #include <isc/result.h>
35 #ifdef ISC_PLATFORM_USEBACKTRACE
37 * Getting a back trace of a running process is tricky and highly platform
38 * dependent. Our current approach is as follows:
39 * 1. If the system library supports the "backtrace()" function, use it.
40 * 2. Otherwise, if the compiler is gcc and the architecture is x86_64 or IA64,
41 * then use gcc's (hidden) Unwind_Backtrace() function. Note that this
42 * function doesn't work for C programs on many other architectures.
43 * 3. Otherwise, if the architecture x86 or x86_64, try to unwind the stack
44 * frame following frame pointers. This assumes the executable binary
45 * compiled with frame pointers; this is not always true for x86_64 (rather,
46 * compiler optimizations often disable frame pointers). The validation
47 * checks in getnextframeptr() hopefully rejects bogus values stored in
48 * the RBP register in such a case. If the backtrace function itself crashes
49 * due to this problem, the whole package should be rebuilt with
50 * --disable-backtrace.
53 #define BACKTRACE_LIBC
54 #elif defined(__GNUC__) && (defined(__x86_64__) || defined(__ia64__))
57 #define BACKTRACE_WIN32
58 #elif defined(__x86_64__) || defined(__i386__)
59 #define BACKTRACE_X86STACK
61 #define BACKTRACE_DISABLED
62 #endif /* HAVE_LIBCTRACE */
63 #else /* !ISC_PLATFORM_USEBACKTRACE */
64 #define BACKTRACE_DISABLED
65 #endif /* ISC_PLATFORM_USEBACKTRACE */
69 isc_backtrace_gettrace(void **addrs
, int maxaddrs
, int *nframes
) {
73 * Validate the arguments: intentionally avoid using REQUIRE().
74 * See notes in backtrace.h.
76 if (addrs
== NULL
|| nframes
== NULL
)
77 return (ISC_R_FAILURE
);
80 * backtrace(3) includes this function itself in the address array,
81 * which should be eliminated from the returned sequence.
83 n
= backtrace(addrs
, maxaddrs
);
85 return (ISC_R_NOTFOUND
);
87 memmove(addrs
, &addrs
[1], sizeof(void *) * n
);
89 return (ISC_R_SUCCESS
);
91 #elif defined(BACKTRACE_GCC)
92 extern int _Unwind_Backtrace(void* fn
, void* a
);
93 extern void* _Unwind_GetIP(void* ctx
);
103 btcallback(void *uc
, void *opq
) {
104 trace_arg_t
*arg
= (trace_arg_t
*)opq
;
106 if (arg
->skip_count
> 0)
109 arg
->result
[arg
->count
++] = (void *)_Unwind_GetIP(uc
);
110 if (arg
->count
== arg
->max_depth
)
111 return (5); /* _URC_END_OF_STACK */
113 return (0); /* _URC_NO_REASON */
117 isc_backtrace_gettrace(void **addrs
, int maxaddrs
, int *nframes
) {
120 /* Argument validation: see above. */
121 if (addrs
== NULL
|| nframes
== NULL
)
122 return (ISC_R_FAILURE
);
126 arg
.max_depth
= maxaddrs
;
128 _Unwind_Backtrace(btcallback
, &arg
);
130 *nframes
= arg
.count
;
132 return (ISC_R_SUCCESS
);
134 #elif defined(BACKTRACE_WIN32)
136 isc_backtrace_gettrace(void **addrs
, int maxaddrs
, int *nframes
) {
137 unsigned long ftc
= (unsigned long)maxaddrs
;
139 *nframes
= (int)CaptureStackBackTrace(1, ftc
, addrs
, NULL
);
140 return ISC_R_SUCCESS
;
142 #elif defined(BACKTRACE_X86STACK)
146 __asm("movq %rbp, %rax\n");
151 getnextframeptr(void **sp
) {
152 void **newsp
= (void **)*sp
;
155 * Perform sanity check for the new frame pointer, derived from
156 * google glog. This can actually be bogus depending on compiler.
159 /* prohibit the stack frames from growing downwards */
163 /* A heuristics to reject "too large" frame: this actually happened. */
164 if ((char *)newsp
- (char *)sp
> 100000)
168 * Not sure if other checks used in glog are needed at this moment.
169 * For our purposes we don't have to consider non-contiguous frames,
177 isc_backtrace_gettrace(void **addrs
, int maxaddrs
, int *nframes
) {
181 /* Argument validation: see above. */
182 if (addrs
== NULL
|| nframes
== NULL
)
183 return (ISC_R_FAILURE
);
186 sp
= (void **)getrbp();
188 return (ISC_R_NOTFOUND
);
190 * sp is the frame ptr of this function itself due to the call to
191 * getrbp(), so need to unwind one frame for consistency.
193 sp
= getnextframeptr(sp
);
196 * i386: the frame pointer is stored 2 words below the address for the
197 * first argument. Note that the body of this function cannot be
198 * inlined since it depends on the address of the function argument.
200 sp
= (void **)(void *)&addrs
- 2;
203 while (sp
!= NULL
&& i
< maxaddrs
) {
204 addrs
[i
++] = *(sp
+ 1);
205 sp
= getnextframeptr(sp
);
210 return (ISC_R_SUCCESS
);
212 #elif defined(BACKTRACE_DISABLED)
214 isc_backtrace_gettrace(void **addrs
, int maxaddrs
, int *nframes
) {
215 /* Argument validation: see above. */
216 if (addrs
== NULL
|| nframes
== NULL
)
217 return (ISC_R_FAILURE
);
221 return (ISC_R_NOTIMPLEMENTED
);
226 isc_backtrace_getsymbolfromindex(int index
, const void **addrp
,
227 const char **symbolp
)
229 REQUIRE(addrp
!= NULL
&& *addrp
== NULL
);
230 REQUIRE(symbolp
!= NULL
&& *symbolp
== NULL
);
232 if (index
< 0 || index
>= isc__backtrace_nsymbols
)
233 return (ISC_R_RANGE
);
235 *addrp
= isc__backtrace_symtable
[index
].addr
;
236 *symbolp
= isc__backtrace_symtable
[index
].symbol
;
237 return (ISC_R_SUCCESS
);
241 symtbl_compare(const void *addr
, const void *entryarg
) {
242 const isc_backtrace_symmap_t
*entry
= entryarg
;
243 const isc_backtrace_symmap_t
*end
=
244 &isc__backtrace_symtable
[isc__backtrace_nsymbols
- 1];
246 if (isc__backtrace_nsymbols
== 1 || entry
== end
) {
247 if (addr
>= entry
->addr
) {
249 * If addr is equal to or larger than that of the last
250 * entry of the table, we cannot be sure if this is
251 * within a valid range so we consider it valid.
258 /* entry + 1 is a valid entry from now on. */
259 if (addr
< entry
->addr
)
261 else if (addr
>= (entry
+ 1)->addr
)
267 isc_backtrace_getsymbol(const void *addr
, const char **symbolp
,
268 unsigned long *offsetp
)
270 isc_result_t result
= ISC_R_SUCCESS
;
271 isc_backtrace_symmap_t
*found
;
274 * Validate the arguments: intentionally avoid using REQUIRE().
275 * See notes in backtrace.h.
277 if (symbolp
== NULL
|| *symbolp
!= NULL
|| offsetp
== NULL
)
278 return (ISC_R_FAILURE
);
280 if (isc__backtrace_nsymbols
< 1)
281 return (ISC_R_NOTFOUND
);
284 * Search the table for the entry that meets:
285 * entry.addr <= addr < next_entry.addr.
287 found
= bsearch(addr
, isc__backtrace_symtable
, isc__backtrace_nsymbols
,
288 sizeof(isc__backtrace_symtable
[0]), symtbl_compare
);
290 result
= ISC_R_NOTFOUND
;
292 *symbolp
= found
->symbol
;
293 *offsetp
= (unsigned long) ((const char *)addr
-
294 (char *)found
->addr
);