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 likely(expr) expect((expr) != 0, 1)
100 #define unlikely(expr) expect((expr) != 0, 0)
103 #define PANIC(fmt, a...) \
104 spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
106 #define VERIFY(cond) \
107 (void) (unlikely(!(cond)) && \
108 spl_assert("VERIFY(" #cond ") failed\n", \
109 __FILE__, __FUNCTION__, __LINE__))
111 #define VERIFYF(cond, str, ...) do { \
112 if (unlikely(!(cond))) \
113 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
114 "VERIFY(" #cond ") failed " str "\n", __VA_ARGS__);\
117 #define VERIFY3B(LEFT, OP, RIGHT) do { \
118 const boolean_t _verify3_left = (boolean_t)(LEFT); \
119 const boolean_t _verify3_right = (boolean_t)(RIGHT); \
120 if (unlikely(!(_verify3_left OP _verify3_right))) \
121 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
122 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
123 "failed (%d " #OP " %d)\n", \
124 (boolean_t)_verify3_left, \
125 (boolean_t)_verify3_right); \
128 #define VERIFY3S(LEFT, OP, RIGHT) do { \
129 const int64_t _verify3_left = (int64_t)(LEFT); \
130 const int64_t _verify3_right = (int64_t)(RIGHT); \
131 if (unlikely(!(_verify3_left OP _verify3_right))) \
132 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
133 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
134 "failed (%lld " #OP " %lld)\n", \
135 (long long)_verify3_left, \
136 (long long)_verify3_right); \
139 #define VERIFY3U(LEFT, OP, RIGHT) do { \
140 const uint64_t _verify3_left = (uint64_t)(LEFT); \
141 const uint64_t _verify3_right = (uint64_t)(RIGHT); \
142 if (unlikely(!(_verify3_left OP _verify3_right))) \
143 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
144 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
145 "failed (%llu " #OP " %llu)\n", \
146 (unsigned long long)_verify3_left, \
147 (unsigned long long)_verify3_right); \
150 #define VERIFY3P(LEFT, OP, RIGHT) do { \
151 const uintptr_t _verify3_left = (uintptr_t)(LEFT); \
152 const uintptr_t _verify3_right = (uintptr_t)(RIGHT); \
153 if (unlikely(!(_verify3_left OP _verify3_right))) \
154 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
155 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
156 "failed (%px " #OP " %px)\n", \
157 (void *)_verify3_left, \
158 (void *)_verify3_right); \
161 #define VERIFY0(RIGHT) do { \
162 const int64_t _verify0_right = (int64_t)(RIGHT); \
163 if (unlikely(!(0 == _verify0_right))) \
164 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
165 "VERIFY0(" #RIGHT ") " \
166 "failed (0 == %lld)\n", \
167 (long long)_verify0_right); \
170 #define VERIFY0P(RIGHT) do { \
171 const uintptr_t _verify0_right = (uintptr_t)(RIGHT); \
172 if (unlikely(!(0 == _verify0_right))) \
173 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
174 "VERIFY0P(" #RIGHT ") " \
175 "failed (NULL == %px)\n", \
176 (void *)_verify0_right); \
180 * Note that you should not put any operations you want to always happen
181 * in the print section for ASSERTs unless you only want them to run on
183 * e.g. ASSERT3UF(2, <, 3, "%s", foo(x)), foo(x) won't run on non-debug
187 #define VERIFY3BF(LEFT, OP, RIGHT, STR, ...) do { \
188 const boolean_t _verify3_left = (boolean_t)(LEFT); \
189 const boolean_t _verify3_right = (boolean_t)(RIGHT); \
190 if (unlikely(!(_verify3_left OP _verify3_right))) \
191 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
192 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
193 "failed (%d " #OP " %d) " STR "\n", \
194 (boolean_t)(_verify3_left), \
195 (boolean_t)(_verify3_right), \
199 #define VERIFY3SF(LEFT, OP, RIGHT, STR, ...) do { \
200 const int64_t _verify3_left = (int64_t)(LEFT); \
201 const int64_t _verify3_right = (int64_t)(RIGHT); \
202 if (unlikely(!(_verify3_left OP _verify3_right))) \
203 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
204 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
205 "failed (%lld " #OP " %lld) " STR "\n", \
206 (long long)(_verify3_left), \
207 (long long)(_verify3_right), \
211 #define VERIFY3UF(LEFT, OP, RIGHT, STR, ...) do { \
212 const uint64_t _verify3_left = (uint64_t)(LEFT); \
213 const uint64_t _verify3_right = (uint64_t)(RIGHT); \
214 if (unlikely(!(_verify3_left OP _verify3_right))) \
215 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
216 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
217 "failed (%llu " #OP " %llu) " STR "\n", \
218 (unsigned long long)(_verify3_left), \
219 (unsigned long long)(_verify3_right), \
223 #define VERIFY3PF(LEFT, OP, RIGHT, STR, ...) do { \
224 const uintptr_t _verify3_left = (uintptr_t)(LEFT); \
225 const uintptr_t _verify3_right = (uintptr_t)(RIGHT); \
226 if (unlikely(!(_verify3_left OP _verify3_right))) \
227 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
228 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
229 "failed (%px " #OP " %px) " STR "\n", \
230 (void *) (_verify3_left), \
231 (void *) (_verify3_right), \
235 #define VERIFY0PF(RIGHT, STR, ...) do { \
236 const uintptr_t _verify3_left = (uintptr_t)(0); \
237 const uintptr_t _verify3_right = (uintptr_t)(RIGHT); \
238 if (unlikely(!(_verify3_left == _verify3_right))) \
239 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
240 "VERIFY0(0 == " #RIGHT ") " \
241 "failed (0 == %px) " STR "\n", \
242 (long long) (_verify3_right), \
246 #define VERIFY0F(RIGHT, STR, ...) do { \
247 const int64_t _verify3_left = (int64_t)(0); \
248 const int64_t _verify3_right = (int64_t)(RIGHT); \
249 if (unlikely(!(_verify3_left == _verify3_right))) \
250 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
251 "VERIFY0(0 == " #RIGHT ") " \
252 "failed (0 == %lld) " STR "\n", \
253 (long long) (_verify3_right), \
257 #define VERIFY_IMPLY(A, B) \
258 ((void)(likely((!(A)) || (B)) || \
259 spl_assert("(" #A ") implies (" #B ")", \
260 __FILE__, __FUNCTION__, __LINE__)))
262 #define VERIFY_EQUIV(A, B) \
263 ((void)(likely(!!(A) == !!(B)) || \
264 spl_assert("(" #A ") is equivalent to (" #B ")", \
265 __FILE__, __FUNCTION__, __LINE__)))
268 * Debugging disabled (--disable-debug)
272 #define ASSERT(x) ((void) sizeof ((uintptr_t)(x)))
273 #define ASSERT3B(x, y, z) \
274 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
275 #define ASSERT3S(x, y, z) \
276 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
277 #define ASSERT3U(x, y, z) \
278 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
279 #define ASSERT3P(x, y, z) \
280 ((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
281 #define ASSERT0(x) ((void) sizeof ((uintptr_t)(x)))
282 #define ASSERT0P(x) ((void) sizeof ((uintptr_t)(x)))
283 #define ASSERT3BF(x, y, z, str, ...) ASSERT3B(x, y, z)
284 #define ASSERT3SF(x, y, z, str, ...) ASSERT3S(x, y, z)
285 #define ASSERT3UF(x, y, z, str, ...) ASSERT3U(x, y, z)
286 #define ASSERT3PF(x, y, z, str, ...) ASSERT3P(x, y, z)
287 #define ASSERT0PF(x, str, ...) ASSERT0P(x)
288 #define ASSERT0F(x, str, ...) ASSERT0(x)
289 #define ASSERTF(x, str, ...) ASSERT(x)
290 #define IMPLY(A, B) \
291 ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
292 #define EQUIV(A, B) \
293 ((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
296 * Debugging enabled (--enable-debug)
300 #define ASSERT3B VERIFY3B
301 #define ASSERT3S VERIFY3S
302 #define ASSERT3U VERIFY3U
303 #define ASSERT3P VERIFY3P
304 #define ASSERT0 VERIFY0
305 #define ASSERT0P VERIFY0P
306 #define ASSERT3BF VERIFY3BF
307 #define ASSERT3SF VERIFY3SF
308 #define ASSERT3UF VERIFY3UF
309 #define ASSERT3PF VERIFY3PF
310 #define ASSERT0PF VERIFY0PF
311 #define ASSERT0F VERIFY0F
312 #define ASSERTF VERIFYF
313 #define ASSERT VERIFY
314 #define IMPLY VERIFY_IMPLY
315 #define EQUIV VERIFY_EQUIV
319 #endif /* SPL_DEBUG_H */