17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5deb8317Mark J Musante * Common Development and Distribution License (the "License").
6deb8317Mark J Musante * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22ba3594bGarrett D'Amore * Copyright 2014 Garrett D'Amore <garrett@damore.org>
23ba3594bGarrett D'Amore *
2456f3320Jonathan Adams * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
257c478bdstevel@tonic-gate * Use is subject to license terms.
267c478bdstevel@tonic-gate */
277c478bdstevel@tonic-gate
28fb09f5aMadhav Suresh/*
291271e4bPrakash Surya * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
3045818eeMatthew Ahrens * Copyright 2013 Saso Kiselkov. All rights reserved.
31fb09f5aMadhav Suresh */
32fb09f5aMadhav Suresh
337c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
34fa9e406ahrens/*	  All Rights Reserved	*/
357c478bdstevel@tonic-gate
367c478bdstevel@tonic-gate#ifndef _SYS_DEBUG_H
377c478bdstevel@tonic-gate#define	_SYS_DEBUG_H
387c478bdstevel@tonic-gate
394a04e8dToomas Soome#if !defined(_STANDALONE)
407c478bdstevel@tonic-gate#include <sys/isa_defs.h>
414a04e8dToomas Soome#endif
42fa9e406ahrens#include <sys/types.h>
4335a5a35Jonathan Adams#include <sys/note.h>
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gate#ifdef	__cplusplus
467c478bdstevel@tonic-gateextern "C" {
477c478bdstevel@tonic-gate#endif
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate/*
507c478bdstevel@tonic-gate * ASSERT(ex) causes a panic or debugger entry if expression ex is not
517c478bdstevel@tonic-gate * true.  ASSERT() is included only for debugging, and is a no-op in
527c478bdstevel@tonic-gate * production kernels.  VERIFY(ex), on the other hand, behaves like
53fa9e406ahrens * ASSERT and is evaluated on both debug and non-debug kernels.
547c478bdstevel@tonic-gate */
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gateextern int assfail(const char *, const char *, int);
57fa9e406ahrens#define	VERIFY(EX) ((void)((EX) || assfail(#EX, __FILE__, __LINE__)))
587c478bdstevel@tonic-gate#if DEBUG
59deb8317Mark J Musante#define	ASSERT(EX) ((void)((EX) || assfail(#EX, __FILE__, __LINE__)))
607c478bdstevel@tonic-gate#else
617c478bdstevel@tonic-gate#define	ASSERT(x)  ((void)0)
627c478bdstevel@tonic-gate#endif
637c478bdstevel@tonic-gate
647c478bdstevel@tonic-gate/*
657c478bdstevel@tonic-gate * Assertion variants sensitive to the compilation data model
667c478bdstevel@tonic-gate */
677c478bdstevel@tonic-gate#if defined(_LP64)
687c478bdstevel@tonic-gate#define	ASSERT64(x)	ASSERT(x)
697c478bdstevel@tonic-gate#define	ASSERT32(x)
707c478bdstevel@tonic-gate#else
717c478bdstevel@tonic-gate#define	ASSERT64(x)
727c478bdstevel@tonic-gate#define	ASSERT32(x)	ASSERT(x)
737c478bdstevel@tonic-gate#endif
747c478bdstevel@tonic-gate
75fa9e406ahrens/*
7656f3320Jonathan Adams * IMPLY and EQUIV are assertions of the form:
7756f3320Jonathan Adams *
7856f3320Jonathan Adams *	if (a) then (b)
7956f3320Jonathan Adams * and
8056f3320Jonathan Adams *	if (a) then (b) *AND* if (b) then (a)
8156f3320Jonathan Adams */
8256f3320Jonathan Adams#if DEBUG
8356f3320Jonathan Adams#define	IMPLY(A, B) \
8456f3320Jonathan Adams	((void)(((!(A)) || (B)) || \
8556f3320Jonathan Adams	    assfail("(" #A ") implies (" #B ")", __FILE__, __LINE__)))
8656f3320Jonathan Adams#define	EQUIV(A, B) \
8756f3320Jonathan Adams	((void)((!!(A) == !!(B)) || \
8856f3320Jonathan Adams	    assfail("(" #A ") is equivalent to (" #B ")", __FILE__, __LINE__)))
8956f3320Jonathan Adams#else
9056f3320Jonathan Adams#define	IMPLY(A, B) ((void)0)
9156f3320Jonathan Adams#define	EQUIV(A, B) ((void)0)
9256f3320Jonathan Adams#endif
9356f3320Jonathan Adams
9456f3320Jonathan Adams/*
95fa9e406ahrens * ASSERT3() behaves like ASSERT() except that it is an explicit conditional,
96fa9e406ahrens * and prints out the values of the left and right hand expressions as part of
97fa9e406ahrens * the panic message to ease debugging.  The three variants imply the type
98fa9e406ahrens * of their arguments.  ASSERT3S() is for signed data types, ASSERT3U() is
99fa9e406ahrens * for unsigned, and ASSERT3P() is for pointers.  The VERIFY3*() macros
100fa9e406ahrens * have the same relationship as above.
101fa9e406ahrens */
102fa9e406ahrensextern void assfail3(const char *, uintmax_t, const char *, uintmax_t,
103fa9e406ahrens    const char *, int);
104fa9e406ahrens#define	VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) do { \
105fa9e406ahrens	const TYPE __left = (TYPE)(LEFT); \
106fa9e406ahrens	const TYPE __right = (TYPE)(RIGHT); \
107fa9e406ahrens	if (!(__left OP __right)) \
108fa9e406ahrens		assfail3(#LEFT " " #OP " " #RIGHT, \
109fa9e406ahrens			(uintmax_t)__left, #OP, (uintmax_t)__right, \
110fa9e406ahrens			__FILE__, __LINE__); \
111fa9e406ahrens_NOTE(CONSTCOND) } while (0)
112fa9e406ahrens
1131271e4bPrakash Surya#define	VERIFY3B(x, y, z)	VERIFY3_IMPL(x, y, z, boolean_t)
114fa9e406ahrens#define	VERIFY3S(x, y, z)	VERIFY3_IMPL(x, y, z, int64_t)
115fa9e406ahrens#define	VERIFY3U(x, y, z)	VERIFY3_IMPL(x, y, z, uint64_t)
116fa9e406ahrens#define	VERIFY3P(x, y, z)	VERIFY3_IMPL(x, y, z, uintptr_t)
117fb09f5aMadhav Suresh#define	VERIFY0(x)		VERIFY3_IMPL(x, ==, 0, uintmax_t)
118fb09f5aMadhav Suresh
119fa9e406ahrens#if DEBUG
1201271e4bPrakash Surya#define	ASSERT3B(x, y, z)	VERIFY3_IMPL(x, y, z, boolean_t)
121deb8317Mark J Musante#define	ASSERT3S(x, y, z)	VERIFY3_IMPL(x, y, z, int64_t)
122deb8317Mark J Musante#define	ASSERT3U(x, y, z)	VERIFY3_IMPL(x, y, z, uint64_t)
123deb8317Mark J Musante#define	ASSERT3P(x, y, z)	VERIFY3_IMPL(x, y, z, uintptr_t)
124fb09f5aMadhav Suresh#define	ASSERT0(x)		VERIFY3_IMPL(x, ==, 0, uintmax_t)
125fa9e406ahrens#else
1261271e4bPrakash Surya#define	ASSERT3B(x, y, z)	((void)0)
127fa9e406ahrens#define	ASSERT3S(x, y, z)	((void)0)
128fa9e406ahrens#define	ASSERT3U(x, y, z)	((void)0)
129fa9e406ahrens#define	ASSERT3P(x, y, z)	((void)0)
130fb09f5aMadhav Suresh#define	ASSERT0(x)		((void)0)
131fa9e406ahrens#endif
132fa9e406ahrens
13345818eeMatthew Ahrens/*
13445818eeMatthew Ahrens * Compile-time assertion. The condition 'x' must be constant.
13545818eeMatthew Ahrens */
13645818eeMatthew Ahrens#define	CTASSERT(x)		_CTASSERT(x, __LINE__)
13745818eeMatthew Ahrens#define	_CTASSERT(x, y)		__CTASSERT(x, y)
13845818eeMatthew Ahrens#define	__CTASSERT(x, y) \
139f9a9801Toomas Soome	typedef char __compile_time_assertion__ ## y [(x) ? 1 : -1] __unused
14045818eeMatthew Ahrens
1418329232Gordon Ross#if defined(_KERNEL) || defined(_FAKE_KERNEL)
1427c478bdstevel@tonic-gate
1437c478bdstevel@tonic-gateextern void abort_sequence_enter(char *);
1447c478bdstevel@tonic-gateextern void debug_enter(char *);
1457c478bdstevel@tonic-gate
1468329232Gordon Ross#endif	/* _KERNEL || _FAKE_KERNEL */
1477c478bdstevel@tonic-gate
1487c478bdstevel@tonic-gate#if defined(DEBUG) && !defined(__sun)
1497c478bdstevel@tonic-gate/* CSTYLED */
1507c478bdstevel@tonic-gate#define	STATIC
1517c478bdstevel@tonic-gate#else
1527c478bdstevel@tonic-gate/* CSTYLED */
1537c478bdstevel@tonic-gate#define	STATIC static
1547c478bdstevel@tonic-gate#endif
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gate#ifdef	__cplusplus
1577c478bdstevel@tonic-gate}
1587c478bdstevel@tonic-gate#endif
1597c478bdstevel@tonic-gate
1607c478bdstevel@tonic-gate#endif	/* _SYS_DEBUG_H */
161