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/isa_defs.h>
  40 #include <sys/types.h>
  41 #include <sys/note.h>
  42 
  43 #ifdef KEBE
  44 #include <sys/sdt.h>
  45 #endif  /* KEBE */
  46 
  47 #ifdef  __cplusplus
  48 extern "C" {
  49 #endif
  50 
  51 /*
  52  * ASSERT(ex) causes a panic or debugger entry if expression ex is not
  53  * true.  ASSERT() is included only for debugging, and is a no-op in
  54  * production kernels.  VERIFY(ex), on the other hand, behaves like
  55  * ASSERT and is evaluated on both debug and non-debug kernels.
  56  *
  57  * KEBE SAYS if "KEBE" is defined on a non-DEBUG kernel, the ASSERT() will
  58  * form an SDT probe of the form sdt:module:function:assert-<lineno>, with
  59  * probe arguments:
  60  *
  61  * arg0 -> file name
  62  * arg1 -> line number
  63  * arg2 -> EX
  64  */
  65 
  66 #ifdef KEBE
  67 /* XXX KEBE HOPES that __LINE__ evaluates the same, but isn't sure. */
  68 #define ASSERTDTSTRBASE(x, y)   x ## y
  69 #define ASSERTDTSTR(x, y)       ASSERTDTSTRBASE(x, y)
  70 #define ASSERTDT(EX)    do { \
  71         extern void ASSERTDTSTR(__dtrace_probe_assert__, __LINE__)(uintptr_t, \
  72             uintptr_t, uintptr_t);                                      \
  73         if (!(EX))                                                      \
  74                 ASSERTDTSTR(__dtrace_probe_assert__, __LINE__)          \
  75                     ((uintptr_t)__FILE__,                               \
  76                     (uintptr_t)__LINE__, (uintptr_t)#EX);               \
  77 _NOTE(CONSTCOND) } while (0)
  78 #endif  /* KEBE */
  79 
  80 
  81 extern int assfail(const char *, const char *, int);
  82 #define VERIFY(EX) ((void)((EX) || assfail(#EX, __FILE__, __LINE__)))
  83 #if DEBUG
  84 #define ASSERT(EX) ((void)((EX) || assfail(#EX, __FILE__, __LINE__)))
  85 #else
  86 #ifdef KEBE
  87 #define ASSERT(EX)      ASSERTDT(EX)
  88 #else
  89 #define ASSERT(x)  ((void)0)
  90 #endif /* KEBE */
  91 #endif /* DEBUG */
  92 
  93 /*
  94  * Assertion variants sensitive to the compilation data model
  95  */
  96 #if defined(_LP64)
  97 #define ASSERT64(x)     ASSERT(x)
  98 #define ASSERT32(x)
  99 #else
 100 #define ASSERT64(x)
 101 #define ASSERT32(x)     ASSERT(x)
 102 #endif
 103 
 104 /*
 105  * IMPLY and EQUIV are assertions of the form:
 106  *
 107  *      if (a) then (b)
 108  * and
 109  *      if (a) then (b) *AND* if (b) then (a)
 110  */
 111 #if DEBUG
 112 #define IMPLY(A, B) \
 113         ((void)(((!(A)) || (B)) || \
 114             assfail("(" #A ") implies (" #B ")", __FILE__, __LINE__)))
 115 #define EQUIV(A, B) \
 116         ((void)((!!(A) == !!(B)) || \
 117             assfail("(" #A ") is equivalent to (" #B ")", __FILE__, __LINE__)))
 118 #else
 119 #define IMPLY(A, B) ((void)0)
 120 #define EQUIV(A, B) ((void)0)
 121 #endif
 122 
 123 /*
 124  * ASSERT3() behaves like ASSERT() except that it is an explicit conditional,
 125  * and prints out the values of the left and right hand expressions as part of
 126  * the panic message to ease debugging.  The three variants imply the type
 127  * of their arguments.  ASSERT3S() is for signed data types, ASSERT3U() is
 128  * for unsigned, and ASSERT3P() is for pointers.  The VERIFY3*() macros
 129  * have the same relationship as above.
 130  */
 131 extern void assfail3(const char *, uintmax_t, const char *, uintmax_t,
 132     const char *, int);
 133 
 134 #ifdef KEBE
 135 /*
 136  * KEBE SAYS, for the sdt:module:function:assert3-<lineno> will have more
 137  * probe arguments:
 138  *
 139  * arg0 -> file name
 140  * arg1 -> line number
 141  * arg2 -> EX
 142  * arg3 -> "left" boolean
 143  * arg4 -> "OP" string
 144  * arg5 -> "right" boolean
 145  */
 146 #define KEBE3_IMPL(LEFT, OP, RIGHT, TYPE)       do { \
 147         extern void ASSERTDTSTR(__dtrace_probe_assert3__, __LINE__)(   \
 148             uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t,      \
 149             uintptr_t);                                                 \
 150         const TYPE __left = (TYPE)(LEFT);                               \
 151         const TYPE __right = (TYPE)(RIGHT);                             \
 152         if (!(__left OP __right))                                       \
 153                 ASSERTDTSTR(__dtrace_probe_assert3__, __LINE__)         \
 154                     ((uintptr_t)__FILE__,                               \
 155                     (uintptr_t)__LINE__,                                \
 156                     (uintptr_t)(#LEFT " " #OP " " #RIGHT),              \
 157                     (uintptr_t)__left, (uintptr_t)#OP,                  \
 158                     (uintptr_t)__right);                                \
 159 _NOTE(CONSTCOND) } while (0)
 160 #endif  /* KEBE */
 161 
 162 #define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) do { \
 163         const TYPE __left = (TYPE)(LEFT); \
 164         const TYPE __right = (TYPE)(RIGHT); \
 165         if (!(__left OP __right)) \
 166                 assfail3(#LEFT " " #OP " " #RIGHT, \
 167                         (uintmax_t)__left, #OP, (uintmax_t)__right, \
 168                         __FILE__, __LINE__); \
 169 _NOTE(CONSTCOND) } while (0)
 170 
 171 #define VERIFY3S(x, y, z)       VERIFY3_IMPL(x, y, z, int64_t)
 172 #define VERIFY3U(x, y, z)       VERIFY3_IMPL(x, y, z, uint64_t)
 173 #define VERIFY3P(x, y, z)       VERIFY3_IMPL(x, y, z, uintptr_t)
 174 #define VERIFY0(x)              VERIFY3_IMPL(x, ==, 0, uintmax_t)
 175 
 176 #if DEBUG
 177 #define ASSERT3S(x, y, z)       VERIFY3_IMPL(x, y, z, int64_t)
 178 #define ASSERT3U(x, y, z)       VERIFY3_IMPL(x, y, z, uint64_t)
 179 #define ASSERT3P(x, y, z)       VERIFY3_IMPL(x, y, z, uintptr_t)
 180 #define ASSERT0(x)              VERIFY3_IMPL(x, ==, 0, uintmax_t)
 181 #else
 182 #ifdef KEBE
 183 #define ASSERT3S(x, y, z)       KEBE3_IMPL(x, y, z, int64_t)
 184 #define ASSERT3U(x, y, z)       KEBE3_IMPL(x, y, z, uint64_t)
 185 #define ASSERT3P(x, y, z)       KEBE3_IMPL(x, y, z, uintptr_t)
 186 #define ASSERT0(x)              KEBE3_IMPL(x, ==, 0, uintmax_t)
 187 #else
 188 #define ASSERT3S(x, y, z)       ((void)0)
 189 #define ASSERT3U(x, y, z)       ((void)0)
 190 #define ASSERT3P(x, y, z)       ((void)0)
 191 #define ASSERT0(x)              ((void)0)
 192 #endif /* KEBE */
 193 #endif
 194 
 195 /*
 196  * Compile-time assertion. The condition 'x' must be constant.
 197  */
 198 #define CTASSERT(x)             _CTASSERT(x, __LINE__)
 199 #define _CTASSERT(x, y)         __CTASSERT(x, y)
 200 #define __CTASSERT(x, y) \
 201         typedef char __compile_time_assertion__ ## y [(x) ? 1 : -1]
 202 
 203 #ifdef  _KERNEL
 204 
 205 extern void abort_sequence_enter(char *);
 206 extern void debug_enter(char *);
 207 
 208 #endif  /* _KERNEL */
 209 
 210 #if defined(DEBUG) && !defined(__sun)
 211 /* CSTYLED */
 212 #define STATIC
 213 #else
 214 /* CSTYLED */
 215 #define STATIC static
 216 #endif
 217 
 218 #ifdef  __cplusplus
 219 }
 220 #endif
 221 
 222 #endif  /* _SYS_DEBUG_H */