backtrace.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /** Change a stacktrace in <b>stack</b> of depth <b>depth</b> so that it will
  45. * log the correct function from which a signal was received with context
  46. * <b>ctx</b>. (When we get a signal, the current function will not have
  47. * called any other function, and will therefore have not pushed its address
  48. * onto the stack. Fortunately, we usually have the program counter in the
  49. * ucontext_t structure.
  50. */
  51. static void
  52. clean_backtrace(void **stack, int depth, const ucontext_t *ctx)
  53. {
  54. #ifdef PC_FROM_UCONTEXT
  55. #if defined(__linux__)
  56. const int n = 1;
  57. #elif defined(__darwin__) || defined(__APPLE__) || defined(__OpenBSD__) \
  58. || defined(__FreeBSD__)
  59. const int n = 2;
  60. #else
  61. const int n = 1;
  62. #endif
  63. if (depth <= n)
  64. return;
  65. stack[n] = (void*) ctx->PC_FROM_UCONTEXT;
  66. #else
  67. (void) depth;
  68. (void) ctx;
  69. #endif
  70. }
  71. /** Log a message <b>msg</b> at <b>severity</b> in <b>domain</b>, and follow
  72. * that with a backtrace log. */
  73. void
  74. log_backtrace(int severity, int domain, const char *msg)
  75. {
  76. int depth = backtrace(cb_buf, MAX_DEPTH);
  77. char **symbols = backtrace_symbols(cb_buf, depth);
  78. int i;
  79. tor_log(severity, domain, "%s. Stack trace:", msg);
  80. if (!symbols) {
  81. tor_log(severity, domain, " Unable to generate backtrace.");
  82. return;
  83. }
  84. for (i=0; i < depth; ++i) {
  85. tor_log(severity, domain, " %s", symbols[i]);
  86. }
  87. free(symbols);
  88. }
  89. static void crash_handler(int sig, siginfo_t *si, void *ctx_)
  90. __attribute__((noreturn));
  91. /** Signal handler: write a crash message with a stack trace, and die. */
  92. static void
  93. crash_handler(int sig, siginfo_t *si, void *ctx_)
  94. {
  95. char buf[40];
  96. int depth;
  97. ucontext_t *ctx = (ucontext_t *) ctx_;
  98. int n_fds, i;
  99. const int *fds = NULL;
  100. (void) si;
  101. depth = backtrace(cb_buf, MAX_DEPTH);
  102. /* Clean up the top stack frame so we get the real function
  103. * name for the most recently failing function. */
  104. clean_backtrace(cb_buf, depth, ctx);
  105. format_dec_number_sigsafe((unsigned)sig, buf, sizeof(buf));
  106. tor_log_err_sigsafe(bt_version, " died: Caught signal ", buf, "\n",
  107. NULL);
  108. n_fds = tor_log_get_sigsafe_err_fds(&fds);
  109. for (i=0; i < n_fds; ++i)
  110. backtrace_symbols_fd(cb_buf, depth, fds[i]);
  111. abort();
  112. }
  113. /** Install signal handlers as needed so that when we crash, we produce a
  114. * useful stack trace. Return 0 on success, -1 on failure. */
  115. static int
  116. install_bt_handler(void)
  117. {
  118. int trap_signals[] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGSYS,
  119. SIGIO, -1 };
  120. int i, rv=0;
  121. struct sigaction sa;
  122. memset(&sa, 0, sizeof(sa));
  123. sa.sa_sigaction = crash_handler;
  124. sa.sa_flags = SA_SIGINFO;
  125. sigfillset(&sa.sa_mask);
  126. for (i = 0; trap_signals[i] >= 0; ++i) {
  127. if (sigaction(trap_signals[i], &sa, NULL) == -1) {
  128. log_warn(LD_BUG, "Sigaction failed: %s", strerror(errno));
  129. rv = -1;
  130. }
  131. }
  132. return rv;
  133. }
  134. /** Uninstall crash handlers. */
  135. static void
  136. remove_bt_handler(void)
  137. {
  138. /* We don't need to actually free anything at exit here. */
  139. }
  140. #endif
  141. #ifdef NO_BACKTRACE_IMPL
  142. void
  143. log_backtrace(int severity, int domain, const char *msg)
  144. {
  145. tor_log(severity, domain, "%s. (Stack trace not available)", msg);
  146. }
  147. static int
  148. install_bt_handler(void)
  149. {
  150. return 0;
  151. }
  152. static void
  153. remove_bt_handler(void)
  154. {
  155. }
  156. #endif
  157. /** Set up code to handle generating error messages on crashes. */
  158. int
  159. configure_backtrace_handler(const char *tor_version)
  160. {
  161. tor_free(bt_version);
  162. if (!tor_version)
  163. tor_version = "";
  164. tor_asprintf(&bt_version, "Tor %s", tor_version);
  165. return install_bt_handler();
  166. }
  167. /** Perform end-of-process cleanup for code that generates error messages on
  168. * crashes. */
  169. void
  170. clean_up_backtrace_handler(void)
  171. {
  172. remove_bt_handler();
  173. tor_free(bt_version);
  174. }