1 /* 2 * Copyright (c) 2020 iXsystems, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 /* 30 * Available Solaris debug functions. All of the ASSERT() macros will be 31 * compiled out when NDEBUG is defined, this is the default behavior for 32 * the SPL. To enable assertions use the --enable-debug with configure. 33 * The VERIFY() functions are never compiled out and cannot be disabled. 34 * 35 * PANIC() - Panic the node and print message. 36 * ASSERT() - Assert X is true, if not panic. 37 * ASSERT3B() - Assert boolean X OP Y is true, if not panic. 38 * ASSERT3S() - Assert signed X OP Y is true, if not panic. 39 * ASSERT3U() - Assert unsigned X OP Y is true, if not panic. 40 * ASSERT3P() - Assert pointer X OP Y is true, if not panic. 41 * ASSERT0() - Assert value is zero, if not panic. 42 * VERIFY() - Verify X is true, if not panic. 43 * VERIFY3B() - Verify boolean X OP Y is true, if not panic. 44 * VERIFY3S() - Verify signed X OP Y is true, if not panic. 45 * VERIFY3U() - Verify unsigned X OP Y is true, if not panic. 46 * VERIFY3P() - Verify pointer X OP Y is true, if not panic. 47 * VERIFY0() - Verify value is zero, if not panic. 48 */ 49 50 #ifndef _SPL_DEBUG_H 51 #define _SPL_DEBUG_H 52 53 54 /* 55 * Common DEBUG functionality. 56 */ 57 int spl_panic(const char *file, const char *func, int line, 58 const char *fmt, ...); 59 void spl_dumpstack(void); 60 61 #ifndef expect 62 #define expect(expr, value) (__builtin_expect((expr), (value))) 63 #endif 64 #define likely(expr) expect((expr) != 0, 1) 65 #define unlikely(expr) expect((expr) != 0, 0) 66 67 #define PANIC(fmt, a...) \ 68 spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a) 69 70 #define VERIFY(cond) \ 71 (void) (unlikely(!(cond)) && \ 72 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 73 "%s", "VERIFY(" #cond ") failed\n")) 74 75 #define VERIFY3B(LEFT, OP, RIGHT) do { \ 76 const boolean_t _verify3_left = (boolean_t)(LEFT); \ 77 const boolean_t _verify3_right = (boolean_t)(RIGHT); \ 78 if (unlikely(!(_verify3_left OP _verify3_right))) \ 79 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 80 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \ 81 "failed (%d " #OP " %d)\n", \ 82 (boolean_t)(_verify3_left), \ 83 (boolean_t)(_verify3_right)); \ 84 } while (0) 85 86 #define VERIFY3S(LEFT, OP, RIGHT) do { \ 87 const int64_t _verify3_left = (int64_t)(LEFT); \ 88 const int64_t _verify3_right = (int64_t)(RIGHT); \ 89 if (unlikely(!(_verify3_left OP _verify3_right))) \ 90 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 91 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \ 92 "failed (%lld " #OP " %lld)\n", \ 93 (long long) (_verify3_left), \ 94 (long long) (_verify3_right)); \ 95 } while (0) 96 97 #define VERIFY3U(LEFT, OP, RIGHT) do { \ 98 const uint64_t _verify3_left = (uint64_t)(LEFT); \ 99 const uint64_t _verify3_right = (uint64_t)(RIGHT); \ 100 if (unlikely(!(_verify3_left OP _verify3_right))) \ 101 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 102 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \ 103 "failed (%llu " #OP " %llu)\n", \ 104 (unsigned long long) (_verify3_left), \ 105 (unsigned long long) (_verify3_right)); \ 106 } while (0) 107 108 #define VERIFY3P(LEFT, OP, RIGHT) do { \ 109 const uintptr_t _verify3_left = (uintptr_t)(LEFT); \ 110 const uintptr_t _verify3_right = (uintptr_t)(RIGHT); \ 111 if (unlikely(!(_verify3_left OP _verify3_right))) \ 112 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 113 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \ 114 "failed (%px " #OP " %px)\n", \ 115 (void *) (_verify3_left), \ 116 (void *) (_verify3_right)); \ 117 } while (0) 118 119 #define VERIFY0(RIGHT) do { \ 120 const int64_t _verify3_left = (int64_t)(0); \ 121 const int64_t _verify3_right = (int64_t)(RIGHT); \ 122 if (unlikely(!(_verify3_left == _verify3_right))) \ 123 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 124 "VERIFY3(0 == " #RIGHT ") " \ 125 "failed (0 == %lld)\n", \ 126 (long long) (_verify3_right)); \ 127 } while (0) 128 129 /* 130 * Debugging disabled (--disable-debug) 131 */ 132 #ifdef NDEBUG 133 134 #define ASSERT(x) ((void) sizeof ((uintptr_t)(x))) 135 #define ASSERT3B(x, y, z) \ 136 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z))) 137 #define ASSERT3S(x, y, z) \ 138 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z))) 139 #define ASSERT3U(x, y, z) \ 140 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z))) 141 #define ASSERT3P(x, y, z) \ 142 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z))) 143 #define ASSERT0(x) ((void) sizeof ((uintptr_t)(x))) 144 #define IMPLY(A, B) \ 145 ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B))) 146 #define EQUIV(A, B) \ 147 ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B))) 148 149 /* 150 * Debugging enabled (--enable-debug) 151 */ 152 #else 153 154 #define ASSERT3B VERIFY3B 155 #define ASSERT3S VERIFY3S 156 #define ASSERT3U VERIFY3U 157 #define ASSERT3P VERIFY3P 158 #define ASSERT0 VERIFY0 159 #define ASSERT VERIFY 160 #define IMPLY(A, B) \ 161 ((void)(likely((!(A)) || (B)) || \ 162 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 163 "(" #A ") implies (" #B ")"))) 164 #define EQUIV(A, B) \ 165 ((void)(likely(!!(A) == !!(B)) || \ 166 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 167 "(" #A ") is equivalent to (" #B ")"))) 168 169 #endif /* NDEBUG */ 170 171 #endif /* SPL_DEBUG_H */ 172