util_bug.c 4.0 KB

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