xref: /titanic_50/usr/src/uts/common/sys/debug.h (revision c9e5c7a710e778d0f1fab8f6731c3974706a0270)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5deb8317bSMark J Musante  * Common Development and Distribution License (the "License").
6deb8317bSMark J Musante  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22ba3594baSGarrett D'Amore  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
23ba3594baSGarrett D'Amore  *
2456f33205SJonathan Adams  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
28fb09f5aaSMadhav Suresh /*
29fb09f5aaSMadhav Suresh  * Copyright (c) 2012 by Delphix. All rights reserved.
3045818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov. All rights reserved.
31*c9e5c7a7SSaso Kiselkov  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
32fb09f5aaSMadhav Suresh  */
33fb09f5aaSMadhav Suresh 
347c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
357c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifndef _SYS_DEBUG_H
387c478bd9Sstevel@tonic-gate #define	_SYS_DEBUG_H
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
41fa9e4066Sahrens #include <sys/types.h>
4235a5a358SJonathan Adams #include <sys/note.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
457c478bd9Sstevel@tonic-gate extern "C" {
467c478bd9Sstevel@tonic-gate #endif
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * ASSERT(ex) causes a panic or debugger entry if expression ex is not
507c478bd9Sstevel@tonic-gate  * true.  ASSERT() is included only for debugging, and is a no-op in
517c478bd9Sstevel@tonic-gate  * production kernels.  VERIFY(ex), on the other hand, behaves like
52fa9e4066Sahrens  * ASSERT and is evaluated on both debug and non-debug kernels.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate extern int assfail(const char *, const char *, int);
56fa9e4066Sahrens #define	VERIFY(EX) ((void)((EX) || assfail(#EX, __FILE__, __LINE__)))
577c478bd9Sstevel@tonic-gate #if DEBUG
58deb8317bSMark J Musante #define	ASSERT(EX) ((void)((EX) || assfail(#EX, __FILE__, __LINE__)))
597c478bd9Sstevel@tonic-gate #else
607c478bd9Sstevel@tonic-gate #define	ASSERT(x)  ((void)0)
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * Assertion variants sensitive to the compilation data model
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate #if defined(_LP64)
677c478bd9Sstevel@tonic-gate #define	ASSERT64(x)	ASSERT(x)
687c478bd9Sstevel@tonic-gate #define	ASSERT32(x)
697c478bd9Sstevel@tonic-gate #else
707c478bd9Sstevel@tonic-gate #define	ASSERT64(x)
717c478bd9Sstevel@tonic-gate #define	ASSERT32(x)	ASSERT(x)
727c478bd9Sstevel@tonic-gate #endif
737c478bd9Sstevel@tonic-gate 
74fa9e4066Sahrens /*
7556f33205SJonathan Adams  * IMPLY and EQUIV are assertions of the form:
7656f33205SJonathan Adams  *
7756f33205SJonathan Adams  *	if (a) then (b)
7856f33205SJonathan Adams  * and
7956f33205SJonathan Adams  *	if (a) then (b) *AND* if (b) then (a)
8056f33205SJonathan Adams  */
8156f33205SJonathan Adams #if DEBUG
8256f33205SJonathan Adams #define	IMPLY(A, B) \
8356f33205SJonathan Adams 	((void)(((!(A)) || (B)) || \
8456f33205SJonathan Adams 	    assfail("(" #A ") implies (" #B ")", __FILE__, __LINE__)))
8556f33205SJonathan Adams #define	EQUIV(A, B) \
8656f33205SJonathan Adams 	((void)((!!(A) == !!(B)) || \
8756f33205SJonathan Adams 	    assfail("(" #A ") is equivalent to (" #B ")", __FILE__, __LINE__)))
8856f33205SJonathan Adams #else
8956f33205SJonathan Adams #define	IMPLY(A, B) ((void)0)
9056f33205SJonathan Adams #define	EQUIV(A, B) ((void)0)
9156f33205SJonathan Adams #endif
9256f33205SJonathan Adams 
9356f33205SJonathan Adams /*
94fa9e4066Sahrens  * ASSERT3() behaves like ASSERT() except that it is an explicit conditional,
95fa9e4066Sahrens  * and prints out the values of the left and right hand expressions as part of
96fa9e4066Sahrens  * the panic message to ease debugging.  The three variants imply the type
97fa9e4066Sahrens  * of their arguments.  ASSERT3S() is for signed data types, ASSERT3U() is
98fa9e4066Sahrens  * for unsigned, and ASSERT3P() is for pointers.  The VERIFY3*() macros
99fa9e4066Sahrens  * have the same relationship as above.
100fa9e4066Sahrens  */
101fa9e4066Sahrens extern void assfail3(const char *, uintmax_t, const char *, uintmax_t,
102fa9e4066Sahrens     const char *, int);
103fa9e4066Sahrens #define	VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) do { \
104fa9e4066Sahrens 	const TYPE __left = (TYPE)(LEFT); \
105fa9e4066Sahrens 	const TYPE __right = (TYPE)(RIGHT); \
106fa9e4066Sahrens 	if (!(__left OP __right)) \
107fa9e4066Sahrens 		assfail3(#LEFT " " #OP " " #RIGHT, \
108fa9e4066Sahrens 			(uintmax_t)__left, #OP, (uintmax_t)__right, \
109fa9e4066Sahrens 			__FILE__, __LINE__); \
110fa9e4066Sahrens _NOTE(CONSTCOND) } while (0)
111fa9e4066Sahrens 
112fa9e4066Sahrens #define	VERIFY3S(x, y, z)	VERIFY3_IMPL(x, y, z, int64_t)
113fa9e4066Sahrens #define	VERIFY3U(x, y, z)	VERIFY3_IMPL(x, y, z, uint64_t)
114fa9e4066Sahrens #define	VERIFY3P(x, y, z)	VERIFY3_IMPL(x, y, z, uintptr_t)
115fb09f5aaSMadhav Suresh #define	VERIFY0(x)		VERIFY3_IMPL(x, ==, 0, uintmax_t)
116fb09f5aaSMadhav Suresh 
117fa9e4066Sahrens #if DEBUG
118deb8317bSMark J Musante #define	ASSERT3S(x, y, z)	VERIFY3_IMPL(x, y, z, int64_t)
119deb8317bSMark J Musante #define	ASSERT3U(x, y, z)	VERIFY3_IMPL(x, y, z, uint64_t)
120deb8317bSMark J Musante #define	ASSERT3P(x, y, z)	VERIFY3_IMPL(x, y, z, uintptr_t)
121fb09f5aaSMadhav Suresh #define	ASSERT0(x)		VERIFY3_IMPL(x, ==, 0, uintmax_t)
122fa9e4066Sahrens #else
123fa9e4066Sahrens #define	ASSERT3S(x, y, z)	((void)0)
124fa9e4066Sahrens #define	ASSERT3U(x, y, z)	((void)0)
125fa9e4066Sahrens #define	ASSERT3P(x, y, z)	((void)0)
126fb09f5aaSMadhav Suresh #define	ASSERT0(x)		((void)0)
127fa9e4066Sahrens #endif
128fa9e4066Sahrens 
12945818ee1SMatthew Ahrens /*
13045818ee1SMatthew Ahrens  * Compile-time assertion. The condition 'x' must be constant.
13145818ee1SMatthew Ahrens  */
13245818ee1SMatthew Ahrens #define	CTASSERT(x)		_CTASSERT(x, __LINE__)
13345818ee1SMatthew Ahrens #define	_CTASSERT(x, y)		__CTASSERT(x, y)
13445818ee1SMatthew Ahrens #define	__CTASSERT(x, y) \
13545818ee1SMatthew Ahrens 	typedef char __compile_time_assertion__ ## y [(x) ? 1 : -1]
13645818ee1SMatthew Ahrens 
1377c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate extern void abort_sequence_enter(char *);
1407c478bd9Sstevel@tonic-gate extern void debug_enter(char *);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #if defined(DEBUG) && !defined(__sun)
1457c478bd9Sstevel@tonic-gate /* CSTYLED */
1467c478bd9Sstevel@tonic-gate #define	STATIC
1477c478bd9Sstevel@tonic-gate #else
1487c478bd9Sstevel@tonic-gate /* CSTYLED */
1497c478bd9Sstevel@tonic-gate #define	STATIC static
1507c478bd9Sstevel@tonic-gate #endif
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate #endif
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate #endif	/* _SYS_DEBUG_H */
157