1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 <jrfonseca@tungstengraphics.com>
42 #include "os/os_misc.h"
51 #define _util_printf_format(fmt, list) __attribute__ ((format (printf, fmt, list)))
53 #define _util_printf_format(fmt, list)
56 void _debug_vprintf(const char *format, va_list ap);
60 _debug_printf(const char *format, ...)
64 _debug_vprintf(format, ap);
70 * Print debug messages.
72 * The actual channel used to output debug message is platform specific. To
73 * avoid misformating or truncation, follow these rules of thumb:
74 * - output whole lines
75 * - avoid outputing large strings (512 bytes is the current maximum length
76 * that is guaranteed to be printed in all platforms)
78 #if !defined(PIPE_OS_HAIKU)
80 debug_printf(const char *format, ...) _util_printf_format(1,2);
83 debug_printf(const char *format, ...)
88 _debug_vprintf(format, ap);
91 (void) format; /* silence warning */
95 /* Haiku provides debug_printf in libroot with OS.h */
101 * ... isn't portable so we need to pass arguments in parentheses.
104 * debug_printf_once(("awnser: %i\n", 42));
106 #define debug_printf_once(args) \
108 static boolean once = TRUE; \
117 #define debug_vprintf(_format, _ap) _debug_vprintf(_format, _ap)
119 #define debug_vprintf(_format, _ap) ((void)0)
125 * Dump a blob in hex to the same place that debug_printf sends its
128 void debug_print_blob( const char *name, const void *blob, unsigned size );
130 /* Print a message along with a prettified format string
132 void debug_print_format(const char *msg, unsigned fmt );
134 #define debug_print_blob(_name, _blob, _size) ((void)0)
135 #define debug_print_format(_msg, _fmt) ((void)0)
140 * Hard-coded breakpoint.
143 #define debug_break() os_break()
145 #define debug_break() ((void)0)
150 debug_get_num_option(const char *name, long dfault);
152 void _debug_assert_fail(const char *expr,
155 const char *function);
161 * Do not expect that the assert call terminates -- errors must be handled
162 * regardless of assert behavior.
164 * For non debug builds the assert macro will expand to a no-op, so do not
165 * call functions with side effects in the assert expression.
168 #define debug_assert(expr) ((expr) ? (void)0 : _debug_assert_fail(#expr, __FILE__, __LINE__, __FUNCTION__))
170 #define debug_assert(expr) do { } while (0 && (expr))
174 /** Override standard assert macro */
178 #define assert(expr) debug_assert(expr)
182 * Output the current function name.
185 #define debug_checkpoint() \
186 _debug_printf("%s\n", __FUNCTION__)
188 #define debug_checkpoint() \
194 * Output the full source code position.
197 #define debug_checkpoint_full() \
198 _debug_printf("%s:%u:%s\n", __FILE__, __LINE__, __FUNCTION__)
200 #define debug_checkpoint_full() \
206 * Output a warning message. Muted on release version.
209 #define debug_warning(__msg) \
210 _debug_printf("%s:%u:%s: warning: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
212 #define debug_warning(__msg) \
218 * Emit a warning message, but only once.
221 #define debug_warn_once(__msg) \
223 static bool warned = FALSE; \
225 _debug_printf("%s:%u:%s: one time warning: %s\n", \
226 __FILE__, __LINE__, __FUNCTION__, __msg); \
231 #define debug_warn_once(__msg) \
237 * Output an error message. Not muted on release version.
240 #define debug_error(__msg) \
241 _debug_printf("%s:%u:%s: error: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
243 #define debug_error(__msg) \
244 _debug_printf("error: %s\n", __msg)
249 * Used by debug_dump_enum and debug_dump_flags to describe symbols.
251 struct debug_named_value
260 * Some C pre-processor magic to simplify creating named values.
264 * static const debug_named_value my_names[] = {
265 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_X),
266 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Y),
267 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Z),
268 * DEBUG_NAMED_VALUE_END
272 * debug_printf("%s = %s\n",
274 * debug_dump_enum(my_names, my_value));
278 #define DEBUG_NAMED_VALUE(__symbol) DEBUG_NAMED_VALUE_WITH_DESCRIPTION(__symbol, NULL)
279 #define DEBUG_NAMED_VALUE_WITH_DESCRIPTION(__symbol, __desc) {#__symbol, (unsigned long)__symbol, __desc}
280 #define DEBUG_NAMED_VALUE_END {NULL, 0, NULL}
284 * Convert a enum value to a string.
287 debug_dump_enum(const struct debug_named_value *names,
288 unsigned long value);
291 debug_dump_enum_noprefix(const struct debug_named_value *names,
293 unsigned long value);
297 * Convert binary flags value to a string.
300 debug_dump_flags(const struct debug_named_value *names,
301 unsigned long value);
305 * Function enter exit loggers
308 int debug_funclog_enter(const char* f, const int line, const char* file);
309 void debug_funclog_exit(const char* f, const int line, const char* file);
310 void debug_funclog_enter_exit(const char* f, const int line, const char* file);
312 #define DEBUG_FUNCLOG_ENTER() \
313 int __debug_decleration_work_around = \
314 debug_funclog_enter(__FUNCTION__, __LINE__, __FILE__)
315 #define DEBUG_FUNCLOG_EXIT() \
317 (void)__debug_decleration_work_around; \
318 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
321 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
323 (void)__debug_decleration_work_around; \
324 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
327 #define DEBUG_FUNCLOG_ENTER_EXIT() \
328 debug_funclog_enter_exit(__FUNCTION__, __LINE__, __FILE__)
331 #define DEBUG_FUNCLOG_ENTER() \
332 int __debug_decleration_work_around
333 #define DEBUG_FUNCLOG_EXIT() \
334 do { (void)__debug_decleration_work_around; return; } while(0)
335 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
336 do { (void)__debug_decleration_work_around; return ret; } while(0)
337 #define DEBUG_FUNCLOG_ENTER_EXIT()
344 * It is an alias for getenv on Linux.
346 * On Windows it reads C:\gallium.cfg, which is a text file with CR+LF line
347 * endings with one option per line as
351 * This file must be terminated with an extra empty line.
354 debug_get_option(const char *name, const char *dfault);
357 debug_get_bool_option(const char *name, boolean dfault);
360 debug_get_num_option(const char *name, long dfault);
363 debug_get_flags_option(const char *name,
364 const struct debug_named_value *flags,
365 unsigned long dfault);
367 #define DEBUG_GET_ONCE_BOOL_OPTION(sufix, name, dfault) \
369 debug_get_option_ ## sufix (void) \
371 static boolean first = TRUE; \
372 static boolean value; \
375 value = debug_get_bool_option(name, dfault); \
380 #define DEBUG_GET_ONCE_NUM_OPTION(sufix, name, dfault) \
382 debug_get_option_ ## sufix (void) \
384 static boolean first = TRUE; \
388 value = debug_get_num_option(name, dfault); \
393 #define DEBUG_GET_ONCE_FLAGS_OPTION(sufix, name, flags, dfault) \
394 static unsigned long \
395 debug_get_option_ ## sufix (void) \
397 static boolean first = TRUE; \
398 static unsigned long value; \
401 value = debug_get_flags_option(name, flags, dfault); \
408 debug_memory_begin(void);
411 debug_memory_end(unsigned long beginning);
417 struct pipe_transfer;
418 struct pipe_resource;
420 void debug_dump_image(const char *prefix,
421 unsigned format, unsigned cpp,
422 unsigned width, unsigned height,
425 void debug_dump_surface(struct pipe_context *pipe,
427 struct pipe_surface *surface);
428 void debug_dump_texture(struct pipe_context *pipe,
430 struct pipe_resource *texture);
431 void debug_dump_surface_bmp(struct pipe_context *pipe,
432 const char *filename,
433 struct pipe_surface *surface);
434 void debug_dump_transfer_bmp(struct pipe_context *pipe,
435 const char *filename,
436 struct pipe_transfer *transfer);
437 void debug_dump_float_rgba_bmp(const char *filename,
438 unsigned width, unsigned height,
439 float *rgba, unsigned stride);
441 #define debug_dump_image(prefix, format, cpp, width, height, stride, data) ((void)0)
442 #define debug_dump_surface(pipe, prefix, surface) ((void)0)
443 #define debug_dump_surface_bmp(pipe, filename, surface) ((void)0)
444 #define debug_dump_transfer_bmp(filename, transfer) ((void)0)
445 #define debug_dump_float_rgba_bmp(filename, width, height, rgba, stride) ((void)0)
453 #endif /* U_DEBUG_H_ */