2 * Copyright (c) 2020 iXsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.
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 * ASSERT0P() - Assert pointer is null, if not panic.
43 * VERIFY() - Verify X is true, if not panic.
44 * VERIFY3B() - Verify boolean X OP Y is true, if not panic.
45 * VERIFY3S() - Verify signed X OP Y is true, if not panic.
46 * VERIFY3U() - Verify unsigned X OP Y is true, if not panic.
47 * VERIFY3P() - Verify pointer X OP Y is true, if not panic.
48 * VERIFY0() - Verify value is zero, if not panic.
49 * VERIFY0P() - Verify pointer is null, if not panic.
57 * Common DEBUG functionality.
60 #include <linux/compiler.h>
64 #define __printflike(a, b) __printf(a, b)
67 #ifndef __maybe_unused
68 #define __maybe_unused __attribute__((unused))
72 * Without this, we see warnings from objtool during normal Linux builds when
73 * the kernel is built with CONFIG_STACK_VALIDATION=y:
75 * warning: objtool: tsd_create() falls through to next function __list_add()
76 * warning: objtool: .text: unexpected end of section
78 * Until the toolchain stops doing this, we must only define this attribute on
79 * spl_panic() when doing static analysis.
81 #if defined(__COVERITY__) || defined(__clang_analyzer__)
82 __attribute__((__noreturn__
))
84 extern void spl_panic(const char *file
, const char *func
, int line
,
85 const char *fmt
, ...);
86 extern void spl_dumpstack(void);
89 spl_assert(const char *buf
, const char *file
, const char *func
, int line
)
91 spl_panic(file
, func
, line
, "%s", buf
);
96 #define expect(expr, value) (__builtin_expect((expr), (value)))
99 #define PANIC(fmt, a...) \
100 spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
102 #define VERIFY(cond) \
103 (void) (unlikely(!(cond)) && \
104 spl_assert("VERIFY(" #cond ") failed\n", \
105 __FILE__, __FUNCTION__, __LINE__))
107 #define VERIFYF(cond, str, ...) do { \
108 if (unlikely(!cond)) \
109 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
110 "VERIFY(" #cond ") failed " str "\n", __VA_ARGS__);\
113 #define VERIFY3B(LEFT, OP, RIGHT) do { \
114 const boolean_t _verify3_left = (boolean_t)(LEFT); \
115 const boolean_t _verify3_right = (boolean_t)(RIGHT); \
116 if (unlikely(!(_verify3_left OP _verify3_right))) \
117 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
118 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
119 "failed (%d " #OP " %d)\n", \
120 (boolean_t)_verify3_left, \
121 (boolean_t)_verify3_right); \
124 #define VERIFY3S(LEFT, OP, RIGHT) do { \
125 const int64_t _verify3_left = (int64_t)(LEFT); \
126 const int64_t _verify3_right = (int64_t)(RIGHT); \
127 if (unlikely(!(_verify3_left OP _verify3_right))) \
128 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
129 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
130 "failed (%lld " #OP " %lld)\n", \
131 (long long)_verify3_left, \
132 (long long)_verify3_right); \
135 #define VERIFY3U(LEFT, OP, RIGHT) do { \
136 const uint64_t _verify3_left = (uint64_t)(LEFT); \
137 const uint64_t _verify3_right = (uint64_t)(RIGHT); \
138 if (unlikely(!(_verify3_left OP _verify3_right))) \
139 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
140 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
141 "failed (%llu " #OP " %llu)\n", \
142 (unsigned long long)_verify3_left, \
143 (unsigned long long)_verify3_right); \
146 #define VERIFY3P(LEFT, OP, RIGHT) do { \
147 const uintptr_t _verify3_left = (uintptr_t)(LEFT); \
148 const uintptr_t _verify3_right = (uintptr_t)(RIGHT); \
149 if (unlikely(!(_verify3_left OP _verify3_right))) \
150 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
151 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
152 "failed (%px " #OP " %px)\n", \
153 (void *)_verify3_left, \
154 (void *)_verify3_right); \
157 #define VERIFY0(RIGHT) do { \
158 const int64_t _verify0_right = (int64_t)(RIGHT); \
159 if (unlikely(!(0 == _verify0_right))) \
160 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
161 "VERIFY0(" #RIGHT ") " \
162 "failed (0 == %lld)\n", \
163 (long long)_verify0_right); \
166 #define VERIFY0P(RIGHT) do { \
167 const uintptr_t _verify0_right = (uintptr_t)(RIGHT); \
168 if (unlikely(!(0 == _verify0_right))) \
169 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
170 "VERIFY0P(" #RIGHT ") " \
171 "failed (NULL == %px)\n", \
172 (void *)_verify0_right); \
176 * Note that you should not put any operations you want to always happen
177 * in the print section for ASSERTs unless you only want them to run on
179 * e.g. ASSERT3UF(2, <, 3, "%s", foo(x)), foo(x) won't run on non-debug
183 #define VERIFY3BF(LEFT, OP, RIGHT, STR, ...) do { \
184 const boolean_t _verify3_left = (boolean_t)(LEFT); \
185 const boolean_t _verify3_right = (boolean_t)(RIGHT); \
186 if (unlikely(!(_verify3_left OP _verify3_right))) \
187 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
188 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
189 "failed (%d " #OP " %d) " STR "\n", \
190 (boolean_t)(_verify3_left), \
191 (boolean_t)(_verify3_right), \
195 #define VERIFY3SF(LEFT, OP, RIGHT, STR, ...) do { \
196 const int64_t _verify3_left = (int64_t)(LEFT); \
197 const int64_t _verify3_right = (int64_t)(RIGHT); \
198 if (unlikely(!(_verify3_left OP _verify3_right))) \
199 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
200 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
201 "failed (%lld " #OP " %lld) " STR "\n", \
202 (long long)(_verify3_left), \
203 (long long)(_verify3_right), \
207 #define VERIFY3UF(LEFT, OP, RIGHT, STR, ...) do { \
208 const uint64_t _verify3_left = (uint64_t)(LEFT); \
209 const uint64_t _verify3_right = (uint64_t)(RIGHT); \
210 if (unlikely(!(_verify3_left OP _verify3_right))) \
211 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
212 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
213 "failed (%llu " #OP " %llu) " STR "\n", \
214 (unsigned long long)(_verify3_left), \
215 (unsigned long long)(_verify3_right), \
219 #define VERIFY3PF(LEFT, OP, RIGHT, STR, ...) do { \
220 const uintptr_t _verify3_left = (uintptr_t)(LEFT); \
221 const uintptr_t _verify3_right = (uintptr_t)(RIGHT); \
222 if (unlikely(!(_verify3_left OP _verify3_right))) \
223 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
224 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
225 "failed (%px " #OP " %px) " STR "\n", \
226 (void *) (_verify3_left), \
227 (void *) (_verify3_right), \
231 #define VERIFY0PF(RIGHT, STR, ...) do { \
232 const uintptr_t _verify3_left = (uintptr_t)(0); \
233 const uintptr_t _verify3_right = (uintptr_t)(RIGHT); \
234 if (unlikely(!(_verify3_left == _verify3_right))) \
235 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
236 "VERIFY0(0 == " #RIGHT ") " \
237 "failed (0 == %px) " STR "\n", \
238 (long long) (_verify3_right), \
242 #define VERIFY0F(RIGHT, STR, ...) do { \
243 const int64_t _verify3_left = (int64_t)(0); \
244 const int64_t _verify3_right = (int64_t)(RIGHT); \
245 if (unlikely(!(_verify3_left == _verify3_right))) \
246 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
247 "VERIFY0(0 == " #RIGHT ") " \
248 "failed (0 == %lld) " STR "\n", \
249 (long long) (_verify3_right), \
253 #define VERIFY_IMPLY(A, B) \
254 ((void)(likely((!(A)) || (B)) || \
255 spl_assert("(" #A ") implies (" #B ")", \
256 __FILE__, __FUNCTION__, __LINE__)))
258 #define VERIFY_EQUIV(A, B) \
259 ((void)(likely(!!(A) == !!(B)) || \
260 spl_assert("(" #A ") is equivalent to (" #B ")", \
261 __FILE__, __FUNCTION__, __LINE__)))
264 * Debugging disabled (--disable-debug)
268 #define ASSERT(x) ((void) sizeof ((uintptr_t)(x)))
269 #define ASSERT3B(x, y, z) \
270 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
271 #define ASSERT3S(x, y, z) \
272 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
273 #define ASSERT3U(x, y, z) \
274 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
275 #define ASSERT3P(x, y, z) \
276 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
277 #define ASSERT0(x) ((void) sizeof ((uintptr_t)(x)))
278 #define ASSERT0P(x) ((void) sizeof ((uintptr_t)(x)))
279 #define ASSERT3BF(x, y, z, str, ...) ASSERT3B(x, y, z)
280 #define ASSERT3SF(x, y, z, str, ...) ASSERT3S(x, y, z)
281 #define ASSERT3UF(x, y, z, str, ...) ASSERT3U(x, y, z)
282 #define ASSERT3PF(x, y, z, str, ...) ASSERT3P(x, y, z)
283 #define ASSERT0PF(x, str, ...) ASSERT0P(x)
284 #define ASSERT0F(x, str, ...) ASSERT0(x)
285 #define ASSERTF(x, str, ...) ASSERT(x)
286 #define IMPLY(A, B) \
287 ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
288 #define EQUIV(A, B) \
289 ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
292 * Debugging enabled (--enable-debug)
296 #define ASSERT3B VERIFY3B
297 #define ASSERT3S VERIFY3S
298 #define ASSERT3U VERIFY3U
299 #define ASSERT3P VERIFY3P
300 #define ASSERT0 VERIFY0
301 #define ASSERT0P VERIFY0P
302 #define ASSERT3BF VERIFY3BF
303 #define ASSERT3SF VERIFY3SF
304 #define ASSERT3UF VERIFY3UF
305 #define ASSERT3PF VERIFY3PF
306 #define ASSERT0PF VERIFY0PF
307 #define ASSERT0F VERIFY0F
308 #define ASSERTF VERIFYF
309 #define ASSERT VERIFY
310 #define IMPLY VERIFY_IMPLY
311 #define EQUIV VERIFY_EQUIV
315 #endif /* SPL_DEBUG_H */