util_bug.c 1.7 KB

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