2009-09-01 18:40:25 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2009-09-01 18:40:25 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2009-09-01 18:40:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <string.h>
|
2009-09-01 18:40:25 +00:00
|
|
|
#ifdef HAVE_LIBCTRACE
|
|
|
|
#include <execinfo.h>
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef HAVE_LIBCTRACE */
|
2009-09-01 18:40:25 +00:00
|
|
|
|
|
|
|
#include <isc/backtrace.h>
|
|
|
|
#include <isc/result.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2018-09-04 10:09:24 +02:00
|
|
|
#ifdef USE_BACKTRACE
|
2009-09-01 18:40:25 +00:00
|
|
|
/*
|
|
|
|
* Getting a back trace of a running process is tricky and highly platform
|
|
|
|
* dependent. Our current approach is as follows:
|
|
|
|
* 1. If the system library supports the "backtrace()" function, use it.
|
|
|
|
* 2. Otherwise, if the compiler is gcc and the architecture is x86_64 or IA64,
|
|
|
|
* then use gcc's (hidden) Unwind_Backtrace() function. Note that this
|
2009-09-02 23:48:03 +00:00
|
|
|
* function doesn't work for C programs on many other architectures.
|
2009-09-01 18:40:25 +00:00
|
|
|
* 3. Otherwise, if the architecture x86 or x86_64, try to unwind the stack
|
|
|
|
* frame following frame pointers. This assumes the executable binary
|
|
|
|
* compiled with frame pointers; this is not always true for x86_64 (rather,
|
|
|
|
* compiler optimizations often disable frame pointers). The validation
|
|
|
|
* checks in getnextframeptr() hopefully rejects bogus values stored in
|
|
|
|
* the RBP register in such a case. If the backtrace function itself crashes
|
|
|
|
* due to this problem, the whole package should be rebuilt with
|
|
|
|
* --disable-backtrace.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_LIBCTRACE
|
|
|
|
#define BACKTRACE_LIBC
|
|
|
|
#elif defined(__GNUC__) && (defined(__x86_64__) || defined(__ia64__))
|
|
|
|
#define BACKTRACE_GCC
|
2013-12-04 12:47:23 +11:00
|
|
|
#elif defined(WIN32)
|
|
|
|
#define BACKTRACE_WIN32
|
2009-09-01 18:40:25 +00:00
|
|
|
#elif defined(__x86_64__) || defined(__i386__)
|
|
|
|
#define BACKTRACE_X86STACK
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef HAVE_LIBCTRACE */
|
2009-09-01 18:40:25 +00:00
|
|
|
#define BACKTRACE_DISABLED
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* HAVE_LIBCTRACE */
|
|
|
|
#else /* USE_BACKTRACE */
|
2009-09-01 18:40:25 +00:00
|
|
|
#define BACKTRACE_DISABLED
|
2020-02-12 13:59:18 +01:00
|
|
|
#endif /* USE_BACKTRACE */
|
2009-09-01 18:40:25 +00:00
|
|
|
|
|
|
|
#ifdef BACKTRACE_LIBC
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
|
2009-09-01 18:40:25 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate the arguments: intentionally avoid using REQUIRE().
|
|
|
|
* See notes in backtrace.h.
|
|
|
|
*/
|
2020-02-13 21:48:23 +01:00
|
|
|
if (addrs == NULL || nframes == NULL) {
|
2009-09-01 18:40:25 +00:00
|
|
|
return (ISC_R_FAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-09-01 18:40:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* backtrace(3) includes this function itself in the address array,
|
|
|
|
* which should be eliminated from the returned sequence.
|
|
|
|
*/
|
|
|
|
n = backtrace(addrs, maxaddrs);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (n < 2) {
|
2009-09-01 18:40:25 +00:00
|
|
|
return (ISC_R_NOTFOUND);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-09-01 18:40:25 +00:00
|
|
|
n--;
|
|
|
|
memmove(addrs, &addrs[1], sizeof(void *) * n);
|
|
|
|
*nframes = n;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
#elif defined(BACKTRACE_GCC)
|
2020-02-14 08:14:03 +01:00
|
|
|
extern int
|
|
|
|
_Unwind_Backtrace(void *fn, void *a);
|
|
|
|
extern void *
|
|
|
|
_Unwind_GetIP(void *ctx);
|
2009-09-01 18:40:25 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void **result;
|
2020-02-13 14:44:37 -08:00
|
|
|
int max_depth;
|
|
|
|
int skip_count;
|
|
|
|
int count;
|
2009-09-01 18:40:25 +00:00
|
|
|
} trace_arg_t;
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
btcallback(void *uc, void *opq) {
|
2009-09-01 18:40:25 +00:00
|
|
|
trace_arg_t *arg = (trace_arg_t *)opq;
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
if (arg->skip_count > 0) {
|
2009-09-01 18:40:25 +00:00
|
|
|
arg->skip_count--;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2009-09-01 18:40:25 +00:00
|
|
|
arg->result[arg->count++] = (void *)_Unwind_GetIP(uc);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
|
|
|
if (arg->count == arg->max_depth) {
|
2009-09-01 18:40:25 +00:00
|
|
|
return (5); /* _URC_END_OF_STACK */
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-09-01 18:40:25 +00:00
|
|
|
return (0); /* _URC_NO_REASON */
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
|
2009-09-01 18:40:25 +00:00
|
|
|
trace_arg_t arg;
|
|
|
|
|
|
|
|
/* Argument validation: see above. */
|
2020-02-13 21:48:23 +01:00
|
|
|
if (addrs == NULL || nframes == NULL) {
|
2009-09-01 18:40:25 +00:00
|
|
|
return (ISC_R_FAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-09-01 18:40:25 +00:00
|
|
|
|
|
|
|
arg.skip_count = 1;
|
|
|
|
arg.result = addrs;
|
|
|
|
arg.max_depth = maxaddrs;
|
|
|
|
arg.count = 0;
|
|
|
|
_Unwind_Backtrace(btcallback, &arg);
|
|
|
|
|
|
|
|
*nframes = arg.count;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
2013-12-04 12:47:23 +11:00
|
|
|
#elif defined(BACKTRACE_WIN32)
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
|
2013-12-04 12:47:23 +11:00
|
|
|
unsigned long ftc = (unsigned long)maxaddrs;
|
|
|
|
|
|
|
|
*nframes = (int)CaptureStackBackTrace(1, ftc, addrs, NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
return (ISC_R_SUCCESS);
|
2013-12-04 12:47:23 +11:00
|
|
|
}
|
2009-09-01 18:40:25 +00:00
|
|
|
#elif defined(BACKTRACE_X86STACK)
|
|
|
|
#ifdef __x86_64__
|
|
|
|
static unsigned long
|
2020-02-13 14:44:37 -08:00
|
|
|
getrbp(void) {
|
2009-09-01 18:40:25 +00:00
|
|
|
__asm("movq %rbp, %rax\n");
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef __x86_64__ */
|
2009-09-01 18:40:25 +00:00
|
|
|
|
|
|
|
static void **
|
2020-02-13 14:44:37 -08:00
|
|
|
getnextframeptr(void **sp) {
|
2009-09-01 18:40:25 +00:00
|
|
|
void **newsp = (void **)*sp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform sanity check for the new frame pointer, derived from
|
|
|
|
* google glog. This can actually be bogus depending on compiler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* prohibit the stack frames from growing downwards */
|
2020-02-13 21:48:23 +01:00
|
|
|
if (newsp <= sp) {
|
2009-09-01 18:40:25 +00:00
|
|
|
return (NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-09-01 18:40:25 +00:00
|
|
|
|
|
|
|
/* A heuristics to reject "too large" frame: this actually happened. */
|
2020-02-13 21:48:23 +01:00
|
|
|
if ((char *)newsp - (char *)sp > 100000) {
|
2009-09-01 18:40:25 +00:00
|
|
|
return (NULL);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-09-01 18:40:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Not sure if other checks used in glog are needed at this moment.
|
|
|
|
* For our purposes we don't have to consider non-contiguous frames,
|
|
|
|
* for example.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (newsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
|
|
|
|
int i = 0;
|
2009-09-01 18:40:25 +00:00
|
|
|
void **sp;
|
|
|
|
|
|
|
|
/* Argument validation: see above. */
|
2020-02-13 21:48:23 +01:00
|
|
|
if (addrs == NULL || nframes == NULL) {
|
2009-09-01 18:40:25 +00:00
|
|
|
return (ISC_R_FAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-09-01 18:40:25 +00:00
|
|
|
|
|
|
|
#ifdef __x86_64__
|
|
|
|
sp = (void **)getrbp();
|
2020-02-13 21:48:23 +01:00
|
|
|
if (sp == NULL) {
|
2009-09-01 18:40:25 +00:00
|
|
|
return (ISC_R_NOTFOUND);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-09-01 18:40:25 +00:00
|
|
|
/*
|
|
|
|
* sp is the frame ptr of this function itself due to the call to
|
|
|
|
* getrbp(), so need to unwind one frame for consistency.
|
|
|
|
*/
|
|
|
|
sp = getnextframeptr(sp);
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* ifdef __x86_64__ */
|
2009-09-01 18:40:25 +00:00
|
|
|
/*
|
|
|
|
* i386: the frame pointer is stored 2 words below the address for the
|
|
|
|
* first argument. Note that the body of this function cannot be
|
|
|
|
* inlined since it depends on the address of the function argument.
|
|
|
|
*/
|
|
|
|
sp = (void **)&addrs - 2;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef __x86_64__ */
|
2009-09-01 18:40:25 +00:00
|
|
|
|
|
|
|
while (sp != NULL && i < maxaddrs) {
|
|
|
|
addrs[i++] = *(sp + 1);
|
|
|
|
sp = getnextframeptr(sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
*nframes = i;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
#elif defined(BACKTRACE_DISABLED)
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
|
2009-09-01 18:40:25 +00:00
|
|
|
/* Argument validation: see above. */
|
2020-02-13 21:48:23 +01:00
|
|
|
if (addrs == NULL || nframes == NULL) {
|
2009-09-01 18:40:25 +00:00
|
|
|
return (ISC_R_FAILURE);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-09-01 18:40:25 +00:00
|
|
|
|
|
|
|
UNUSED(maxaddrs);
|
|
|
|
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef BACKTRACE_LIBC */
|