#include "config.h"
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_LIBCTRACE
#include <execinfo.h>
#endif
#include <isc/backtrace.h>
#include <isc/result.h>
#include <isc/util.h>
#ifdef ISC_PLATFORM_USEBACKTRACE
#ifdef HAVE_LIBCTRACE
#define BACKTRACE_LIBC
#elif defined(__GNUC__) && (defined(__x86_64__) || defined(__ia64__))
#define BACKTRACE_GCC
#elif defined(__x86_64__) || defined(__i386__)
#define BACKTRACE_X86STACK
#else
#define BACKTRACE_DISABLED
#endif
#else
#define BACKTRACE_DISABLED
#endif
#ifdef BACKTRACE_LIBC
isc_result_t
isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
int n;
if (addrs == NULL || nframes == NULL)
return (ISC_R_FAILURE);
n = backtrace(addrs, maxaddrs);
if (n < 2)
return (ISC_R_NOTFOUND);
n--;
memmove(addrs, &addrs[1], sizeof(void *) * n);
*nframes = n;
return (ISC_R_SUCCESS);
}
#elif defined(BACKTRACE_GCC)
extern int _Unwind_Backtrace(void* fn, void* a);
extern void* _Unwind_GetIP(void* ctx);
typedef struct {
void **result;
int max_depth;
int skip_count;
int count;
} trace_arg_t;
static int
btcallback(void *uc, void *opq) {
trace_arg_t *arg = (trace_arg_t *)opq;
if (arg->skip_count > 0)
arg->skip_count--;
else
arg->result[arg->count++] = (void *)_Unwind_GetIP(uc);
if (arg->count == arg->max_depth)
return (5);
return (0);
}
isc_result_t
isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
trace_arg_t arg;
if (addrs == NULL || nframes == NULL)
return (ISC_R_FAILURE);
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);
}
#elif defined(BACKTRACE_X86STACK)
#ifdef __x86_64__
static unsigned long
getrbp() {
__asm("movq %rbp, %rax\n");
}
#endif
static void **
getnextframeptr(void **sp) {
void **newsp = (void **)*sp;
if (newsp <= sp)
return (NULL);
if ((char *)newsp - (char *)sp > 100000)
return (NULL);
return (newsp);
}
isc_result_t
isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
int i = 0;
void **sp;
if (addrs == NULL || nframes == NULL)
return (ISC_R_FAILURE);
#ifdef __x86_64__
sp = (void **)getrbp();
if (sp == NULL)
return (ISC_R_NOTFOUND);
sp = getnextframeptr(sp);
#else
sp = (void **)&addrs - 2;
#endif
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
isc_backtrace_gettrace(void **addrs, int maxaddrs, int *nframes) {
if (addrs == NULL || nframes == NULL)
return (ISC_R_FAILURE);
UNUSED(maxaddrs);
return (ISC_R_NOTIMPLEMENTED);
}
#endif
isc_result_t
isc_backtrace_getsymbolfromindex(int idx, const void **addrp,
const char **symbolp)
{
REQUIRE(addrp != NULL && *addrp == NULL);
REQUIRE(symbolp != NULL && *symbolp == NULL);
if (idx < 0 || idx >= isc__backtrace_nsymbols)
return (ISC_R_RANGE);
*addrp = isc__backtrace_symtable[idx].addr;
*symbolp = isc__backtrace_symtable[idx].symbol;
return (ISC_R_SUCCESS);
}
static int
symtbl_compare(const void *addr, const void *entryarg) {
const isc_backtrace_symmap_t *entry = entryarg;
const isc_backtrace_symmap_t *end =
&isc__backtrace_symtable[isc__backtrace_nsymbols - 1];
if (isc__backtrace_nsymbols == 1 || entry == end) {
if (addr >= entry->addr) {
return (0);
}
return (-1);
}
if (addr < entry->addr)
return (-1);
else if (addr >= (entry + 1)->addr)
return (1);
return (0);
}
isc_result_t
isc_backtrace_getsymbol(const void *addr, const char **symbolp,
unsigned long *offsetp)
{
isc_result_t result = ISC_R_SUCCESS;
isc_backtrace_symmap_t *found;
if (symbolp == NULL || *symbolp != NULL || offsetp == NULL)
return (ISC_R_FAILURE);
if (isc__backtrace_nsymbols < 1)
return (ISC_R_NOTFOUND);
found = bsearch(addr, isc__backtrace_symtable, isc__backtrace_nsymbols,
sizeof(isc__backtrace_symtable[0]), symtbl_compare);
if (found == NULL)
result = ISC_R_NOTFOUND;
else {
*symbolp = found->symbol;
*offsetp = (u_long)((const char *)addr - (char *)found->addr);
}
return (result);
}