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 "p_compiler.h"
63 void _debug_vprintf(const char *format
, va_list ap
);
67 _debug_printf(const char *format
, ...)
71 _debug_vprintf(format
, ap
);
77 * Print debug messages.
79 * The actual channel used to output debug message is platform specific. To
80 * avoid misformating or truncation, follow these rules of thumb:
81 * - output whole lines
82 * - avoid outputing large strings (512 bytes is the current maximum length
83 * that is guaranteed to be printed in all platforms)
86 debug_printf(const char *format
, ...)
91 _debug_vprintf(format
, ap
);
94 (void) format
; /* silence warning */
100 #define debug_vprintf(_format, _ap) _debug_vprintf(_format, _ap)
102 #define debug_vprintf(_format, _ap) ((void)0)
107 * Dump a blob in hex to the same place that debug_printf sends its
111 void debug_print_blob( const char *name
,
115 #define debug_print_blob(_name, _blob, _size) ((void)0)
119 void _debug_break(void);
123 * Hard-coded breakpoint.
126 #if (defined(__i386__) || defined(__386__)) && defined(__GNUC__)
127 #define debug_break() __asm("int3")
128 #elif (defined(__i386__) || defined(__386__)) && defined(__MSC__)
129 #define debug_break() _asm {int 3}
131 #define debug_break() _debug_break()
134 #define debug_break() ((void)0)
139 debug_get_num_option(const char *name
, long dfault
);
141 void _debug_assert_fail(const char *expr
,
144 const char *function
);
150 * Do not expect that the assert call terminates -- errors must be handled
151 * regardless of assert behavior.
154 #define debug_assert(expr) ((expr) ? (void)0 : _debug_assert_fail(#expr, __FILE__, __LINE__, __FUNCTION__))
156 #define debug_assert(expr) ((void)0)
160 /** Override standard assert macro */
164 #define assert(expr) debug_assert(expr)
168 * Output the current function name.
171 #define debug_checkpoint() \
172 _debug_printf("%s\n", __FUNCTION__)
174 #define debug_checkpoint() \
180 * Output the full source code position.
183 #define debug_checkpoint_full() \
184 _debug_printf("%s:%u:%s", __FILE__, __LINE__, __FUNCTION__)
186 #define debug_checkpoint_full() \
192 * Output a warning message. Muted on release version.
195 #define debug_warning(__msg) \
196 _debug_printf("%s:%u:%s: warning: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
198 #define debug_warning(__msg) \
204 * Output an error message. Not muted on release version.
207 #define debug_error(__msg) \
208 _debug_printf("%s:%u:%s: error: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
210 #define debug_error(__msg) \
211 _debug_printf("error: %s\n", __msg)
216 * Used by debug_dump_enum and debug_dump_flags to describe symbols.
218 struct debug_named_value
226 * Some C pre-processor magic to simplify creating named values.
230 * static const debug_named_value my_names[] = {
231 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_X),
232 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Y),
233 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Z),
234 * DEBUG_NAMED_VALUE_END
238 * debug_printf("%s = %s\n",
240 * debug_dump_enum(my_names, my_value));
244 #define DEBUG_NAMED_VALUE(__symbol) {#__symbol, (unsigned long)__symbol}
245 #define DEBUG_NAMED_VALUE_END {NULL, 0}
249 * Convert a enum value to a string.
252 debug_dump_enum(const struct debug_named_value
*names
,
253 unsigned long value
);
257 * Convert binary flags value to a string.
260 debug_dump_flags(const struct debug_named_value
*names
,
261 unsigned long value
);
267 * It is an alias for getenv on Linux.
269 * On Windows it reads C:\gallium.cfg, which is a text file with CR+LF line
270 * endings with one option per line as
274 * This file must be terminated with an extra empty line.
277 debug_get_option(const char *name
, const char *dfault
);
280 debug_get_bool_option(const char *name
, boolean dfault
);
283 debug_get_unsigned_option(const char *name
, long dfault
);
286 debug_get_flags_option(const char *name
,
287 const struct debug_named_value
*flags
,
288 unsigned long dfault
);
292 debug_malloc(const char *file
, unsigned line
, const char *function
,
296 debug_free(const char *file
, unsigned line
, const char *function
,
300 debug_calloc(const char *file
, unsigned line
, const char *function
,
301 size_t count
, size_t size
);
304 debug_realloc(const char *file
, unsigned line
, const char *function
,
305 void *old_ptr
, size_t old_size
, size_t new_size
);
308 debug_memory_reset(void);
311 debug_memory_report(void);
318 #endif /* P_DEBUG_H_ */