backtrace.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* Copyright (c) 2013-2015, 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. (void) stack;
  73. #endif
  74. }
  75. /** Log a message <b>msg</b> at <b>severity</b> in <b>domain</b>, and follow
  76. * that with a backtrace log. */
  77. void
  78. log_backtrace(int severity, int domain, const char *msg)
  79. {
  80. int depth;
  81. char **symbols;
  82. int i;
  83. tor_mutex_acquire(&cb_buf_mutex);
  84. depth = backtrace(cb_buf, MAX_DEPTH);
  85. symbols = backtrace_symbols(cb_buf, depth);
  86. tor_log(severity, domain, "%s. Stack trace:", msg);
  87. if (!symbols) {
  88. tor_log(severity, domain, " Unable to generate backtrace.");
  89. goto done;
  90. }
  91. for (i=0; i < depth; ++i) {
  92. tor_log(severity, domain, " %s", symbols[i]);
  93. }
  94. free(symbols);
  95. done:
  96. tor_mutex_release(&cb_buf_mutex);
  97. }
  98. static void crash_handler(int sig, siginfo_t *si, void *ctx_)
  99. __attribute__((noreturn));
  100. /** Signal handler: write a crash message with a stack trace, and die. */
  101. static void
  102. crash_handler(int sig, siginfo_t *si, void *ctx_)
  103. {
  104. char buf[40];
  105. int depth;
  106. ucontext_t *ctx = (ucontext_t *) ctx_;
  107. int n_fds, i;
  108. const int *fds = NULL;
  109. (void) si;
  110. depth = backtrace(cb_buf, MAX_DEPTH);
  111. /* Clean up the top stack frame so we get the real function
  112. * name for the most recently failing function. */
  113. clean_backtrace(cb_buf, depth, ctx);
  114. format_dec_number_sigsafe((unsigned)sig, buf, sizeof(buf));
  115. tor_log_err_sigsafe(bt_version, " died: Caught signal ", buf, "\n",
  116. NULL);
  117. n_fds = tor_log_get_sigsafe_err_fds(&fds);
  118. for (i=0; i < n_fds; ++i)
  119. backtrace_symbols_fd(cb_buf, depth, fds[i]);
  120. abort();
  121. }
  122. /** Install signal handlers as needed so that when we crash, we produce a
  123. * useful stack trace. Return 0 on success, -1 on failure. */
  124. static int
  125. install_bt_handler(void)
  126. {
  127. int trap_signals[] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGSYS,
  128. SIGIO, -1 };
  129. int i, rv=0;
  130. struct sigaction sa;
  131. tor_mutex_init(&cb_buf_mutex);
  132. memset(&sa, 0, sizeof(sa));
  133. sa.sa_sigaction = crash_handler;
  134. sa.sa_flags = SA_SIGINFO;
  135. sigfillset(&sa.sa_mask);
  136. for (i = 0; trap_signals[i] >= 0; ++i) {
  137. if (sigaction(trap_signals[i], &sa, NULL) == -1) {
  138. log_warn(LD_BUG, "Sigaction failed: %s", strerror(errno));
  139. rv = -1;
  140. }
  141. }
  142. {
  143. /* Now, generate (but do not log) a backtrace. This ensures that
  144. * libc has pre-loaded the symbols we need to dump things, so that later
  145. * reads won't be denied by the sandbox code */
  146. char **symbols;
  147. int depth = backtrace(cb_buf, MAX_DEPTH);
  148. symbols = backtrace_symbols(cb_buf, depth);
  149. if (symbols)
  150. free(symbols);
  151. }
  152. return rv;
  153. }
  154. /** Uninstall crash handlers. */
  155. static void
  156. remove_bt_handler(void)
  157. {
  158. tor_mutex_uninit(&cb_buf_mutex);
  159. }
  160. #endif
  161. #ifdef NO_BACKTRACE_IMPL
  162. void
  163. log_backtrace(int severity, int domain, const char *msg)
  164. {
  165. tor_log(severity, domain, "%s. (Stack trace not available)", msg);
  166. }
  167. static int
  168. install_bt_handler(void)
  169. {
  170. return 0;
  171. }
  172. static void
  173. remove_bt_handler(void)
  174. {
  175. }
  176. #endif
  177. /** Set up code to handle generating error messages on crashes. */
  178. int
  179. configure_backtrace_handler(const char *tor_version)
  180. {
  181. tor_free(bt_version);
  182. if (!tor_version)
  183. tor_version = "";
  184. tor_asprintf(&bt_version, "Tor %s", tor_version);
  185. return install_bt_handler();
  186. }
  187. /** Perform end-of-process cleanup for code that generates error messages on
  188. * crashes. */
  189. void
  190. clean_up_backtrace_handler(void)
  191. {
  192. remove_bt_handler();
  193. tor_free(bt_version);
  194. }