1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Cross-platform debugging helpers.
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.
35 * @author Jose Fonseca <jfonseca@vmware.com>
42 #include "os/os_misc.h"
44 #include "pipe/p_format.h"
45 #include "pipe/p_defines.h"
54 #define _util_printf_format(fmt, list) __attribute__ ((format (printf, fmt, list)))
56 #define _util_printf_format(fmt, list)
59 void _debug_vprintf(const char *format
, va_list ap
);
63 _debug_printf(const char *format
, ...)
67 _debug_vprintf(format
, ap
);
73 * Print debug messages.
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)
81 #if !defined(PIPE_OS_HAIKU)
83 debug_printf(const char *format
, ...) _util_printf_format(1,2);
86 debug_printf(const char *format
, ...)
91 _debug_vprintf(format
, ap
);
94 (void) format
; /* silence warning */
98 /* Haiku provides debug_printf in libroot with OS.h */
104 * ... isn't portable so we need to pass arguments in parentheses.
107 * debug_printf_once(("answer: %i\n", 42));
109 #define debug_printf_once(args) \
111 static boolean once = TRUE; \
120 #define debug_vprintf(_format, _ap) _debug_vprintf(_format, _ap)
122 #define debug_vprintf(_format, _ap) ((void)0)
128 * Dump a blob in hex to the same place that debug_printf sends its
131 void debug_print_blob( const char *name
, const void *blob
, unsigned size
);
133 /* Print a message along with a prettified format string
135 void debug_print_format(const char *msg
, unsigned fmt
);
137 #define debug_print_blob(_name, _blob, _size) ((void)0)
138 #define debug_print_format(_msg, _fmt) ((void)0)
143 * Disable interactive error message boxes.
145 * Should be called as soon as possible for effectiveness.
148 debug_disable_error_message_boxes(void);
152 * Hard-coded breakpoint.
155 #define debug_break() os_break()
157 #define debug_break() ((void)0)
162 debug_get_num_option(const char *name
, long dfault
);
167 void _debug_assert_fail(const char *expr
,
170 const char *function
)
172 __attribute__((__noreturn__
))
180 * Do not expect that the assert call terminates -- errors must be handled
181 * regardless of assert behavior.
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.
187 #define debug_assert(expr) ((expr) ? (void)0 : _debug_assert_fail(#expr, __FILE__, __LINE__, __FUNCTION__))
189 #define debug_assert(expr) (void)(0 && (expr))
193 /** Override standard assert macro */
197 #define assert(expr) debug_assert(expr)
201 * Output the current function name.
204 #define debug_checkpoint() \
205 _debug_printf("%s\n", __FUNCTION__)
207 #define debug_checkpoint() \
213 * Output the full source code position.
216 #define debug_checkpoint_full() \
217 _debug_printf("%s:%u:%s\n", __FILE__, __LINE__, __FUNCTION__)
219 #define debug_checkpoint_full() \
225 * Output a warning message. Muted on release version.
228 #define debug_warning(__msg) \
229 _debug_printf("%s:%u:%s: warning: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
231 #define debug_warning(__msg) \
237 * Emit a warning message, but only once.
240 #define debug_warn_once(__msg) \
242 static bool warned = FALSE; \
244 _debug_printf("%s:%u:%s: one time warning: %s\n", \
245 __FILE__, __LINE__, __FUNCTION__, __msg); \
250 #define debug_warn_once(__msg) \
256 * Output an error message. Not muted on release version.
259 #define debug_error(__msg) \
260 _debug_printf("%s:%u:%s: error: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
262 #define debug_error(__msg) \
263 _debug_printf("error: %s\n", __msg)
267 * Output a debug log message to the debug info callback.
269 #define pipe_debug_message(cb, type, fmt, ...) do { \
270 static unsigned id = 0; \
271 if ((cb) && (cb)->debug_message) { \
272 _pipe_debug_message(cb, &id, \
273 PIPE_DEBUG_TYPE_ ## type, \
274 fmt, ##__VA_ARGS__); \
278 struct pipe_debug_callback
;
282 struct pipe_debug_callback
*cb
,
284 enum pipe_debug_type type
,
285 const char *fmt
, ...) _util_printf_format(4, 5);
289 * Used by debug_dump_enum and debug_dump_flags to describe symbols.
291 struct debug_named_value
300 * Some C pre-processor magic to simplify creating named values.
304 * static const debug_named_value my_names[] = {
305 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_X),
306 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Y),
307 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Z),
308 * DEBUG_NAMED_VALUE_END
312 * debug_printf("%s = %s\n",
314 * debug_dump_enum(my_names, my_value));
318 #define DEBUG_NAMED_VALUE(__symbol) {#__symbol, (unsigned long)__symbol, NULL}
319 #define DEBUG_NAMED_VALUE_WITH_DESCRIPTION(__symbol, __desc) {#__symbol, (unsigned long)__symbol, __desc}
320 #define DEBUG_NAMED_VALUE_END {NULL, 0, NULL}
324 * Convert a enum value to a string.
327 debug_dump_enum(const struct debug_named_value
*names
,
328 unsigned long value
);
331 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
333 unsigned long value
);
337 * Convert binary flags value to a string.
340 debug_dump_flags(const struct debug_named_value
*names
,
341 unsigned long value
);
345 * Function enter exit loggers
348 int debug_funclog_enter(const char* f
, const int line
, const char* file
);
349 void debug_funclog_exit(const char* f
, const int line
, const char* file
);
350 void debug_funclog_enter_exit(const char* f
, const int line
, const char* file
);
352 #define DEBUG_FUNCLOG_ENTER() \
353 int __debug_decleration_work_around = \
354 debug_funclog_enter(__FUNCTION__, __LINE__, __FILE__)
355 #define DEBUG_FUNCLOG_EXIT() \
357 (void)__debug_decleration_work_around; \
358 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
361 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
363 (void)__debug_decleration_work_around; \
364 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
367 #define DEBUG_FUNCLOG_ENTER_EXIT() \
368 debug_funclog_enter_exit(__FUNCTION__, __LINE__, __FILE__)
371 #define DEBUG_FUNCLOG_ENTER() \
372 int __debug_decleration_work_around
373 #define DEBUG_FUNCLOG_EXIT() \
374 do { (void)__debug_decleration_work_around; return; } while(0)
375 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
376 do { (void)__debug_decleration_work_around; return ret; } while(0)
377 #define DEBUG_FUNCLOG_ENTER_EXIT()
384 * It is an alias for getenv on Linux.
386 * On Windows it reads C:\gallium.cfg, which is a text file with CR+LF line
387 * endings with one option per line as
391 * This file must be terminated with an extra empty line.
394 debug_get_option(const char *name
, const char *dfault
);
397 debug_get_bool_option(const char *name
, boolean dfault
);
400 debug_get_num_option(const char *name
, long dfault
);
403 debug_get_flags_option(const char *name
,
404 const struct debug_named_value
*flags
,
407 #define DEBUG_GET_ONCE_OPTION(suffix, name, dfault) \
408 static const char * \
409 debug_get_option_ ## suffix (void) \
411 static boolean first = TRUE; \
412 static const char * value; \
415 value = debug_get_option(name, dfault); \
420 #define DEBUG_GET_ONCE_BOOL_OPTION(sufix, name, dfault) \
422 debug_get_option_ ## sufix (void) \
424 static boolean first = TRUE; \
425 static boolean value; \
428 value = debug_get_bool_option(name, dfault); \
433 #define DEBUG_GET_ONCE_NUM_OPTION(sufix, name, dfault) \
435 debug_get_option_ ## sufix (void) \
437 static boolean first = TRUE; \
441 value = debug_get_num_option(name, dfault); \
446 #define DEBUG_GET_ONCE_FLAGS_OPTION(sufix, name, flags, dfault) \
447 static unsigned long \
448 debug_get_option_ ## sufix (void) \
450 static boolean first = TRUE; \
451 static unsigned long value; \
454 value = debug_get_flags_option(name, flags, dfault); \
461 debug_memory_begin(void);
464 debug_memory_end(unsigned long beginning
);
470 struct pipe_transfer
;
471 struct pipe_resource
;
473 void debug_dump_image(const char *prefix
,
474 enum pipe_format format
, unsigned cpp
,
475 unsigned width
, unsigned height
,
478 void debug_dump_surface(struct pipe_context
*pipe
,
480 struct pipe_surface
*surface
);
481 void debug_dump_texture(struct pipe_context
*pipe
,
483 struct pipe_resource
*texture
);
484 void debug_dump_surface_bmp(struct pipe_context
*pipe
,
485 const char *filename
,
486 struct pipe_surface
*surface
);
487 void debug_dump_transfer_bmp(struct pipe_context
*pipe
,
488 const char *filename
,
489 struct pipe_transfer
*transfer
, void *ptr
);
490 void debug_dump_float_rgba_bmp(const char *filename
,
491 unsigned width
, unsigned height
,
492 float *rgba
, unsigned stride
);
493 void debug_dump_ubyte_rgba_bmp(const char *filename
,
494 unsigned width
, unsigned height
,
495 const ubyte
*rgba
, unsigned stride
);
497 #define debug_dump_image(prefix, format, cpp, width, height, stride, data) ((void)0)
498 #define debug_dump_surface(pipe, prefix, surface) ((void)0)
499 #define debug_dump_surface_bmp(pipe, filename, surface) ((void)0)
500 #define debug_dump_transfer_bmp(filename, transfer, ptr) ((void)0)
501 #define debug_dump_float_rgba_bmp(filename, width, height, rgba, stride) ((void)0)
502 #define debug_dump_ubyte_rgba_bmp(filename, width, height, rgba, stride) ((void)0)
507 debug_print_transfer_flags(const char *msg
, unsigned usage
);
510 debug_print_bind_flags(const char *msg
, unsigned usage
);
513 debug_print_usage_enum(const char *msg
, unsigned usage
);
520 #endif /* U_DEBUG_H_ */