Print this page
Test of DTrace assert probes


  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  __cplusplus
  44 extern "C" {
  45 #endif
  46 
  47 /*
  48  * ASSERT(ex) causes a panic or debugger entry if expression ex is not
  49  * true.  ASSERT() is included only for debugging, and is a no-op in
  50  * production kernels.  VERIFY(ex), on the other hand, behaves like
  51  * ASSERT and is evaluated on both debug and non-debug kernels.








  52  */
  53 















  54 extern int assfail(const char *, const char *, int);
  55 #define VERIFY(EX) ((void)((EX) || assfail(#EX, __FILE__, __LINE__)))
  56 #if DEBUG
  57 #define ASSERT(EX) ((void)((EX) || assfail(#EX, __FILE__, __LINE__)))
  58 #else



  59 #define ASSERT(x)  ((void)0)
  60 #endif

  61 
  62 /*
  63  * Assertion variants sensitive to the compilation data model
  64  */
  65 #if defined(_LP64)
  66 #define ASSERT64(x)     ASSERT(x)
  67 #define ASSERT32(x)
  68 #else
  69 #define ASSERT64(x)
  70 #define ASSERT32(x)     ASSERT(x)
  71 #endif
  72 
  73 /*
  74  * IMPLY and EQUIV are assertions of the form:
  75  *
  76  *      if (a) then (b)
  77  * and
  78  *      if (a) then (b) *AND* if (b) then (a)
  79  */
  80 #if DEBUG


  82         ((void)(((!(A)) || (B)) || \
  83             assfail("(" #A ") implies (" #B ")", __FILE__, __LINE__)))
  84 #define EQUIV(A, B) \
  85         ((void)((!!(A) == !!(B)) || \
  86             assfail("(" #A ") is equivalent to (" #B ")", __FILE__, __LINE__)))
  87 #else
  88 #define IMPLY(A, B) ((void)0)
  89 #define EQUIV(A, B) ((void)0)
  90 #endif
  91 
  92 /*
  93  * ASSERT3() behaves like ASSERT() except that it is an explicit conditional,
  94  * and prints out the values of the left and right hand expressions as part of
  95  * the panic message to ease debugging.  The three variants imply the type
  96  * of their arguments.  ASSERT3S() is for signed data types, ASSERT3U() is
  97  * for unsigned, and ASSERT3P() is for pointers.  The VERIFY3*() macros
  98  * have the same relationship as above.
  99  */
 100 extern void assfail3(const char *, uintmax_t, const char *, uintmax_t,
 101     const char *, int);





























 102 #define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE) do { \
 103         const TYPE __left = (TYPE)(LEFT); \
 104         const TYPE __right = (TYPE)(RIGHT); \
 105         if (!(__left OP __right)) \
 106                 assfail3(#LEFT " " #OP " " #RIGHT, \
 107                         (uintmax_t)__left, #OP, (uintmax_t)__right, \
 108                         __FILE__, __LINE__); \
 109 _NOTE(CONSTCOND) } while (0)
 110 
 111 #define VERIFY3S(x, y, z)       VERIFY3_IMPL(x, y, z, int64_t)
 112 #define VERIFY3U(x, y, z)       VERIFY3_IMPL(x, y, z, uint64_t)
 113 #define VERIFY3P(x, y, z)       VERIFY3_IMPL(x, y, z, uintptr_t)
 114 #define VERIFY0(x)              VERIFY3_IMPL(x, ==, 0, uintmax_t)
 115 
 116 #if DEBUG
 117 #define ASSERT3S(x, y, z)       VERIFY3_IMPL(x, y, z, int64_t)
 118 #define ASSERT3U(x, y, z)       VERIFY3_IMPL(x, y, z, uint64_t)
 119 #define ASSERT3P(x, y, z)       VERIFY3_IMPL(x, y, z, uintptr_t)
 120 #define ASSERT0(x)              VERIFY3_IMPL(x, ==, 0, uintmax_t)
 121 #else






 122 #define ASSERT3S(x, y, z)       ((void)0)
 123 #define ASSERT3U(x, y, z)       ((void)0)
 124 #define ASSERT3P(x, y, z)       ((void)0)
 125 #define ASSERT0(x)              ((void)0)

 126 #endif
 127 
 128 /*
 129  * Compile-time assertion. The condition 'x' must be constant.
 130  */
 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 
 136 #ifdef  _KERNEL
 137 
 138 extern void abort_sequence_enter(char *);
 139 extern void debug_enter(char *);
 140 
 141 #endif  /* _KERNEL */
 142 
 143 #if defined(DEBUG) && !defined(__sun)
 144 /* CSTYLED */
 145 #define STATIC


  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


 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