util_bug.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. /** Helper for tor_assert: report the assertion failure. */
  13. void
  14. tor_assertion_failed_(const char *fname, unsigned int line,
  15. const char *func, const char *expr)
  16. {
  17. char buf[256];
  18. log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.",
  19. fname, line, func, expr);
  20. tor_snprintf(buf, sizeof(buf),
  21. "Assertion %s failed in %s at %s:%u",
  22. expr, func, fname, line);
  23. log_backtrace(LOG_ERR, LD_BUG, buf);
  24. }
  25. void
  26. tor_bug_occurred_(const char *fname, unsigned int line,
  27. const char *func, const char *expr,
  28. int once)
  29. {
  30. char buf[256];
  31. const char *once_str = once ?
  32. " (Future instances of this warning will be silenced.)": "";
  33. if (! expr) {
  34. log_warn(LD_BUG, "%s:%u: %s: This line should not have been reached.%s",
  35. fname, line, func, once_str);
  36. tor_snprintf(buf, sizeof(buf),
  37. "Line unexpectedly reached at %s at %s:%u",
  38. func, fname, line);
  39. } else {
  40. log_warn(LD_BUG, "%s:%u: %s: Non-fatal assertion %s failed.%s",
  41. fname, line, func, expr, once_str);
  42. tor_snprintf(buf, sizeof(buf),
  43. "Non-fatal assertion %s failed in %s at %s:%u",
  44. expr, func, fname, line);
  45. }
  46. log_backtrace(LOG_WARN, LD_BUG, buf);
  47. }