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