util_bug.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, 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 int n_bugs_to_capture = 0;
  15. static smartlist_t *bug_messages = NULL;
  16. #define capturing_bugs() (bug_messages != NULL && n_bugs_to_capture)
  17. void
  18. tor_capture_bugs_(int n)
  19. {
  20. tor_end_capture_bugs_();
  21. bug_messages = smartlist_new();
  22. n_bugs_to_capture = n;
  23. }
  24. void
  25. tor_end_capture_bugs_(void)
  26. {
  27. n_bugs_to_capture = 0;
  28. if (!bug_messages)
  29. return;
  30. SMARTLIST_FOREACH(bug_messages, char *, cp, tor_free(cp));
  31. smartlist_free(bug_messages);
  32. bug_messages = NULL;
  33. }
  34. const smartlist_t *
  35. tor_get_captured_bug_log_(void)
  36. {
  37. return bug_messages;
  38. }
  39. static void
  40. add_captured_bug(const char *s)
  41. {
  42. --n_bugs_to_capture;
  43. smartlist_add(bug_messages, tor_strdup(s));
  44. }
  45. #else
  46. #define capturing_bugs() (0)
  47. #define add_captured_bug(s) do { } while (0)
  48. #endif
  49. /** Helper for tor_assert: report the assertion failure. */
  50. void
  51. tor_assertion_failed_(const char *fname, unsigned int line,
  52. const char *func, const char *expr)
  53. {
  54. char buf[256];
  55. log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.",
  56. fname, line, func, expr);
  57. tor_snprintf(buf, sizeof(buf),
  58. "Assertion %s failed in %s at %s:%u",
  59. expr, func, fname, line);
  60. log_backtrace(LOG_ERR, LD_BUG, buf);
  61. }
  62. /** Helper for tor_assert_nonfatal: report the assertion failure. */
  63. void
  64. tor_bug_occurred_(const char *fname, unsigned int line,
  65. const char *func, const char *expr,
  66. int once)
  67. {
  68. char buf[256];
  69. const char *once_str = once ?
  70. " (Future instances of this warning will be silenced.)": "";
  71. if (! expr) {
  72. if (capturing_bugs()) {
  73. add_captured_bug("This line should not have been reached.");
  74. return;
  75. }
  76. log_warn(LD_BUG, "%s:%u: %s: This line should not have been reached.%s",
  77. fname, line, func, once_str);
  78. tor_snprintf(buf, sizeof(buf),
  79. "Line unexpectedly reached at %s at %s:%u",
  80. func, fname, line);
  81. } else {
  82. if (capturing_bugs()) {
  83. add_captured_bug(expr);
  84. return;
  85. }
  86. log_warn(LD_BUG, "%s:%u: %s: Non-fatal assertion %s failed.%s",
  87. fname, line, func, expr, once_str);
  88. tor_snprintf(buf, sizeof(buf),
  89. "Non-fatal assertion %s failed in %s at %s:%u",
  90. expr, func, fname, line);
  91. }
  92. log_backtrace(LOG_WARN, LD_BUG, buf);
  93. }