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