2013-07-19 20:09:58 +02:00
|
|
|
/* Copyright (c) 2013, The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2013-11-18 19:05:23 +01:00
|
|
|
#define __USE_GNU
|
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
|
2013-07-19 20:09:58 +02:00
|
|
|
#include "orconfig.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "util.h"
|
2013-07-20 04:47:49 +02:00
|
|
|
#include "torlog.h"
|
2013-07-19 20:09:58 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_EXECINFO_H
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2013-07-26 13:22:56 +02:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2013-11-18 17:34:15 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_CYGWIN_SIGNAL_H
|
|
|
|
#include <cygwin/signal.h>
|
2013-11-18 17:36:23 +01:00
|
|
|
#elif defined(HAVE_SYS_UCONTEXT_H)
|
2013-07-26 13:22:56 +02:00
|
|
|
#include <sys/ucontext.h>
|
2013-11-18 17:34:15 +01:00
|
|
|
#elif defined(HAVE_UCONTEXT_H)
|
|
|
|
#include <ucontext.h>
|
2013-07-26 13:22:56 +02:00
|
|
|
#endif
|
2013-07-19 20:09:58 +02:00
|
|
|
|
2014-04-10 21:44:52 +02:00
|
|
|
#define EXPOSE_CLEAN_BACKTRACE
|
|
|
|
#include "backtrace.h"
|
|
|
|
|
2013-07-19 20:09:58 +02:00
|
|
|
#if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && \
|
|
|
|
defined(HAVE_BACKTRACE_SYMBOLS_FD) && defined(HAVE_SIGACTION)
|
|
|
|
#define USE_BACKTRACE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(USE_BACKTRACE)
|
|
|
|
#define NO_BACKTRACE_IMPL
|
|
|
|
#endif
|
|
|
|
|
2013-07-26 13:22:56 +02:00
|
|
|
/** Version of Tor to report in backtrace messages. */
|
2013-07-19 20:09:58 +02:00
|
|
|
static char *bt_version = NULL;
|
|
|
|
|
|
|
|
#ifdef USE_BACKTRACE
|
2013-07-26 13:22:56 +02:00
|
|
|
/** Largest stack depth to try to dump. */
|
2013-07-19 20:09:58 +02:00
|
|
|
#define MAX_DEPTH 256
|
2013-07-26 13:22:56 +02:00
|
|
|
/** Static allocation of stack to dump. This is static so we avoid stack
|
|
|
|
* pressure. */
|
2013-07-19 20:09:58 +02:00
|
|
|
static void *cb_buf[MAX_DEPTH];
|
2014-02-25 16:23:38 +01:00
|
|
|
/** Protects cb_buf from concurrent access */
|
2014-02-24 18:15:32 +01:00
|
|
|
static tor_mutex_t cb_buf_mutex;
|
2013-07-19 20:09:58 +02:00
|
|
|
|
2013-07-26 13:22:56 +02:00
|
|
|
/** Change a stacktrace in <b>stack</b> of depth <b>depth</b> so that it will
|
|
|
|
* log the correct function from which a signal was received with context
|
|
|
|
* <b>ctx</b>. (When we get a signal, the current function will not have
|
|
|
|
* called any other function, and will therefore have not pushed its address
|
|
|
|
* onto the stack. Fortunately, we usually have the program counter in the
|
|
|
|
* ucontext_t structure.
|
|
|
|
*/
|
2014-04-10 21:44:52 +02:00
|
|
|
void
|
2013-07-26 13:22:56 +02:00
|
|
|
clean_backtrace(void **stack, int depth, const ucontext_t *ctx)
|
|
|
|
{
|
|
|
|
#ifdef PC_FROM_UCONTEXT
|
|
|
|
#if defined(__linux__)
|
|
|
|
const int n = 1;
|
|
|
|
#elif defined(__darwin__) || defined(__APPLE__) || defined(__OpenBSD__) \
|
|
|
|
|| defined(__FreeBSD__)
|
|
|
|
const int n = 2;
|
|
|
|
#else
|
|
|
|
const int n = 1;
|
|
|
|
#endif
|
|
|
|
if (depth <= n)
|
|
|
|
return;
|
|
|
|
|
|
|
|
stack[n] = (void*) ctx->PC_FROM_UCONTEXT;
|
|
|
|
#else
|
|
|
|
(void) depth;
|
|
|
|
(void) ctx;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Log a message <b>msg</b> at <b>severity</b> in <b>domain</b>, and follow
|
|
|
|
* that with a backtrace log. */
|
2013-07-19 20:09:58 +02:00
|
|
|
void
|
2013-07-26 13:22:56 +02:00
|
|
|
log_backtrace(int severity, int domain, const char *msg)
|
2013-07-19 20:09:58 +02:00
|
|
|
{
|
2014-02-24 18:15:32 +01:00
|
|
|
int depth;
|
|
|
|
char **symbols;
|
2013-07-20 04:47:49 +02:00
|
|
|
int i;
|
2014-02-24 18:15:32 +01:00
|
|
|
|
|
|
|
tor_mutex_acquire(&cb_buf_mutex);
|
|
|
|
|
|
|
|
depth = backtrace(cb_buf, MAX_DEPTH);
|
|
|
|
symbols = backtrace_symbols(cb_buf, depth);
|
|
|
|
|
2013-07-26 13:22:56 +02:00
|
|
|
tor_log(severity, domain, "%s. Stack trace:", msg);
|
|
|
|
if (!symbols) {
|
|
|
|
tor_log(severity, domain, " Unable to generate backtrace.");
|
2014-02-24 18:15:32 +01:00
|
|
|
goto done;
|
2013-07-26 13:22:56 +02:00
|
|
|
}
|
|
|
|
for (i=0; i < depth; ++i) {
|
|
|
|
tor_log(severity, domain, " %s", symbols[i]);
|
|
|
|
}
|
|
|
|
free(symbols);
|
2014-02-24 18:15:32 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
tor_mutex_release(&cb_buf_mutex);
|
2013-07-26 13:22:56 +02:00
|
|
|
}
|
2013-07-20 04:47:49 +02:00
|
|
|
|
2013-07-26 13:22:56 +02:00
|
|
|
static void crash_handler(int sig, siginfo_t *si, void *ctx_)
|
|
|
|
__attribute__((noreturn));
|
|
|
|
|
|
|
|
/** Signal handler: write a crash message with a stack trace, and die. */
|
|
|
|
static void
|
|
|
|
crash_handler(int sig, siginfo_t *si, void *ctx_)
|
|
|
|
{
|
|
|
|
char buf[40];
|
|
|
|
int depth;
|
|
|
|
ucontext_t *ctx = (ucontext_t *) ctx_;
|
|
|
|
int n_fds, i;
|
|
|
|
const int *fds = NULL;
|
|
|
|
|
|
|
|
(void) si;
|
2013-07-19 20:09:58 +02:00
|
|
|
|
|
|
|
depth = backtrace(cb_buf, MAX_DEPTH);
|
2013-07-26 13:22:56 +02:00
|
|
|
/* Clean up the top stack frame so we get the real function
|
|
|
|
* name for the most recently failing function. */
|
|
|
|
clean_backtrace(cb_buf, depth, ctx);
|
|
|
|
|
|
|
|
format_dec_number_sigsafe((unsigned)sig, buf, sizeof(buf));
|
2013-07-19 20:09:58 +02:00
|
|
|
|
2013-07-26 13:22:56 +02:00
|
|
|
tor_log_err_sigsafe(bt_version, " died: Caught signal ", buf, "\n",
|
2013-07-20 04:47:49 +02:00
|
|
|
NULL);
|
2013-07-26 13:22:56 +02:00
|
|
|
|
2013-07-20 04:47:49 +02:00
|
|
|
n_fds = tor_log_get_sigsafe_err_fds(&fds);
|
|
|
|
for (i=0; i < n_fds; ++i)
|
|
|
|
backtrace_symbols_fd(cb_buf, depth, fds[i]);
|
2013-07-26 13:22:56 +02:00
|
|
|
|
|
|
|
abort();
|
2013-07-19 20:09:58 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 13:22:56 +02:00
|
|
|
/** Install signal handlers as needed so that when we crash, we produce a
|
|
|
|
* useful stack trace. Return 0 on success, -1 on failure. */
|
2013-07-19 20:09:58 +02:00
|
|
|
static int
|
|
|
|
install_bt_handler(void)
|
|
|
|
{
|
2013-07-26 13:22:56 +02:00
|
|
|
int trap_signals[] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGSYS,
|
|
|
|
SIGIO, -1 };
|
|
|
|
int i, rv=0;
|
|
|
|
|
|
|
|
struct sigaction sa;
|
2014-02-24 18:15:32 +01:00
|
|
|
|
|
|
|
tor_mutex_init(&cb_buf_mutex);
|
|
|
|
|
2013-07-26 13:22:56 +02:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_sigaction = crash_handler;
|
|
|
|
sa.sa_flags = SA_SIGINFO;
|
|
|
|
sigfillset(&sa.sa_mask);
|
|
|
|
|
|
|
|
for (i = 0; trap_signals[i] >= 0; ++i) {
|
|
|
|
if (sigaction(trap_signals[i], &sa, NULL) == -1) {
|
|
|
|
log_warn(LD_BUG, "Sigaction failed: %s", strerror(errno));
|
|
|
|
rv = -1;
|
|
|
|
}
|
|
|
|
}
|
2014-04-10 21:44:52 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
/* Now, generate (but do not log) a backtrace. This ensures that
|
|
|
|
* libc has pre-loaded the symbols we need to dump things, so that later
|
|
|
|
* reads won't be denied by the sandbox code */
|
|
|
|
char **symbols;
|
|
|
|
int depth = backtrace(cb_buf, MAX_DEPTH);
|
|
|
|
symbols = backtrace_symbols(cb_buf, depth);
|
|
|
|
if (symbols)
|
|
|
|
free(symbols);
|
|
|
|
}
|
|
|
|
|
2013-07-26 13:22:56 +02:00
|
|
|
return rv;
|
2013-07-19 20:09:58 +02:00
|
|
|
}
|
2013-07-26 13:22:56 +02:00
|
|
|
|
|
|
|
/** Uninstall crash handlers. */
|
2013-07-19 20:09:58 +02:00
|
|
|
static void
|
|
|
|
remove_bt_handler(void)
|
|
|
|
{
|
2014-02-24 18:15:32 +01:00
|
|
|
tor_mutex_uninit(&cb_buf_mutex);
|
2013-07-19 20:09:58 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NO_BACKTRACE_IMPL
|
|
|
|
void
|
2013-07-26 13:22:56 +02:00
|
|
|
log_backtrace(int severity, int domain, const char *msg)
|
2013-07-19 20:09:58 +02:00
|
|
|
{
|
2013-07-26 13:22:56 +02:00
|
|
|
tor_log(severity, domain, "%s. (Stack trace not available)", msg);
|
2013-07-19 20:09:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
install_bt_handler(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_bt_handler(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-08-15 18:45:46 +02:00
|
|
|
/** Set up code to handle generating error messages on crashes. */
|
2013-07-19 20:09:58 +02:00
|
|
|
int
|
2013-07-20 04:47:49 +02:00
|
|
|
configure_backtrace_handler(const char *tor_version)
|
2013-07-19 20:09:58 +02:00
|
|
|
{
|
|
|
|
tor_free(bt_version);
|
|
|
|
if (!tor_version)
|
2013-07-26 13:22:56 +02:00
|
|
|
tor_version = "";
|
|
|
|
tor_asprintf(&bt_version, "Tor %s", tor_version);
|
2013-07-19 20:09:58 +02:00
|
|
|
|
|
|
|
return install_bt_handler();
|
|
|
|
}
|
|
|
|
|
2013-08-15 18:45:46 +02:00
|
|
|
/** Perform end-of-process cleanup for code that generates error messages on
|
|
|
|
* crashes. */
|
2013-07-19 20:09:58 +02:00
|
|
|
void
|
|
|
|
clean_up_backtrace_handler(void)
|
|
|
|
{
|
|
|
|
remove_bt_handler();
|
|
|
|
|
|
|
|
tor_free(bt_version);
|
|
|
|
}
|
|
|
|
|