]> git.proxmox.com Git - mirror_spl-debian.git/blob - include/sys/debug.h
New upstream version 0.7.2
[mirror_spl-debian.git] / include / sys / debug.h
1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 \*****************************************************************************/
24
25 /*
26 * Available Solaris debug functions. All of the ASSERT() macros will be
27 * compiled out when NDEBUG is defined, this is the default behavior for
28 * the SPL. To enable assertions use the --enable-debug with configure.
29 * The VERIFY() functions are never compiled out and cannot be disabled.
30 *
31 * PANIC() - Panic the node and print message.
32 * ASSERT() - Assert X is true, if not panic.
33 * ASSERTV() - Wraps a variable declaration which is only used by ASSERT().
34 * ASSERT3B() - Assert boolean X OP Y is true, if not panic.
35 * ASSERT3S() - Assert signed X OP Y is true, if not panic.
36 * ASSERT3U() - Assert unsigned X OP Y is true, if not panic.
37 * ASSERT3P() - Assert pointer X OP Y is true, if not panic.
38 * ASSERT0() - Assert value is zero, if not panic.
39 * VERIFY() - Verify X is true, if not panic.
40 * VERIFY3B() - Verify boolean X OP Y is true, if not panic.
41 * VERIFY3S() - Verify signed X OP Y is true, if not panic.
42 * VERIFY3U() - Verify unsigned X OP Y is true, if not panic.
43 * VERIFY3P() - Verify pointer X OP Y is true, if not panic.
44 * VERIFY0() - Verify value is zero, if not panic.
45 */
46
47 #ifndef _SPL_DEBUG_H
48 #define _SPL_DEBUG_H
49
50 /*
51 * Common DEBUG functionality.
52 */
53 int spl_panic(const char *file, const char *func, int line,
54 const char *fmt, ...);
55 void spl_dumpstack(void);
56
57 #define PANIC(fmt, a...) \
58 spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
59
60 #define VERIFY(cond) \
61 (void)(unlikely(!(cond)) && \
62 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
63 "%s", "VERIFY(" #cond ") failed\n"))
64
65 #define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE, FMT, CAST) \
66 (void)((!((TYPE)(LEFT) OP (TYPE)(RIGHT))) && \
67 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
68 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
69 "failed (" FMT " " #OP " " FMT ")\n", \
70 CAST (LEFT), CAST (RIGHT)))
71
72 #define VERIFY3B(x,y,z) VERIFY3_IMPL(x, y, z, boolean_t, "%d", (boolean_t))
73 #define VERIFY3S(x,y,z) VERIFY3_IMPL(x, y, z, int64_t, "%lld", (long long))
74 #define VERIFY3U(x,y,z) VERIFY3_IMPL(x, y, z, uint64_t, "%llu", \
75 (unsigned long long))
76 #define VERIFY3P(x,y,z) VERIFY3_IMPL(x, y, z, uintptr_t, "%p", (void *))
77 #define VERIFY0(x) VERIFY3_IMPL(0, ==, x, int64_t, "%lld", (long long))
78
79 #define CTASSERT_GLOBAL(x) _CTASSERT(x, __LINE__)
80 #define CTASSERT(x) { _CTASSERT(x, __LINE__); }
81 #define _CTASSERT(x, y) __CTASSERT(x, y)
82 #define __CTASSERT(x, y) \
83 typedef char __attribute__ ((unused)) \
84 __compile_time_assertion__ ## y[(x) ? 1 : -1]
85
86 /*
87 * Debugging disabled (--disable-debug)
88 */
89 #ifdef NDEBUG
90
91 #define SPL_DEBUG_STR ""
92 #define ASSERT(x) ((void)0)
93 #define ASSERTV(x)
94 #define ASSERT3B(x,y,z) ((void)0)
95 #define ASSERT3S(x,y,z) ((void)0)
96 #define ASSERT3U(x,y,z) ((void)0)
97 #define ASSERT3P(x,y,z) ((void)0)
98 #define ASSERT0(x) ((void)0)
99 #define IMPLY(A, B) ((void)0)
100 #define EQUIV(A, B) ((void)0)
101
102 /*
103 * Debugging enabled (--enable-debug)
104 */
105 #else
106
107 #define SPL_DEBUG_STR " (DEBUG mode)"
108 #define ASSERT(cond) VERIFY(cond)
109 #define ASSERTV(x) x
110 #define ASSERT3B(x,y,z) VERIFY3B(x, y, z)
111 #define ASSERT3S(x,y,z) VERIFY3S(x, y, z)
112 #define ASSERT3U(x,y,z) VERIFY3U(x, y, z)
113 #define ASSERT3P(x,y,z) VERIFY3P(x, y, z)
114 #define ASSERT0(x) VERIFY0(x)
115 #define IMPLY(A, B) \
116 ((void)(((!(A)) || (B)) || \
117 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
118 "(" #A ") implies (" #B ")")))
119 #define EQUIV(A, B) \
120 ((void)((!!(A) == !!(B)) || \
121 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
122 "(" #A ") is equivalent to (" #B ")")))
123
124 #endif /* NDEBUG */
125
126 #endif /* SPL_DEBUG_H */