backtrace.c 4.7 KB

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