util_bug.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file util_bug.c
  7. **/
  8. #include "orconfig.h"
  9. #include "util_bug.h"
  10. #include "torlog.h"
  11. #include "backtrace.h"
  12. #include "container.h"
  13. #ifdef __COVERITY__
  14. int bug_macro_deadcode_dummy__ = 0;
  15. #endif
  16. #ifdef TOR_UNIT_TESTS
  17. static void (*failed_assertion_cb)(void) = NULL;
  18. static int n_bugs_to_capture = 0;
  19. static smartlist_t *bug_messages = NULL;
  20. #define capturing_bugs() (bug_messages != NULL && n_bugs_to_capture)
  21. void
  22. tor_capture_bugs_(int n)
  23. {
  24. tor_end_capture_bugs_();
  25. bug_messages = smartlist_new();
  26. n_bugs_to_capture = n;
  27. }
  28. void
  29. tor_end_capture_bugs_(void)
  30. {
  31. n_bugs_to_capture = 0;
  32. if (!bug_messages)
  33. return;
  34. SMARTLIST_FOREACH(bug_messages, char *, cp, tor_free(cp));
  35. smartlist_free(bug_messages);
  36. bug_messages = NULL;
  37. }
  38. const smartlist_t *
  39. tor_get_captured_bug_log_(void)
  40. {
  41. return bug_messages;
  42. }
  43. static void
  44. add_captured_bug(const char *s)
  45. {
  46. --n_bugs_to_capture;
  47. smartlist_add_strdup(bug_messages, s);
  48. }
  49. /** Set a callback to be invoked when we get any tor_bug_occurred_
  50. * invocation. We use this in the unit tests so that a nonfatal
  51. * assertion failure can also count as a test failure.
  52. */
  53. void
  54. tor_set_failed_assertion_callback(void (*fn)(void))
  55. {
  56. failed_assertion_cb = fn;
  57. }
  58. #else /* !(defined(TOR_UNIT_TESTS)) */
  59. #define capturing_bugs() (0)
  60. #define add_captured_bug(s) do { } while (0)
  61. #endif /* defined(TOR_UNIT_TESTS) */
  62. /** Helper for tor_assert: report the assertion failure. */
  63. void
  64. tor_assertion_failed_(const char *fname, unsigned int line,
  65. const char *func, const char *expr)
  66. {
  67. char buf[256];
  68. log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.",
  69. fname, line, func, expr);
  70. tor_snprintf(buf, sizeof(buf),
  71. "Assertion %s failed in %s at %s:%u",
  72. expr, func, fname, line);
  73. log_backtrace(LOG_ERR, LD_BUG, buf);
  74. }
  75. /** Helper for tor_assert_nonfatal: report the assertion failure. */
  76. void
  77. tor_bug_occurred_(const char *fname, unsigned int line,
  78. const char *func, const char *expr,
  79. int once)
  80. {
  81. char buf[256];
  82. const char *once_str = once ?
  83. " (Future instances of this warning will be silenced.)": "";
  84. if (! expr) {
  85. if (capturing_bugs()) {
  86. add_captured_bug("This line should not have been reached.");
  87. return;
  88. }
  89. log_warn(LD_BUG, "%s:%u: %s: This line should not have been reached.%s",
  90. fname, line, func, once_str);
  91. tor_snprintf(buf, sizeof(buf),
  92. "Line unexpectedly reached at %s at %s:%u",
  93. func, fname, line);
  94. } else {
  95. if (capturing_bugs()) {
  96. add_captured_bug(expr);
  97. return;
  98. }
  99. log_warn(LD_BUG, "%s:%u: %s: Non-fatal assertion %s failed.%s",
  100. fname, line, func, expr, once_str);
  101. tor_snprintf(buf, sizeof(buf),
  102. "Non-fatal assertion %s failed in %s at %s:%u",
  103. expr, func, fname, line);
  104. }
  105. log_backtrace(LOG_WARN, LD_BUG, buf);
  106. #ifdef TOR_UNIT_TESTS
  107. if (failed_assertion_cb) {
  108. failed_assertion_cb();
  109. }
  110. #endif
  111. }