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 /* BEGIN CSTYLED */
62 #define	PANIC(fmt, a...)						\
63 	spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
64 
65 #define	VERIFY(cond)							\
66 	(void) (unlikely(!(cond)) &&					\
67 	    spl_panic(__FILE__, __FUNCTION__, __LINE__,			\
68 	    "%s", "VERIFY(" #cond ") failed\n"))
69 
70 #define	VERIFY3B(LEFT, OP, RIGHT)	do {				\
71 		boolean_t _verify3_left = (boolean_t)(LEFT);		\
72 		boolean_t _verify3_right = (boolean_t)(RIGHT);		\
73 		if (!(_verify3_left OP _verify3_right))			\
74 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
75 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
76 		    "failed (%d " #OP " %d)\n",				\
77 		    (boolean_t) (_verify3_left),			\
78 		    (boolean_t) (_verify3_right));			\
79 	} while (0)
80 
81 #define	VERIFY3S(LEFT, OP, RIGHT)	do {				\
82 		int64_t _verify3_left = (int64_t)(LEFT);		\
83 		int64_t _verify3_right = (int64_t)(RIGHT);		\
84 		if (!(_verify3_left OP _verify3_right))			\
85 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
86 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
87 		    "failed (%lld " #OP " %lld)\n",			\
88 		    (long long) (_verify3_left),			\
89 		    (long long) (_verify3_right));			\
90 	} while (0)
91 
92 #define	VERIFY3U(LEFT, OP, RIGHT)	do {				\
93 		uint64_t _verify3_left = (uint64_t)(LEFT);		\
94 		uint64_t _verify3_right = (uint64_t)(RIGHT);		\
95 		if (!(_verify3_left OP _verify3_right))			\
96 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
97 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
98 		    "failed (%llu " #OP " %llu)\n",			\
99 		    (unsigned long long) (_verify3_left),		\
100 		    (unsigned long long) (_verify3_right));		\
101 	} while (0)
102 
103 #define	VERIFY3P(LEFT, OP, RIGHT)	do {				\
104 		uintptr_t _verify3_left = (uintptr_t)(LEFT);		\
105 		uintptr_t _verify3_right = (uintptr_t)(RIGHT);		\
106 		if (!(_verify3_left OP _verify3_right))			\
107 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
108 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
109 		    "failed (%px " #OP " %px)\n",			\
110 		    (void *) (_verify3_left),				\
111 		    (void *) (_verify3_right));				\
112 	} while (0)
113 
114 #define	VERIFY0(RIGHT)	do {				\
115 		int64_t _verify3_left = (int64_t)(0);		\
116 		int64_t _verify3_right = (int64_t)(RIGHT);		\
117 		if (!(_verify3_left == _verify3_right))			\
118 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
119 		    "VERIFY3(0 == " #RIGHT ") "				\
120 		    "failed (0 == %lld)\n",				\
121 		    (long long) (_verify3_right));			\
122 	} while (0)
123 
124 #define	CTASSERT_GLOBAL(x)		_CTASSERT(x, __LINE__)
125 #define	CTASSERT(x)			{ _CTASSERT(x, __LINE__); }
126 #define	_CTASSERT(x, y)			__CTASSERT(x, y)
127 #define	__CTASSERT(x, y)						\
128 	typedef char __attribute__ ((unused))				\
129 	__compile_time_assertion__ ## y[(x) ? 1 : -1]
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)(((!(A)) || (B)) || \
158 	    spl_panic(__FILE__, __FUNCTION__, __LINE__, \
159 	    "(" #A ") implies (" #B ")")))
160 #define	EQUIV(A, B) \
161 	((void)((!!(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