backtrace.c 7.2 KB

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