Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / include / assert.h
1 #ifndef CEPH_ASSERT_H
2 #define CEPH_ASSERT_H
3
4 #include <stdlib.h>
5
6 #if defined(__linux__)
7 #include <features.h>
8
9 #ifndef __STRING
10 # define __STRING(x) #x
11 #endif
12
13 #elif defined(__FreeBSD__)
14 #include <sys/cdefs.h>
15 #define __GNUC_PREREQ(minor, major)     __GNUC_PREREQ__(minor, major)
16 #elif defined(__sun) || defined(_AIX)
17 #include "include/compat.h"
18 #include <assert.h>
19 #endif
20
21 #ifdef __CEPH__
22 # include "acconfig.h"
23 #endif
24
25 #ifdef __cplusplus
26 class CephContext;
27
28 namespace ceph {
29
30 struct BackTrace;
31 #endif
32
33 /*
34  * For GNU, test specific version features. Otherwise (e.g. LLVM) we'll use
35  * the defaults selected below.
36  */
37 #ifdef __GNUC_PREREQ
38
39 /*
40  * Version 2.4 and later of GCC define a magical variable
41  * `__PRETTY_FUNCTION__' which contains the name of the function currently
42  * being defined.  This is broken in G++ before version 2.6.  C9x has a
43  * similar variable called __func__, but prefer the GCC one since it demangles
44  * C++ function names. We define __CEPH_NO_PRETTY_FUNC if we want to avoid
45  * broken versions of G++.
46  */
47 # if defined __cplusplus ? !__GNUC_PREREQ (2, 6) : !__GNUC_PREREQ (2, 4)
48 #   define __CEPH_NO_PRETTY_FUNC
49 # endif
50
51 #endif
52
53 /*
54  * Select a function-name variable based on compiler tests, and any compiler
55  * specific overrides.
56  */
57 #if defined(HAVE_PRETTY_FUNC) && !defined(__CEPH_NO_PRETTY_FUNC)
58 # define __CEPH_ASSERT_FUNCTION __PRETTY_FUNCTION__
59 #elif defined(HAVE_FUNC)
60 # define __CEPH_ASSERT_FUNCTION __func__
61 #else
62 # define __CEPH_ASSERT_FUNCTION ((__const char *) 0)
63 #endif
64
65 #ifdef __cplusplus
66 extern void register_assert_context(CephContext *cct);
67 #endif
68
69 extern void __ceph_assert_fail(const char *assertion, const char *file, int line, const char *function)
70   __attribute__ ((__noreturn__));
71 extern void __ceph_assertf_fail(const char *assertion, const char *file, int line, const char *function, const char* msg, ...)
72   __attribute__ ((__noreturn__));
73 extern void __ceph_assert_warn(const char *assertion, const char *file, int line, const char *function);
74
75 #ifdef __cplusplus
76 # define _CEPH_ASSERT_VOID_CAST static_cast<void>
77 #else
78 # define _CEPH_ASSERT_VOID_CAST (void)
79 #endif
80
81 #define assert_warn(expr)                                                       \
82   ((expr)                                                               \
83    ? _CEPH_ASSERT_VOID_CAST (0)                                 \
84    : __ceph_assert_warn (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION))
85
86 #ifdef __cplusplus
87 }
88
89 using namespace ceph;
90
91 #endif
92
93 /*
94  * ceph_abort aborts the program with a nice backtrace.
95  *
96  * Currently, it's the same as assert(0), but we may one day make assert a
97  * debug-only thing, like it is in many projects.
98  */
99 #define ceph_abort() abort()
100
101 #define ceph_abort_msg(cct, msg) {                                      \
102                 lgeneric_derr(cct) << "abort: " << msg << dendl;        \
103                 abort();                                                \
104         }
105
106 #endif
107
108 // wipe any prior assert definition
109 #ifdef assert
110 # undef assert
111 #endif
112
113 // make _ASSERT_H something that *must* have a value other than what
114 // /usr/include/assert.h gives it (nothing!), so that we detect when
115 // our assert is clobbered.
116 #undef _ASSERT_H
117 #define _ASSERT_H _dout_cct
118
119 // make __ASSERT_FUNCTION empty (/usr/include/assert.h makes it a function)
120 // and make our encoding macros break if it non-empty.
121 #undef __ASSERT_FUNCTION
122 #define __ASSERT_FUNCTION
123
124 #define assert(expr)                                                    \
125   ((expr)                                                               \
126    ? _CEPH_ASSERT_VOID_CAST (0)                                 \
127    : __ceph_assert_fail (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION))
128 #define ceph_assert(expr)                                                       \
129   ((expr)                                                               \
130    ? _CEPH_ASSERT_VOID_CAST (0)                                 \
131    : __ceph_assert_fail (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION))
132
133 // this variant will *never* get compiled out to NDEBUG in the future.
134 // (ceph_assert currently doesn't either, but in the future it might.)
135 #define ceph_assert_always(expr)                                                        \
136   ((expr)                                                               \
137    ? _CEPH_ASSERT_VOID_CAST (0)                                 \
138    : __ceph_assert_fail (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION))
139
140 // Named by analogy with printf.  Along with an expression, takes a format
141 // string and parameters which are printed if the assertion fails.
142 #define assertf(expr, ...)                  \
143   ((expr)                                                               \
144    ? _CEPH_ASSERT_VOID_CAST (0)                                 \
145    : __ceph_assertf_fail (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION, __VA_ARGS__))
146 #define ceph_assertf(expr, ...)                  \
147   ((expr)                                                               \
148    ? _CEPH_ASSERT_VOID_CAST (0)                                 \
149    : __ceph_assertf_fail (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION, __VA_ARGS__))
150
151 // this variant will *never* get compiled out to NDEBUG in the future.
152 // (ceph_assertf currently doesn't either, but in the future it might.)
153 #define ceph_assertf_always(expr, ...)                  \
154   ((expr)                                                               \
155    ? _CEPH_ASSERT_VOID_CAST (0)                                 \
156    : __ceph_assertf_fail (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION, __VA_ARGS__))