Skip to content
Permalink
Browse files
Allow logging extra stuff on assert failure
  • Loading branch information
rl1987 committed Mar 18, 2018
1 parent 4e5e973 commit 7fcca6bd61a00fd9eaae246914155a4af5bd7a9d
Showing with 26 additions and 6 deletions.
  1. +15 −3 src/common/util_bug.c
  2. +11 −3 src/common/util_bug.h
@@ -67,15 +67,27 @@ tor_set_failed_assertion_callback(void (*fn)(void))
/** Helper for tor_assert: report the assertion failure. */
void
tor_assertion_failed_(const char *fname, unsigned int line,
const char *func, const char *expr)
const char *func, const char *expr,
const char *fmt, ...)
{
char buf[256];
char *extra = NULL;
va_list ap;

if (fmt) {
va_start(ap,fmt);
tor_vasprintf(&extra, fmt, ap);
va_end(ap);
}

log_err(LD_BUG, "%s:%u: %s: Assertion %s failed; aborting.",
fname, line, func, expr);
tor_snprintf(buf, sizeof(buf),
"Assertion %s failed in %s at %s:%u",
expr, func, fname, line);
"Assertion %s failed in %s at %s:%u: %s",
expr, func, fname, line, extra ? extra : "");
log_backtrace(LOG_ERR, LD_BUG, buf);

tor_free(extra);
}

/** Helper for tor_assert_nonfatal: report the assertion failure. */
@@ -62,12 +62,19 @@
#define tor_assert(a) STMT_BEGIN \
(void)(a); \
STMT_END
#define tor_assertf(a, fmt, ...) STMT_BEGIN \
(void)(a); \
(void)(fmt); \
STMT_END
#else
/** Like assert(3), but send assertion failures to the log as well as to
* stderr. */
#define tor_assert(expr) STMT_BEGIN \
#define tor_assert(expr) tor_assertf(expr, NULL)

#define tor_assertf(expr, fmt, ...) STMT_BEGIN \
if (PREDICT_UNLIKELY(!(expr))) { \
tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, #expr); \
tor_assertion_failed_(SHORT_FILE__, __LINE__, __func__, #expr, \
fmt, ##__VA_ARGS__); \
abort(); \
} STMT_END
#endif /* defined(TOR_UNIT_TESTS) && defined(DISABLE_ASSERTS_IN_UNIT_TESTS) */
@@ -186,7 +193,8 @@ extern int bug_macro_deadcode_dummy__;
#define tor_fragile_assert() tor_assert_nonfatal_unreached_once()

void tor_assertion_failed_(const char *fname, unsigned int line,
const char *func, const char *expr);
const char *func, const char *expr,
const char *fmt, ...);
void tor_bug_occurred_(const char *fname, unsigned int line,
const char *func, const char *expr,
int once);

0 comments on commit 7fcca6b

Please sign in to comment.