backtrace.c 6.1 KB

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