xref: /illumos-gate/usr/src/uts/common/sys/debug.h (revision 7f9dc0eec05dc1280e3a494124572c5814e7aef9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
23  *
24  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*
29  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
30  * Copyright 2013 Saso Kiselkov. All rights reserved.
31  */
32 
33 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
34 /*	  All Rights Reserved	*/
35 
36 #ifndef _SYS_DEBUG_H
37 #define	_SYS_DEBUG_H
38 
39 #if !defined(_STANDALONE)
40 #include <sys/isa_defs.h>
41 #endif
42 #include <sys/types.h>
43 #include <sys/note.h>
44 
45 #ifdef	__cplusplus
46 extern "C" {
47 #endif
48 
49 /*
50  * ASSERT(ex) causes a panic or debugger entry if expression ex is not
51  * true.  ASSERT() is included only for debugging, and is a no-op in
52  * production kernels.  VERIFY(ex), on the other hand, behaves like
53  * ASSERT and is evaluated on both debug and non-debug kernels.
54  */
55 
56 extern void assfail(const char *, const char *, int);
57 #define	VERIFY(EX) ((void)((EX) || (assfail(#EX, __FILE__, __LINE__), 0)))
58 #if DEBUG
59 #define	ASSERT(EX) ((void)((EX) || (assfail(#EX, __FILE__, __LINE__), 0)))
60 #else
61 #define	ASSERT(x)  ((void)0)
62 #endif
63 
64 /*
65  * Assertion variants sensitive to the compilation data model
66  */
67 #if defined(_LP64)
68 #define	ASSERT64(x)	ASSERT(x)
69 #define	ASSERT32(x)
70 #else
71 #define	ASSERT64(x)
72 #define	ASSERT32(x)	ASSERT(x)
73 #endif
74 
75 /*
76  * IMPLY and EQUIV are assertions of the form:
77  *
78  *	if (a) then (b)
79  * and
80  *	if (a) then (b) *AND* if (b) then (a)
81  */
82 #if DEBUG
83 #define	IMPLY(A, B) \
84 	((void)(((!(A)) || (B)) || \
85 	    (assfail("(" #A ") implies (" #B ")", __FILE__, __LINE__), 0)))
86 #define	EQUIV(A, B) \
87 	((void)((!!(A) == !!(B)) || \
88 	    (assfail("(" #A ") is equivalent to (" #B ")", \
89 		__FILE__, __LINE__), 0)))
90 #else
91 #define	IMPLY(A, B) ((void)0)
92 #define	EQUIV(A, B) ((void)0)
93 #endif
94 
95 /*
96  * ASSERT3() behaves like ASSERT() except that it is an explicit conditional,
97  * and prints out the values of the left and right hand expressions as part of
98  * the panic message to ease debugging.  The three variants imply the type
99  * of their arguments.  ASSERT3S() is for signed data types, ASSERT3U() is
100  * for unsigned, and ASSERT3P() is for pointers.  The VERIFY3*() macros
101  * have the same relationship as above.
102  */
103 extern void assfail3(const char *, uintmax_t, const char *, uintmax_t,
104     const char *, int);
105 #define	VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) do { \
106 	const TYPE __left = (TYPE)(LEFT); \
107 	const TYPE __right = (TYPE)(RIGHT); \
108 	if (!(__left OP __right)) \
109 		assfail3(#LEFT " " #OP " " #RIGHT, \
110 			(uintmax_t)__left, #OP, (uintmax_t)__right, \
111 			__FILE__, __LINE__); \
112 _NOTE(CONSTCOND) } while (0)
113 
114 #define	VERIFY3B(x, y, z)	VERIFY3_IMPL(x, y, z, boolean_t)
115 #define	VERIFY3S(x, y, z)	VERIFY3_IMPL(x, y, z, int64_t)
116 #define	VERIFY3U(x, y, z)	VERIFY3_IMPL(x, y, z, uint64_t)
117 #define	VERIFY3P(x, y, z)	VERIFY3_IMPL(x, y, z, uintptr_t)
118 #define	VERIFY0(x)		VERIFY3_IMPL(x, ==, 0, uintmax_t)
119 
120 #if DEBUG
121 #define	ASSERT3B(x, y, z)	VERIFY3_IMPL(x, y, z, boolean_t)
122 #define	ASSERT3S(x, y, z)	VERIFY3_IMPL(x, y, z, int64_t)
123 #define	ASSERT3U(x, y, z)	VERIFY3_IMPL(x, y, z, uint64_t)
124 #define	ASSERT3P(x, y, z)	VERIFY3_IMPL(x, y, z, uintptr_t)
125 #define	ASSERT0(x)		VERIFY3_IMPL(x, ==, 0, uintmax_t)
126 #else
127 #define	ASSERT3B(x, y, z)	((void)0)
128 #define	ASSERT3S(x, y, z)	((void)0)
129 #define	ASSERT3U(x, y, z)	((void)0)
130 #define	ASSERT3P(x, y, z)	((void)0)
131 #define	ASSERT0(x)		((void)0)
132 #endif
133 
134 /*
135  * Compile-time assertion. The condition 'x' must be constant.
136  */
137 #define	CTASSERT(x)	_Static_assert(x, "compile-time assertion failed")
138 
139 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
140 
141 extern void abort_sequence_enter(char *);
142 extern void debug_enter(char *);
143 
144 #endif	/* _KERNEL || _FAKE_KERNEL */
145 
146 #if defined(DEBUG) && !defined(__sun)
147 /* CSTYLED */
148 #define	STATIC
149 #else
150 /* CSTYLED */
151 #define	STATIC static
152 #endif
153 
154 #ifdef	__cplusplus
155 }
156 #endif
157 
158 #endif	/* _SYS_DEBUG_H */
159