]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #ifndef CEPH_ASSERT_H |
2 | #define CEPH_ASSERT_H | |
3 | ||
11fdf7f2 TL |
4 | #include <cstdlib> |
5 | #include <string> | |
7c673cae FG |
6 | |
7 | #if defined(__linux__) | |
8 | #include <features.h> | |
9 | ||
10 | #ifndef __STRING | |
11 | # define __STRING(x) #x | |
12 | #endif | |
13 | ||
14 | #elif defined(__FreeBSD__) | |
15 | #include <sys/cdefs.h> | |
16 | #define __GNUC_PREREQ(minor, major) __GNUC_PREREQ__(minor, major) | |
17 | #elif defined(__sun) || defined(_AIX) | |
18 | #include "include/compat.h" | |
19 | #include <assert.h> | |
20 | #endif | |
21 | ||
22 | #ifdef __CEPH__ | |
23 | # include "acconfig.h" | |
24 | #endif | |
25 | ||
9f95a23c | 26 | #include "include/common_fwd.h" |
7c673cae | 27 | |
7c673cae FG |
28 | namespace ceph { |
29 | ||
30 | struct BackTrace; | |
7c673cae FG |
31 | |
32 | /* | |
33 | * Select a function-name variable based on compiler tests, and any compiler | |
34 | * specific overrides. | |
35 | */ | |
11fdf7f2 | 36 | #if defined(HAVE_PRETTY_FUNC) |
7c673cae FG |
37 | # define __CEPH_ASSERT_FUNCTION __PRETTY_FUNCTION__ |
38 | #elif defined(HAVE_FUNC) | |
39 | # define __CEPH_ASSERT_FUNCTION __func__ | |
40 | #else | |
41 | # define __CEPH_ASSERT_FUNCTION ((__const char *) 0) | |
42 | #endif | |
43 | ||
44 | extern void register_assert_context(CephContext *cct); | |
11fdf7f2 TL |
45 | |
46 | struct assert_data { | |
47 | const char *assertion; | |
48 | const char *file; | |
49 | const int line; | |
50 | const char *function; | |
51 | }; | |
c07f9fc5 | 52 | |
7c673cae FG |
53 | extern void __ceph_assert_fail(const char *assertion, const char *file, int line, const char *function) |
54 | __attribute__ ((__noreturn__)); | |
11fdf7f2 TL |
55 | extern void __ceph_assert_fail(const assert_data &ctx) |
56 | __attribute__ ((__noreturn__)); | |
57 | ||
7c673cae FG |
58 | extern void __ceph_assertf_fail(const char *assertion, const char *file, int line, const char *function, const char* msg, ...) |
59 | __attribute__ ((__noreturn__)); | |
60 | extern void __ceph_assert_warn(const char *assertion, const char *file, int line, const char *function); | |
61 | ||
11fdf7f2 TL |
62 | [[noreturn]] void __ceph_abort(const char *file, int line, const char *func, |
63 | const std::string& msg); | |
64 | ||
65 | [[noreturn]] void __ceph_abortf(const char *file, int line, const char *func, | |
66 | const char* msg, ...); | |
67 | ||
68 | #define _CEPH_ASSERT_VOID_CAST static_cast<void> | |
7c673cae FG |
69 | |
70 | #define assert_warn(expr) \ | |
71 | ((expr) \ | |
c07f9fc5 | 72 | ? _CEPH_ASSERT_VOID_CAST (0) \ |
7c673cae FG |
73 | : __ceph_assert_warn (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION)) |
74 | ||
7c673cae FG |
75 | } |
76 | ||
77 | using namespace ceph; | |
78 | ||
7c673cae FG |
79 | |
80 | /* | |
81 | * ceph_abort aborts the program with a nice backtrace. | |
82 | * | |
83 | * Currently, it's the same as assert(0), but we may one day make assert a | |
84 | * debug-only thing, like it is in many projects. | |
85 | */ | |
11fdf7f2 | 86 | #define ceph_abort(msg, ...) \ |
9f95a23c | 87 | ::ceph::__ceph_abort( __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION, "abort() called") |
11fdf7f2 TL |
88 | |
89 | #define ceph_abort_msg(msg) \ | |
9f95a23c | 90 | ::ceph::__ceph_abort( __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION, msg) |
11fdf7f2 TL |
91 | |
92 | #define ceph_abort_msgf(...) \ | |
9f95a23c | 93 | ::ceph::__ceph_abortf( __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION, __VA_ARGS__) |
11fdf7f2 TL |
94 | |
95 | #ifdef __SANITIZE_ADDRESS__ | |
96 | #define ceph_assert(expr) \ | |
97 | do { \ | |
98 | ((expr)) \ | |
99 | ? _CEPH_ASSERT_VOID_CAST (0) \ | |
9f95a23c | 100 | : ::ceph::__ceph_assert_fail(__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION); \ |
11fdf7f2 TL |
101 | } while (false) |
102 | #else | |
7c673cae | 103 | #define ceph_assert(expr) \ |
11fdf7f2 TL |
104 | do { static const ceph::assert_data assert_data_ctx = \ |
105 | {__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION}; \ | |
106 | ((expr) \ | |
107 | ? _CEPH_ASSERT_VOID_CAST (0) \ | |
9f95a23c | 108 | : ::ceph::__ceph_assert_fail(assert_data_ctx)); } while(false) |
11fdf7f2 | 109 | #endif |
7c673cae FG |
110 | |
111 | // this variant will *never* get compiled out to NDEBUG in the future. | |
112 | // (ceph_assert currently doesn't either, but in the future it might.) | |
11fdf7f2 TL |
113 | #ifdef __SANITIZE_ADDRESS__ |
114 | #define ceph_assert_always(expr) \ | |
115 | do { \ | |
116 | ((expr)) \ | |
117 | ? _CEPH_ASSERT_VOID_CAST (0) \ | |
9f95a23c | 118 | : ::ceph::__ceph_assert_fail(__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION); \ |
11fdf7f2 TL |
119 | } while(false) |
120 | #else | |
7c673cae | 121 | #define ceph_assert_always(expr) \ |
11fdf7f2 TL |
122 | do { static const ceph::assert_data assert_data_ctx = \ |
123 | {__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION}; \ | |
124 | ((expr) \ | |
125 | ? _CEPH_ASSERT_VOID_CAST (0) \ | |
9f95a23c | 126 | : ::ceph::__ceph_assert_fail(assert_data_ctx)); } while(false) |
11fdf7f2 | 127 | #endif |
7c673cae FG |
128 | |
129 | // Named by analogy with printf. Along with an expression, takes a format | |
130 | // string and parameters which are printed if the assertion fails. | |
131 | #define assertf(expr, ...) \ | |
132 | ((expr) \ | |
c07f9fc5 | 133 | ? _CEPH_ASSERT_VOID_CAST (0) \ |
9f95a23c | 134 | : ::ceph::__ceph_assertf_fail (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION, __VA_ARGS__)) |
7c673cae FG |
135 | #define ceph_assertf(expr, ...) \ |
136 | ((expr) \ | |
c07f9fc5 | 137 | ? _CEPH_ASSERT_VOID_CAST (0) \ |
9f95a23c | 138 | : ::ceph::__ceph_assertf_fail (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION, __VA_ARGS__)) |
7c673cae FG |
139 | |
140 | // this variant will *never* get compiled out to NDEBUG in the future. | |
141 | // (ceph_assertf currently doesn't either, but in the future it might.) | |
142 | #define ceph_assertf_always(expr, ...) \ | |
143 | ((expr) \ | |
c07f9fc5 | 144 | ? _CEPH_ASSERT_VOID_CAST (0) \ |
9f95a23c | 145 | : ::ceph::__ceph_assertf_fail (__STRING(expr), __FILE__, __LINE__, __CEPH_ASSERT_FUNCTION, __VA_ARGS__)) |
11fdf7f2 TL |
146 | |
147 | #endif |