backtrace.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright (c) 2013-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file backtrace.c
  5. *
  6. * \brief Functions to produce backtraces on bugs, crashes, or assertion
  7. * failures.
  8. *
  9. * Currently, we've only got an implementation here using the backtrace()
  10. * family of functions, which are sometimes provided by libc and sometimes
  11. * provided by libexecinfo. We tie into the sigaction() backend in order to
  12. * detect crashes.
  13. */
  14. #define __USE_GNU
  15. #define _GNU_SOURCE 1
  16. #include "orconfig.h"
  17. #include "compat.h"
  18. #include "util.h"
  19. #include "torlog.h"
  20. #ifdef HAVE_EXECINFO_H
  21. #include <execinfo.h>
  22. #endif
  23. #ifdef HAVE_FCNTL_H
  24. #include <fcntl.h>
  25. #endif
  26. #ifdef HAVE_UNISTD_H
  27. #include <unistd.h>
  28. #endif
  29. #ifdef HAVE_SIGNAL_H
  30. #include <signal.h>
  31. #endif
  32. #ifdef HAVE_CYGWIN_SIGNAL_H
  33. #include <cygwin/signal.h>
  34. #elif defined(HAVE_SYS_UCONTEXT_H)
  35. #include <sys/ucontext.h>
  36. #elif defined(HAVE_UCONTEXT_H)
  37. #include <ucontext.h>
  38. #endif
  39. #define EXPOSE_CLEAN_BACKTRACE
  40. #include "backtrace.h"
  41. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && \
  42. defined(HAVE_BACKTRACE_SYMBOLS_FD) && defined(HAVE_SIGACTION)
  43. #define USE_BACKTRACE
  44. #endif
  45. #if !defined(USE_BACKTRACE)
  46. #define NO_BACKTRACE_IMPL
  47. #endif
  48. /** Version of Tor to report in backtrace messages. */
  49. static char *bt_version = NULL;
  50. #ifdef USE_BACKTRACE
  51. /** Largest stack depth to try to dump. */
  52. #define MAX_DEPTH 256
  53. /** Static allocation of stack to dump. This is static so we avoid stack
  54. * pressure. */
  55. static void *cb_buf[MAX_DEPTH];
  56. /** Protects cb_buf from concurrent access */
  57. static tor_mutex_t cb_buf_mutex;
  58. /** Change a stacktrace in <b>stack</b> of depth <b>depth</b> so that it will
  59. * log the correct function from which a signal was received with context
  60. * <b>ctx</b>. (When we get a signal, the current function will not have
  61. * called any other function, and will therefore have not pushed its address
  62. * onto the stack. Fortunately, we usually have the program counter in the
  63. * ucontext_t structure.
  64. */
  65. void
  66. clean_backtrace(void **stack, size_t depth, const ucontext_t *ctx)
  67. {
  68. #ifdef PC_FROM_UCONTEXT
  69. #if defined(__linux__)
  70. const size_t n = 1;
  71. #elif defined(__darwin__) || defined(__APPLE__) || defined(__OpenBSD__) \
  72. || defined(__FreeBSD__)
  73. const size_t n = 2;
  74. #else
  75. const size_t n = 1;
  76. #endif
  77. if (depth <= n)
  78. return;
  79. stack[n] = (void*) ctx->PC_FROM_UCONTEXT;
  80. #else
  81. (void) depth;
  82. (void) ctx;
  83. (void) stack;
  84. #endif
  85. }
  86. /** Log a message <b>msg</b> at <b>severity</b> in <b>domain</b>, and follow
  87. * that with a backtrace log. */
  88. void
  89. log_backtrace(int severity, int domain, const char *msg)
  90. {
  91. size_t depth;
  92. char **symbols;
  93. size_t i;
  94. tor_mutex_acquire(&cb_buf_mutex);
  95. depth = backtrace(cb_buf, MAX_DEPTH);
  96. symbols = backtrace_symbols(cb_buf, (int)depth);
  97. tor_log(severity, domain, "%s. Stack trace:", msg);
  98. if (!symbols) {
  99. tor_log(severity, domain, " Unable to generate backtrace.");
  100. goto done;
  101. }
  102. for (i=0; i < depth; ++i) {
  103. tor_log(severity, domain, " %s", symbols[i]);
  104. }
  105. free(symbols);
  106. done:
  107. tor_mutex_release(&cb_buf_mutex);
  108. }
  109. static void crash_handler(int sig, siginfo_t *si, void *ctx_)
  110. __attribute__((noreturn));
  111. /** Signal handler: write a crash message with a stack trace, and die. */
  112. static void
  113. crash_handler(int sig, siginfo_t *si, void *ctx_)
  114. {
  115. char buf[40];
  116. size_t depth;
  117. ucontext_t *ctx = (ucontext_t *) ctx_;
  118. int n_fds, i;
  119. const int *fds = NULL;
  120. (void) si;
  121. depth = backtrace(cb_buf, MAX_DEPTH);
  122. /* Clean up the top stack frame so we get the real function
  123. * name for the most recently failing function. */
  124. clean_backtrace(cb_buf, depth, ctx);
  125. format_dec_number_sigsafe((unsigned)sig, buf, sizeof(buf));
  126. tor_log_err_sigsafe(bt_version, " died: Caught signal ", buf, "\n",
  127. NULL);
  128. n_fds = tor_log_get_sigsafe_err_fds(&fds);
  129. for (i=0; i < n_fds; ++i)
  130. backtrace_symbols_fd(cb_buf, (int)depth, fds[i]);
  131. abort();
  132. }
  133. /** Install signal handlers as needed so that when we crash, we produce a
  134. * useful stack trace. Return 0 on success, -1 on failure. */
  135. static int
  136. install_bt_handler(void)
  137. {
  138. int trap_signals[] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGSYS,
  139. SIGIO, -1 };
  140. int i, rv=0;
  141. struct sigaction sa;
  142. tor_mutex_init(&cb_buf_mutex);
  143. memset(&sa, 0, sizeof(sa));
  144. sa.sa_sigaction = crash_handler;
  145. sa.sa_flags = SA_SIGINFO;
  146. sigfillset(&sa.sa_mask);
  147. for (i = 0; trap_signals[i] >= 0; ++i) {
  148. if (sigaction(trap_signals[i], &sa, NULL) == -1) {
  149. log_warn(LD_BUG, "Sigaction failed: %s", strerror(errno));
  150. rv = -1;
  151. }
  152. }
  153. {
  154. /* Now, generate (but do not log) a backtrace. This ensures that
  155. * libc has pre-loaded the symbols we need to dump things, so that later
  156. * reads won't be denied by the sandbox code */
  157. char **symbols;
  158. size_t depth = backtrace(cb_buf, MAX_DEPTH);
  159. symbols = backtrace_symbols(cb_buf, (int) depth);
  160. if (symbols)
  161. free(symbols);
  162. }
  163. return rv;
  164. }
  165. /** Uninstall crash handlers. */
  166. static void
  167. remove_bt_handler(void)
  168. {
  169. tor_mutex_uninit(&cb_buf_mutex);
  170. }
  171. #endif
  172. #ifdef NO_BACKTRACE_IMPL
  173. void
  174. log_backtrace(int severity, int domain, const char *msg)
  175. {
  176. tor_log(severity, domain, "%s. (Stack trace not available)", msg);
  177. }
  178. static int
  179. install_bt_handler(void)
  180. {
  181. return 0;
  182. }
  183. static void
  184. remove_bt_handler(void)
  185. {
  186. }
  187. #endif
  188. /** Set up code to handle generating error messages on crashes. */
  189. int
  190. configure_backtrace_handler(const char *tor_version)
  191. {
  192. tor_free(bt_version);
  193. if (tor_version)
  194. tor_asprintf(&bt_version, "Tor %s", tor_version);
  195. else
  196. tor_asprintf(&bt_version, "Tor");
  197. return install_bt_handler();
  198. }
  199. /** Perform end-of-process cleanup for code that generates error messages on
  200. * crashes. */
  201. void
  202. clean_up_backtrace_handler(void)
  203. {
  204. remove_bt_handler();
  205. tor_free(bt_version);
  206. }