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 /*
58  * Without this, we see warnings from objtool during normal Linux builds when
59  * the kernel is built with CONFIG_STACK_VALIDATION=y:
60  *
61  * warning: objtool: tsd_create() falls through to next function __list_add()
62  * warning: objtool: .text: unexpected end of section
63  *
64  * Until the toolchain stops doing this, we must only define this attribute on
65  * spl_panic() when doing static analysis.
66  */
67 #if defined(__COVERITY__) || defined(__clang_analyzer__)
68 __attribute__((__noreturn__))
69 #endif
70 extern void spl_panic(const char *file, const char *func, int line,
71     const char *fmt, ...);
72 extern void spl_dumpstack(void);
73 
74 static inline int
75 spl_assert(const char *buf, const char *file, const char *func, int line)
76 {
77 	spl_panic(file, func, line, "%s", buf);
78 	return (0);
79 }
80 
81 #define	PANIC(fmt, a...)						\
82 	spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
83 
84 #define	VERIFY(cond)							\
85 	(void) (unlikely(!(cond)) &&					\
86 	    spl_assert("VERIFY(" #cond ") failed\n",			\
87 	    __FILE__, __FUNCTION__, __LINE__))
88 
89 #define	VERIFY3B(LEFT, OP, RIGHT)	do {				\
90 		const boolean_t _verify3_left = (boolean_t)(LEFT);	\
91 		const boolean_t _verify3_right = (boolean_t)(RIGHT);	\
92 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
93 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
94 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
95 		    "failed (%d " #OP " %d)\n",				\
96 		    (boolean_t)(_verify3_left),				\
97 		    (boolean_t)(_verify3_right));			\
98 	} while (0)
99 
100 #define	VERIFY3S(LEFT, OP, RIGHT)	do {				\
101 		const int64_t _verify3_left = (int64_t)(LEFT);		\
102 		const int64_t _verify3_right = (int64_t)(RIGHT);	\
103 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
104 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
105 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
106 		    "failed (%lld " #OP " %lld)\n",			\
107 		    (long long)(_verify3_left),				\
108 		    (long long)(_verify3_right));			\
109 	} while (0)
110 
111 #define	VERIFY3U(LEFT, OP, RIGHT)	do {				\
112 		const uint64_t _verify3_left = (uint64_t)(LEFT);	\
113 		const uint64_t _verify3_right = (uint64_t)(RIGHT);	\
114 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
115 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
116 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
117 		    "failed (%llu " #OP " %llu)\n",			\
118 		    (unsigned long long)(_verify3_left),		\
119 		    (unsigned long long)(_verify3_right));		\
120 	} while (0)
121 
122 #define	VERIFY3P(LEFT, OP, RIGHT)	do {				\
123 		const uintptr_t _verify3_left = (uintptr_t)(LEFT);	\
124 		const uintptr_t _verify3_right = (uintptr_t)(RIGHT);	\
125 		if (unlikely(!(_verify3_left OP _verify3_right)))	\
126 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
127 		    "VERIFY3(" #LEFT " "  #OP " "  #RIGHT ") "		\
128 		    "failed (%px " #OP " %px)\n",			\
129 		    (void *) (_verify3_left),				\
130 		    (void *) (_verify3_right));				\
131 	} while (0)
132 
133 #define	VERIFY0(RIGHT)	do {						\
134 		const int64_t _verify3_left = (int64_t)(0);		\
135 		const int64_t _verify3_right = (int64_t)(RIGHT);	\
136 		if (unlikely(!(_verify3_left == _verify3_right)))	\
137 		    spl_panic(__FILE__, __FUNCTION__, __LINE__,		\
138 		    "VERIFY0(0 == " #RIGHT ") "				\
139 		    "failed (0 == %lld)\n",				\
140 		    (long long) (_verify3_right));			\
141 	} while (0)
142 
143 #define	VERIFY_IMPLY(A, B) \
144 	((void)(likely((!(A)) || (B)) ||				\
145 	    spl_assert("(" #A ") implies (" #B ")",			\
146 	    __FILE__, __FUNCTION__, __LINE__)))
147 
148 #define	VERIFY_EQUIV(A, B) \
149 	((void)(likely(!!(A) == !!(B)) || 				\
150 	    spl_assert("(" #A ") is equivalent to (" #B ")",		\
151 	    __FILE__, __FUNCTION__, __LINE__)))
152 
153 /*
154  * Debugging disabled (--disable-debug)
155  */
156 #ifdef NDEBUG
157 
158 #define	ASSERT(x)		((void) sizeof ((uintptr_t)(x)))
159 #define	ASSERT3B(x, y, z)						\
160 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
161 #define	ASSERT3S(x, y, z)						\
162 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
163 #define	ASSERT3U(x, y, z)						\
164 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
165 #define	ASSERT3P(x, y, z)						\
166 	((void) sizeof ((uintptr_t)(x)), (void) sizeof ((uintptr_t)(z)))
167 #define	ASSERT0(x)		((void) sizeof ((uintptr_t)(x)))
168 #define	IMPLY(A, B)							\
169 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
170 #define	EQUIV(A, B)		\
171 	((void) sizeof ((uintptr_t)(A)), (void) sizeof ((uintptr_t)(B)))
172 
173 /*
174  * Debugging enabled (--enable-debug)
175  */
176 #else
177 
178 #define	ASSERT3B	VERIFY3B
179 #define	ASSERT3S	VERIFY3S
180 #define	ASSERT3U	VERIFY3U
181 #define	ASSERT3P	VERIFY3P
182 #define	ASSERT0		VERIFY0
183 #define	ASSERT		VERIFY
184 #define	IMPLY		VERIFY_IMPLY
185 #define	EQUIV		VERIFY_EQUIV
186 
187 #endif /* NDEBUG */
188 
189 #endif /* SPL_DEBUG_H */
190