xref: /freebsd/sys/contrib/openzfs/include/os/linux/spl/sys/debug.h (revision d0abb9a6399accc9053e2808052be00a6754ecef)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2020 iXsystems, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 /*
31  * Available Solaris debug functions.  All of the ASSERT() macros will be
32  * compiled out when NDEBUG is defined, this is the default behavior for
33  * the SPL.  To enable assertions use the --enable-debug with configure.
34  * The VERIFY() functions are never compiled out and cannot be disabled.
35  *
36  * PANIC()	- Panic the node and print message.
37  * ASSERT()	- Assert X is true, if not panic.
38  * ASSERT3B()	- Assert boolean X OP Y is true, if not panic.
39  * ASSERT3S()	- Assert signed X OP Y is true, if not panic.
40  * ASSERT3U()	- Assert unsigned X OP Y is true, if not panic.
41  * ASSERT3P()	- Assert pointer X OP Y is true, if not panic.
42  * ASSERT0()	- Assert value is zero, if not panic.
43  * ASSERT0P()	- Assert pointer is null, if not panic.
44  * VERIFY()	- Verify X is true, if not panic.
45  * VERIFY3B()	- Verify boolean X OP Y is true, if not panic.
46  * VERIFY3S()	- Verify signed X OP Y is true, if not panic.
47  * VERIFY3U()	- Verify unsigned X OP Y is true, if not panic.
48  * VERIFY3P()	- Verify pointer X OP Y is true, if not panic.
49  * VERIFY0()	- Verify value is zero, if not panic.
50  * VERIFY0P()	- Verify pointer is null, if not panic.
51  */
52 
53 #ifndef _SPL_DEBUG_H
54 #define	_SPL_DEBUG_H
55 
56 
57 /*
58  * Common DEBUG functionality.
59  */
60 #ifdef __FreeBSD__
61 #include <linux/compiler.h>
62 #endif
63 
64 #ifndef __printflike
65 #define	__printflike(a, b)	__printf(a, b)
66 #endif
67 
68 #ifndef __maybe_unused
69 #define	__maybe_unused __attribute__((unused))
70 #endif
71 
72 #ifndef __must_check
73 #define	__must_check __attribute__((__warn_unused_result__))
74 #endif
75 
76 /*
77  * Without this, we see warnings from objtool during normal Linux builds when
78  * the kernel is built with CONFIG_STACK_VALIDATION=y:
79  *
80  * warning: objtool: tsd_create() falls through to next function __list_add()
81  * warning: objtool: .text: unexpected end of section
82  *
83  * Until the toolchain stops doing this, we must only define this attribute on
84  * spl_panic() when doing static analysis.
85  */
86 #if defined(__COVERITY__) || defined(__clang_analyzer__)
87 __attribute__((__noreturn__))
88 #endif
89 extern void spl_panic(const char *file, const char *func, int line,
90     const char *fmt, ...);
91 extern void spl_dumpstack(void);
92 
93 static inline int
spl_assert(const char * buf,const char * file,const char * func,int line)94 spl_assert(const char *buf, const char *file, const char *func, int line)
95 {
96 	spl_panic(file, func, line, "%s", buf);
97 	return (0);
98 }
99 
100 #ifndef expect
101 #define	expect(expr, value) (__builtin_expect((expr), (value)))
102 #endif
103 #ifndef __linux__
104 #define	likely(expr)   expect((expr) != 0, 1)
105 #define	unlikely(expr) expect((expr) != 0, 0)
106 #endif
107 
108 #define	PANIC(fmt, a...)						\
109 	spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
110 
111 #define	VERIFY(cond)							\
112 	(void) (unlikely(!(cond)) &&					\
113 	    spl_assert("VERIFY(" #cond ") failed\n",			\
114 	    __FILE__, __FUNCTION__, __LINE__))
115 
116 #define	VERIFYF(cond, str, ...)		do {				\
117 		if (unlikely(!(cond)))					\
118 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
119 		    "VERIFY(" #cond ") failed " str "\n", __VA_ARGS__);\
120 	} while (0)
121 
122 #define	VERIFY3B(LEFT, OP, RIGHT)	do {				\
123 		const boolean_t _verify3_left = (boolean_t)!!(LEFT);	\
124 		const boolean_t _verify3_right = (boolean_t)!!(RIGHT);	\
125 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
126 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
127 		    "VERIFY3B(" #LEFT ", "  #OP ", "  #RIGHT ") "	\
128 		    "failed (%d " #OP " %d)\n",				\
129 		    _verify3_left, _verify3_right);			\
130 	} while (0)
131 
132 #define	VERIFY3S(LEFT, OP, RIGHT)	do {				\
133 		const int64_t _verify3_left = (int64_t)(LEFT);		\
134 		const int64_t _verify3_right = (int64_t)(RIGHT);	\
135 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
136 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
137 		    "VERIFY3S(" #LEFT ", "  #OP ", "  #RIGHT ") "	\
138 		    "failed (%lld " #OP " %lld)\n",			\
139 		    (long long)_verify3_left,				\
140 		    (long long)_verify3_right);				\
141 	} while (0)
142 
143 #define	VERIFY3U(LEFT, OP, RIGHT)	do {				\
144 		const uint64_t _verify3_left = (uint64_t)(LEFT);	\
145 		const uint64_t _verify3_right = (uint64_t)(RIGHT);	\
146 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
147 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
148 		    "VERIFY3U(" #LEFT ", "  #OP ", "  #RIGHT ") "	\
149 		    "failed (%llu " #OP " %llu)\n",			\
150 		    (unsigned long long)_verify3_left,			\
151 		    (unsigned long long)_verify3_right);		\
152 	} while (0)
153 
154 #define	VERIFY3P(LEFT, OP, RIGHT)	do {				\
155 		const uintptr_t _verify3_left = (uintptr_t)(LEFT);	\
156 		const uintptr_t _verify3_right = (uintptr_t)(RIGHT);	\
157 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
158 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
159 		    "VERIFY3P(" #LEFT ", "  #OP ", "  #RIGHT ") "	\
160 		    "failed (%px " #OP " %px)\n",			\
161 		    (void *)_verify3_left,				\
162 		    (void *)_verify3_right);				\
163 	} while (0)
164 
165 #define	VERIFY0(RIGHT)	do {						\
166 		const int64_t _verify0_right = (int64_t)(RIGHT);	\
167 		if (unlikely(!(0 == _verify0_right)))			\
168 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
169 		    "VERIFY0(" #RIGHT ") failed (%lld)\n",		\
170 		    (long long)_verify0_right);				\
171 	} while (0)
172 
173 #define	VERIFY0P(RIGHT)	do {						\
174 		const uintptr_t _verify0_right = (uintptr_t)(RIGHT);	\
175 		if (unlikely(!(0 == _verify0_right)))			\
176 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
177 		    "VERIFY0P(" #RIGHT ") failed (%px)\n",		\
178 		    (void *)_verify0_right);				\
179 	} while (0)
180 
181 /*
182  * Note that you should not put any operations you want to always happen
183  * in the print section for ASSERTs unless you only want them to run on
184  * debug builds!
185  * e.g. ASSERT3UF(2, <, 3, "%s", foo(x)), foo(x) won't run on non-debug
186  * builds.
187  */
188 
189 #define	VERIFY3BF(LEFT, OP, RIGHT, STR, ...)	do {			\
190 		const boolean_t _verify3_left = (boolean_t)!!(LEFT);	\
191 		const boolean_t _verify3_right = (boolean_t)!!(RIGHT);	\
192 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
193 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
194 		    "VERIFY3B(" #LEFT ", "  #OP ", "  #RIGHT ") "	\
195 		    "failed (%d " #OP " %d) " STR "\n",			\
196 		    _verify3_left, _verify3_right,			\
197 		    __VA_ARGS__);					\
198 	} while (0)
199 
200 #define	VERIFY3SF(LEFT, OP, RIGHT, STR, ...)	do {			\
201 		const int64_t _verify3_left = (int64_t)(LEFT);		\
202 		const int64_t _verify3_right = (int64_t)(RIGHT);	\
203 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
204 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
205 		    "VERIFY3S(" #LEFT ", "  #OP ", "  #RIGHT ") "	\
206 		    "failed (%lld " #OP " %lld) " STR "\n",		\
207 		    (long long)_verify3_left, (long long)_verify3_right,\
208 		    __VA_ARGS__);					\
209 	} while (0)
210 
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 		    "VERIFY3U(" #LEFT ", "  #OP ", "  #RIGHT ") "	\
217 		    "failed (%llu " #OP " %llu) " STR "\n",		\
218 		    (unsigned long long)_verify3_left,			\
219 		    (unsigned long long)_verify3_right,			\
220 		    __VA_ARGS__);					\
221 	} while (0)
222 
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 		    "VERIFY3P(" #LEFT ", "  #OP ", "  #RIGHT ") "	\
229 		    "failed (%px " #OP " %px) " STR "\n",		\
230 		    (void *)_verify3_left, (void *)_verify3_right,	\
231 		    __VA_ARGS__);					\
232 	} while (0)
233 
234 #define	VERIFY0PF(RIGHT, STR, ...)	do {				\
235 		const uintptr_t _verify3_right = (uintptr_t)(RIGHT);	\
236 		if (unlikely(!(0 == _verify3_right)))			\
237 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
238 		    "VERIFY0P(" #RIGHT ") failed (%px) " STR "\n",	\
239 		    (void *)_verify3_right,				\
240 		    __VA_ARGS__);					\
241 	} while (0)
242 
243 #define	VERIFY0F(RIGHT, STR, ...)	do {				\
244 		const int64_t _verify3_right = (int64_t)(RIGHT);	\
245 		if (unlikely(!(0 == _verify3_right)))			\
246 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
247 		    "VERIFY0(" #RIGHT ") failed (%lld) " STR "\n",	\
248 		    (long long)_verify3_right,				\
249 		    __VA_ARGS__);					\
250 	} while (0)
251 
252 #define	VERIFY_IMPLY(A, B) \
253 	((void)(likely((!(A)) || (B)) ||				\
254 	    spl_assert("(" #A ") implies (" #B ")",			\
255 	    __FILE__, __FUNCTION__, __LINE__)))
256 
257 #define	VERIFY_EQUIV(A, B)	VERIFY3B(A, ==, B)
258 
259 /*
260  * Debugging disabled (--disable-debug)
261  */
262 #ifdef NDEBUG
263 
264 #define	ASSERT(x)		((void) sizeof ((uintptr_t)(x)))
265 #define	ASSERT3B(x, y, z)						\
266 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
267 #define	ASSERT3S(x, y, z)						\
268 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
269 #define	ASSERT3U(x, y, z)						\
270 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
271 #define	ASSERT3P(x, y, z)						\
272 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
273 #define	ASSERT0(x)		((void) sizeof ((uintptr_t)(x)))
274 #define	ASSERT0P(x)		((void) sizeof ((uintptr_t)(x)))
275 #define	ASSERT3BF(x, y, z, str, ...)	ASSERT3B(x, y, z)
276 #define	ASSERT3SF(x, y, z, str, ...)	ASSERT3S(x, y, z)
277 #define	ASSERT3UF(x, y, z, str, ...)	ASSERT3U(x, y, z)
278 #define	ASSERT3PF(x, y, z, str, ...)	ASSERT3P(x, y, z)
279 #define	ASSERT0PF(x, str, ...)		ASSERT0P(x)
280 #define	ASSERT0F(x, str, ...)		ASSERT0(x)
281 #define	ASSERTF(x, str, ...)		ASSERT(x)
282 #define	IMPLY(A, B)							\
283 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
284 #define	EQUIV(A, B)		\
285 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
286 
287 /*
288  * Debugging enabled (--enable-debug)
289  */
290 #else
291 
292 #define	ASSERT3B	VERIFY3B
293 #define	ASSERT3S	VERIFY3S
294 #define	ASSERT3U	VERIFY3U
295 #define	ASSERT3P	VERIFY3P
296 #define	ASSERT0		VERIFY0
297 #define	ASSERT0P	VERIFY0P
298 #define	ASSERT3BF	VERIFY3BF
299 #define	ASSERT3SF	VERIFY3SF
300 #define	ASSERT3UF	VERIFY3UF
301 #define	ASSERT3PF	VERIFY3PF
302 #define	ASSERT0PF	VERIFY0PF
303 #define	ASSERT0F	VERIFY0F
304 #define	ASSERTF		VERIFYF
305 #define	ASSERT		VERIFY
306 #define	IMPLY		VERIFY_IMPLY
307 #define	EQUIV		VERIFY_EQUIV
308 
309 #endif /* NDEBUG */
310 
311 #endif /* SPL_DEBUG_H */
312