backtrace.c 4.7 KB

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