backtrace.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* Copyright (c) 2013-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file backtrace.c
  5. *
  6. * \brief Functions to produce backtraces on bugs, crashes, or assertion
  7. * failures.
  8. *
  9. * Currently, we've only got an implementation here using the backtrace()
  10. * family of functions, which are sometimes provided by libc and sometimes
  11. * provided by libexecinfo. We tie into the sigaction() backend in order to
  12. * detect crashes.
  13. */
  14. #include "orconfig.h"
  15. #include "lib/err/torerr.h"
  16. #ifdef HAVE_EXECINFO_H
  17. #include <execinfo.h>
  18. #endif
  19. #ifdef HAVE_FCNTL_H
  20. #include <fcntl.h>
  21. #endif
  22. #ifdef HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #ifdef HAVE_SIGNAL_H
  26. #include <signal.h>
  27. #endif
  28. #include <errno.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #ifdef HAVE_CYGWIN_SIGNAL_H
  32. #include <cygwin/signal.h>
  33. #elif defined(HAVE_SYS_UCONTEXT_H)
  34. #include <sys/ucontext.h>
  35. #elif defined(HAVE_UCONTEXT_H)
  36. #include <ucontext.h>
  37. #endif /* defined(HAVE_CYGWIN_SIGNAL_H) || ... */
  38. #ifdef HAVE_PTHREAD_H
  39. #include <pthread.h>
  40. #endif
  41. #define EXPOSE_CLEAN_BACKTRACE
  42. #include "lib/err/backtrace.h"
  43. #include "lib/err/torerr.h"
  44. #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && \
  45. defined(HAVE_BACKTRACE_SYMBOLS_FD) && defined(HAVE_SIGACTION)
  46. #define USE_BACKTRACE
  47. #endif
  48. #if !defined(USE_BACKTRACE)
  49. #define NO_BACKTRACE_IMPL
  50. #endif
  51. // Redundant with util.h, but doing it here so we can avoid that dependency.
  52. #define raw_free free
  53. #ifdef USE_BACKTRACE
  54. /** Version of Tor to report in backtrace messages. */
  55. static char bt_version[128] = "";
  56. /** Largest stack depth to try to dump. */
  57. #define MAX_DEPTH 256
  58. /** Static allocation of stack to dump. This is static so we avoid stack
  59. * pressure. */
  60. static void *cb_buf[MAX_DEPTH];
  61. /** Protects cb_buf from concurrent access. Pthreads, since this code
  62. * is Unix-only, and since this code needs to be lowest-level. */
  63. static pthread_mutex_t cb_buf_mutex = PTHREAD_MUTEX_INITIALIZER;
  64. /** Change a stacktrace in <b>stack</b> of depth <b>depth</b> so that it will
  65. * log the correct function from which a signal was received with context
  66. * <b>ctx</b>. (When we get a signal, the current function will not have
  67. * called any other function, and will therefore have not pushed its address
  68. * onto the stack. Fortunately, we usually have the program counter in the
  69. * ucontext_t structure.
  70. */
  71. void
  72. clean_backtrace(void **stack, size_t depth, const ucontext_t *ctx)
  73. {
  74. #ifdef PC_FROM_UCONTEXT
  75. #if defined(__linux__)
  76. const size_t n = 1;
  77. #elif defined(__darwin__) || defined(__APPLE__) || defined(OpenBSD) \
  78. || defined(__FreeBSD__)
  79. const size_t n = 2;
  80. #else
  81. const size_t n = 1;
  82. #endif /* defined(__linux__) || ... */
  83. if (depth <= n)
  84. return;
  85. stack[n] = (void*) ctx->PC_FROM_UCONTEXT;
  86. #else /* !(defined(PC_FROM_UCONTEXT)) */
  87. (void) depth;
  88. (void) ctx;
  89. (void) stack;
  90. #endif /* defined(PC_FROM_UCONTEXT) */
  91. }
  92. /** Log a message <b>msg</b> at <b>severity</b> in <b>domain</b>, and follow
  93. * that with a backtrace log. Send messages via the tor_log function at
  94. * logger". */
  95. void
  96. log_backtrace_impl(int severity, int domain, const char *msg,
  97. tor_log_fn logger)
  98. {
  99. size_t depth;
  100. char **symbols;
  101. size_t i;
  102. pthread_mutex_lock(&cb_buf_mutex);
  103. depth = backtrace(cb_buf, MAX_DEPTH);
  104. symbols = backtrace_symbols(cb_buf, (int)depth);
  105. logger(severity, domain, "%s. Stack trace:", msg);
  106. if (!symbols) {
  107. /* LCOV_EXCL_START -- we can't provoke this. */
  108. logger(severity, domain, " Unable to generate backtrace.");
  109. goto done;
  110. /* LCOV_EXCL_STOP */
  111. }
  112. for (i=0; i < depth; ++i) {
  113. logger(severity, domain, " %s", symbols[i]);
  114. }
  115. raw_free(symbols);
  116. done:
  117. pthread_mutex_unlock(&cb_buf_mutex);
  118. }
  119. static void crash_handler(int sig, siginfo_t *si, void *ctx_)
  120. __attribute__((noreturn));
  121. /** Signal handler: write a crash message with a stack trace, and die. */
  122. static void
  123. crash_handler(int sig, siginfo_t *si, void *ctx_)
  124. {
  125. char buf[40];
  126. size_t depth;
  127. ucontext_t *ctx = (ucontext_t *) ctx_;
  128. int n_fds, i;
  129. const int *fds = NULL;
  130. (void) si;
  131. depth = backtrace(cb_buf, MAX_DEPTH);
  132. /* Clean up the top stack frame so we get the real function
  133. * name for the most recently failing function. */
  134. clean_backtrace(cb_buf, depth, ctx);
  135. format_dec_number_sigsafe((unsigned)sig, buf, sizeof(buf));
  136. tor_log_err_sigsafe(bt_version, " died: Caught signal ", buf, "\n",
  137. NULL);
  138. n_fds = tor_log_get_sigsafe_err_fds(&fds);
  139. for (i=0; i < n_fds; ++i)
  140. backtrace_symbols_fd(cb_buf, (int)depth, fds[i]);
  141. abort();
  142. }
  143. /** Write a backtrace to all of the emergency-error fds. */
  144. void
  145. dump_stack_symbols_to_error_fds(void)
  146. {
  147. int n_fds, i;
  148. const int *fds = NULL;
  149. size_t depth;
  150. depth = backtrace(cb_buf, MAX_DEPTH);
  151. n_fds = tor_log_get_sigsafe_err_fds(&fds);
  152. for (i=0; i < n_fds; ++i)
  153. backtrace_symbols_fd(cb_buf, (int)depth, fds[i]);
  154. }
  155. /** Install signal handlers as needed so that when we crash, we produce a
  156. * useful stack trace. Return 0 on success, -errno on failure. */
  157. static int
  158. install_bt_handler(const char *software)
  159. {
  160. int trap_signals[] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGSYS,
  161. SIGIO, -1 };
  162. int i, rv=0;
  163. strncpy(bt_version, software, sizeof(bt_version) - 1);
  164. bt_version[sizeof(bt_version) - 1] = 0;
  165. struct sigaction sa;
  166. memset(&sa, 0, sizeof(sa));
  167. sa.sa_sigaction = crash_handler;
  168. sa.sa_flags = SA_SIGINFO;
  169. sigfillset(&sa.sa_mask);
  170. for (i = 0; trap_signals[i] >= 0; ++i) {
  171. if (sigaction(trap_signals[i], &sa, NULL) == -1) {
  172. /* LCOV_EXCL_START */
  173. rv = -errno;
  174. /* LCOV_EXCL_STOP */
  175. }
  176. }
  177. {
  178. /* Now, generate (but do not log) a backtrace. This ensures that
  179. * libc has pre-loaded the symbols we need to dump things, so that later
  180. * reads won't be denied by the sandbox code */
  181. char **symbols;
  182. size_t depth = backtrace(cb_buf, MAX_DEPTH);
  183. symbols = backtrace_symbols(cb_buf, (int) depth);
  184. if (symbols)
  185. raw_free(symbols);
  186. }
  187. return rv;
  188. }
  189. /** Uninstall crash handlers. */
  190. static void
  191. remove_bt_handler(void)
  192. {
  193. }
  194. #endif /* defined(USE_BACKTRACE) */
  195. #ifdef NO_BACKTRACE_IMPL
  196. void
  197. log_backtrace_impl(int severity, int domain, const char *msg,
  198. tor_log_fn logger)
  199. {
  200. logger(severity, domain, "%s. (Stack trace not available)", msg);
  201. }
  202. static int
  203. install_bt_handler(const char *software)
  204. {
  205. (void) software;
  206. return 0;
  207. }
  208. static void
  209. remove_bt_handler(void)
  210. {
  211. }
  212. void
  213. dump_stack_symbols_to_error_fds(void)
  214. {
  215. }
  216. #endif /* defined(NO_BACKTRACE_IMPL) */
  217. /** Set up code to handle generating error messages on crashes. */
  218. int
  219. configure_backtrace_handler(const char *tor_version)
  220. {
  221. char version[128];
  222. strncpy(version, "Tor", sizeof(version)-1);
  223. if (tor_version) {
  224. strncat(version, " ", sizeof(version)-1);
  225. strncat(version, tor_version, sizeof(version)-1);
  226. }
  227. version[sizeof(version) - 1] = 0;
  228. return install_bt_handler(version);
  229. }
  230. /** Perform end-of-process cleanup for code that generates error messages on
  231. * crashes. */
  232. void
  233. clean_up_backtrace_handler(void)
  234. {
  235. remove_bt_handler();
  236. }