backtrace.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Copyright (c) 2013-2017, 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. #include "orconfig.h"
  15. #include "compat.h"
  16. #include "util.h"
  17. #include "torlog.h"
  18. #ifdef HAVE_EXECINFO_H
  19. #include <execinfo.h>
  20. #endif
  21. #ifdef HAVE_FCNTL_H
  22. #include <fcntl.h>
  23. #endif
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. #ifdef HAVE_SIGNAL_H
  28. #include <signal.h>
  29. #endif
  30. #ifdef HAVE_CYGWIN_SIGNAL_H
  31. #include <cygwin/signal.h>
  32. #elif defined(HAVE_SYS_UCONTEXT_H)
  33. #include <sys/ucontext.h>
  34. #elif defined(HAVE_UCONTEXT_H)
  35. #include <ucontext.h>
  36. #endif
  37. #define EXPOSE_CLEAN_BACKTRACE
  38. #include "backtrace.h"
  39. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && \
  40. defined(HAVE_BACKTRACE_SYMBOLS_FD) && defined(HAVE_SIGACTION)
  41. #define USE_BACKTRACE
  42. #endif
  43. #if !defined(USE_BACKTRACE)
  44. #define NO_BACKTRACE_IMPL
  45. #endif
  46. /** Version of Tor to report in backtrace messages. */
  47. static char *bt_version = NULL;
  48. #ifdef USE_BACKTRACE
  49. /** Largest stack depth to try to dump. */
  50. #define MAX_DEPTH 256
  51. /** Static allocation of stack to dump. This is static so we avoid stack
  52. * pressure. */
  53. static void *cb_buf[MAX_DEPTH];
  54. /** Protects cb_buf from concurrent access */
  55. static tor_mutex_t cb_buf_mutex;
  56. /** Change a stacktrace in <b>stack</b> of depth <b>depth</b> so that it will
  57. * log the correct function from which a signal was received with context
  58. * <b>ctx</b>. (When we get a signal, the current function will not have
  59. * called any other function, and will therefore have not pushed its address
  60. * onto the stack. Fortunately, we usually have the program counter in the
  61. * ucontext_t structure.
  62. */
  63. void
  64. clean_backtrace(void **stack, size_t depth, const ucontext_t *ctx)
  65. {
  66. #ifdef PC_FROM_UCONTEXT
  67. #if defined(__linux__)
  68. const size_t n = 1;
  69. #elif defined(__darwin__) || defined(__APPLE__) || defined(OpenBSD) \
  70. || defined(__FreeBSD__)
  71. const size_t n = 2;
  72. #else
  73. const size_t n = 1;
  74. #endif
  75. if (depth <= n)
  76. return;
  77. stack[n] = (void*) ctx->PC_FROM_UCONTEXT;
  78. #else
  79. (void) depth;
  80. (void) ctx;
  81. (void) stack;
  82. #endif
  83. }
  84. /** Log a message <b>msg</b> at <b>severity</b> in <b>domain</b>, and follow
  85. * that with a backtrace log. */
  86. void
  87. log_backtrace(int severity, int domain, const char *msg)
  88. {
  89. size_t depth;
  90. char **symbols;
  91. size_t i;
  92. tor_mutex_acquire(&cb_buf_mutex);
  93. depth = backtrace(cb_buf, MAX_DEPTH);
  94. symbols = backtrace_symbols(cb_buf, (int)depth);
  95. tor_log(severity, domain, "%s. Stack trace:", msg);
  96. if (!symbols) {
  97. /* LCOV_EXCL_START -- we can't provoke this. */
  98. tor_log(severity, domain, " Unable to generate backtrace.");
  99. goto done;
  100. /* LCOV_EXCL_STOP */
  101. }
  102. for (i=0; i < depth; ++i) {
  103. tor_log(severity, domain, " %s", symbols[i]);
  104. }
  105. raw_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. /* LCOV_EXCL_START */
  150. log_warn(LD_BUG, "Sigaction failed: %s", strerror(errno));
  151. rv = -1;
  152. /* LCOV_EXCL_STOP */
  153. }
  154. }
  155. {
  156. /* Now, generate (but do not log) a backtrace. This ensures that
  157. * libc has pre-loaded the symbols we need to dump things, so that later
  158. * reads won't be denied by the sandbox code */
  159. char **symbols;
  160. size_t depth = backtrace(cb_buf, MAX_DEPTH);
  161. symbols = backtrace_symbols(cb_buf, (int) depth);
  162. if (symbols)
  163. raw_free(symbols);
  164. }
  165. return rv;
  166. }
  167. /** Uninstall crash handlers. */
  168. static void
  169. remove_bt_handler(void)
  170. {
  171. tor_mutex_uninit(&cb_buf_mutex);
  172. }
  173. #endif
  174. #ifdef NO_BACKTRACE_IMPL
  175. void
  176. log_backtrace(int severity, int domain, const char *msg)
  177. {
  178. tor_log(severity, domain, "%s. (Stack trace not available)", msg);
  179. }
  180. static int
  181. install_bt_handler(void)
  182. {
  183. return 0;
  184. }
  185. static void
  186. remove_bt_handler(void)
  187. {
  188. }
  189. #endif
  190. /** Set up code to handle generating error messages on crashes. */
  191. int
  192. configure_backtrace_handler(const char *tor_version)
  193. {
  194. tor_free(bt_version);
  195. if (tor_version)
  196. tor_asprintf(&bt_version, "Tor %s", tor_version);
  197. else
  198. tor_asprintf(&bt_version, "Tor");
  199. return install_bt_handler();
  200. }
  201. /** Perform end-of-process cleanup for code that generates error messages on
  202. * crashes. */
  203. void
  204. clean_up_backtrace_handler(void)
  205. {
  206. remove_bt_handler();
  207. tor_free(bt_version);
  208. }