2013-04-16 23:59:23 -04:00
|
|
|
/* -*- mode: C; c-basic-offset: 4 -*- */
|
2013-04-16 23:59:09 -04:00
|
|
|
#ident "Copyright (c) 2007-2010 Tokutek Inc. All rights reserved."
|
|
|
|
|
2013-04-16 23:57:53 -04:00
|
|
|
#include <toku_portability.h>
|
2013-04-16 23:57:22 -04:00
|
|
|
#include "toku_assert.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2013-04-16 23:59:01 -04:00
|
|
|
#if !TOKU_WINDOWS
|
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if !TOKU_WINDOWS
|
|
|
|
#define N_POINTERS 1000
|
|
|
|
// These are statically allocated so that the backtrace can run without any calls to malloc()
|
|
|
|
static void *backtrace_pointers[N_POINTERS];
|
|
|
|
#endif
|
|
|
|
|
2013-04-16 23:59:15 -04:00
|
|
|
void (*do_assert_hook)(void) = NULL;
|
|
|
|
|
2013-04-16 23:59:23 -04:00
|
|
|
static void toku_do_backtrace_abort(void) __attribute__((noreturn));
|
2008-02-07 16:35:39 +00:00
|
|
|
|
2013-04-16 23:59:23 -04:00
|
|
|
static void
|
|
|
|
toku_do_backtrace_abort(void) {
|
|
|
|
|
|
|
|
// backtrace
|
2013-04-16 23:59:01 -04:00
|
|
|
#if !TOKU_WINDOWS
|
2013-04-16 23:59:23 -04:00
|
|
|
int n = backtrace(backtrace_pointers, N_POINTERS);
|
|
|
|
fprintf(stderr, "Backtrace: (Note: toku_do_assert=0x%p)\n", toku_do_assert); fflush(stderr);
|
|
|
|
backtrace_symbols_fd(backtrace_pointers, n, fileno(stderr));
|
2013-04-16 23:59:01 -04:00
|
|
|
#endif
|
2008-05-17 01:06:03 +00:00
|
|
|
|
2013-04-16 23:59:23 -04:00
|
|
|
fflush(stderr);
|
2008-05-17 01:06:03 +00:00
|
|
|
|
2013-04-16 23:59:01 -04:00
|
|
|
#if TOKU_WINDOWS
|
2013-04-16 23:59:23 -04:00
|
|
|
//Following commented methods will not always end the process (could hang).
|
|
|
|
//They could be unacceptable for other reasons as well (popups,
|
|
|
|
//flush buffers before quitting, etc)
|
|
|
|
// abort()
|
|
|
|
// assert(FALSE) (assert.h assert)
|
|
|
|
// raise(SIGABRT)
|
|
|
|
// divide by 0
|
|
|
|
// null dereference
|
|
|
|
// _exit
|
|
|
|
// exit
|
|
|
|
// ExitProcess
|
|
|
|
TerminateProcess(GetCurrentProcess(), 134); //Only way found so far to unconditionally
|
|
|
|
//Terminate the process
|
2013-04-16 23:59:01 -04:00
|
|
|
#endif
|
2013-04-16 23:59:15 -04:00
|
|
|
|
2013-04-16 23:59:23 -04:00
|
|
|
if (do_assert_hook) do_assert_hook();
|
|
|
|
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
toku_do_assert_fail (const char *expr_as_string, const char *function, const char *file, int line, int caller_errno) {
|
|
|
|
fprintf(stderr, "%s:%d %s: Assertion `%s' failed (errno=%d)\n", file, line, function, expr_as_string, caller_errno);
|
|
|
|
toku_do_backtrace_abort();
|
|
|
|
}
|
2013-04-16 23:59:15 -04:00
|
|
|
|
2013-04-16 23:59:23 -04:00
|
|
|
void
|
|
|
|
toku_do_assert_zero_fail (uintptr_t expr, const char *expr_as_string, const char *function, const char *file, int line, int caller_errno) {
|
|
|
|
fprintf(stderr, "%s:%d %s: Assertion `%s == 0' failed (errno=%d) (%s=%"PRIuPTR")\n", file, line, function, expr_as_string, caller_errno, expr_as_string, expr);
|
|
|
|
toku_do_backtrace_abort();
|
2008-05-17 01:06:03 +00:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:59:23 -04:00
|
|
|
void
|
|
|
|
toku_do_assert(int expr, const char *expr_as_string, const char *function, const char* file, int line, int caller_errno) {
|
|
|
|
if (expr == 0)
|
|
|
|
toku_do_assert_fail(expr_as_string, function, file, line, caller_errno);
|
2008-02-07 16:35:39 +00:00
|
|
|
}
|
2013-04-16 23:59:23 -04:00
|
|
|
|