util_bug.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, 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/torlog.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. #ifdef __COVERITY__
  20. int bug_macro_deadcode_dummy__ = 0;
  21. #endif
  22. #ifdef TOR_UNIT_TESTS
  23. static void (*failed_assertion_cb)(void) = NULL;
  24. static int n_bugs_to_capture = 0;
  25. static smartlist_t *bug_messages = NULL;
  26. #define capturing_bugs() (bug_messages != NULL && n_bugs_to_capture)
  27. void
  28. tor_capture_bugs_(int n)
  29. {
  30. tor_end_capture_bugs_();
  31. bug_messages = smartlist_new();
  32. n_bugs_to_capture = n;
  33. }
  34. void
  35. tor_end_capture_bugs_(void)
  36. {
  37. n_bugs_to_capture = 0;
  38. if (!bug_messages)
  39. return;
  40. SMARTLIST_FOREACH(bug_messages, char *, cp, tor_free(cp));
  41. smartlist_free(bug_messages);
  42. bug_messages = NULL;
  43. }
  44. const smartlist_t *
  45. tor_get_captured_bug_log_(void)
  46. {
  47. return bug_messages;
  48. }
  49. static void
  50. add_captured_bug(const char *s)
  51. {
  52. --n_bugs_to_capture;
  53. smartlist_add_strdup(bug_messages, s);
  54. }
  55. /** Set a callback to be invoked when we get any tor_bug_occurred_
  56. * invocation. We use this in the unit tests so that a nonfatal
  57. * assertion failure can also count as a test failure.
  58. */
  59. void
  60. tor_set_failed_assertion_callback(void (*fn)(void))
  61. {
  62. failed_assertion_cb = fn;
  63. }
  64. #else /* !(defined(TOR_UNIT_TESTS)) */
  65. #define capturing_bugs() (0)
  66. #define add_captured_bug(s) do { } while (0)
  67. #endif /* defined(TOR_UNIT_TESTS) */
  68. /** Helper for tor_assert: report the assertion failure. */
  69. void
  70. tor_assertion_failed_(const char *fname, unsigned int line,
  71. const char *func, const char *expr)
  72. {
  73. char buf[256];
  74. log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.",
  75. fname, line, func, expr);
  76. tor_snprintf(buf, sizeof(buf),
  77. "Assertion %s failed in %s at %s:%u",
  78. expr, func, fname, line);
  79. log_backtrace(LOG_ERR, LD_BUG, buf);
  80. }
  81. /** Helper for tor_assert_nonfatal: report the assertion failure. */
  82. void
  83. tor_bug_occurred_(const char *fname, unsigned int line,
  84. const char *func, const char *expr,
  85. int once)
  86. {
  87. char buf[256];
  88. const char *once_str = once ?
  89. " (Future instances of this warning will be silenced.)": "";
  90. if (! expr) {
  91. if (capturing_bugs()) {
  92. add_captured_bug("This line should not have been reached.");
  93. return;
  94. }
  95. log_warn(LD_BUG, "%s:%u: %s: This line should not have been reached.%s",
  96. fname, line, func, once_str);
  97. tor_snprintf(buf, sizeof(buf),
  98. "Line unexpectedly reached at %s at %s:%u",
  99. func, fname, line);
  100. } else {
  101. if (capturing_bugs()) {
  102. add_captured_bug(expr);
  103. return;
  104. }
  105. log_warn(LD_BUG, "%s:%u: %s: Non-fatal assertion %s failed.%s",
  106. fname, line, func, expr, once_str);
  107. tor_snprintf(buf, sizeof(buf),
  108. "Non-fatal assertion %s failed in %s at %s:%u",
  109. expr, func, fname, line);
  110. }
  111. log_backtrace(LOG_WARN, LD_BUG, buf);
  112. #ifdef TOR_UNIT_TESTS
  113. if (failed_assertion_cb) {
  114. failed_assertion_cb();
  115. }
  116. #endif
  117. }
  118. #ifdef _WIN32
  119. /** Take a filename and return a pointer to its final element. This
  120. * function is called on __FILE__ to fix a MSVC nit where __FILE__
  121. * contains the full path to the file. This is bad, because it
  122. * confuses users to find the home directory of the person who
  123. * compiled the binary in their warning messages.
  124. */
  125. const char *
  126. tor_fix_source_file(const char *fname)
  127. {
  128. const char *cp1, *cp2, *r;
  129. cp1 = strrchr(fname, '/');
  130. cp2 = strrchr(fname, '\\');
  131. if (cp1 && cp2) {
  132. r = (cp1<cp2)?(cp2+1):(cp1+1);
  133. } else if (cp1) {
  134. r = cp1+1;
  135. } else if (cp2) {
  136. r = cp2+1;
  137. } else {
  138. r = fname;
  139. }
  140. return r;
  141. }
  142. #endif /* defined(_WIN32) */