xref: /freebsd-src/sys/contrib/openzfs/include/os/linux/spl/sys/debug.h (revision e67e85659c0de33e617e5fbf1028c6e8b49eee53)
1 /*
2  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3  *  Copyright (C) 2007 The Regents of the University of California.
4  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6  *  UCRL-CODE-235197
7  *
8  *  This file is part of the SPL, Solaris Porting Layer.
9  *
10  *  The SPL is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License as published by the
12  *  Free Software Foundation; either version 2 of the License, or (at your
13  *  option) any later version.
14  *
15  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18  *  for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 /*
25  * Available Solaris debug functions.  All of the ASSERT() macros will be
26  * compiled out when NDEBUG is defined, this is the default behavior for
27  * the SPL.  To enable assertions use the --enable-debug with configure.
28  * The VERIFY() functions are never compiled out and cannot be disabled.
29  *
30  * PANIC()	- Panic the node and print message.
31  * ASSERT()	- Assert X is true, if not panic.
32  * ASSERT3B()	- Assert boolean X OP Y is true, if not panic.
33  * ASSERT3S()	- Assert signed X OP Y is true, if not panic.
34  * ASSERT3U()	- Assert unsigned X OP Y is true, if not panic.
35  * ASSERT3P()	- Assert pointer X OP Y is true, if not panic.
36  * ASSERT0()	- Assert value is zero, if not panic.
37  * VERIFY()	- Verify X is true, if not panic.
38  * VERIFY3B()	- Verify boolean X OP Y is true, if not panic.
39  * VERIFY3S()	- Verify signed X OP Y is true, if not panic.
40  * VERIFY3U()	- Verify unsigned X OP Y is true, if not panic.
41  * VERIFY3P()	- Verify pointer X OP Y is true, if not panic.
42  * VERIFY0()	- Verify value is zero, if not panic.
43  */
44 
45 #ifndef _SPL_DEBUG_H
46 #define	_SPL_DEBUG_H
47 
48 /*
49  * Common DEBUG functionality.
50  */
51 #define	__printflike(a, b)	__printf(a, b)
52 
53 #ifndef __maybe_unused
54 #define	__maybe_unused __attribute__((unused))
55 #endif
56 
57 int spl_panic(const char *file, const char *func, int line,
58     const char *fmt, ...);
59 void spl_dumpstack(void);
60 
61 #define	PANIC(fmt, a...)						\
62 	spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
63 
64 #define	VERIFY(cond)							\
65 	(void) (unlikely(!(cond)) &&					\
66 	    spl_panic(__FILE__, __FUNCTION__, __LINE__,			\
67 	    "%s", "VERIFY(" #cond ") failed\n"))
68 
69 #define	VERIFY3B(LEFT, OP, RIGHT)	do {				\
70 		const boolean_t _verify3_left = (boolean_t)(LEFT);	\
71 		const boolean_t _verify3_right = (boolean_t)(RIGHT);	\
72 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
73 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
74 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
75 		    "failed (%d " #OP " %d)\n",				\
76 		    (boolean_t)(_verify3_left),				\
77 		    (boolean_t)(_verify3_right));			\
78 	} while (0)
79 
80 #define	VERIFY3S(LEFT, OP, RIGHT)	do {				\
81 		const int64_t _verify3_left = (int64_t)(LEFT);		\
82 		const int64_t _verify3_right = (int64_t)(RIGHT);	\
83 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
84 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
85 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
86 		    "failed (%lld " #OP " %lld)\n",			\
87 		    (long long)(_verify3_left),				\
88 		    (long long)(_verify3_right));			\
89 	} while (0)
90 
91 #define	VERIFY3U(LEFT, OP, RIGHT)	do {				\
92 		const uint64_t _verify3_left = (uint64_t)(LEFT);	\
93 		const uint64_t _verify3_right = (uint64_t)(RIGHT);	\
94 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
95 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
96 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
97 		    "failed (%llu " #OP " %llu)\n",			\
98 		    (unsigned long long)(_verify3_left),		\
99 		    (unsigned long long)(_verify3_right));		\
100 	} while (0)
101 
102 #define	VERIFY3P(LEFT, OP, RIGHT)	do {				\
103 		const uintptr_t _verify3_left = (uintptr_t)(LEFT);	\
104 		const uintptr_t _verify3_right = (uintptr_t)(RIGHT);	\
105 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
106 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
107 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
108 		    "failed (%px " #OP " %px)\n",			\
109 		    (void *) (_verify3_left),				\
110 		    (void *) (_verify3_right));				\
111 	} while (0)
112 
113 #define	VERIFY0(RIGHT)	do {						\
114 		const int64_t _verify3_left = (int64_t)(0);		\
115 		const int64_t _verify3_right = (int64_t)(RIGHT);	\
116 		if (unlikely(!(_verify3_left == _verify3_right)))	\
117 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
118 		    "VERIFY3(0 == " #RIGHT ") "				\
119 		    "failed (0 == %lld)\n",				\
120 		    (long long) (_verify3_right));			\
121 	} while (0)
122 
123 /*
124  * Debugging disabled (--disable-debug)
125  */
126 #ifdef NDEBUG
127 
128 #define	ASSERT(x)		((void) sizeof ((uintptr_t)(x)))
129 #define	ASSERT3B(x, y, z)						\
130 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
131 #define	ASSERT3S(x, y, z)						\
132 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
133 #define	ASSERT3U(x, y, z)						\
134 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
135 #define	ASSERT3P(x, y, z)						\
136 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
137 #define	ASSERT0(x)		((void) sizeof ((uintptr_t)(x)))
138 #define	IMPLY(A, B)							\
139 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
140 #define	EQUIV(A, B)		\
141 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
142 
143 /*
144  * Debugging enabled (--enable-debug)
145  */
146 #else
147 
148 #define	ASSERT3B	VERIFY3B
149 #define	ASSERT3S	VERIFY3S
150 #define	ASSERT3U	VERIFY3U
151 #define	ASSERT3P	VERIFY3P
152 #define	ASSERT0		VERIFY0
153 #define	ASSERT		VERIFY
154 #define	IMPLY(A, B) \
155 	((void)(likely((!(A)) || (B)) || \
156 	    spl_panic(__FILE__, __FUNCTION__, __LINE__, \
157 	    "(" #A ") implies (" #B ")")))
158 #define	EQUIV(A, B) \
159 	((void)(likely(!!(A) == !!(B)) || \
160 	    spl_panic(__FILE__, __FUNCTION__, __LINE__, \
161 	    "(" #A ") is equivalent to (" #B ")")))
162 
163 #endif /* NDEBUG */
164 
165 #endif /* SPL_DEBUG_H */
166