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  * 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.
50  */
51 
52 #ifndef _SPL_DEBUG_H
53 #define	_SPL_DEBUG_H
54 
55 
56 /*
57  * Common DEBUG functionality.
58  */
59 #if defined(__COVERITY__) || defined(__clang_analyzer__)
60 __attribute__((__noreturn__))
61 #endif
62 extern void spl_panic(const char *file, const char *func, int line,
63     const char *fmt, ...) __attribute__((__noreturn__));
64 extern void spl_dumpstack(void);
65 
66 static inline int
67 spl_assert(const char *buf, const char *file, const char *func, int line)
68 {
69 	spl_panic(file, func, line, "%s", buf);
70 	return (0);
71 }
72 
73 #ifndef expect
74 #define	expect(expr, value) (__builtin_expect((expr), (value)))
75 #endif
76 #define	likely(expr)   expect((expr) != 0, 1)
77 #define	unlikely(expr) expect((expr) != 0, 0)
78 
79 #define	PANIC(fmt, a...)						\
80 	spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
81 
82 #define	VERIFY(cond)							\
83 	(void) (unlikely(!(cond)) &&					\
84 	    spl_assert("VERIFY(" #cond ") failed\n",			\
85 	    __FILE__, __FUNCTION__, __LINE__))
86 
87 #define	VERIFY3B(LEFT, OP, RIGHT)	do {				\
88 		const boolean_t _verify3_left = (boolean_t)(LEFT);	\
89 		const boolean_t _verify3_right = (boolean_t)(RIGHT);	\
90 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
91 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
92 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
93 		    "failed (%d " #OP " %d)\n",				\
94 		    (boolean_t)_verify3_left,				\
95 		    (boolean_t)_verify3_right);				\
96 	} while (0)
97 
98 #define	VERIFY3S(LEFT, OP, RIGHT)	do {				\
99 		const int64_t _verify3_left = (int64_t)(LEFT);		\
100 		const int64_t _verify3_right = (int64_t)(RIGHT);	\
101 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
102 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
103 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
104 		    "failed (%lld " #OP " %lld)\n",			\
105 		    (long long)_verify3_left,				\
106 		    (long long)_verify3_right);				\
107 	} while (0)
108 
109 #define	VERIFY3U(LEFT, OP, RIGHT)	do {				\
110 		const uint64_t _verify3_left = (uint64_t)(LEFT);	\
111 		const uint64_t _verify3_right = (uint64_t)(RIGHT);	\
112 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
113 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
114 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
115 		    "failed (%llu " #OP " %llu)\n",			\
116 		    (unsigned long long)_verify3_left,			\
117 		    (unsigned long long)_verify3_right);		\
118 	} while (0)
119 
120 #define	VERIFY3P(LEFT, OP, RIGHT)	do {				\
121 		const uintptr_t _verify3_left = (uintptr_t)(LEFT);	\
122 		const uintptr_t _verify3_right = (uintptr_t)(RIGHT);	\
123 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
124 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
125 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
126 		    "failed (%p " #OP " %p)\n",				\
127 		    (void *)_verify3_left,				\
128 		    (void *)_verify3_right);				\
129 	} while (0)
130 
131 #define	VERIFY0(RIGHT)	do {						\
132 		const int64_t _verify0_right = (int64_t)(RIGHT);	\
133 		if (unlikely(!(0 == _verify0_right)))			\
134 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
135 		    "VERIFY0(" #RIGHT ") "				\
136 		    "failed (0 == %lld)\n",				\
137 		    (long long)_verify0_right);				\
138 	} while (0)
139 
140 #define	VERIFY0P(RIGHT)	do {						\
141 		const uintptr_t _verify0_right = (uintptr_t)(RIGHT);	\
142 		if (unlikely(!(0 == _verify0_right)))			\
143 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
144 		    "VERIFY0P(" #RIGHT ") "				\
145 		    "failed (NULL == %p)\n",				\
146 		    (void *)_verify0_right);				\
147 	} while (0)
148 
149 /*
150  * Debugging disabled (--disable-debug)
151  */
152 #ifdef NDEBUG
153 
154 #define	ASSERT(x)		((void) sizeof ((uintptr_t)(x)))
155 #define	ASSERT3B(x, y, z)						\
156 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
157 #define	ASSERT3S(x, y, z)						\
158 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
159 #define	ASSERT3U(x, y, z)						\
160 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
161 #define	ASSERT3P(x, y, z)						\
162 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
163 #define	ASSERT0(x)		((void) sizeof ((uintptr_t)(x)))
164 #define	ASSERT0P(x)		((void) sizeof ((uintptr_t)(x)))
165 #define	IMPLY(A, B)							\
166 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
167 #define	EQUIV(A, B)		\
168 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
169 
170 /*
171  * Debugging enabled (--enable-debug)
172  */
173 #else
174 
175 #define	ASSERT3B	VERIFY3B
176 #define	ASSERT3S	VERIFY3S
177 #define	ASSERT3U	VERIFY3U
178 #define	ASSERT3P	VERIFY3P
179 #define	ASSERT0		VERIFY0
180 #define	ASSERT0P	VERIFY0P
181 #define	ASSERT		VERIFY
182 #define	IMPLY(A, B) \
183 	((void)(likely((!(A)) || (B)) ||				\
184 	    spl_assert("(" #A ") implies (" #B ")",			\
185 	    __FILE__, __FUNCTION__, __LINE__)))
186 #define	EQUIV(A, B) \
187 	((void)(likely(!!(A) == !!(B)) || 				\
188 	    spl_assert("(" #A ") is equivalent to (" #B ")",		\
189 	    __FILE__, __FUNCTION__, __LINE__)))
190 
191 
192 #endif /* NDEBUG */
193 
194 #endif /* SPL_DEBUG_H */
195