debug.h revision 297542
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 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#include <sys/types.h>
40#include <sys/note.h>
41
42#ifdef	__cplusplus
43extern "C" {
44#endif
45
46/*
47 * ASSERT(ex) causes a panic or debugger entry if expression ex is not
48 * true.  ASSERT() is included only for debugging, and is a no-op in
49 * production kernels.  VERIFY(ex), on the other hand, behaves like
50 * ASSERT and is evaluated on both debug and non-debug kernels.
51 */
52
53extern int assfail(const char *, const char *, int);
54#define	VERIFY(EX) ((void)((EX) || assfail(#EX, __FILE__, __LINE__)))
55#ifdef DEBUG
56#define	ASSERT(EX) ((void)((EX) || assfail(#EX, __FILE__, __LINE__)))
57#else
58#define	ASSERT(x)  ((void)0)
59#endif
60
61/*
62 * Assertion variants sensitive to the compilation data model
63 */
64#if defined(_LP64)
65#define	ASSERT64(x)	ASSERT(x)
66#define	ASSERT32(x)
67#else
68#define	ASSERT64(x)
69#define	ASSERT32(x)	ASSERT(x)
70#endif
71
72/*
73 * IMPLY and EQUIV are assertions of the form:
74 *
75 *	if (a) then (b)
76 * and
77 *	if (a) then (b) *AND* if (b) then (a)
78 */
79#ifdef DEBUG
80#define	IMPLY(A, B) \
81	((void)(((!(A)) || (B)) || \
82	    assfail("(" #A ") implies (" #B ")", __FILE__, __LINE__)))
83#define	EQUIV(A, B) \
84	((void)((!!(A) == !!(B)) || \
85	    assfail("(" #A ") is equivalent to (" #B ")", __FILE__, __LINE__)))
86#else
87#define	IMPLY(A, B) ((void)0)
88#define	EQUIV(A, B) ((void)0)
89#endif
90
91/*
92 * ASSERT3() behaves like ASSERT() except that it is an explicit conditional,
93 * and prints out the values of the left and right hand expressions as part of
94 * the panic message to ease debugging.  The three variants imply the type
95 * of their arguments.  ASSERT3S() is for signed data types, ASSERT3U() is
96 * for unsigned, and ASSERT3P() is for pointers.  The VERIFY3*() macros
97 * have the same relationship as above.
98 */
99extern void assfail3(const char *, uintmax_t, const char *, uintmax_t,
100    const char *, int);
101#define	VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) do { \
102	const TYPE __left = (TYPE)(LEFT); \
103	const TYPE __right = (TYPE)(RIGHT); \
104	if (!(__left OP __right)) \
105		assfail3(#LEFT " " #OP " " #RIGHT, \
106			(uintmax_t)__left, #OP, (uintmax_t)__right, \
107			__FILE__, __LINE__); \
108_NOTE(CONSTCOND) } while (0)
109
110#define	VERIFY3S(x, y, z)	VERIFY3_IMPL(x, y, z, int64_t)
111#define	VERIFY3U(x, y, z)	VERIFY3_IMPL(x, y, z, uint64_t)
112#define	VERIFY3P(x, y, z)	VERIFY3_IMPL(x, y, z, uintptr_t)
113#define	VERIFY0(x)		VERIFY3_IMPL(x, ==, 0, uintmax_t)
114
115#ifdef DEBUG
116#define	ASSERT3S(x, y, z)	VERIFY3_IMPL(x, y, z, int64_t)
117#define	ASSERT3U(x, y, z)	VERIFY3_IMPL(x, y, z, uint64_t)
118#define	ASSERT3P(x, y, z)	VERIFY3_IMPL(x, y, z, uintptr_t)
119#define	ASSERT0(x)		VERIFY3_IMPL(x, ==, 0, uintmax_t)
120#else
121#define	ASSERT3S(x, y, z)	((void)0)
122#define	ASSERT3U(x, y, z)	((void)0)
123#define	ASSERT3P(x, y, z)	((void)0)
124#define	ASSERT0(x)		((void)0)
125#endif
126
127/*
128 * Compile-time assertion. The condition 'x' must be constant.
129 */
130#ifndef CTASSERT
131#define	CTASSERT(x)		_CTASSERT(x, __LINE__)
132#define	_CTASSERT(x, y)		__CTASSERT(x, y)
133#define	__CTASSERT(x, y) \
134	typedef char __compile_time_assertion__ ## y [(x) ? 1 : -1]
135#endif
136
137#ifdef	_KERNEL
138
139extern void abort_sequence_enter(char *);
140extern void debug_enter(char *);
141
142#endif	/* _KERNEL */
143
144#if defined(DEBUG) && !defined(__sun)
145/* CSTYLED */
146#define	STATIC
147#else
148/* CSTYLED */
149#define	STATIC static
150#endif
151
152#ifdef	__cplusplus
153}
154#endif
155
156#endif	/* _SYS_DEBUG_H */
157