]> git.proxmox.com Git - mirror_spl.git/blame - include/sys/debug.h
Safer debugging and assertion macros.
[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.
33 * ASSERTF() - Assert X is true, if not panic and print message.
34 * ASSERTV() - Wraps a variable declaration which is only used by ASSERT().
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
BB
39 * VERIFY() - Verify X is true, if not panic.
40 * VERIFY3S() - Verify signed X OP Y is true, if not panic.
41 * VERIFY3U() - Verify unsigned X OP Y is true, if not panic.
42 * VERIFY3P() - Verify pointer X OP Y is true, if not panic.
1c6d149f 43 * VERIFY0() - Verify value is zero, if not panic.
55abb092
BB
44 */
45
f4b37741 46#ifndef _SPL_DEBUG_H
47#define _SPL_DEBUG_H
48
4b2220f0
BB
49#include <spl-debug.h>
50
55abb092 51#ifdef NDEBUG /* Debugging Disabled */
cc7449cc 52
81672c01
RC
53/* Define SPL_DEBUG_STR to make clear which ASSERT definitions are used */
54#define SPL_DEBUG_STR ""
55
55abb092 56#define PANIC(fmt, a...) \
f6a86961 57 spl_PANIC(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
cc7449cc 58
59#define __ASSERT(x) ((void)0)
cc7449cc 60#define ASSERT(x) ((void)0)
51117639 61#define ASSERTF(x, y, z...) ((void)0)
3a68dc53 62#define ASSERTV(x)
55abb092 63#define VERIFY(cond) \
f6a86961
TC
64 (void)(unlikely(!(cond)) && \
65 spl_PANIC(__FILE__, __FUNCTION__, __LINE__, \
66 "%s", "VERIFY(" #cond ") failed\n"))
cc7449cc 67
55abb092 68#define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE, FMT, CAST) \
f6a86961
TC
69 (void)((!((TYPE)(LEFT) OP (TYPE)(RIGHT))) && \
70 spl_PANIC(__FILE__, __FUNCTION__, __LINE__, \
71 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
55abb092 72 "failed (" FMT " " #OP " " FMT ")\n", \
f6a86961 73 CAST (LEFT), CAST (RIGHT)))
cc7449cc 74
55abb092
BB
75#define VERIFY3S(x,y,z) VERIFY3_IMPL(x, y, z, int64_t, "%lld", (long long))
76#define VERIFY3U(x,y,z) VERIFY3_IMPL(x, y, z, uint64_t, "%llu", \
77 (unsigned long long))
78#define VERIFY3P(x,y,z) VERIFY3_IMPL(x, y, z, uintptr_t, "%p", (void *))
ab0fdfef 79#define VERIFY0(x) VERIFY3_IMPL(0, ==, x, int64_t, "%lld", (long long))
cc7449cc 80
55abb092
BB
81#define ASSERT3S(x,y,z) ((void)0)
82#define ASSERT3U(x,y,z) ((void)0)
83#define ASSERT3P(x,y,z) ((void)0)
1c6d149f 84#define ASSERT0(x) ((void)0)
cc7449cc 85
55abb092 86#else /* Debugging Enabled */
cc7449cc 87
81672c01
RC
88/* Define SPL_DEBUG_STR to make clear which ASSERT definitions are used */
89#define SPL_DEBUG_STR " (DEBUG mode)"
90
55abb092 91#define PANIC(fmt, a...) \
f6a86961 92 spl_PANIC(__FILE__, __FUNCTION__, __LINE__, fmt, ## a)
937879f1 93
57d1b188 94/* ASSERTION that is safe to use within the debug system */
55abb092
BB
95#define __ASSERT(cond) \
96do { \
97 if (unlikely(!(cond))) { \
98 printk(KERN_EMERG "ASSERTION(" #cond ") failed\n"); \
99 BUG(); \
100 } \
57d1b188 101} while (0)
102
57d1b188 103/* ASSERTION that will debug log used outside the debug sysytem */
55abb092 104#define ASSERT(cond) \
f6a86961
TC
105 (void)(unlikely(!(cond)) && \
106 spl_PANIC(__FILE__, __FUNCTION__, __LINE__, \
107 "%s", "ASSERTION(" #cond ") failed\n"))
57d1b188 108
55abb092 109#define ASSERTF(cond, fmt, a...) \
f6a86961
TC
110 (void)(unlikely(!(cond)) && \
111 spl_PANIC(__FILE__, __FUNCTION__, __LINE__, \
112 "ASSERTION(" #cond ") failed: " fmt, ## a))
57d1b188 113
55abb092 114#define VERIFY3_IMPL(LEFT, OP, RIGHT, TYPE, FMT, CAST) \
f6a86961
TC
115 (void)((!((TYPE)(LEFT) OP (TYPE)(RIGHT))) && \
116 spl_PANIC(__FILE__, __FUNCTION__, __LINE__, \
117 "VERIFY3(" #LEFT " " #OP " " #RIGHT ") " \
55abb092 118 "failed (" FMT " " #OP " " FMT ")\n", \
f6a86961 119 CAST (LEFT), CAST (RIGHT)))
57d1b188 120
55abb092
BB
121#define VERIFY3S(x,y,z) VERIFY3_IMPL(x, y, z, int64_t, "%lld", (long long))
122#define VERIFY3U(x,y,z) VERIFY3_IMPL(x, y, z, uint64_t, "%llu", \
123 (unsigned long long))
124#define VERIFY3P(x,y,z) VERIFY3_IMPL(x, y, z, uintptr_t, "%p", (void *))
ab0fdfef 125#define VERIFY0(x) VERIFY3_IMPL(0, ==, x, int64_t, "%lld", (long long))
57d1b188 126
55abb092
BB
127#define ASSERT3S(x,y,z) VERIFY3S(x, y, z)
128#define ASSERT3U(x,y,z) VERIFY3U(x, y, z)
129#define ASSERT3P(x,y,z) VERIFY3P(x, y, z)
1c6d149f 130#define ASSERT0(x) VERIFY0(x)
57d1b188 131
55abb092
BB
132#define ASSERTV(x) x
133#define VERIFY(x) ASSERT(x)
57d1b188 134
cc7449cc 135#endif /* NDEBUG */
6c48cd8a 136
f6a86961
TC
137/*
138 * Helpers for the Solaris debug macros above
139 */
140extern int spl_PANIC(char *filename, const char *functionname,
141 int lineno, const char *fmt, ...);
142
6c48cd8a
YS
143/*
144 * Compile-time assertion. The condition 'x' must be constant.
145 */
146#define CTASSERT_GLOBAL(x) _CTASSERT(x, __LINE__)
147#define CTASSERT(x) { _CTASSERT(x, __LINE__); }
148#define _CTASSERT(x, y) __CTASSERT(x, y)
149#define __CTASSERT(x, y) \
150 typedef char __attribute__ ((unused)) \
151 __compile_time_assertion__ ## y[(x) ? 1 : -1]
152
f4b37741 153#endif /* SPL_DEBUG_H */