xref: /freebsd-src/sys/contrib/openzfs/include/os/linux/spl/sys/debug.h (revision b97ee269eae3cbaf35c18f51a459aea581c2a7dc)
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 extern void spl_panic(const char *file, const char *func, int line,
58     const char *fmt, ...) __attribute__((__noreturn__));
59 extern void spl_dumpstack(void);
60 
61 static inline int
62 spl_assert(const char *buf, const char *file, const char *func, int line)
63 {
64 	spl_panic(file, func, line, "%s", buf);
65 	return (0);
66 }
67 
68 #define	PANIC(fmt, a...)						\
69 	spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
70 
71 #define	VERIFY(cond)							\
72 	(void) (unlikely(!(cond)) &&					\
73 	    spl_assert("VERIFY(" #cond ") failed\n",			\
74 	    __FILE__, __FUNCTION__, __LINE__))
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 
130 /*
131  * Debugging disabled (--disable-debug)
132  */
133 #ifdef NDEBUG
134 
135 #define	ASSERT(x)		((void) sizeof ((uintptr_t)(x)))
136 #define	ASSERT3B(x, y, z)						\
137 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
138 #define	ASSERT3S(x, y, z)						\
139 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
140 #define	ASSERT3U(x, y, z)						\
141 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
142 #define	ASSERT3P(x, y, z)						\
143 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
144 #define	ASSERT0(x)		((void) sizeof ((uintptr_t)(x)))
145 #define	IMPLY(A, B)							\
146 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
147 #define	EQUIV(A, B)		\
148 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
149 
150 /*
151  * Debugging enabled (--enable-debug)
152  */
153 #else
154 
155 #define	ASSERT3B	VERIFY3B
156 #define	ASSERT3S	VERIFY3S
157 #define	ASSERT3U	VERIFY3U
158 #define	ASSERT3P	VERIFY3P
159 #define	ASSERT0		VERIFY0
160 #define	ASSERT		VERIFY
161 #define	IMPLY(A, B) \
162 	((void)(likely((!(A)) || (B)) ||				\
163 	    spl_assert("(" #A ") implies (" #B ")",			\
164 	    __FILE__, __FUNCTION__, __LINE__)))
165 #define	EQUIV(A, B) \
166 	((void)(likely(!!(A) == !!(B)) || 				\
167 	    spl_assert("(" #A ") is equivalent to (" #B ")",		\
168 	    __FILE__, __FUNCTION__, __LINE__)))
169 
170 #endif /* NDEBUG */
171 
172 #endif /* SPL_DEBUG_H */
173