1 // Copyright 2016 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef ZIRCON_ASSERT_ 6 #define ZIRCON_ASSERT_ 7 8 #ifdef _KERNEL 9 #include <assert.h> 10 #define ZX_PANIC(args...) PANIC(args) 11 #define ZX_ASSERT(args...) ASSERT(args) 12 #define ZX_ASSERT_MSG(args...) ASSERT_MSG(args) 13 #define ZX_DEBUG_ASSERT(args...) DEBUG_ASSERT(args) 14 #define ZX_DEBUG_ASSERT_MSG(args...) DEBUG_ASSERT_MSG(args) 15 #define ZX_DEBUG_ASSERT_COND(args...) DEBUG_ASSERT_COND(args) 16 #define ZX_DEBUG_ASSERT_MSG_COND(args...) DEBUG_ASSERT_MSG_COND(args) 17 #define ZX_DEBUG_ASSERT_IMPLEMENTED DEBUG_ASSERT_IMPLEMENTED 18 19 #ifdef ZX_DEBUGLEVEL 20 #undef ZX_DEBUGLEVEL 21 #endif 22 #define ZX_DEBUGLEVEL LK_DEBUGLEVEL 23 24 #else // #ifdef _KERNEL 25 26 #include <stdio.h> // for printf 27 #include <stdlib.h> // for abort 28 29 #include <zircon/compiler.h> 30 31 #define ZX_PANIC(fmt, ...) \ 32 do { \ 33 printf(fmt, ##__VA_ARGS__); \ 34 abort(); \ 35 } while (0) 36 37 #define ZX_ASSERT(x) \ 38 do { \ 39 if (unlikely(!(x))) { \ 40 ZX_PANIC("ASSERT FAILED at (%s:%d): %s\n", __FILE__, __LINE__, #x); \ 41 } \ 42 } while (0) 43 44 #define ZX_ASSERT_MSG(x, msg, msgargs...) \ 45 do { \ 46 if (unlikely(!(x))) { \ 47 ZX_PANIC("ASSERT FAILED at (%s:%d): %s\n" msg "\n", \ 48 __FILE__, __LINE__, #x, ##msgargs); \ 49 } \ 50 } while (0) 51 52 // conditionally implement DEBUG_ASSERT based on ZX_DEBUGLEVEL in kernel space 53 // user space does not currently implement DEBUG_ASSERT 54 #ifdef ZX_DEBUGLEVEL 55 #define ZX_DEBUG_ASSERT_IMPLEMENTED (ZX_DEBUGLEVEL > 1) 56 #else 57 #define ZX_DEBUG_ASSERT_IMPLEMENTED 0 58 #endif 59 60 #define ZX_DEBUG_ASSERT(x) \ 61 do { \ 62 if (ZX_DEBUG_ASSERT_IMPLEMENTED && unlikely(!(x))) { \ 63 ZX_PANIC("DEBUG ASSERT FAILED at (%s:%d): %s\n", __FILE__, __LINE__, #x); \ 64 } \ 65 } while (0) 66 67 #define ZX_DEBUG_ASSERT_MSG(x, msg, msgargs...) \ 68 do { \ 69 if (ZX_DEBUG_ASSERT_IMPLEMENTED && unlikely(!(x))) { \ 70 ZX_PANIC("DEBUG ASSERT FAILED at (%s:%d): %s\n" msg "\n", \ 71 __FILE__, __LINE__, #x, ##msgargs); \ 72 } \ 73 } while (0) 74 75 // implement _COND versions of ZX_DEBUG_ASSERT which only emit the body if 76 // ZX_DEBUG_ASSERT_IMPLEMENTED is set 77 #if ZX_DEBUG_ASSERT_IMPLEMENTED 78 #define ZX_DEBUG_ASSERT_COND(x) ZX_DEBUG_ASSERT(x) 79 #define ZX_DEBUG_ASSERT_MSG_COND(x, msg, msgargs...) ZX_DEBUG_ASSERT_MSG(x, msg, msgargs) 80 #else 81 #define ZX_DEBUG_ASSERT_COND(x) do { } while (0) 82 #define ZX_DEBUG_ASSERT_MSG_COND(x, msg, msgargs...) do { } while (0) 83 #endif 84 #endif // #ifdef _KERNEL 85 86 #endif // ZIRCON_ASSERT_ 87