backtrace.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
/*
* Copyright (C) 2009, 2013-2016 Internet Systems Consortium, Inc. ("ISC")
*
* 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/.
*/
/* $Id: backtrace.c,v 1.3 2009/09/02 23:48:02 tbox Exp $ */
/*! \file */
#include "config.h"
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_LIBCTRACE
#include <execinfo.h>
#endif
#include <isc/backtrace.h>
#ifdef ISC_PLATFORM_USEBACKTRACE
/*
* 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
* function doesn't work for C programs on many other architectures.
* 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
#define BACKTRACE_GCC
#define BACKTRACE_WIN32
#define BACKTRACE_X86STACK
#else
#define BACKTRACE_DISABLED
#endif /* HAVE_LIBCTRACE */
#else /* !ISC_PLATFORM_USEBACKTRACE */
#define BACKTRACE_DISABLED
#endif /* ISC_PLATFORM_USEBACKTRACE */
#ifdef BACKTRACE_LIBC
int n;
/*
* Validate the arguments: intentionally avoid using REQUIRE().
* See notes in backtrace.h.
*/
return (ISC_R_FAILURE);
/*
* backtrace(3) includes this function itself in the address array,
* which should be eliminated from the returned sequence.
*/
if (n < 2)
return (ISC_R_NOTFOUND);
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
if (arg->skip_count > 0)
arg->skip_count--;
else
return (5); /* _URC_END_OF_STACK */
return (0); /* _URC_NO_REASON */
}
/* Argument validation: see above. */
return (ISC_R_FAILURE);
return (ISC_R_SUCCESS);
}
#elif defined(BACKTRACE_WIN32)
return ISC_R_SUCCESS;
}
#elif defined(BACKTRACE_X86STACK)
#ifdef __x86_64__
static unsigned long
getrbp(void) {
__asm("movq %rbp, %rax\n");
}
#endif
static void **
getnextframeptr(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 */
return (NULL);
/* A heuristics to reject "too large" frame: this actually happened. */
return (NULL);
/*
* 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);
}
int i = 0;
void **sp;
/* Argument validation: see above. */
return (ISC_R_FAILURE);
#ifdef __x86_64__
return (ISC_R_NOTFOUND);
/*
* sp is the frame ptr of this function itself due to the call to
* getrbp(), so need to unwind one frame for consistency.
*/
#else
/*
* 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.
*/
#endif
}
*nframes = i;
return (ISC_R_SUCCESS);
}
#elif defined(BACKTRACE_DISABLED)
/* Argument validation: see above. */
return (ISC_R_FAILURE);
return (ISC_R_NOTIMPLEMENTED);
}
#endif
const char **symbolp)
{
return (ISC_R_RANGE);
return (ISC_R_SUCCESS);
}
static int
const isc_backtrace_symmap_t *end =
/*
* If addr is equal to or larger than that of the last
* entry of the table, we cannot be sure if this is
* within a valid range so we consider it valid.
*/
return (0);
}
return (-1);
}
/* entry + 1 is a valid entry from now on. */
return (-1);
return (1);
return (0);
}
unsigned long *offsetp)
{
/*
* Validate the arguments: intentionally avoid using REQUIRE().
* See notes in backtrace.h.
*/
return (ISC_R_FAILURE);
if (isc__backtrace_nsymbols < 1)
return (ISC_R_NOTFOUND);
/*
* Search the table for the entry that meets:
* entry.addr <= addr < next_entry.addr.
*/
sizeof(isc__backtrace_symtable[0]), symtbl_compare);
else {
}
return (result);
}