util_bug.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file util_bug.c
  7. **/
  8. #include "orconfig.h"
  9. #include "lib/log/util_bug.h"
  10. #include "lib/log/log.h"
  11. #include "lib/err/backtrace.h"
  12. #include "lib/err/torerr.h"
  13. #ifdef TOR_UNIT_TESTS
  14. #include "lib/smartlist_core/smartlist_core.h"
  15. #include "lib/smartlist_core/smartlist_foreach.h"
  16. #endif
  17. #include "lib/malloc/malloc.h"
  18. #include "lib/string/printf.h"
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #ifdef TOR_UNIT_TESTS
  22. static void (*failed_assertion_cb)(void) = NULL;
  23. static int n_bugs_to_capture = 0;
  24. static smartlist_t *bug_messages = NULL;
  25. #define capturing_bugs() (bug_messages != NULL && n_bugs_to_capture)
  26. void
  27. tor_capture_bugs_(int n)
  28. {
  29. tor_end_capture_bugs_();
  30. bug_messages = smartlist_new();
  31. n_bugs_to_capture = n;
  32. }
  33. void
  34. tor_end_capture_bugs_(void)
  35. {
  36. n_bugs_to_capture = 0;
  37. if (!bug_messages)
  38. return;
  39. SMARTLIST_FOREACH(bug_messages, char *, cp, tor_free(cp));
  40. smartlist_free(bug_messages);
  41. bug_messages = NULL;
  42. }
  43. const smartlist_t *
  44. tor_get_captured_bug_log_(void)
  45. {
  46. return bug_messages;
  47. }
  48. static void
  49. add_captured_bug(const char *s)
  50. {
  51. --n_bugs_to_capture;
  52. smartlist_add_strdup(bug_messages, s);
  53. }
  54. /** Set a callback to be invoked when we get any tor_bug_occurred_
  55. * invocation. We use this in the unit tests so that a nonfatal
  56. * assertion failure can also count as a test failure.
  57. */
  58. void
  59. tor_set_failed_assertion_callback(void (*fn)(void))
  60. {
  61. failed_assertion_cb = fn;
  62. }
  63. #else /* !(defined(TOR_UNIT_TESTS)) */
  64. #define capturing_bugs() (0)
  65. #define add_captured_bug(s) do { } while (0)
  66. #endif /* defined(TOR_UNIT_TESTS) */
  67. /** Helper for tor_assert: report the assertion failure. */
  68. void
  69. CHECK_PRINTF(5, 6)
  70. tor_assertion_failed_(const char *fname, unsigned int line,
  71. const char *func, const char *expr,
  72. const char *fmt, ...)
  73. {
  74. char *buf = NULL;
  75. char *extra = NULL;
  76. va_list ap;
  77. #ifdef __clang__
  78. #pragma clang diagnostic push
  79. #pragma clang diagnostic ignored "-Wformat-nonliteral"
  80. #endif
  81. if (fmt) {
  82. va_start(ap,fmt);
  83. tor_vasprintf(&extra, fmt, ap);
  84. va_end(ap);
  85. }
  86. #ifdef __clang__
  87. #pragma clang diagnostic pop
  88. #endif
  89. log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.",
  90. fname, line, func, expr);
  91. tor_asprintf(&buf, "Assertion %s failed in %s at %s:%u: %s",
  92. expr, func, fname, line, extra ? extra : "");
  93. tor_free(extra);
  94. log_backtrace(LOG_ERR, LD_BUG, buf);
  95. tor_free(buf);
  96. }
  97. /** Helper for tor_assert_nonfatal: report the assertion failure. */
  98. void
  99. CHECK_PRINTF(6, 7)
  100. tor_bug_occurred_(const char *fname, unsigned int line,
  101. const char *func, const char *expr,
  102. int once, const char *fmt, ...)
  103. {
  104. char *buf = NULL;
  105. const char *once_str = once ?
  106. " (Future instances of this warning will be silenced.)": "";
  107. if (! expr) {
  108. if (capturing_bugs()) {
  109. add_captured_bug("This line should not have been reached.");
  110. return;
  111. }
  112. log_warn(LD_BUG, "%s:%u: %s: This line should not have been reached.%s",
  113. fname, line, func, once_str);
  114. tor_asprintf(&buf,
  115. "Line unexpectedly reached at %s at %s:%u",
  116. func, fname, line);
  117. } else {
  118. if (capturing_bugs()) {
  119. add_captured_bug(expr);
  120. return;
  121. }
  122. va_list ap;
  123. char *extra = NULL;
  124. #ifdef __clang__
  125. #pragma clang diagnostic push
  126. #pragma clang diagnostic ignored "-Wformat-nonliteral"
  127. #endif
  128. if (fmt) {
  129. va_start(ap,fmt);
  130. tor_vasprintf(&extra, fmt, ap);
  131. va_end(ap);
  132. }
  133. #ifdef __clang__
  134. #pragma clang diagnostic pop
  135. #endif
  136. log_warn(LD_BUG, "%s:%u: %s: Non-fatal assertion %s failed.%s",
  137. fname, line, func, expr, once_str);
  138. tor_asprintf(&buf, "Non-fatal assertion %s failed in %s at %s:%u%s%s",
  139. expr, func, fname, line, fmt ? " : " : "",
  140. extra ? extra : "");
  141. tor_free(extra);
  142. }
  143. log_backtrace(LOG_WARN, LD_BUG, buf);
  144. tor_free(buf);
  145. #ifdef TOR_UNIT_TESTS
  146. if (failed_assertion_cb) {
  147. failed_assertion_cb();
  148. }
  149. #endif
  150. }
  151. /**
  152. * Call the tor_raw_abort_() function to close raw logs, then kill the current
  153. * process with a fatal error. But first, close the file-based log file
  154. * descriptors, so error messages are written before process termination.
  155. *
  156. * (This is a separate function so that we declare it in util_bug.h without
  157. * including torerr.h in all the users of util_bug.h)
  158. **/
  159. void
  160. tor_abort_(void)
  161. {
  162. logs_close_sigsafe();
  163. tor_raw_abort_();
  164. }
  165. #ifdef _WIN32
  166. /** Take a filename and return a pointer to its final element. This
  167. * function is called on __FILE__ to fix a MSVC nit where __FILE__
  168. * contains the full path to the file. This is bad, because it
  169. * confuses users to find the home directory of the person who
  170. * compiled the binary in their warning messages.
  171. */
  172. const char *
  173. tor_fix_source_file(const char *fname)
  174. {
  175. const char *cp1, *cp2, *r;
  176. cp1 = strrchr(fname, '/');
  177. cp2 = strrchr(fname, '\\');
  178. if (cp1 && cp2) {
  179. r = (cp1<cp2)?(cp2+1):(cp1+1);
  180. } else if (cp1) {
  181. r = cp1+1;
  182. } else if (cp2) {
  183. r = cp2+1;
  184. } else {
  185. r = fname;
  186. }
  187. return r;
  188. }
  189. #endif /* defined(_WIN32) */