99007039a7c6b08e0158426f3d94c7a5b08e5d7d
[mesa.git] / src / gallium / auxiliary / 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 "os/os_misc.h"
43
44 #include "pipe/p_format.h"
45 #include "pipe/p_defines.h"
46
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52
53 #if defined(__GNUC__)
54 #define _util_printf_format(fmt, list) __attribute__ ((format (printf, fmt, list)))
55 #else
56 #define _util_printf_format(fmt, list)
57 #endif
58
59 void _debug_vprintf(const char *format, va_list ap);
60
61
62 static inline void
63 _debug_printf(const char *format, ...)
64 {
65 va_list ap;
66 va_start(ap, format);
67 _debug_vprintf(format, ap);
68 va_end(ap);
69 }
70
71
72 /**
73 * Print debug messages.
74 *
75 * The actual channel used to output debug message is platform specific. To
76 * avoid misformating or truncation, follow these rules of thumb:
77 * - output whole lines
78 * - avoid outputing large strings (512 bytes is the current maximum length
79 * that is guaranteed to be printed in all platforms)
80 */
81 #if !defined(PIPE_OS_HAIKU)
82 static inline void
83 debug_printf(const char *format, ...) _util_printf_format(1,2);
84
85 static inline void
86 debug_printf(const char *format, ...)
87 {
88 #ifdef DEBUG
89 va_list ap;
90 va_start(ap, format);
91 _debug_vprintf(format, ap);
92 va_end(ap);
93 #else
94 (void) format; /* silence warning */
95 #endif
96 }
97 #else /* is Haiku */
98 /* Haiku provides debug_printf in libroot with OS.h */
99 #include <OS.h>
100 #endif
101
102
103 /*
104 * ... isn't portable so we need to pass arguments in parentheses.
105 *
106 * usage:
107 * debug_printf_once(("answer: %i\n", 42));
108 */
109 #define debug_printf_once(args) \
110 do { \
111 static boolean once = TRUE; \
112 if (once) { \
113 once = FALSE; \
114 debug_printf args; \
115 } \
116 } while (0)
117
118
119 #ifdef DEBUG
120 #define debug_vprintf(_format, _ap) _debug_vprintf(_format, _ap)
121 #else
122 #define debug_vprintf(_format, _ap) ((void)0)
123 #endif
124
125
126 #ifdef DEBUG
127 /**
128 * Dump a blob in hex to the same place that debug_printf sends its
129 * messages.
130 */
131 void debug_print_blob( const char *name, const void *blob, unsigned size );
132
133 /* Print a message along with a prettified format string
134 */
135 void debug_print_format(const char *msg, unsigned fmt );
136 #else
137 #define debug_print_blob(_name, _blob, _size) ((void)0)
138 #define debug_print_format(_msg, _fmt) ((void)0)
139 #endif
140
141
142 /**
143 * Disable interactive error message boxes.
144 *
145 * Should be called as soon as possible for effectiveness.
146 */
147 void
148 debug_disable_error_message_boxes(void);
149
150
151 /**
152 * Hard-coded breakpoint.
153 */
154 #ifdef DEBUG
155 #define debug_break() os_break()
156 #else /* !DEBUG */
157 #define debug_break() ((void)0)
158 #endif /* !DEBUG */
159
160
161 long
162 debug_get_num_option(const char *name, long dfault);
163
164 #ifdef _MSC_VER
165 __declspec(noreturn)
166 #endif
167 void _debug_assert_fail(const char *expr,
168 const char *file,
169 unsigned line,
170 const char *function)
171 #ifdef __GNUC__
172 __attribute__((__noreturn__))
173 #endif
174 ;
175
176
177 /**
178 * Assert macro
179 *
180 * Do not expect that the assert call terminates -- errors must be handled
181 * regardless of assert behavior.
182 *
183 * For non debug builds the assert macro will expand to a no-op, so do not
184 * call functions with side effects in the assert expression.
185 */
186 #ifdef DEBUG
187 #define debug_assert(expr) ((expr) ? (void)0 : _debug_assert_fail(#expr, __FILE__, __LINE__, __FUNCTION__))
188 #else
189 #define debug_assert(expr) (void)(0 && (expr))
190 #endif
191
192
193 /** Override standard assert macro */
194 #ifdef assert
195 #undef assert
196 #endif
197 #define assert(expr) debug_assert(expr)
198
199
200 /**
201 * Output the current function name.
202 */
203 #ifdef DEBUG
204 #define debug_checkpoint() \
205 _debug_printf("%s\n", __FUNCTION__)
206 #else
207 #define debug_checkpoint() \
208 ((void)0)
209 #endif
210
211
212 /**
213 * Output the full source code position.
214 */
215 #ifdef DEBUG
216 #define debug_checkpoint_full() \
217 _debug_printf("%s:%u:%s\n", __FILE__, __LINE__, __FUNCTION__)
218 #else
219 #define debug_checkpoint_full() \
220 ((void)0)
221 #endif
222
223
224 /**
225 * Output a warning message. Muted on release version.
226 */
227 #ifdef DEBUG
228 #define debug_warning(__msg) \
229 _debug_printf("%s:%u:%s: warning: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
230 #else
231 #define debug_warning(__msg) \
232 ((void)0)
233 #endif
234
235
236 /**
237 * Emit a warning message, but only once.
238 */
239 #ifdef DEBUG
240 #define debug_warn_once(__msg) \
241 do { \
242 static bool warned = FALSE; \
243 if (!warned) { \
244 _debug_printf("%s:%u:%s: one time warning: %s\n", \
245 __FILE__, __LINE__, __FUNCTION__, __msg); \
246 warned = TRUE; \
247 } \
248 } while (0)
249 #else
250 #define debug_warn_once(__msg) \
251 ((void)0)
252 #endif
253
254
255 /**
256 * Output an error message. Not muted on release version.
257 */
258 #ifdef DEBUG
259 #define debug_error(__msg) \
260 _debug_printf("%s:%u:%s: error: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
261 #else
262 #define debug_error(__msg) \
263 _debug_printf("error: %s\n", __msg)
264 #endif
265
266 /**
267 * Output a debug log message to the debug info callback.
268 */
269 #define pipe_debug_message(cb, type, fmt, ...) do { \
270 static unsigned id = 0; \
271 _pipe_debug_message(cb, &id, \
272 PIPE_DEBUG_TYPE_ ## type, \
273 fmt, ##__VA_ARGS__); \
274 } while (0)
275
276 struct pipe_debug_callback;
277
278 void
279 _pipe_debug_message(
280 struct pipe_debug_callback *cb,
281 unsigned *id,
282 enum pipe_debug_type type,
283 const char *fmt, ...) _util_printf_format(4, 5);
284
285
286 /**
287 * Used by debug_dump_enum and debug_dump_flags to describe symbols.
288 */
289 struct debug_named_value
290 {
291 const char *name;
292 uint64_t value;
293 const char *desc;
294 };
295
296
297 /**
298 * Some C pre-processor magic to simplify creating named values.
299 *
300 * Example:
301 * @code
302 * static const debug_named_value my_names[] = {
303 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_X),
304 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Y),
305 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Z),
306 * DEBUG_NAMED_VALUE_END
307 * };
308 *
309 * ...
310 * debug_printf("%s = %s\n",
311 * name,
312 * debug_dump_enum(my_names, my_value));
313 * ...
314 * @endcode
315 */
316 #define DEBUG_NAMED_VALUE(__symbol) {#__symbol, (unsigned long)__symbol, NULL}
317 #define DEBUG_NAMED_VALUE_WITH_DESCRIPTION(__symbol, __desc) {#__symbol, (unsigned long)__symbol, __desc}
318 #define DEBUG_NAMED_VALUE_END {NULL, 0, NULL}
319
320
321 /**
322 * Convert a enum value to a string.
323 */
324 const char *
325 debug_dump_enum(const struct debug_named_value *names,
326 unsigned long value);
327
328 const char *
329 debug_dump_enum_noprefix(const struct debug_named_value *names,
330 const char *prefix,
331 unsigned long value);
332
333
334 /**
335 * Convert binary flags value to a string.
336 */
337 const char *
338 debug_dump_flags(const struct debug_named_value *names,
339 unsigned long value);
340
341
342 /**
343 * Function enter exit loggers
344 */
345 #ifdef DEBUG
346 int debug_funclog_enter(const char* f, const int line, const char* file);
347 void debug_funclog_exit(const char* f, const int line, const char* file);
348 void debug_funclog_enter_exit(const char* f, const int line, const char* file);
349
350 #define DEBUG_FUNCLOG_ENTER() \
351 int __debug_decleration_work_around = \
352 debug_funclog_enter(__FUNCTION__, __LINE__, __FILE__)
353 #define DEBUG_FUNCLOG_EXIT() \
354 do { \
355 (void)__debug_decleration_work_around; \
356 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
357 return; \
358 } while(0)
359 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
360 do { \
361 (void)__debug_decleration_work_around; \
362 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
363 return ret; \
364 } while(0)
365 #define DEBUG_FUNCLOG_ENTER_EXIT() \
366 debug_funclog_enter_exit(__FUNCTION__, __LINE__, __FILE__)
367
368 #else
369 #define DEBUG_FUNCLOG_ENTER() \
370 int __debug_decleration_work_around
371 #define DEBUG_FUNCLOG_EXIT() \
372 do { (void)__debug_decleration_work_around; return; } while(0)
373 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
374 do { (void)__debug_decleration_work_around; return ret; } while(0)
375 #define DEBUG_FUNCLOG_ENTER_EXIT()
376 #endif
377
378
379 /**
380 * Get option.
381 *
382 * It is an alias for getenv on Linux.
383 *
384 * On Windows it reads C:\gallium.cfg, which is a text file with CR+LF line
385 * endings with one option per line as
386 *
387 * NAME=value
388 *
389 * This file must be terminated with an extra empty line.
390 */
391 const char *
392 debug_get_option(const char *name, const char *dfault);
393
394 boolean
395 debug_get_bool_option(const char *name, boolean dfault);
396
397 long
398 debug_get_num_option(const char *name, long dfault);
399
400 uint64_t
401 debug_get_flags_option(const char *name,
402 const struct debug_named_value *flags,
403 uint64_t dfault);
404
405 #define DEBUG_GET_ONCE_BOOL_OPTION(sufix, name, dfault) \
406 static boolean \
407 debug_get_option_ ## sufix (void) \
408 { \
409 static boolean first = TRUE; \
410 static boolean value; \
411 if (first) { \
412 first = FALSE; \
413 value = debug_get_bool_option(name, dfault); \
414 } \
415 return value; \
416 }
417
418 #define DEBUG_GET_ONCE_NUM_OPTION(sufix, name, dfault) \
419 static long \
420 debug_get_option_ ## sufix (void) \
421 { \
422 static boolean first = TRUE; \
423 static long value; \
424 if (first) { \
425 first = FALSE; \
426 value = debug_get_num_option(name, dfault); \
427 } \
428 return value; \
429 }
430
431 #define DEBUG_GET_ONCE_FLAGS_OPTION(sufix, name, flags, dfault) \
432 static unsigned long \
433 debug_get_option_ ## sufix (void) \
434 { \
435 static boolean first = TRUE; \
436 static unsigned long value; \
437 if (first) { \
438 first = FALSE; \
439 value = debug_get_flags_option(name, flags, dfault); \
440 } \
441 return value; \
442 }
443
444
445 unsigned long
446 debug_memory_begin(void);
447
448 void
449 debug_memory_end(unsigned long beginning);
450
451
452 #ifdef DEBUG
453 struct pipe_context;
454 struct pipe_surface;
455 struct pipe_transfer;
456 struct pipe_resource;
457
458 void debug_dump_image(const char *prefix,
459 enum pipe_format format, unsigned cpp,
460 unsigned width, unsigned height,
461 unsigned stride,
462 const void *data);
463 void debug_dump_surface(struct pipe_context *pipe,
464 const char *prefix,
465 struct pipe_surface *surface);
466 void debug_dump_texture(struct pipe_context *pipe,
467 const char *prefix,
468 struct pipe_resource *texture);
469 void debug_dump_surface_bmp(struct pipe_context *pipe,
470 const char *filename,
471 struct pipe_surface *surface);
472 void debug_dump_transfer_bmp(struct pipe_context *pipe,
473 const char *filename,
474 struct pipe_transfer *transfer, void *ptr);
475 void debug_dump_float_rgba_bmp(const char *filename,
476 unsigned width, unsigned height,
477 float *rgba, unsigned stride);
478 #else
479 #define debug_dump_image(prefix, format, cpp, width, height, stride, data) ((void)0)
480 #define debug_dump_surface(pipe, prefix, surface) ((void)0)
481 #define debug_dump_surface_bmp(pipe, filename, surface) ((void)0)
482 #define debug_dump_transfer_bmp(filename, transfer, ptr) ((void)0)
483 #define debug_dump_float_rgba_bmp(filename, width, height, rgba, stride) ((void)0)
484 #endif
485
486
487 void
488 debug_print_transfer_flags(const char *msg, unsigned usage);
489
490 void
491 debug_print_bind_flags(const char *msg, unsigned usage);
492
493 void
494 debug_print_usage_enum(const char *msg, unsigned usage);
495
496
497 #ifdef __cplusplus
498 }
499 #endif
500
501 #endif /* U_DEBUG_H_ */