util_bug.c 3.0 KB

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