1 /*
2  * kmp_debug.h -- debug / assertion code for Assure library
3  */
4 
5 //===----------------------------------------------------------------------===//
6 //
7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8 // See https://llvm.org/LICENSE.txt for license information.
9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef KMP_DEBUG_H
14 #define KMP_DEBUG_H
15 
16 #include <stdarg.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif // __cplusplus
21 
22 // -----------------------------------------------------------------------------
23 // Build-time assertion.
24 
25 // New C++11 style build assert
26 #define KMP_BUILD_ASSERT(expr) static_assert(expr, "Build condition error")
27 
28 // -----------------------------------------------------------------------------
29 // Run-time assertions.
30 
31 extern void __kmp_dump_debug_buffer(void);
32 
33 #ifdef KMP_USE_ASSERT
34 extern int __kmp_debug_assert(char const *expr, char const *file, int line);
35 #ifdef KMP_DEBUG
36 #define KMP_ASSERT(cond)                                                       \
37   if (!(cond)) {                                                               \
38     __kmp_debug_assert(#cond, __FILE__, __LINE__);                             \
39   }
40 #define KMP_ASSERT2(cond, msg)                                                 \
41   if (!(cond)) {                                                               \
42     __kmp_debug_assert((msg), __FILE__, __LINE__);                             \
43   }
44 #define KMP_DEBUG_ASSERT(cond) KMP_ASSERT(cond)
45 #define KMP_DEBUG_ASSERT2(cond, msg) KMP_ASSERT2(cond, msg)
46 #define KMP_DEBUG_USE_VAR(x) /* Nothing (it is used!) */
47 #else
48 // Do not expose condition in release build. Use "assertion failure".
49 #define KMP_ASSERT(cond)                                                       \
50   if (!(cond)) {                                                               \
51     __kmp_debug_assert("assertion failure", __FILE__, __LINE__);               \
52   }
53 #define KMP_ASSERT2(cond, msg) KMP_ASSERT(cond)
54 #define KMP_DEBUG_ASSERT(cond) /* Nothing */
55 #define KMP_DEBUG_ASSERT2(cond, msg) /* Nothing */
56 #define KMP_DEBUG_USE_VAR(x) ((void)(x))
57 #endif // KMP_DEBUG
58 #else
59 #define KMP_ASSERT(cond) /* Nothing */
60 #define KMP_ASSERT2(cond, msg) /* Nothing */
61 #define KMP_DEBUG_ASSERT(cond) /* Nothing */
62 #define KMP_DEBUG_ASSERT2(cond, msg) /* Nothing */
63 #define KMP_DEBUG_USE_VAR(x) ((void)(x))
64 #endif // KMP_USE_ASSERT
65 
66 #ifdef KMP_DEBUG
67 extern void __kmp_debug_printf_stdout(char const *format, ...);
68 #endif
69 extern void __kmp_debug_printf(char const *format, ...);
70 
71 #ifdef KMP_DEBUG
72 
73 extern int kmp_a_debug;
74 extern int kmp_b_debug;
75 extern int kmp_c_debug;
76 extern int kmp_d_debug;
77 extern int kmp_e_debug;
78 extern int kmp_f_debug;
79 extern int kmp_diag;
80 
81 #define KA_TRACE(d, x)                                                         \
82   if (kmp_a_debug >= d) {                                                      \
83     __kmp_debug_printf x;                                                      \
84   }
85 #define KB_TRACE(d, x)                                                         \
86   if (kmp_b_debug >= d) {                                                      \
87     __kmp_debug_printf x;                                                      \
88   }
89 #define KC_TRACE(d, x)                                                         \
90   if (kmp_c_debug >= d) {                                                      \
91     __kmp_debug_printf x;                                                      \
92   }
93 #define KD_TRACE(d, x)                                                         \
94   if (kmp_d_debug >= d) {                                                      \
95     __kmp_debug_printf x;                                                      \
96   }
97 #define KE_TRACE(d, x)                                                         \
98   if (kmp_e_debug >= d) {                                                      \
99     __kmp_debug_printf x;                                                      \
100   }
101 #define KF_TRACE(d, x)                                                         \
102   if (kmp_f_debug >= d) {                                                      \
103     __kmp_debug_printf x;                                                      \
104   }
105 #define K_DIAG(d, x)                                                           \
106   {                                                                            \
107     if (kmp_diag == d) {                                                       \
108       __kmp_debug_printf_stdout x;                                             \
109     }                                                                          \
110   }
111 
112 #define KA_DUMP(d, x)                                                          \
113   if (kmp_a_debug >= d) {                                                      \
114     int ks;                                                                    \
115     __kmp_disable(&ks);                                                        \
116     (x);                                                                       \
117     __kmp_enable(ks);                                                          \
118   }
119 #define KB_DUMP(d, x)                                                          \
120   if (kmp_b_debug >= d) {                                                      \
121     int ks;                                                                    \
122     __kmp_disable(&ks);                                                        \
123     (x);                                                                       \
124     __kmp_enable(ks);                                                          \
125   }
126 #define KC_DUMP(d, x)                                                          \
127   if (kmp_c_debug >= d) {                                                      \
128     int ks;                                                                    \
129     __kmp_disable(&ks);                                                        \
130     (x);                                                                       \
131     __kmp_enable(ks);                                                          \
132   }
133 #define KD_DUMP(d, x)                                                          \
134   if (kmp_d_debug >= d) {                                                      \
135     int ks;                                                                    \
136     __kmp_disable(&ks);                                                        \
137     (x);                                                                       \
138     __kmp_enable(ks);                                                          \
139   }
140 #define KE_DUMP(d, x)                                                          \
141   if (kmp_e_debug >= d) {                                                      \
142     int ks;                                                                    \
143     __kmp_disable(&ks);                                                        \
144     (x);                                                                       \
145     __kmp_enable(ks);                                                          \
146   }
147 #define KF_DUMP(d, x)                                                          \
148   if (kmp_f_debug >= d) {                                                      \
149     int ks;                                                                    \
150     __kmp_disable(&ks);                                                        \
151     (x);                                                                       \
152     __kmp_enable(ks);                                                          \
153   }
154 
155 #else
156 
157 #define KA_TRACE(d, x) /* nothing to do */
158 #define KB_TRACE(d, x) /* nothing to do */
159 #define KC_TRACE(d, x) /* nothing to do */
160 #define KD_TRACE(d, x) /* nothing to do */
161 #define KE_TRACE(d, x) /* nothing to do */
162 #define KF_TRACE(d, x) /* nothing to do */
163 #define K_DIAG(d, x)                                                           \
164   {} /* nothing to do */
165 
166 #define KA_DUMP(d, x) /* nothing to do */
167 #define KB_DUMP(d, x) /* nothing to do */
168 #define KC_DUMP(d, x) /* nothing to do */
169 #define KD_DUMP(d, x) /* nothing to do */
170 #define KE_DUMP(d, x) /* nothing to do */
171 #define KF_DUMP(d, x) /* nothing to do */
172 
173 #endif // KMP_DEBUG
174 
175 #ifdef __cplusplus
176 } // extern "C"
177 #endif // __cplusplus
178 
179 #endif /* KMP_DEBUG_H */
180