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>
44 #include "pipe/p_compiler.h"
52 #if defined(DBG) || defined(DEBUG)
63 /* MSVC bebore VC7 does not have the __FUNCTION__ macro */
64 #if defined(_MSC_VER) && _MSC_VER < 1300
65 #define __FUNCTION__ "???"
69 #define _util_printf_format(fmt, list) __attribute__ ((format (printf, fmt, list)))
71 #define _util_printf_format(fmt, list)
74 void _debug_vprintf(const char *format
, va_list ap
);
78 _debug_printf(const char *format
, ...)
82 _debug_vprintf(format
, ap
);
88 * Print debug messages.
90 * The actual channel used to output debug message is platform specific. To
91 * avoid misformating or truncation, follow these rules of thumb:
92 * - output whole lines
93 * - avoid outputing large strings (512 bytes is the current maximum length
94 * that is guaranteed to be printed in all platforms)
96 #if !defined(PIPE_OS_HAIKU)
98 debug_printf(const char *format
, ...) _util_printf_format(1,2);
101 debug_printf(const char *format
, ...)
105 va_start(ap
, format
);
106 _debug_vprintf(format
, ap
);
109 (void) format
; /* silence warning */
113 #endif /* !PIPE_OS_HAIKU */
116 * ... isn't portable so we need to pass arguments in parentheses.
119 * debug_printf_once(("awnser: %i\n", 42));
121 #define debug_printf_once(args) \
123 static boolean once = TRUE; \
132 #define debug_vprintf(_format, _ap) _debug_vprintf(_format, _ap)
134 #define debug_vprintf(_format, _ap) ((void)0)
140 * Dump a blob in hex to the same place that debug_printf sends its
143 void debug_print_blob( const char *name
, const void *blob
, unsigned size
);
145 /* Print a message along with a prettified format string
147 void debug_print_format(const char *msg
, unsigned fmt
);
149 #define debug_print_blob(_name, _blob, _size) ((void)0)
150 #define debug_print_format(_msg, _fmt) ((void)0)
155 * Hard-coded breakpoint.
158 #if (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)) && defined(PIPE_CC_GCC)
159 #define debug_break() __asm("int3")
160 #elif defined(PIPE_CC_MSVC)
161 #define debug_break() __debugbreak()
163 void debug_break(void);
166 #define debug_break() ((void)0)
171 debug_get_num_option(const char *name
, long dfault
);
173 void _debug_assert_fail(const char *expr
,
176 const char *function
);
182 * Do not expect that the assert call terminates -- errors must be handled
183 * regardless of assert behavior.
185 * For non debug builds the assert macro will expand to a no-op, so do not
186 * call functions with side effects in the assert expression.
189 #define debug_assert(expr) ((expr) ? (void)0 : _debug_assert_fail(#expr, __FILE__, __LINE__, __FUNCTION__))
191 #define debug_assert(expr) ((void)0)
195 /** Override standard assert macro */
199 #define assert(expr) debug_assert(expr)
203 * Output the current function name.
206 #define debug_checkpoint() \
207 _debug_printf("%s\n", __FUNCTION__)
209 #define debug_checkpoint() \
215 * Output the full source code position.
218 #define debug_checkpoint_full() \
219 _debug_printf("%s:%u:%s", __FILE__, __LINE__, __FUNCTION__)
221 #define debug_checkpoint_full() \
227 * Output a warning message. Muted on release version.
230 #define debug_warning(__msg) \
231 _debug_printf("%s:%u:%s: warning: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
233 #define debug_warning(__msg) \
239 * Output an error message. Not muted on release version.
242 #define debug_error(__msg) \
243 _debug_printf("%s:%u:%s: error: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
245 #define debug_error(__msg) \
246 _debug_printf("error: %s\n", __msg)
251 * Used by debug_dump_enum and debug_dump_flags to describe symbols.
253 struct debug_named_value
261 * Some C pre-processor magic to simplify creating named values.
265 * static const debug_named_value my_names[] = {
266 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_X),
267 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Y),
268 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Z),
269 * DEBUG_NAMED_VALUE_END
273 * debug_printf("%s = %s\n",
275 * debug_dump_enum(my_names, my_value));
279 #define DEBUG_NAMED_VALUE(__symbol) {#__symbol, (unsigned long)__symbol}
280 #define DEBUG_NAMED_VALUE_END {NULL, 0}
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
);
307 * It is an alias for getenv on Linux.
309 * On Windows it reads C:\gallium.cfg, which is a text file with CR+LF line
310 * endings with one option per line as
314 * This file must be terminated with an extra empty line.
317 debug_get_option(const char *name
, const char *dfault
);
320 debug_get_bool_option(const char *name
, boolean dfault
);
323 debug_get_num_option(const char *name
, long dfault
);
326 debug_get_flags_option(const char *name
,
327 const struct debug_named_value
*flags
,
328 unsigned long dfault
);
332 debug_malloc(const char *file
, unsigned line
, const char *function
,
336 debug_free(const char *file
, unsigned line
, const char *function
,
340 debug_calloc(const char *file
, unsigned line
, const char *function
,
341 size_t count
, size_t size
);
344 debug_realloc(const char *file
, unsigned line
, const char *function
,
345 void *old_ptr
, size_t old_size
, size_t new_size
);
348 debug_memory_begin(void);
351 debug_memory_end(unsigned long beginning
);
354 #if defined(PROFILE) && defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
357 debug_profile_start(void);
360 debug_profile_stop(void);
367 struct pipe_transfer
;
368 void debug_dump_image(const char *prefix
,
369 unsigned format
, unsigned cpp
,
370 unsigned width
, unsigned height
,
373 void debug_dump_surface(const char *prefix
,
374 struct pipe_surface
*surface
);
375 void debug_dump_surface_bmp(const char *filename
,
376 struct pipe_surface
*surface
);
377 void debug_dump_transfer_bmp(const char *filename
,
378 struct pipe_transfer
*transfer
);
379 void debug_dump_float_rgba_bmp(const char *filename
,
380 unsigned width
, unsigned height
,
381 float *rgba
, unsigned stride
);
383 #define debug_dump_image(prefix, format, cpp, width, height, stride, data) ((void)0)
384 #define debug_dump_surface(prefix, surface) ((void)0)
385 #define debug_dump_surface_bmp(filename, surface) ((void)0)
386 #define debug_dump_transfer_bmp(filename, transfer) ((void)0)
387 #define debug_dump_rgba_float_bmp(filename, width, height, rgba, stride) ((void)0)
395 #endif /* U_DEBUG_H_ */