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 int spl_panic(const char *file, const char *func, int line,
58     const char *fmt, ...);
59 void spl_dumpstack(void);
60 
61 #ifndef expect
62 #define	expect(expr, value) (__builtin_expect((expr), (value)))
63 #endif
64 #define	likely(expr)   expect((expr) != 0, 1)
65 #define	unlikely(expr) expect((expr) != 0, 0)
66 
67 /* BEGIN CSTYLED */
68 #define	PANIC(fmt, a...)						\
69 	spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
70 
71 #define	VERIFY(cond)										\
72 	(void) (unlikely(!(cond)) &&							\
73 	    spl_panic(__FILE__, __FUNCTION__, __LINE__,			\
74 	    "%s", "VERIFY(" #cond ") failed\n"))
75 
76 #define	VERIFY3B(LEFT, OP, RIGHT)	do {					\
77 		const boolean_t _verify3_left = (boolean_t)(LEFT);	\
78 		const boolean_t _verify3_right = (boolean_t)(RIGHT);\
79 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
80 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
81 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
82 		    "failed (%d " #OP " %d)\n",						\
83 		    (boolean_t) (_verify3_left),					\
84 		    (boolean_t) (_verify3_right));					\
85 	} while (0)
86 
87 #define	VERIFY3S(LEFT, OP, RIGHT)	do {					\
88 		const int64_t _verify3_left = (int64_t)(LEFT);		\
89 		const int64_t _verify3_right = (int64_t)(RIGHT);	\
90 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
91 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
92 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
93 		    "failed (%lld " #OP " %lld)\n",					\
94 		    (long long) (_verify3_left),					\
95 		    (long long) (_verify3_right));					\
96 	} while (0)
97 
98 #define	VERIFY3U(LEFT, OP, RIGHT)	do {					\
99 		const uint64_t _verify3_left = (uint64_t)(LEFT);	\
100 		const uint64_t _verify3_right = (uint64_t)(RIGHT);	\
101 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
102 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
103 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
104 		    "failed (%llu " #OP " %llu)\n",					\
105 		    (unsigned long long) (_verify3_left),			\
106 		    (unsigned long long) (_verify3_right));			\
107 	} while (0)
108 
109 #define	VERIFY3P(LEFT, OP, RIGHT)	do {					\
110 		const uintptr_t _verify3_left = (uintptr_t)(LEFT);	\
111 		const uintptr_t _verify3_right = (uintptr_t)(RIGHT);\
112 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
113 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
114 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
115 		    "failed (%px " #OP " %px)\n",					\
116 		    (void *) (_verify3_left),						\
117 		    (void *) (_verify3_right));						\
118 	} while (0)
119 
120 #define	VERIFY0(RIGHT)	do {								\
121 		const int64_t _verify3_left = (int64_t)(0);			\
122 		const int64_t _verify3_right = (int64_t)(RIGHT);	\
123 		if (unlikely(!(_verify3_left == _verify3_right)))	\
124 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
125 		    "VERIFY3(0 == " #RIGHT ") "						\
126 		    "failed (0 == %lld)\n",							\
127 		    (long long) (_verify3_right));					\
128 	} while (0)
129 #define        CTASSERT_GLOBAL(x)              CTASSERT(x)
130 
131 /*
132  * Debugging disabled (--disable-debug)
133  */
134 #ifdef NDEBUG
135 
136 #define	ASSERT(x)		((void)0)
137 #define	ASSERT3B(x,y,z)		((void)0)
138 #define	ASSERT3S(x,y,z)		((void)0)
139 #define	ASSERT3U(x,y,z)		((void)0)
140 #define	ASSERT3P(x,y,z)		((void)0)
141 #define	ASSERT0(x)		((void)0)
142 #define	IMPLY(A, B)		((void)0)
143 #define	EQUIV(A, B)		((void)0)
144 
145 /*
146  * Debugging enabled (--enable-debug)
147  */
148 #else
149 
150 #define	ASSERT3B	VERIFY3B
151 #define	ASSERT3S	VERIFY3S
152 #define	ASSERT3U	VERIFY3U
153 #define	ASSERT3P	VERIFY3P
154 #define	ASSERT0		VERIFY0
155 #define	ASSERT		VERIFY
156 #define	IMPLY(A, B) \
157 	((void)(likely((!(A)) || (B)) || \
158 	    spl_panic(__FILE__, __FUNCTION__, __LINE__, \
159 	    "(" #A ") implies (" #B ")")))
160 #define	EQUIV(A, B) \
161 	((void)(likely(!!(A) == !!(B)) || \
162 	    spl_panic(__FILE__, __FUNCTION__, __LINE__, \
163 	    "(" #A ") is equivalent to (" #B ")")))
164 /* END CSTYLED */
165 
166 #endif /* NDEBUG */
167 
168 #endif /* SPL_DEBUG_H */
169