util_bug.c 5.0 KB

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