1 /* 2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC. 3 * Copyright (C) 2007 The Regents of the University of California. 4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). 5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>. 6 * UCRL-CODE-235197 7 * 8 * This file is part of the SPL, Solaris Porting Layer. 9 * 10 * The SPL is free software; you can redistribute it and/or modify it 11 * under the terms of the GNU General Public License as published by the 12 * Free Software Foundation; either version 2 of the License, or (at your 13 * option) any later version. 14 * 15 * The SPL is distributed in the hope that it will be useful, but WITHOUT 16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 18 * for more details. 19 * 20 * You should have received a copy of the GNU General Public License along 21 * with the SPL. If not, see <http://www.gnu.org/licenses/>. 22 */ 23 24 /* 25 * Available Solaris debug functions. All of the ASSERT() macros will be 26 * compiled out when NDEBUG is defined, this is the default behavior for 27 * the SPL. To enable assertions use the --enable-debug with configure. 28 * The VERIFY() functions are never compiled out and cannot be disabled. 29 * 30 * PANIC() - Panic the node and print message. 31 * ASSERT() - Assert X is true, if not panic. 32 * ASSERT3B() - Assert boolean X OP Y is true, if not panic. 33 * ASSERT3S() - Assert signed X OP Y is true, if not panic. 34 * ASSERT3U() - Assert unsigned X OP Y is true, if not panic. 35 * ASSERT3P() - Assert pointer X OP Y is true, if not panic. 36 * ASSERT0() - Assert value is zero, if not panic. 37 * ASSERT0P() - Assert pointer is null, if not panic. 38 * VERIFY() - Verify X is true, if not panic. 39 * VERIFY3B() - Verify boolean X OP Y is true, if not panic. 40 * VERIFY3S() - Verify signed X OP Y is true, if not panic. 41 * VERIFY3U() - Verify unsigned X OP Y is true, if not panic. 42 * VERIFY3P() - Verify pointer X OP Y is true, if not panic. 43 * VERIFY0() - Verify value is zero, if not panic. 44 * VERIFY0P() - Verify pointer is null, if not panic. 45 */ 46 47 #ifndef _SPL_DEBUG_H 48 #define _SPL_DEBUG_H 49 50 /* 51 * Common DEBUG functionality. 52 */ 53 #define __printflike(a, b) __printf(a, b) 54 55 #ifndef __maybe_unused 56 #define __maybe_unused __attribute__((unused)) 57 #endif 58 59 /* 60 * Without this, we see warnings from objtool during normal Linux builds when 61 * the kernel is built with CONFIG_STACK_VALIDATION=y: 62 * 63 * warning: objtool: tsd_create() falls through to next function __list_add() 64 * warning: objtool: .text: unexpected end of section 65 * 66 * Until the toolchain stops doing this, we must only define this attribute on 67 * spl_panic() when doing static analysis. 68 */ 69 #if defined(__COVERITY__) || defined(__clang_analyzer__) 70 __attribute__((__noreturn__)) 71 #endif 72 extern void spl_panic(const char *file, const char *func, int line, 73 const char *fmt, ...); 74 extern void spl_dumpstack(void); 75 76 static inline int 77 spl_assert(const char *buf, const char *file, const char *func, int line) 78 { 79 spl_panic(file, func, line, "%s", buf); 80 return (0); 81 } 82 83 #define PANIC(fmt, a...) \ 84 spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a) 85 86 #define VERIFY(cond) \ 87 (void) (unlikely(!(cond)) && \ 88 spl_assert("VERIFY(" #cond ") failed\n", \ 89 __FILE__, __FUNCTION__, __LINE__)) 90 91 #define VERIFY3B(LEFT, OP, RIGHT) do { \ 92 const boolean_t _verify3_left = (boolean_t)(LEFT); \ 93 const boolean_t _verify3_right = (boolean_t)(RIGHT); \ 94 if (unlikely(!(_verify3_left OP _verify3_right))) \ 95 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 96 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \ 97 "failed (%d " #OP " %d)\n", \ 98 (boolean_t)_verify3_left, \ 99 (boolean_t)_verify3_right); \ 100 } while (0) 101 102 #define VERIFY3S(LEFT, OP, RIGHT) do { \ 103 const int64_t _verify3_left = (int64_t)(LEFT); \ 104 const int64_t _verify3_right = (int64_t)(RIGHT); \ 105 if (unlikely(!(_verify3_left OP _verify3_right))) \ 106 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 107 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \ 108 "failed (%lld " #OP " %lld)\n", \ 109 (long long)_verify3_left, \ 110 (long long)_verify3_right); \ 111 } while (0) 112 113 #define VERIFY3U(LEFT, OP, RIGHT) do { \ 114 const uint64_t _verify3_left = (uint64_t)(LEFT); \ 115 const uint64_t _verify3_right = (uint64_t)(RIGHT); \ 116 if (unlikely(!(_verify3_left OP _verify3_right))) \ 117 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 118 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \ 119 "failed (%llu " #OP " %llu)\n", \ 120 (unsigned long long)_verify3_left, \ 121 (unsigned long long)_verify3_right); \ 122 } while (0) 123 124 #define VERIFY3P(LEFT, OP, RIGHT) do { \ 125 const uintptr_t _verify3_left = (uintptr_t)(LEFT); \ 126 const uintptr_t _verify3_right = (uintptr_t)(RIGHT); \ 127 if (unlikely(!(_verify3_left OP _verify3_right))) \ 128 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 129 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \ 130 "failed (%px " #OP " %px)\n", \ 131 (void *)_verify3_left, \ 132 (void *)_verify3_right); \ 133 } while (0) 134 135 #define VERIFY0(RIGHT) do { \ 136 const int64_t _verify0_right = (int64_t)(RIGHT); \ 137 if (unlikely(!(0 == _verify0_right))) \ 138 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 139 "VERIFY0(" #RIGHT ") " \ 140 "failed (0 == %lld)\n", \ 141 (long long)_verify0_right); \ 142 } while (0) 143 144 #define VERIFY0P(RIGHT) do { \ 145 const uintptr_t _verify0_right = (uintptr_t)(RIGHT); \ 146 if (unlikely(!(0 == _verify0_right))) \ 147 spl_panic(__FILE__, __FUNCTION__, __LINE__, \ 148 "VERIFY0P(" #RIGHT ") " \ 149 "failed (NULL == %px)\n", \ 150 (void *)_verify0_right); \ 151 } while (0) 152 153 #define VERIFY_IMPLY(A, B) \ 154 ((void)(likely((!(A)) || (B)) || \ 155 spl_assert("(" #A ") implies (" #B ")", \ 156 __FILE__, __FUNCTION__, __LINE__))) 157 158 #define VERIFY_EQUIV(A, B) \ 159 ((void)(likely(!!(A) == !!(B)) || \ 160 spl_assert("(" #A ") is equivalent to (" #B ")", \ 161 __FILE__, __FUNCTION__, __LINE__))) 162 163 /* 164 * Debugging disabled (--disable-debug) 165 */ 166 #ifdef NDEBUG 167 168 #define ASSERT(x) ((void) sizeof ((uintptr_t)(x))) 169 #define ASSERT3B(x, y, z) \ 170 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z))) 171 #define ASSERT3S(x, y, z) \ 172 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z))) 173 #define ASSERT3U(x, y, z) \ 174 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z))) 175 #define ASSERT3P(x, y, z) \ 176 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z))) 177 #define ASSERT0(x) ((void) sizeof ((uintptr_t)(x))) 178 #define ASSERT0P(x) ((void) sizeof ((uintptr_t)(x))) 179 #define IMPLY(A, B) \ 180 ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B))) 181 #define EQUIV(A, B) \ 182 ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B))) 183 184 /* 185 * Debugging enabled (--enable-debug) 186 */ 187 #else 188 189 #define ASSERT3B VERIFY3B 190 #define ASSERT3S VERIFY3S 191 #define ASSERT3U VERIFY3U 192 #define ASSERT3P VERIFY3P 193 #define ASSERT0 VERIFY0 194 #define ASSERT0P VERIFY0P 195 #define ASSERT VERIFY 196 #define IMPLY VERIFY_IMPLY 197 #define EQUIV VERIFY_EQUIV 198 199 #endif /* NDEBUG */ 200 201 #endif /* SPL_DEBUG_H */ 202