backtrace.c 5.3 KB

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