Changeset View
Changeset View
Standalone View
Standalone View
sys/sys/kassert.h
| Show First 20 Lines • Show All 101 Lines • ▼ Show 20 Lines | |||||
| } while (0) | } while (0) | ||||
| #define __assert_unreachable() __unreachable() | #define __assert_unreachable() __unreachable() | ||||
| #endif /* INVARIANTS */ | #endif /* INVARIANTS */ | ||||
| #ifndef CTASSERT /* Allow lint to override */ | #ifndef CTASSERT /* Allow lint to override */ | ||||
| #define CTASSERT(x) _Static_assert(x, "compile-time assertion failed") | #define CTASSERT(x) _Static_assert(x, "compile-time assertion failed") | ||||
| #endif | #endif | ||||
| struct panic_codeptr { | |||||
| const char * const fname; | |||||
| const int linen; | |||||
| const char * const funcn; | |||||
| }; | |||||
| #if !defined(KASSERT_PANIC_NOCONTEXT) | |||||
| # define PANIC_CTX ({static const struct panic_codeptr _panic_ctx = { \ | |||||
| .fname = __FILE__, .linen = __LINE__, .funcn = __func__}; \ | |||||
| &_panic_ctx;}) | |||||
| #else | |||||
| # define PANIC_CTX ((const struct panic_codeptr *)(0)) | |||||
| #endif /* KASSERT_PANIC_NOCONTEXT */ | |||||
| /* | /* | ||||
| * These functions need to be declared before the KASSERT macro is invoked in | * These functions need to be declared before the KASSERT macro is invoked in | ||||
| * !KASSERT_PANIC_OPTIONAL builds, so their declarations are sort of out of | * !KASSERT_PANIC_OPTIONAL builds, so their declarations are sort of out of | ||||
| * place compared to other function definitions in this header. On the other | * place compared to other function definitions in this header. On the other | ||||
| * hand, this header is a bit disorganized anyway. | * hand, this header is a bit disorganized anyway. | ||||
| */ | */ | ||||
| void panic(const char *, ...) __dead2 __printflike(1, 2); | void do_panic(const struct panic_codeptr *, const char *, ...) | ||||
| void vpanic(const char *, __va_list) __dead2 __printflike(1, 0); | __dead2 __printflike(2, 3); | ||||
| void do_vpanic(const struct panic_codeptr *, const char *, __va_list) | |||||
| __dead2 __printflike(2, 0); | |||||
| # define panic(args...) do_panic(PANIC_CTX, ## args) | |||||
| # define vpanic(args...) do_vpanic(PANIC_CTX, ## args) | |||||
| #endif /* _KERNEL */ | #endif /* _KERNEL */ | ||||
| #if defined(_STANDALONE) | #if defined(_STANDALONE) | ||||
| /* | /* | ||||
| * Until we have more experience with KASSERTS that are called | * Until we have more experience with KASSERTS that are called | ||||
| * from the boot loader, they are off. The bootloader does this | * from the boot loader, they are off. The bootloader does this | ||||
| * a little differently than the kernel (we just call printf atm). | * a little differently than the kernel (we just call printf atm). | ||||
| * we avoid most of the common functions in the boot loader, so | * we avoid most of the common functions in the boot loader, so | ||||
| * declare printf() here too. | * declare printf() here too. | ||||
| */ | */ | ||||
| int printf(const char *, ...) __printflike(1, 2); | int printf(const char *, ...) __printflike(1, 2); | ||||
| # define kassert_panic printf | # define kassert_panic printf | ||||
| void panic(const char *, ...) __dead2 __printflike(1, 2); | |||||
| #else /* !_STANDALONE */ | #else /* !_STANDALONE */ | ||||
| # if defined(WITNESS) || defined(INVARIANT_SUPPORT) | # if defined(WITNESS) || defined(INVARIANT_SUPPORT) | ||||
| # ifdef KASSERT_PANIC_OPTIONAL | # ifdef KASSERT_PANIC_OPTIONAL | ||||
| void kassert_panic(const char *fmt, ...) __printflike(1, 2); | |||||
| void do_kassert_panic(const struct panic_codeptr *, const char *fmt, ...) | |||||
| __printflike(2, 3); | |||||
| # define kassert_panic(args...) do_kassert_panic(PANIC_CTX, ## args) | |||||
| # else | # else | ||||
| # define kassert_panic panic | # define kassert_panic panic | ||||
| # endif /* KASSERT_PANIC_OPTIONAL */ | # endif /* KASSERT_PANIC_OPTIONAL */ | ||||
| # endif /* defined(WITNESS) || defined(INVARIANT_SUPPORT) */ | # endif /* defined(WITNESS) || defined(INVARIANT_SUPPORT) */ | ||||
| #endif /* _STANDALONE */ | #endif /* _STANDALONE */ | ||||
| /* | /* | ||||
| * Kernel assertion; see KASSERT(9) for details. | * Kernel assertion; see KASSERT(9) for details. | ||||
| Show All 14 Lines | |||||
| * | * | ||||
| * NOTE: Use these with care, as the resulting message might omit key | * NOTE: Use these with care, as the resulting message might omit key | ||||
| * information required to understand the assertion failure. Consult the | * information required to understand the assertion failure. Consult the | ||||
| * MPASS(9) man page for guidance. | * MPASS(9) man page for guidance. | ||||
| */ | */ | ||||
| #define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__) | #define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__) | ||||
| #define MPASS2(ex, what) MPASS4(ex, what, __FILE__, __LINE__) | #define MPASS2(ex, what) MPASS4(ex, what, __FILE__, __LINE__) | ||||
| #define MPASS3(ex, file, line) MPASS4(ex, #ex, file, line) | #define MPASS3(ex, file, line) MPASS4(ex, #ex, file, line) | ||||
| #if !defined(KASSERT_PANIC_NOCONTEXT) | |||||
| #define MPASS4(ex, what, file, line) \ | #define MPASS4(ex, what, file, line) \ | ||||
| KASSERT((ex), ("Assertion %s failed", what)) | |||||
| #else | |||||
| #define MPASS4(ex, what, file, line) \ | |||||
| KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line)) | KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line)) | ||||
| #endif | |||||
| /* | /* | ||||
| * Assert that a pointer can be loaded from memory atomically. | * Assert that a pointer can be loaded from memory atomically. | ||||
| * | * | ||||
| * This assertion enforces stronger alignment than necessary. For example, | * This assertion enforces stronger alignment than necessary. For example, | ||||
| * on some architectures, atomicity for unaligned loads will depend on | * on some architectures, atomicity for unaligned loads will depend on | ||||
| * whether or not the load spans multiple cache lines. | * whether or not the load spans multiple cache lines. | ||||
| */ | */ | ||||
| Show All 12 Lines | |||||