michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsDebug_h___ michael@0: #define nsDebug_h___ michael@0: michael@0: #include "nscore.h" michael@0: #include "nsError.h" michael@0: michael@0: #include "nsXPCOM.h" michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/Likely.h" michael@0: #include michael@0: michael@0: #ifdef DEBUG michael@0: #include "prprf.h" michael@0: #endif michael@0: michael@0: /** michael@0: * Warn if the given condition is true. The condition is evaluated in both michael@0: * release and debug builds, and the result is an expression which can be michael@0: * used in subsequent expressions, such as: michael@0: * michael@0: * if (NS_WARN_IF(NS_FAILED(rv)) michael@0: * return rv; michael@0: * michael@0: * This explicit warning and return is preferred to the NS_ENSURE_* macros michael@0: * which hide the warning and the return control flow. michael@0: * michael@0: * @note This is C++-only michael@0: */ michael@0: #ifdef __cplusplus michael@0: #ifdef DEBUG michael@0: inline bool NS_warn_if_impl(bool condition, const char* expr, const char* file, michael@0: int32_t line) michael@0: { michael@0: if (MOZ_UNLIKELY(condition)) { michael@0: NS_DebugBreak(NS_DEBUG_WARNING, nullptr, expr, file, line); michael@0: } michael@0: return condition; michael@0: } michael@0: #define NS_WARN_IF(condition) \ michael@0: NS_warn_if_impl(condition, #condition, __FILE__, __LINE__) michael@0: #else michael@0: #define NS_WARN_IF(condition) (bool)(condition) michael@0: #endif michael@0: #endif michael@0: michael@0: /** michael@0: * Abort the execution of the program if the expression evaluates to michael@0: * false. michael@0: * michael@0: * There is no status value returned from the macro. michael@0: * michael@0: * Note that the non-debug version of this macro does not michael@0: * evaluate the expression argument. Hence side effect statements michael@0: * as arguments to the macro will yield improper execution in a michael@0: * non-debug build. For example: michael@0: * michael@0: * NS_ABORT_IF_FALSE(0 == foo++, "yikes foo should be zero"); michael@0: * michael@0: * Note also that the non-debug version of this macro does not michael@0: * evaluate the message argument. michael@0: */ michael@0: #ifdef DEBUG michael@0: #define NS_ABORT_IF_FALSE(_expr, _msg) \ michael@0: do { \ michael@0: if (!(_expr)) { \ michael@0: NS_DebugBreak(NS_DEBUG_ABORT, _msg, #_expr, __FILE__, __LINE__); \ michael@0: } \ michael@0: } while(0) michael@0: #else michael@0: #define NS_ABORT_IF_FALSE(_expr, _msg) do { /* nothing */ } while(0) michael@0: #endif michael@0: michael@0: /** michael@0: * Warn if a given condition is false. michael@0: * michael@0: * Program execution continues past the usage of this macro. michael@0: * michael@0: * Note also that the non-debug version of this macro does not michael@0: * evaluate the message argument. michael@0: */ michael@0: #ifdef DEBUG michael@0: #define NS_WARN_IF_FALSE(_expr,_msg) \ michael@0: do { \ michael@0: if (!(_expr)) { \ michael@0: NS_DebugBreak(NS_DEBUG_WARNING, _msg, #_expr, __FILE__, __LINE__); \ michael@0: } \ michael@0: } while(0) michael@0: #else michael@0: #define NS_WARN_IF_FALSE(_expr, _msg) do { /* nothing */ } while(0) michael@0: #endif michael@0: michael@0: michael@0: /** michael@0: * Test an assertion for truth. If the expression is not true then michael@0: * trigger a program failure. michael@0: * michael@0: * Note that the non-debug version of this macro does not michael@0: * evaluate the message argument. michael@0: */ michael@0: #ifdef DEBUG michael@0: #define NS_ASSERTION(expr, str) \ michael@0: do { \ michael@0: if (!(expr)) { \ michael@0: NS_DebugBreak(NS_DEBUG_ASSERTION, str, #expr, __FILE__, __LINE__); \ michael@0: } \ michael@0: } while(0) michael@0: #else michael@0: #define NS_ASSERTION(expr, str) do { /* nothing */ } while(0) michael@0: #endif michael@0: michael@0: /** michael@0: * NS_PRECONDITION/POSTCONDITION are synonyms for NS_ASSERTION. michael@0: */ michael@0: #define NS_PRECONDITION(expr, str) NS_ASSERTION(expr, str) michael@0: #define NS_POSTCONDITION(expr, str) NS_ASSERTION(expr, str) michael@0: michael@0: /** michael@0: * This macros triggers a program failure if executed. It indicates that michael@0: * an attempt was made to execute some unimplemented functionality. michael@0: */ michael@0: #ifdef DEBUG michael@0: #define NS_NOTYETIMPLEMENTED(str) \ michael@0: NS_DebugBreak(NS_DEBUG_ASSERTION, str, "NotYetImplemented", __FILE__, __LINE__) michael@0: #else michael@0: #define NS_NOTYETIMPLEMENTED(str) do { /* nothing */ } while(0) michael@0: #endif michael@0: michael@0: /** michael@0: * This macros triggers a program failure if executed. It indicates that michael@0: * an attempt was made to execute a codepath which should not be reachable. michael@0: */ michael@0: #ifdef DEBUG michael@0: #define NS_NOTREACHED(str) \ michael@0: NS_DebugBreak(NS_DEBUG_ASSERTION, str, "Not Reached", __FILE__, __LINE__) michael@0: #else michael@0: #define NS_NOTREACHED(str) do { /* nothing */ } while(0) michael@0: #endif michael@0: michael@0: /** michael@0: * Log an error message. michael@0: */ michael@0: #ifdef DEBUG michael@0: #define NS_ERROR(str) \ michael@0: NS_DebugBreak(NS_DEBUG_ASSERTION, str, "Error", __FILE__, __LINE__) michael@0: #else michael@0: #define NS_ERROR(str) do { /* nothing */ } while(0) michael@0: #endif michael@0: michael@0: /** michael@0: * Log a warning message. michael@0: */ michael@0: #ifdef DEBUG michael@0: #define NS_WARNING(str) \ michael@0: NS_DebugBreak(NS_DEBUG_WARNING, str, nullptr, __FILE__, __LINE__) michael@0: #else michael@0: #define NS_WARNING(str) do { /* nothing */ } while(0) michael@0: #endif michael@0: michael@0: /** michael@0: * Trigger an debug-only abort. michael@0: * michael@0: * @see NS_RUNTIMEABORT for release-mode asserts. michael@0: */ michael@0: #ifdef DEBUG michael@0: #define NS_ABORT() \ michael@0: NS_DebugBreak(NS_DEBUG_ABORT, nullptr, nullptr, __FILE__, __LINE__) michael@0: #else michael@0: #define NS_ABORT() do { /* nothing */ } while(0) michael@0: #endif michael@0: michael@0: /** michael@0: * Trigger a debugger breakpoint, only in debug builds. michael@0: */ michael@0: #ifdef DEBUG michael@0: #define NS_BREAK() \ michael@0: NS_DebugBreak(NS_DEBUG_BREAK, nullptr, nullptr, __FILE__, __LINE__) michael@0: #else michael@0: #define NS_BREAK() do { /* nothing */ } while(0) michael@0: #endif michael@0: michael@0: /****************************************************************************** michael@0: ** Macros for static assertions. These are used by the sixgill tool. michael@0: ** When the tool is not running these macros are no-ops. michael@0: ******************************************************************************/ michael@0: michael@0: /* Avoid name collision if included with other headers defining annotations. */ michael@0: #ifndef HAVE_STATIC_ANNOTATIONS michael@0: #define HAVE_STATIC_ANNOTATIONS michael@0: michael@0: #ifdef XGILL_PLUGIN michael@0: michael@0: #define STATIC_PRECONDITION(COND) __attribute__((precondition(#COND))) michael@0: #define STATIC_PRECONDITION_ASSUME(COND) __attribute__((precondition_assume(#COND))) michael@0: #define STATIC_POSTCONDITION(COND) __attribute__((postcondition(#COND))) michael@0: #define STATIC_POSTCONDITION_ASSUME(COND) __attribute__((postcondition_assume(#COND))) michael@0: #define STATIC_INVARIANT(COND) __attribute__((invariant(#COND))) michael@0: #define STATIC_INVARIANT_ASSUME(COND) __attribute__((invariant_assume(#COND))) michael@0: michael@0: /* Used to make identifiers for assert/assume annotations in a function. */ michael@0: #define STATIC_PASTE2(X,Y) X ## Y michael@0: #define STATIC_PASTE1(X,Y) STATIC_PASTE2(X,Y) michael@0: michael@0: #define STATIC_ASSERT(COND) \ michael@0: do { \ michael@0: __attribute__((assert_static(#COND), unused)) \ michael@0: int STATIC_PASTE1(assert_static_, __COUNTER__); \ michael@0: } while(0) michael@0: michael@0: #define STATIC_ASSUME(COND) \ michael@0: do { \ michael@0: __attribute__((assume_static(#COND), unused)) \ michael@0: int STATIC_PASTE1(assume_static_, __COUNTER__); \ michael@0: } while(0) michael@0: michael@0: #define STATIC_ASSERT_RUNTIME(COND) \ michael@0: do { \ michael@0: __attribute__((assert_static_runtime(#COND), unused)) \ michael@0: int STATIC_PASTE1(assert_static_runtime_, __COUNTER__); \ michael@0: } while(0) michael@0: michael@0: #else /* XGILL_PLUGIN */ michael@0: michael@0: #define STATIC_PRECONDITION(COND) /* nothing */ michael@0: #define STATIC_PRECONDITION_ASSUME(COND) /* nothing */ michael@0: #define STATIC_POSTCONDITION(COND) /* nothing */ michael@0: #define STATIC_POSTCONDITION_ASSUME(COND) /* nothing */ michael@0: #define STATIC_INVARIANT(COND) /* nothing */ michael@0: #define STATIC_INVARIANT_ASSUME(COND) /* nothing */ michael@0: michael@0: #define STATIC_ASSERT(COND) do { /* nothing */ } while(0) michael@0: #define STATIC_ASSUME(COND) do { /* nothing */ } while(0) michael@0: #define STATIC_ASSERT_RUNTIME(COND) do { /* nothing */ } while(0) michael@0: michael@0: #endif /* XGILL_PLUGIN */ michael@0: michael@0: #define STATIC_SKIP_INFERENCE STATIC_INVARIANT(skip_inference()) michael@0: michael@0: #endif /* HAVE_STATIC_ANNOTATIONS */ michael@0: michael@0: #ifdef XGILL_PLUGIN michael@0: michael@0: /* Redefine runtime assertion macros to perform static assertions, for both michael@0: * debug and release builds. Don't include the original runtime assertions; michael@0: * this ensures the tool will consider cases where the assertion fails. */ michael@0: michael@0: #undef NS_PRECONDITION michael@0: #undef NS_ASSERTION michael@0: #undef NS_POSTCONDITION michael@0: michael@0: #define NS_PRECONDITION(expr, str) STATIC_ASSERT_RUNTIME(expr) michael@0: #define NS_ASSERTION(expr, str) STATIC_ASSERT_RUNTIME(expr) michael@0: #define NS_POSTCONDITION(expr, str) STATIC_ASSERT_RUNTIME(expr) michael@0: michael@0: #endif /* XGILL_PLUGIN */ michael@0: michael@0: /****************************************************************************** michael@0: ** Macros for terminating execution when an unrecoverable condition is michael@0: ** reached. These need to be compiled regardless of the DEBUG flag. michael@0: ******************************************************************************/ michael@0: michael@0: /** michael@0: * Terminate execution immediately, and if possible on the current michael@0: * platform, in such a way that execution can't be continued by other michael@0: * code (e.g., by intercepting a signal). michael@0: */ michael@0: #define NS_RUNTIMEABORT(msg) \ michael@0: NS_DebugBreak(NS_DEBUG_ABORT, msg, nullptr, __FILE__, __LINE__) michael@0: michael@0: michael@0: /* Macros for checking the trueness of an expression passed in within an michael@0: * interface implementation. These need to be compiled regardless of the michael@0: * DEBUG flag. New code should use NS_WARN_IF(condition) instead! michael@0: * @status deprecated michael@0: */ michael@0: michael@0: #define NS_ENSURE_TRUE(x, ret) \ michael@0: do { \ michael@0: if (MOZ_UNLIKELY(!(x))) { \ michael@0: NS_WARNING("NS_ENSURE_TRUE(" #x ") failed"); \ michael@0: return ret; \ michael@0: } \ michael@0: } while(0) michael@0: michael@0: #define NS_ENSURE_FALSE(x, ret) \ michael@0: NS_ENSURE_TRUE(!(x), ret) michael@0: michael@0: #define NS_ENSURE_TRUE_VOID(x) \ michael@0: do { \ michael@0: if (MOZ_UNLIKELY(!(x))) { \ michael@0: NS_WARNING("NS_ENSURE_TRUE(" #x ") failed"); \ michael@0: return; \ michael@0: } \ michael@0: } while(0) michael@0: michael@0: #define NS_ENSURE_FALSE_VOID(x) \ michael@0: NS_ENSURE_TRUE_VOID(!(x)) michael@0: michael@0: /****************************************************************************** michael@0: ** Macros for checking results michael@0: ******************************************************************************/ michael@0: michael@0: #if defined(DEBUG) && !defined(XPCOM_GLUE_AVOID_NSPR) michael@0: michael@0: #define NS_ENSURE_SUCCESS_BODY(res, ret) \ michael@0: char *msg = PR_smprintf("NS_ENSURE_SUCCESS(%s, %s) failed with " \ michael@0: "result 0x%X", #res, #ret, __rv); \ michael@0: NS_WARNING(msg); \ michael@0: PR_smprintf_free(msg); michael@0: michael@0: #define NS_ENSURE_SUCCESS_BODY_VOID(res) \ michael@0: char *msg = PR_smprintf("NS_ENSURE_SUCCESS_VOID(%s) failed with " \ michael@0: "result 0x%X", #res, __rv); \ michael@0: NS_WARNING(msg); \ michael@0: PR_smprintf_free(msg); michael@0: michael@0: #else michael@0: michael@0: #define NS_ENSURE_SUCCESS_BODY(res, ret) \ michael@0: NS_WARNING("NS_ENSURE_SUCCESS(" #res ", " #ret ") failed"); michael@0: michael@0: #define NS_ENSURE_SUCCESS_BODY_VOID(res) \ michael@0: NS_WARNING("NS_ENSURE_SUCCESS_VOID(" #res ") failed"); michael@0: michael@0: #endif michael@0: michael@0: #define NS_ENSURE_SUCCESS(res, ret) \ michael@0: do { \ michael@0: nsresult __rv = res; /* Don't evaluate |res| more than once */ \ michael@0: if (NS_FAILED(__rv)) { \ michael@0: NS_ENSURE_SUCCESS_BODY(res, ret) \ michael@0: return ret; \ michael@0: } \ michael@0: } while(0) michael@0: michael@0: #define NS_ENSURE_SUCCESS_VOID(res) \ michael@0: do { \ michael@0: nsresult __rv = res; \ michael@0: if (NS_FAILED(__rv)) { \ michael@0: NS_ENSURE_SUCCESS_BODY_VOID(res) \ michael@0: return; \ michael@0: } \ michael@0: } while(0) michael@0: michael@0: /****************************************************************************** michael@0: ** Macros for checking state and arguments upon entering interface boundaries michael@0: ******************************************************************************/ michael@0: michael@0: #define NS_ENSURE_ARG(arg) \ michael@0: NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_ARG) michael@0: michael@0: #define NS_ENSURE_ARG_POINTER(arg) \ michael@0: NS_ENSURE_TRUE(arg, NS_ERROR_INVALID_POINTER) michael@0: michael@0: #define NS_ENSURE_ARG_MIN(arg, min) \ michael@0: NS_ENSURE_TRUE((arg) >= min, NS_ERROR_INVALID_ARG) michael@0: michael@0: #define NS_ENSURE_ARG_MAX(arg, max) \ michael@0: NS_ENSURE_TRUE((arg) <= max, NS_ERROR_INVALID_ARG) michael@0: michael@0: #define NS_ENSURE_ARG_RANGE(arg, min, max) \ michael@0: NS_ENSURE_TRUE(((arg) >= min) && ((arg) <= max), NS_ERROR_INVALID_ARG) michael@0: michael@0: #define NS_ENSURE_STATE(state) \ michael@0: NS_ENSURE_TRUE(state, NS_ERROR_UNEXPECTED) michael@0: michael@0: #define NS_ENSURE_NO_AGGREGATION(outer) \ michael@0: NS_ENSURE_FALSE(outer, NS_ERROR_NO_AGGREGATION) michael@0: michael@0: /*****************************************************************************/ michael@0: michael@0: #ifdef XPCOM_GLUE michael@0: #define NS_CheckThreadSafe(owningThread, msg) michael@0: #else michael@0: #define NS_CheckThreadSafe(owningThread, msg) \ michael@0: if (MOZ_UNLIKELY(owningThread != PR_GetCurrentThread())) { \ michael@0: MOZ_CRASH(msg); \ michael@0: } michael@0: #endif michael@0: michael@0: #ifdef MOZILLA_INTERNAL_API michael@0: void NS_ABORT_OOM(size_t size); michael@0: #else michael@0: inline void NS_ABORT_OOM(size_t) michael@0: { michael@0: MOZ_CRASH(); michael@0: } michael@0: #endif michael@0: michael@0: /* When compiling the XPCOM Glue on Windows, we pretend that it's going to michael@0: * be linked with a static CRT (-MT) even when it's not. This means that we michael@0: * cannot link to data exports from the CRT, only function exports. So, michael@0: * instead of referencing "stderr" directly, use fdopen. michael@0: */ michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: NS_COM_GLUE void michael@0: printf_stderr(const char *fmt, ...); michael@0: michael@0: NS_COM_GLUE void michael@0: vprintf_stderr(const char *fmt, va_list args); michael@0: michael@0: // fprintf with special handling for stderr to print to the console michael@0: NS_COM_GLUE void michael@0: fprintf_stderr(FILE* aFile, const char *fmt, ...); michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: #endif /* nsDebug_h___ */