backtrace.c 4.9 KB

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