1 #ifndef POCL_DEBUG_H
2 #define POCL_DEBUG_H
3 
4 #ifdef _WIN32
5 #  include <stdint.h>
6 #  include <stddef.h> // size_t
7 #  define PRIu64 "I64u"
8 #  define PRIX64 "I64x"
9 #  define PRIXPTR "p"
10 #  define PRIuS "Iu"
11 #else
12 # ifndef __STDC_FORMAT_MACROS
13 # define __STDC_FORMAT_MACROS
14 # endif
15 # include <inttypes.h>
16 #endif
17 
18 #include "config.h"
19 
20 #include "pocl_export.h"
21 
22 // size_t print spec
23 #ifndef PRIuS
24 # define PRIuS "zu"
25 #endif
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 // should use some terminfo library, but..
32 #define POCL_COLOR_RESET   "\033[0m"
33 #define POCL_COLOR_BLACK   "\033[30m"      /* Black */
34 #define POCL_COLOR_RED     "\033[31m"      /* Red */
35 #define POCL_COLOR_GREEN   "\033[32m"      /* Green */
36 #define POCL_COLOR_YELLOW  "\033[33m"      /* Yellow */
37 #define POCL_COLOR_BLUE    "\033[34m"      /* Blue */
38 #define POCL_COLOR_MAGENTA "\033[35m"      /* Magenta */
39 #define POCL_COLOR_CYAN    "\033[36m"      /* Cyan */
40 #define POCL_COLOR_WHITE   "\033[37m"      /* White */
41 #define POCL_COLOR_BOLDBLACK   "\033[1m\033[30m"      /* Bold Black */
42 #define POCL_COLOR_BOLDRED     "\033[1m\033[31m"      /* Bold Red */
43 #define POCL_COLOR_BOLDGREEN   "\033[1m\033[32m"      /* Bold Green */
44 #define POCL_COLOR_BOLDYELLOW  "\033[1m\033[33m"      /* Bold Yellow */
45 #define POCL_COLOR_BOLDBLUE    "\033[1m\033[34m"      /* Bold Blue */
46 #define POCL_COLOR_BOLDMAGENTA "\033[1m\033[35m"      /* Bold Magenta */
47 #define POCL_COLOR_BOLDCYAN    "\033[1m\033[36m"      /* Bold Cyan */
48 #define POCL_COLOR_BOLDWHITE   "\033[1m\033[37m"      /* Bold White */
49 
50 /* bitfield values for pocl_debug_messages_filter */
51 #define POCL_DEBUG_FLAG_GENERAL 0x1
52 #define POCL_DEBUG_FLAG_MEMORY 0x2
53 #define POCL_DEBUG_FLAG_LLVM 0x4
54 #define POCL_DEBUG_FLAG_EVENTS 0x8
55 #define POCL_DEBUG_FLAG_CACHE 0x10
56 #define POCL_DEBUG_FLAG_LOCKING 0x20
57 #define POCL_DEBUG_FLAG_REFCOUNTS 0x40
58 #define POCL_DEBUG_FLAG_TIMING 0x80
59 #define POCL_DEBUG_FLAG_HSA 0x100
60 #define POCL_DEBUG_FLAG_TCE 0x200
61 #define POCL_DEBUG_FLAG_CUDA 0x400
62 #define POCL_DEBUG_FLAG_ACCEL 0x800
63 
64 #define POCL_DEBUG_FLAG_WARNING  0x8000000000UL
65 #define POCL_DEBUG_FLAG_ERROR   0x10000000000UL
66 #define POCL_DEBUG_FLAG_ALL (uint64_t)(-1)
67 
68 #define POCL_FILTER_TYPE_INFO 1
69 #define POCL_FILTER_TYPE_WARN 2
70 #define POCL_FILTER_TYPE_ERR 3
71 
72 /* Debugging macros. Also macros for marking unimplemented parts of specs or
73    untested parts of the implementation. */
74 
75 #define POCL_ABORT_UNIMPLEMENTED(MSG)                                   \
76     do {                                                                \
77         fprintf(stderr,"%s is unimplemented (%s:%d)\n",                 \
78                         MSG, __FILE__, __LINE__);                       \
79         exit(2);                                                        \
80     } while (0)
81 
82 #define POCL_WARN_UNTESTED()                                            \
83     do {                                                                \
84         fprintf( stderr,                                                \
85             "pocl warning: encountered untested part of the "           \
86             "implementation in %s:%d\n", __FILE__, __LINE__);           \
87     } while (0)
88 
89 #define POCL_WARN_INCOMPLETE()                                          \
90     do {                                                                \
91         fprintf( stderr,                                                \
92             "pocl warning: encountered incomplete implementation"       \
93             " in %s:%d\n", __FILE__, __LINE__);                         \
94     } while (0)
95 
96 #define POCL_ABORT(...)                                                 \
97     do {                                                                \
98         fprintf(stderr, __VA_ARGS__);                                   \
99         abort();                                                        \
100     } while (0)
101 
102 #define POCL_ERROR(x) do { if (errcode_ret != NULL) {                   \
103                               *errcode_ret = (x);                       \
104                            } return NULL; } while (0)
105 
106 #define POCL_SUCCESS() do { if (errcode_ret != NULL) {                  \
107                               *errcode_ret = CL_SUCCESS;                \
108                             } } while (0)
109 
110 #ifdef POCL_DEBUG_MESSAGES
111 
112 POCL_EXPORT
113     extern uint64_t pocl_debug_messages_filter;
114 POCL_EXPORT
115     extern int pocl_stderr_is_a_tty;
116 
117     #define POCL_DEBUGGING_ON (pocl_debug_messages_filter)
118 
119         #define POCL_DEBUG_HEADER(FILTER, FILTER_TYPE) \
120             pocl_debug_print_header (__func__, __LINE__, #FILTER, FILTER_TYPE);
121 
122 POCL_EXPORT
123         extern void pocl_debug_output_lock (void);
124 POCL_EXPORT
125         extern void pocl_debug_output_unlock (void);
126 POCL_EXPORT
127         extern void pocl_debug_messages_setup (const char *debug);
128 POCL_EXPORT
129         extern void pocl_debug_print_header (const char * func, unsigned line,
130                                              const char* filter, int filter_type);
131         extern void pocl_debug_measure_start (uint64_t* start);
132         extern void pocl_debug_measure_finish (uint64_t* start, uint64_t* finish,
133                                                const char* msg,
134                                                const char *func,
135                                                unsigned line);
136         extern void pocl_debug_print_duration (const char* func, unsigned line,
137                                                const char* msg, uint64_t nanosecs);
138         #define POCL_MEASURE_START(SUFFIX) \
139           uint64_t pocl_time_start_ ## SUFFIX, pocl_time_finish_ ## SUFFIX; \
140           pocl_debug_measure_start(&pocl_time_start_ ## SUFFIX);
141 
142         #define POCL_MEASURE_FINISH(SUFFIX) \
143           pocl_debug_measure_finish (&pocl_time_start_ ## SUFFIX,           \
144                          &pocl_time_finish_ ## SUFFIX, "API: " #SUFFIX,     \
145                          __func__, __LINE__);
146 
147     #define POCL_MSG_PRINT_F(FILTER, TYPE, ERRCODE, ...)                    \
148         do {                                                                \
149           if (pocl_debug_messages_filter & POCL_DEBUG_FLAG_ ## FILTER) {    \
150             pocl_debug_output_lock ();                                      \
151                 POCL_DEBUG_HEADER(FILTER, POCL_FILTER_TYPE_ ## TYPE)        \
152                 if (pocl_stderr_is_a_tty)                                   \
153                   fprintf (stderr, "%s", POCL_COLOR_BOLDRED                 \
154                                     ERRCODE " "  POCL_COLOR_RESET);         \
155                 else                                                        \
156                   fprintf (stderr, "%s", ERRCODE " ");                      \
157                 fprintf (stderr, __VA_ARGS__);                              \
158             pocl_debug_output_unlock ();                                    \
159           }                                                                 \
160         } while (0)
161 
162     #define POCL_MSG_PRINT2(FILTER, func, line, ...)                        \
163         do {                                                                \
164           if (pocl_debug_messages_filter & POCL_DEBUG_FLAG_ ## FILTER) {    \
165             pocl_debug_output_lock ();                                      \
166                 pocl_debug_print_header (func, line,                        \
167                                  #FILTER, POCL_FILTER_TYPE_INFO);           \
168                 fprintf  (stderr, __VA_ARGS__);                             \
169             pocl_debug_output_unlock ();                                    \
170           }                                                                 \
171         } while (0)
172 
173 
174     #define POCL_MSG_WARN2(errcode, ...) \
175               POCL_MSG_PRINT_F(WARNING, WARN, errcode, __VA_ARGS__)
176     #define POCL_MSG_WARN(...)  POCL_MSG_WARN2("", __VA_ARGS__)
177 
178     #define POCL_MSG_ERR2(errcode, ...) \
179           POCL_MSG_PRINT_F(ERROR, ERR, errcode, __VA_ARGS__)
180     #define POCL_MSG_ERR(...)  POCL_MSG_ERR2("", __VA_ARGS__)
181 
182     #define POCL_MSG_PRINT_INFO2(errcode, ...) \
183           POCL_MSG_PRINT_F(GENERAL, INFO, errcode, __VA_ARGS__)
184     #define POCL_MSG_PRINT_INFO(...) POCL_MSG_PRINT_INFO2("", __VA_ARGS__)
185 
186     #define POCL_MSG_PRINT_INFO_F(filter, errcode, ...) \
187           POCL_MSG_PRINT_F(filter, INFO, errcode, __VA_ARGS__)
188 
189     #define POCL_MSG_PRINT_ACCEL2(errcode, ...) POCL_MSG_PRINT_INFO_F(ACCEL, errcode, __VA_ARGS__)
190     #define POCL_MSG_PRINT_ACCEL(...) POCL_MSG_PRINT_INFO_F(ACCEL, "", __VA_ARGS__)
191     #define POCL_MSG_PRINT_CUDA2(errcode, ...) POCL_MSG_PRINT_INFO_F(CUDA, errcode, __VA_ARGS__)
192     #define POCL_MSG_PRINT_CUDA(...) POCL_MSG_PRINT_INFO_F(CUDA, "", __VA_ARGS__)
193     #define POCL_MSG_PRINT_HSA2(errcode, ...) POCL_MSG_PRINT_INFO_F(HSA, errcode, __VA_ARGS__)
194     #define POCL_MSG_PRINT_HSA(...) POCL_MSG_PRINT_INFO_F(HSA, "", __VA_ARGS__)
195     #define POCL_MSG_PRINT_TCE2(errcode, ...) POCL_MSG_PRINT_INFO_F(TCE, errcode, __VA_ARGS__)
196     #define POCL_MSG_PRINT_TCE(...) POCL_MSG_PRINT_INFO_F(TCE, "", __VA_ARGS__)
197     #define POCL_MSG_PRINT_LOCKING2(errcode, ...) POCL_MSG_PRINT_INFO_F(LOCKING, errcode, __VA_ARGS__)
198     #define POCL_MSG_PRINT_LOCKING(...) POCL_MSG_PRINT_INFO_F(LOCKING, "", __VA_ARGS__)
199     #define POCL_MSG_PRINT_REFCOUNTS2(errcode, ...) POCL_MSG_PRINT_INFO_F(REFCOUNTS, errcode, __VA_ARGS__)
200     #define POCL_MSG_PRINT_REFCOUNTS(...) POCL_MSG_PRINT_INFO_F(REFCOUNTS, "", __VA_ARGS__)
201     #define POCL_MSG_PRINT_CACHE2(errcode, ...) POCL_MSG_PRINT_INFO_F(CACHE, errcode, __VA_ARGS__)
202     #define POCL_MSG_PRINT_CACHE(...) POCL_MSG_PRINT_INFO_F(CACHE, "", __VA_ARGS__)
203     #define POCL_MSG_PRINT_EVENTS2(errcode, ...) POCL_MSG_PRINT_INFO_F(EVENTS, errcode, __VA_ARGS__)
204     #define POCL_MSG_PRINT_EVENTS(...) POCL_MSG_PRINT_INFO_F(EVENTS, "", __VA_ARGS__)
205     #define POCL_MSG_PRINT_LLVM2(errcode, ...) POCL_MSG_PRINT_INFO_F(LLVM, errcode, __VA_ARGS__)
206     #define POCL_MSG_PRINT_LLVM(...) POCL_MSG_PRINT_INFO_F(LLVM, "", __VA_ARGS__)
207     #define POCL_MSG_PRINT_MEMORY2(errcode, ...) POCL_MSG_PRINT_INFO_F(MEMORY, errcode, __VA_ARGS__)
208     #define POCL_MSG_PRINT_MEMORY(...) POCL_MSG_PRINT_INFO_F(MEMORY, "", __VA_ARGS__)
209     #define POCL_MSG_PRINT_GENERAL2(errcode, ...) POCL_MSG_PRINT_INFO_F(GENERAL, errcode, __VA_ARGS__)
210     #define POCL_MSG_PRINT_GENERAL(...) POCL_MSG_PRINT_INFO_F(GENERAL, "", __VA_ARGS__)
211 
212 #else
213 
214     #define POCL_DEBUGGING_ON 0
215 
216     #define POCL_MSG_PRINT_F(...)  do {} while (0)
217     #define POCL_MSG_PRINT(...)  do {} while (0)
218     #define POCL_MSG_PRINT2(...)  do {} while (0)
219     #define POCL_MSG_WARN(...)  do {} while (0)
220     #define POCL_MSG_WARN2(...)  do {} while (0)
221     #define POCL_MSG_ERR(...)  do {} while (0)
222     #define POCL_MSG_ERR2(...)  do {} while (0)
223     #define POCL_MSG_PRINT_INFO(...)  do {} while (0)
224     #define POCL_MSG_PRINT_INFO2(...)  do {} while (0)
225     #define POCL_MSG_PRINT_INFO_F(...)  do {} while (0)
226 
227     #define POCL_DEBUG_HEADER
228     #define POCL_MEASURE_START(...)  do {} while (0)
229     #define POCL_MEASURE_FINISH(...)  do {} while (0)
230     #define POCL_DEBUG_EVENT_TIME(...)  do {} while (0)
231 
232     #define POCL_MSG_PRINT_ACCEL2(...)  do {} while (0)
233     #define POCL_MSG_PRINT_ACCEL(...)  do {} while (0)
234     #define POCL_MSG_PRINT_CUDA2(...)  do {} while (0)
235     #define POCL_MSG_PRINT_CUDA(...)  do {} while (0)
236     #define POCL_MSG_PRINT_HSA2(...)  do {} while (0)
237     #define POCL_MSG_PRINT_HSA(...)  do {} while (0)
238     #define POCL_MSG_PRINT_TCE2(...)  do {} while (0)
239     #define POCL_MSG_PRINT_TCE(...)  do {} while (0)
240     #define POCL_MSG_PRINT_LOCKING2(...)  do {} while (0)
241     #define POCL_MSG_PRINT_LOCKING(...)  do {} while (0)
242     #define POCL_MSG_PRINT_REFCOUNTS2(...)  do {} while (0)
243     #define POCL_MSG_PRINT_REFCOUNTS(...)  do {} while (0)
244     #define POCL_MSG_PRINT_CACHE2(...)  do {} while (0)
245     #define POCL_MSG_PRINT_CACHE(...)  do {} while (0)
246     #define POCL_MSG_PRINT_EVENTS2(...)  do {} while (0)
247     #define POCL_MSG_PRINT_EVENTS(...)  do {} while (0)
248     #define POCL_MSG_PRINT_LLVM2(...)  do {} while (0)
249     #define POCL_MSG_PRINT_LLVM(...)  do {} while (0)
250     #define POCL_MSG_PRINT_MEMORY2(...)  do {} while (0)
251     #define POCL_MSG_PRINT_MEMORY(...)  do {} while (0)
252     #define POCL_MSG_PRINT_GENERAL2(...)  do {} while (0)
253     #define POCL_MSG_PRINT_GENERAL(...)  do {} while (0)
254 
255 #endif
256 
257 
258 #define POCL_GOTO_ERROR_ON(cond, err_code, ...)                             \
259   do                                                                        \
260     {                                                                       \
261       if (cond)                                                             \
262         {                                                                   \
263             POCL_MSG_ERR2(#err_code, __VA_ARGS__);                          \
264             errcode = err_code;                                             \
265             goto ERROR;                                                     \
266         }                                                                   \
267     }                                                                       \
268   while (0)
269 
270 #define POCL_RETURN_ERROR_ON(cond, err_code, ...)                           \
271   do                                                                        \
272     {                                                                       \
273       if (cond)                                                             \
274         {                                                                   \
275             POCL_MSG_ERR2(#err_code, __VA_ARGS__);                          \
276             return err_code;                                                \
277         }                                                                   \
278     }                                                                       \
279   while (0)
280 
281 #define POCL_RETURN_ERROR_COND(cond, err_code)                              \
282   do                                                                        \
283     {                                                                       \
284       if (cond)                                                             \
285         {                                                                   \
286           POCL_MSG_ERR2(#err_code, "%s\n", #cond);                          \
287           return err_code;                                                  \
288         }                                                                   \
289     }                                                                       \
290   while (0)
291 
292 #define POCL_GOTO_ERROR_COND(cond, err_code)                                \
293   do                                                                        \
294     {                                                                       \
295       if (cond)                                                             \
296         {                                                                   \
297           POCL_MSG_ERR2(#err_code, "%s\n", #cond);                          \
298           errcode = err_code;                                               \
299           goto ERROR;                                                       \
300         }                                                                   \
301     }                                                                       \
302   while (0)
303 
304 #define POCL_GOTO_LABEL_COND(label, cond, err_code)                           \
305   do                                                                          \
306     {                                                                         \
307       if (cond)                                                               \
308         {                                                                     \
309           POCL_MSG_ERR2 (#err_code, "%s\n", #cond);                           \
310           errcode = err_code;                                                 \
311           goto label;                                                         \
312         }                                                                     \
313     }                                                                         \
314   while (0)
315 
316 #define POCL_GOTO_LABEL_ON(label, cond, err_code, ...)                        \
317   do                                                                          \
318     {                                                                         \
319       if (cond)                                                               \
320         {                                                                     \
321           POCL_MSG_ERR2 (#err_code, __VA_ARGS__);                             \
322           errcode = err_code;                                                 \
323           goto label;                                                         \
324         }                                                                     \
325     }                                                                         \
326   while (0)
327 
328 #ifdef __cplusplus
329 }
330 #endif
331 
332 #endif
333