util: skip NEON detection if built with -mfpu=neon
[mesa.git] / src / util / u_debug.h
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Cross-platform debugging helpers.
31 *
32 * For now it just has assert and printf replacements, but it might be extended
33 * with stack trace reports and more advanced logging in the near future.
34 *
35 * @author Jose Fonseca <jfonseca@vmware.com>
36 */
37
38 #ifndef U_DEBUG_H_
39 #define U_DEBUG_H_
40
41
42 #include "util/os_misc.h"
43 #include "util/detect_os.h"
44
45 #if DETECT_OS_HAIKU
46 /* Haiku provides debug_printf in libroot with OS.h */
47 #include <OS.h>
48 #endif
49
50 #include "pipe/p_defines.h"
51
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56
57
58 #if defined(__GNUC__)
59 #define _util_printf_format(fmt, list) __attribute__ ((format (printf, fmt, list)))
60 #else
61 #define _util_printf_format(fmt, list)
62 #endif
63
64 void _debug_vprintf(const char *format, va_list ap);
65
66
67 static inline void
68 _debug_printf(const char *format, ...)
69 {
70 va_list ap;
71 va_start(ap, format);
72 _debug_vprintf(format, ap);
73 va_end(ap);
74 }
75
76
77 /**
78 * Print debug messages.
79 *
80 * The actual channel used to output debug message is platform specific. To
81 * avoid misformating or truncation, follow these rules of thumb:
82 * - output whole lines
83 * - avoid outputing large strings (512 bytes is the current maximum length
84 * that is guaranteed to be printed in all platforms)
85 */
86 #if !DETECT_OS_HAIKU
87 static inline void
88 debug_printf(const char *format, ...) _util_printf_format(1,2);
89
90 static inline void
91 debug_printf(const char *format, ...)
92 {
93 #ifdef DEBUG
94 va_list ap;
95 va_start(ap, format);
96 _debug_vprintf(format, ap);
97 va_end(ap);
98 #else
99 (void) format; /* silence warning */
100 #endif
101 }
102 #endif
103
104
105 /*
106 * ... isn't portable so we need to pass arguments in parentheses.
107 *
108 * usage:
109 * debug_printf_once(("answer: %i\n", 42));
110 */
111 #define debug_printf_once(args) \
112 do { \
113 static bool once = true; \
114 if (once) { \
115 once = false; \
116 debug_printf args; \
117 } \
118 } while (0)
119
120
121 #ifdef DEBUG
122 #define debug_vprintf(_format, _ap) _debug_vprintf(_format, _ap)
123 #else
124 #define debug_vprintf(_format, _ap) ((void)0)
125 #endif
126
127
128 #ifdef DEBUG
129 /**
130 * Dump a blob in hex to the same place that debug_printf sends its
131 * messages.
132 */
133 void debug_print_blob( const char *name, const void *blob, unsigned size );
134 #else
135 #define debug_print_blob(_name, _blob, _size) ((void)0)
136 #endif
137
138
139 /**
140 * Disable interactive error message boxes.
141 *
142 * Should be called as soon as possible for effectiveness.
143 */
144 void
145 debug_disable_error_message_boxes(void);
146
147
148 /**
149 * Hard-coded breakpoint.
150 */
151 #ifdef DEBUG
152 #define debug_break() os_break()
153 #else /* !DEBUG */
154 #define debug_break() ((void)0)
155 #endif /* !DEBUG */
156
157
158 long
159 debug_get_num_option(const char *name, long dfault);
160
161 #ifdef _MSC_VER
162 __declspec(noreturn)
163 #endif
164 void _debug_assert_fail(const char *expr,
165 const char *file,
166 unsigned line,
167 const char *function)
168 #if defined(__GNUC__) && !defined(DEBUG)
169 __attribute__((noreturn))
170 #endif
171 ;
172
173
174 /**
175 * Assert macro
176 *
177 * Do not expect that the assert call terminates -- errors must be handled
178 * regardless of assert behavior.
179 *
180 * For non debug builds the assert macro will expand to a no-op, so do not
181 * call functions with side effects in the assert expression.
182 */
183 #ifndef NDEBUG
184 #define debug_assert(expr) ((expr) ? (void)0 : _debug_assert_fail(#expr, __FILE__, __LINE__, __FUNCTION__))
185 #else
186 #define debug_assert(expr) (void)(0 && (expr))
187 #endif
188
189
190 /** Override standard assert macro */
191 #ifdef assert
192 #undef assert
193 #endif
194 #define assert(expr) debug_assert(expr)
195
196
197 /**
198 * Output the current function name.
199 */
200 #ifdef DEBUG
201 #define debug_checkpoint() \
202 _debug_printf("%s\n", __FUNCTION__)
203 #else
204 #define debug_checkpoint() \
205 ((void)0)
206 #endif
207
208
209 /**
210 * Output the full source code position.
211 */
212 #ifdef DEBUG
213 #define debug_checkpoint_full() \
214 _debug_printf("%s:%u:%s\n", __FILE__, __LINE__, __FUNCTION__)
215 #else
216 #define debug_checkpoint_full() \
217 ((void)0)
218 #endif
219
220
221 /**
222 * Output a warning message. Muted on release version.
223 */
224 #ifdef DEBUG
225 #define debug_warning(__msg) \
226 _debug_printf("%s:%u:%s: warning: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
227 #else
228 #define debug_warning(__msg) \
229 ((void)0)
230 #endif
231
232
233 /**
234 * Emit a warning message, but only once.
235 */
236 #ifdef DEBUG
237 #define debug_warn_once(__msg) \
238 do { \
239 static bool warned = false; \
240 if (!warned) { \
241 _debug_printf("%s:%u:%s: one time warning: %s\n", \
242 __FILE__, __LINE__, __FUNCTION__, __msg); \
243 warned = true; \
244 } \
245 } while (0)
246 #else
247 #define debug_warn_once(__msg) \
248 ((void)0)
249 #endif
250
251
252 /**
253 * Output an error message. Not muted on release version.
254 */
255 #ifdef DEBUG
256 #define debug_error(__msg) \
257 _debug_printf("%s:%u:%s: error: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
258 #else
259 #define debug_error(__msg) \
260 _debug_printf("error: %s\n", __msg)
261 #endif
262
263 /**
264 * Output a debug log message to the debug info callback.
265 */
266 #define pipe_debug_message(cb, type, fmt, ...) do { \
267 static unsigned id = 0; \
268 if ((cb) && (cb)->debug_message) { \
269 _pipe_debug_message(cb, &id, \
270 PIPE_DEBUG_TYPE_ ## type, \
271 fmt, ##__VA_ARGS__); \
272 } \
273 } while (0)
274
275 struct pipe_debug_callback;
276
277 void
278 _pipe_debug_message(
279 struct pipe_debug_callback *cb,
280 unsigned *id,
281 enum pipe_debug_type type,
282 const char *fmt, ...) _util_printf_format(4, 5);
283
284
285 /**
286 * Used by debug_dump_enum and debug_dump_flags to describe symbols.
287 */
288 struct debug_named_value
289 {
290 const char *name;
291 uint64_t value;
292 const char *desc;
293 };
294
295
296 /**
297 * Some C pre-processor magic to simplify creating named values.
298 *
299 * Example:
300 * @code
301 * static const debug_named_value my_names[] = {
302 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_X),
303 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Y),
304 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Z),
305 * DEBUG_NAMED_VALUE_END
306 * };
307 *
308 * ...
309 * debug_printf("%s = %s\n",
310 * name,
311 * debug_dump_enum(my_names, my_value));
312 * ...
313 * @endcode
314 */
315 #define DEBUG_NAMED_VALUE(__symbol) {#__symbol, (unsigned long)__symbol, NULL}
316 #define DEBUG_NAMED_VALUE_WITH_DESCRIPTION(__symbol, __desc) {#__symbol, (unsigned long)__symbol, __desc}
317 #define DEBUG_NAMED_VALUE_END {NULL, 0, NULL}
318
319
320 /**
321 * Convert a enum value to a string.
322 */
323 const char *
324 debug_dump_enum(const struct debug_named_value *names,
325 unsigned long value);
326
327 const char *
328 debug_dump_enum_noprefix(const struct debug_named_value *names,
329 const char *prefix,
330 unsigned long value);
331
332
333 /**
334 * Convert binary flags value to a string.
335 */
336 const char *
337 debug_dump_flags(const struct debug_named_value *names,
338 unsigned long value);
339
340
341 /**
342 * Function enter exit loggers
343 */
344 #ifdef DEBUG
345 int debug_funclog_enter(const char* f, const int line, const char* file);
346 void debug_funclog_exit(const char* f, const int line, const char* file);
347 void debug_funclog_enter_exit(const char* f, const int line, const char* file);
348
349 #define DEBUG_FUNCLOG_ENTER() \
350 int __debug_decleration_work_around = \
351 debug_funclog_enter(__FUNCTION__, __LINE__, __FILE__)
352 #define DEBUG_FUNCLOG_EXIT() \
353 do { \
354 (void)__debug_decleration_work_around; \
355 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
356 return; \
357 } while(0)
358 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
359 do { \
360 (void)__debug_decleration_work_around; \
361 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
362 return ret; \
363 } while(0)
364 #define DEBUG_FUNCLOG_ENTER_EXIT() \
365 debug_funclog_enter_exit(__FUNCTION__, __LINE__, __FILE__)
366
367 #else
368 #define DEBUG_FUNCLOG_ENTER() \
369 int __debug_decleration_work_around
370 #define DEBUG_FUNCLOG_EXIT() \
371 do { (void)__debug_decleration_work_around; return; } while(0)
372 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
373 do { (void)__debug_decleration_work_around; return ret; } while(0)
374 #define DEBUG_FUNCLOG_ENTER_EXIT()
375 #endif
376
377
378 /**
379 * Get option.
380 *
381 * It is an alias for getenv on Linux.
382 *
383 * On Windows it reads C:\gallium.cfg, which is a text file with CR+LF line
384 * endings with one option per line as
385 *
386 * NAME=value
387 *
388 * This file must be terminated with an extra empty line.
389 */
390 const char *
391 debug_get_option(const char *name, const char *dfault);
392
393 bool
394 debug_get_bool_option(const char *name, bool dfault);
395
396 long
397 debug_get_num_option(const char *name, long dfault);
398
399 uint64_t
400 debug_get_flags_option(const char *name,
401 const struct debug_named_value *flags,
402 uint64_t dfault);
403
404 #define DEBUG_GET_ONCE_OPTION(suffix, name, dfault) \
405 static const char * \
406 debug_get_option_ ## suffix (void) \
407 { \
408 static bool first = true; \
409 static const char * value; \
410 if (first) { \
411 first = false; \
412 value = debug_get_option(name, dfault); \
413 } \
414 return value; \
415 }
416
417 #define DEBUG_GET_ONCE_BOOL_OPTION(sufix, name, dfault) \
418 static bool \
419 debug_get_option_ ## sufix (void) \
420 { \
421 static bool first = true; \
422 static bool value; \
423 if (first) { \
424 first = false; \
425 value = debug_get_bool_option(name, dfault); \
426 } \
427 return value; \
428 }
429
430 #define DEBUG_GET_ONCE_NUM_OPTION(sufix, name, dfault) \
431 static long \
432 debug_get_option_ ## sufix (void) \
433 { \
434 static bool first = true; \
435 static long value; \
436 if (first) { \
437 first = false; \
438 value = debug_get_num_option(name, dfault); \
439 } \
440 return value; \
441 }
442
443 #define DEBUG_GET_ONCE_FLAGS_OPTION(sufix, name, flags, dfault) \
444 static unsigned long \
445 debug_get_option_ ## sufix (void) \
446 { \
447 static bool first = true; \
448 static unsigned long value; \
449 if (first) { \
450 first = false; \
451 value = debug_get_flags_option(name, flags, dfault); \
452 } \
453 return value; \
454 }
455
456
457 #ifdef __cplusplus
458 }
459 #endif
460
461 #endif /* U_DEBUG_H_ */