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