backtrace.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /*XXXX make this idempotent */
  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. /*XXXX writeme*/
  138. }
  139. #endif
  140. #ifdef NO_BACKTRACE_IMPL
  141. /**DOCDOC */
  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. /**DOCDOC*/
  148. static int
  149. install_bt_handler(void)
  150. {
  151. return 0;
  152. }
  153. /**DOCDOC*/
  154. static void
  155. remove_bt_handler(void)
  156. {
  157. }
  158. #endif
  159. /**DOCDOC*/
  160. int
  161. configure_backtrace_handler(const char *tor_version)
  162. {
  163. tor_free(bt_version);
  164. if (!tor_version)
  165. tor_version = "";
  166. tor_asprintf(&bt_version, "Tor %s", tor_version);
  167. return install_bt_handler();
  168. }
  169. /**DOCDOC*/
  170. void
  171. clean_up_backtrace_handler(void)
  172. {
  173. remove_bt_handler();
  174. tor_free(bt_version);
  175. }