1 /* $NetBSD: backtrace.c,v 1.1.1.1 2009/10/25 00:02:42 christos Exp $ */
4 * Copyright (C) 2009 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__))
56 #elif defined(__x86_64__) || defined(__i386__)
57 #define BACKTRACE_X86STACK
59 #define BACKTRACE_DISABLED
60 #endif /* HAVE_LIBCTRACE */
61 #else /* !ISC_PLATFORM_USEBACKTRACE */
62 #define BACKTRACE_DISABLED
63 #endif /* ISC_PLATFORM_USEBACKTRACE */
67 isc_backtrace_gettrace(void **addrs
, int maxaddrs
, int *nframes
) {
71 * Validate the arguments: intentionally avoid using REQUIRE().
72 * See notes in backtrace.h.
74 if (addrs
== NULL
|| nframes
== NULL
)
75 return (ISC_R_FAILURE
);
78 * backtrace(3) includes this function itself in the address array,
79 * which should be eliminated from the returned sequence.
81 n
= backtrace(addrs
, maxaddrs
);
83 return (ISC_R_NOTFOUND
);
85 memmove(addrs
, &addrs
[1], sizeof(void *) * n
);
87 return (ISC_R_SUCCESS
);
89 #elif defined(BACKTRACE_GCC)
90 extern int _Unwind_Backtrace(void* fn
, void* a
);
91 extern void* _Unwind_GetIP(void* ctx
);
101 btcallback(void *uc
, void *opq
) {
102 trace_arg_t
*arg
= (trace_arg_t
*)opq
;
104 if (arg
->skip_count
> 0)
107 arg
->result
[arg
->count
++] = (void *)_Unwind_GetIP(uc
);
108 if (arg
->count
== arg
->max_depth
)
109 return (5); /* _URC_END_OF_STACK */
111 return (0); /* _URC_NO_REASON */
115 isc_backtrace_gettrace(void **addrs
, int maxaddrs
, int *nframes
) {
118 /* Argument validation: see above. */
119 if (addrs
== NULL
|| nframes
== NULL
)
120 return (ISC_R_FAILURE
);
124 arg
.max_depth
= maxaddrs
;
126 _Unwind_Backtrace(btcallback
, &arg
);
128 *nframes
= arg
.count
;
130 return (ISC_R_SUCCESS
);
132 #elif defined(BACKTRACE_X86STACK)
136 __asm("movq %rbp, %rax\n");
141 getnextframeptr(void **sp
) {
142 void **newsp
= (void **)*sp
;
145 * Perform sanity check for the new frame pointer, derived from
146 * google glog. This can actually be bogus depending on compiler.
149 /* prohibit the stack frames from growing downwards */
153 /* A heuristics to reject "too large" frame: this actually happened. */
154 if ((char *)newsp
- (char *)sp
> 100000)
158 * Not sure if other checks used in glog are needed at this moment.
159 * For our purposes we don't have to consider non-contiguous frames,
167 isc_backtrace_gettrace(void **addrs
, int maxaddrs
, int *nframes
) {
171 /* Argument validation: see above. */
172 if (addrs
== NULL
|| nframes
== NULL
)
173 return (ISC_R_FAILURE
);
176 sp
= (void **)getrbp();
178 return (ISC_R_NOTFOUND
);
180 * sp is the frame ptr of this function itself due to the call to
181 * getrbp(), so need to unwind one frame for consistency.
183 sp
= getnextframeptr(sp
);
186 * i386: the frame pointer is stored 2 words below the address for the
187 * first argument. Note that the body of this function cannot be
188 * inlined since it depends on the address of the function argument.
190 sp
= (void **)(void *)&addrs
- 2;
193 while (sp
!= NULL
&& i
< maxaddrs
) {
194 addrs
[i
++] = *(sp
+ 1);
195 sp
= getnextframeptr(sp
);
200 return (ISC_R_SUCCESS
);
202 #elif defined(BACKTRACE_DISABLED)
204 isc_backtrace_gettrace(void **addrs
, int maxaddrs
, int *nframes
) {
205 /* Argument validation: see above. */
206 if (addrs
== NULL
|| nframes
== NULL
)
207 return (ISC_R_FAILURE
);
211 return (ISC_R_NOTIMPLEMENTED
);
216 isc_backtrace_getsymbolfromindex(int index
, const void **addrp
,
217 const char **symbolp
)
219 REQUIRE(addrp
!= NULL
&& *addrp
== NULL
);
220 REQUIRE(symbolp
!= NULL
&& *symbolp
== NULL
);
222 if (index
< 0 || index
>= isc__backtrace_nsymbols
)
223 return (ISC_R_RANGE
);
225 *addrp
= isc__backtrace_symtable
[index
].addr
;
226 *symbolp
= isc__backtrace_symtable
[index
].symbol
;
227 return (ISC_R_SUCCESS
);
231 symtbl_compare(const void *addr
, const void *entryarg
) {
232 const isc_backtrace_symmap_t
*entry
= entryarg
;
233 const isc_backtrace_symmap_t
*end
=
234 &isc__backtrace_symtable
[isc__backtrace_nsymbols
- 1];
236 if (isc__backtrace_nsymbols
== 1 || entry
== end
) {
237 if (addr
>= entry
->addr
) {
239 * If addr is equal to or larger than that of the last
240 * entry of the table, we cannot be sure if this is
241 * within a valid range so we consider it valid.
248 /* entry + 1 is a valid entry from now on. */
249 if (addr
< entry
->addr
)
251 else if (addr
>= (entry
+ 1)->addr
)
257 isc_backtrace_getsymbol(const void *addr
, const char **symbolp
,
258 unsigned long *offsetp
)
260 isc_result_t result
= ISC_R_SUCCESS
;
261 isc_backtrace_symmap_t
*found
;
264 * Validate the arguments: intentionally avoid using REQUIRE().
265 * See notes in backtrace.h.
267 if (symbolp
== NULL
|| *symbolp
!= NULL
|| offsetp
== NULL
)
268 return (ISC_R_FAILURE
);
270 if (isc__backtrace_nsymbols
< 1)
271 return (ISC_R_NOTFOUND
);
274 * Search the table for the entry that meets:
275 * entry.addr <= addr < next_entry.addr.
277 found
= bsearch(addr
, isc__backtrace_symtable
, isc__backtrace_nsymbols
,
278 sizeof(isc__backtrace_symtable
[0]), symtbl_compare
);
280 result
= ISC_R_NOTFOUND
;
282 *symbolp
= found
->symbol
;
283 *offsetp
= (const char *)addr
- (char *)found
->addr
;