aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2013-07-19 14:09:58 -0400
committerNick Mathewson <nickm@torproject.org>2013-11-18 10:43:14 -0500
commit063bea58bcc1c27864a0351bba07254855903377 (patch)
tree8a0f0ea303fbbc4b86d31c54dd9da761f658f4e2 /src/common
parentf6d8bc9389db72dc654560d0f86fd3edd3b14934 (diff)
downloadtor-063bea58bcc1c27864a0351bba07254855903377.tar
tor-063bea58bcc1c27864a0351bba07254855903377.tar.gz
Basic backtrace ability
On platforms with the backtrace/backtrace_symbols_fd interface, Tor can now dump stack traces on assertion failure. By default, I log them to DataDir/stack_dump and to stderr.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/backtrace.c157
-rw-r--r--src/common/backtrace.h12
-rw-r--r--src/common/include.am2
-rw-r--r--src/common/util.c6
4 files changed, 177 insertions, 0 deletions
diff --git a/src/common/backtrace.c b/src/common/backtrace.c
new file mode 100644
index 000000000..d3f59b2c7
--- /dev/null
+++ b/src/common/backtrace.c
@@ -0,0 +1,157 @@
+/* Copyright (c) 2013, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+#include "backtrace.h"
+#include "compat.h"
+#include "util.h"
+
+#ifdef HAVE_EXECINFO_H
+#include <execinfo.h>
+#endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#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
+
+static char *bt_filename = NULL;
+static char *bt_version = NULL;
+
+#ifndef NO_BACKTRACE_IMPL
+/**DOCDOC*/
+static int
+open_bt_target(void)
+{
+ int fd = -1;
+ if (bt_filename)
+ fd = open(bt_filename, O_WRONLY|O_CREAT|O_APPEND, 0700);
+ return fd;
+}
+#endif
+
+/**DOCDOC*/
+static void
+bt_write(int fd, const char *s, ssize_t n)
+{
+ int r;
+ if (n < 0) n = strlen(s);
+
+ r = write(STDERR_FILENO, s, n);
+ if (fd >= 0)
+ r = write(fd, s, n);
+ (void)r;
+}
+
+#ifdef USE_BACKTRACE
+#define MAX_DEPTH 256
+static void *cb_buf[MAX_DEPTH];
+
+/**DOCDOC*/
+void
+dump_backtrace(const char *msg)
+{
+ char timebuf[32];
+ time_t t = time(NULL);
+ int timebuf_len;
+ int depth;
+ int fd;
+ if (!msg) msg = "unspecified crash";
+
+ depth = backtrace(cb_buf, MAX_DEPTH);
+
+ t /= 900; t *= 900; /* Round to the previous 15 minutes */
+ timebuf[0] = '\0';
+ timebuf_len = format_dec_number_sigsafe(t, timebuf, sizeof(timebuf));
+
+ fd = open_bt_target();
+ bt_write(fd, "========================================"
+ "====================================\n", -1);
+ bt_write(fd, bt_version, -1);
+ bt_write(fd, " died around T=", -1);
+ bt_write(fd, timebuf, timebuf_len);
+ bt_write(fd, ": ", 2);
+ bt_write(fd, msg, -1);
+ bt_write(fd, "\n", 1);
+ backtrace_symbols_fd(cb_buf, depth, STDERR_FILENO);
+ if (fd >= 0)
+ backtrace_symbols_fd(cb_buf, depth, fd);
+
+ close(fd);
+}
+
+/**DOCDOC*/
+static int
+install_bt_handler(void)
+{
+ /*XXXX add signal handlers */
+ /*XXXX make this idempotent */
+ return 0;
+}
+/**DOCDOC*/
+static void
+remove_bt_handler(void)
+{
+}
+#endif
+
+#ifdef NO_BACKTRACE_IMPL
+/**DOCDOC*/
+void
+dump_backtrace(const char *msg)
+{
+ bt_write(-1, bt_version, -1);
+ bt_write(-1, " died: ", -1);
+ bt_write(-1, msg, -1);
+ bt_write(-1, "\n", -1);
+}
+
+/**DOCDOC*/
+static int
+install_bt_handler(void)
+{
+ return 0;
+}
+
+/**DOCDOC*/
+static void
+remove_bt_handler(void)
+{
+}
+#endif
+
+/**DOCDOC*/
+int
+configure_backtrace_handler(const char *filename, const char *tor_version)
+{
+ tor_free(bt_filename);
+ if (filename)
+ bt_filename = tor_strdup(filename);
+ tor_free(bt_version);
+ if (!tor_version)
+ tor_version = "Tor";
+ bt_version = tor_strdup(tor_version);
+
+ return install_bt_handler();
+}
+
+/**DOCDOC*/
+void
+clean_up_backtrace_handler(void)
+{
+ remove_bt_handler();
+
+ tor_free(bt_filename);
+ tor_free(bt_version);
+}
+
diff --git a/src/common/backtrace.h b/src/common/backtrace.h
new file mode 100644
index 000000000..bb2396080
--- /dev/null
+++ b/src/common/backtrace.h
@@ -0,0 +1,12 @@
+/* Copyright (c) 2013, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef TOR_BACKTRACE_H
+#define TOR_BACKTRACE_H
+
+void dump_backtrace(const char *msg);
+int configure_backtrace_handler(const char *filename, const char *tor_version);
+void clean_up_backtrace_handler(void);
+
+#endif
+
diff --git a/src/common/include.am b/src/common/include.am
index 032befd20..814786b77 100644
--- a/src/common/include.am
+++ b/src/common/include.am
@@ -50,6 +50,7 @@ endif
LIBOR_A_SOURCES = \
src/common/address.c \
+ src/common/backtrace.c \
src/common/compat.c \
src/common/container.c \
src/common/di_ops.c \
@@ -90,6 +91,7 @@ src_common_libor_event_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
COMMONHEADERS = \
src/common/address.h \
+ src/common/backtrace.h \
src/common/aes.h \
src/common/ciphers.inc \
src/common/compat.h \
diff --git a/src/common/util.c b/src/common/util.c
index 814eb1379..0b65437b0 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -24,6 +24,7 @@
#include "torint.h"
#include "container.h"
#include "address.h"
+#include "backtrace.h"
#ifdef _WIN32
#include <io.h>
@@ -101,8 +102,13 @@ void
tor_assertion_failed_(const char *fname, unsigned int line,
const char *func, const char *expr)
{
+ char buf[256];
log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.",
fname, line, func, expr);
+ tor_snprintf(buf, sizeof(buf),
+ "Assertion %s failed in %s at %s:%u",
+ expr, func, fname, line);
+ dump_backtrace(buf);
fprintf(stderr,"%s:%u: %s: Assertion %s failed; aborting.\n",
fname, line, func, expr);
}