util_bug.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/container/smartlist.h"
  14. #endif
  15. #include "lib/malloc/util_malloc.h"
  16. #include "lib/string/printf.h"
  17. #include <string.h>
  18. #ifdef __COVERITY__
  19. int bug_macro_deadcode_dummy__ = 0;
  20. #endif
  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. tor_assertion_failed_(const char *fname, unsigned int line,
  70. const char *func, const char *expr)
  71. {
  72. char buf[256];
  73. log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.",
  74. fname, line, func, expr);
  75. tor_snprintf(buf, sizeof(buf),
  76. "Assertion %s failed in %s at %s:%u",
  77. expr, func, fname, line);
  78. log_backtrace(LOG_ERR, LD_BUG, buf);
  79. }
  80. /** Helper for tor_assert_nonfatal: report the assertion failure. */
  81. void
  82. tor_bug_occurred_(const char *fname, unsigned int line,
  83. const char *func, const char *expr,
  84. int once)
  85. {
  86. char buf[256];
  87. const char *once_str = once ?
  88. " (Future instances of this warning will be silenced.)": "";
  89. if (! expr) {
  90. if (capturing_bugs()) {
  91. add_captured_bug("This line should not have been reached.");
  92. return;
  93. }
  94. log_warn(LD_BUG, "%s:%u: %s: This line should not have been reached.%s",
  95. fname, line, func, once_str);
  96. tor_snprintf(buf, sizeof(buf),
  97. "Line unexpectedly reached at %s at %s:%u",
  98. func, fname, line);
  99. } else {
  100. if (capturing_bugs()) {
  101. add_captured_bug(expr);
  102. return;
  103. }
  104. log_warn(LD_BUG, "%s:%u: %s: Non-fatal assertion %s failed.%s",
  105. fname, line, func, expr, once_str);
  106. tor_snprintf(buf, sizeof(buf),
  107. "Non-fatal assertion %s failed in %s at %s:%u",
  108. expr, func, fname, line);
  109. }
  110. log_backtrace(LOG_WARN, LD_BUG, buf);
  111. #ifdef TOR_UNIT_TESTS
  112. if (failed_assertion_cb) {
  113. failed_assertion_cb();
  114. }
  115. #endif
  116. }
  117. #ifdef _WIN32
  118. /** Take a filename and return a pointer to its final element. This
  119. * function is called on __FILE__ to fix a MSVC nit where __FILE__
  120. * contains the full path to the file. This is bad, because it
  121. * confuses users to find the home directory of the person who
  122. * compiled the binary in their warning messages.
  123. */
  124. const char *
  125. tor_fix_source_file(const char *fname)
  126. {
  127. const char *cp1, *cp2, *r;
  128. cp1 = strrchr(fname, '/');
  129. cp2 = strrchr(fname, '\\');
  130. if (cp1 && cp2) {
  131. r = (cp1<cp2)?(cp2+1):(cp1+1);
  132. } else if (cp1) {
  133. r = cp1+1;
  134. } else if (cp2) {
  135. r = cp2+1;
  136. } else {
  137. r = fname;
  138. }
  139. return r;
  140. }
  141. #endif /* defined(_WIN32) */