backtrace.c 5.5 KB

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