]> git.proxmox.com Git - mirror_spl.git/blame - include/sys/debug.h
Fix multiple evaluations of VERIFY() and ASSERT() on failures
[mirror_spl.git] / include / sys / debug.h
CommitLineData
716154c5
BB
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>.
715f6251 6 * UCRL-CODE-235197
7 *
716154c5 8 * This file is part of the SPL, Solaris Porting Layer.
3d6af2dd 9 * For details, see <http://zfsonlinux.org/>.
716154c5
BB
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.
715f6251 15 *
716154c5 16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251 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
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23\*****************************************************************************/
715f6251 24
55abb092
BB
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.
55abb092 33 * ASSERTV() - Wraps a variable declaration which is only used by ASSERT().
dfbd813e 34 * ASSERT3B() - Assert boolean X OP Y is true, if not panic.
55abb092
BB
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.
1c6d149f 38 * ASSERT0() - Assert value is zero, if not panic.
55abb092 39 * VERIFY() - Verify X is true, if not panic.
dfbd813e 40 * VERIFY3B() - Verify boolean X OP Y is true, if not panic.
55abb092
BB
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.
1c6d149f 44 * VERIFY0() - Verify value is zero, if not panic.
55abb092
BB
45 */
46
f4b37741 47#ifndef _SPL_DEBUG_H
8d9a23e8 48#define _SPL_DEBUG_H
f4b37741 49
8d9a23e8
BB
50/*
51 * Common DEBUG functionality.
52 */
53int spl_panic(const char *file, const char *func, int line,
54 const char *fmt, ...);
55void spl_dumpstack(void);
57d1b188 56
4852db99 57/* BEGIN CSTYLED */
8d9a23e8
BB
58#define PANIC(fmt, a...) \
59 spl_panic(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
57d1b188 60
8d9a23e8 61#define VERIFY(cond) \
4852db99 62 (void) (unlikely(!(cond)) && \
8d9a23e8
BB
63 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
64 "%s", "VERIFY(" #cond ") failed\n"))
57d1b188 65
610988f2
D
66#define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE, FMT, CAST) do { \
67 TYPE _verify3_left = (TYPE)(LEFT); \
68 TYPE _verify3_right = (TYPE)(RIGHT); \
69 if (!(_verify3_left OP _verify3_right)) \
70 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
71 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
72 "failed (" FMT " " #OP " " FMT ")\n", \
73 CAST (_verify3_left), CAST (_verify3_right)); \
74 } while (0)
57d1b188 75
dfbd813e 76#define VERIFY3B(x,y,z) VERIFY3_IMPL(x, y, z, boolean_t, "%d", (boolean_t))
8d9a23e8
BB
77#define VERIFY3S(x,y,z) VERIFY3_IMPL(x, y, z, int64_t, "%lld", (long long))
78#define VERIFY3U(x,y,z) VERIFY3_IMPL(x, y, z, uint64_t, "%llu", \
55abb092 79 (unsigned long long))
8d9a23e8
BB
80#define VERIFY3P(x,y,z) VERIFY3_IMPL(x, y, z, uintptr_t, "%p", (void *))
81#define VERIFY0(x) VERIFY3_IMPL(0, ==, x, int64_t, "%lld", (long long))
57d1b188 82
8d9a23e8
BB
83#define CTASSERT_GLOBAL(x) _CTASSERT(x, __LINE__)
84#define CTASSERT(x) { _CTASSERT(x, __LINE__); }
85#define _CTASSERT(x, y) __CTASSERT(x, y)
86#define __CTASSERT(x, y) \
87 typedef char __attribute__ ((unused)) \
88 __compile_time_assertion__ ## y[(x) ? 1 : -1]
6c48cd8a 89
f6a86961 90/*
8d9a23e8 91 * Debugging disabled (--disable-debug)
f6a86961 92 */
8d9a23e8
BB
93#ifdef NDEBUG
94
95#define SPL_DEBUG_STR ""
96#define ASSERT(x) ((void)0)
97#define ASSERTV(x)
dfbd813e 98#define ASSERT3B(x,y,z) ((void)0)
8d9a23e8
BB
99#define ASSERT3S(x,y,z) ((void)0)
100#define ASSERT3U(x,y,z) ((void)0)
101#define ASSERT3P(x,y,z) ((void)0)
102#define ASSERT0(x) ((void)0)
5acb2307
BB
103#define IMPLY(A, B) ((void)0)
104#define EQUIV(A, B) ((void)0)
f6a86961 105
6c48cd8a 106/*
8d9a23e8 107 * Debugging enabled (--enable-debug)
6c48cd8a 108 */
8d9a23e8
BB
109#else
110
111#define SPL_DEBUG_STR " (DEBUG mode)"
112#define ASSERT(cond) VERIFY(cond)
113#define ASSERTV(x) x
dfbd813e 114#define ASSERT3B(x,y,z) VERIFY3B(x, y, z)
8d9a23e8
BB
115#define ASSERT3S(x,y,z) VERIFY3S(x, y, z)
116#define ASSERT3U(x,y,z) VERIFY3U(x, y, z)
117#define ASSERT3P(x,y,z) VERIFY3P(x, y, z)
118#define ASSERT0(x) VERIFY0(x)
5acb2307
BB
119#define IMPLY(A, B) \
120 ((void)(((!(A)) || (B)) || \
121 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
122 "(" #A ") implies (" #B ")")))
123#define EQUIV(A, B) \
124 ((void)((!!(A) == !!(B)) || \
125 spl_panic(__FILE__, __FUNCTION__, __LINE__, \
126 "(" #A ") is equivalent to (" #B ")")))
4852db99 127/* END CSTYLED */
8d9a23e8
BB
128
129#endif /* NDEBUG */
6c48cd8a 130
f4b37741 131#endif /* SPL_DEBUG_H */