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 "util/os_misc.h"
43 #include "util/detect_os.h"
46 /* Haiku provides debug_printf in libroot with OS.h */
50 #include "pipe/p_defines.h"
59 #define _util_printf_format(fmt, list) __attribute__ ((format (printf, fmt, list)))
61 #define _util_printf_format(fmt, list)
64 void _debug_vprintf(const char *format
, va_list ap
);
68 _debug_printf(const char *format
, ...)
72 _debug_vprintf(format
, ap
);
78 * Print debug messages.
80 * The actual channel used to output debug message is platform specific. To
81 * avoid misformating or truncation, follow these rules of thumb:
82 * - output whole lines
83 * - avoid outputing large strings (512 bytes is the current maximum length
84 * that is guaranteed to be printed in all platforms)
88 debug_printf(const char *format
, ...) _util_printf_format(1,2);
91 debug_printf(const char *format
, ...)
96 _debug_vprintf(format
, ap
);
99 (void) format
; /* silence warning */
106 * ... isn't portable so we need to pass arguments in parentheses.
109 * debug_printf_once(("answer: %i\n", 42));
111 #define debug_printf_once(args) \
113 static bool once = true; \
122 #define debug_vprintf(_format, _ap) _debug_vprintf(_format, _ap)
124 #define debug_vprintf(_format, _ap) ((void)0)
130 * Dump a blob in hex to the same place that debug_printf sends its
133 void debug_print_blob( const char *name
, const void *blob
, unsigned size
);
135 #define debug_print_blob(_name, _blob, _size) ((void)0)
140 * Disable interactive error message boxes.
142 * Should be called as soon as possible for effectiveness.
145 debug_disable_error_message_boxes(void);
149 * Hard-coded breakpoint.
152 #define debug_break() os_break()
154 #define debug_break() ((void)0)
159 debug_get_num_option(const char *name
, long dfault
);
164 void _debug_assert_fail(const char *expr
,
167 const char *function
)
168 #if defined(__GNUC__) && !defined(DEBUG)
169 __attribute__((noreturn
))
177 * Do not expect that the assert call terminates -- errors must be handled
178 * regardless of assert behavior.
180 * For non debug builds the assert macro will expand to a no-op, so do not
181 * call functions with side effects in the assert expression.
184 #define debug_assert(expr) ((expr) ? (void)0 : _debug_assert_fail(#expr, __FILE__, __LINE__, __FUNCTION__))
186 #define debug_assert(expr) (void)(0 && (expr))
190 /** Override standard assert macro */
194 #define assert(expr) debug_assert(expr)
198 * Output the current function name.
201 #define debug_checkpoint() \
202 _debug_printf("%s\n", __FUNCTION__)
204 #define debug_checkpoint() \
210 * Output the full source code position.
213 #define debug_checkpoint_full() \
214 _debug_printf("%s:%u:%s\n", __FILE__, __LINE__, __FUNCTION__)
216 #define debug_checkpoint_full() \
222 * Output a warning message. Muted on release version.
225 #define debug_warning(__msg) \
226 _debug_printf("%s:%u:%s: warning: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
228 #define debug_warning(__msg) \
234 * Emit a warning message, but only once.
237 #define debug_warn_once(__msg) \
239 static bool warned = false; \
241 _debug_printf("%s:%u:%s: one time warning: %s\n", \
242 __FILE__, __LINE__, __FUNCTION__, __msg); \
247 #define debug_warn_once(__msg) \
253 * Output an error message. Not muted on release version.
256 #define debug_error(__msg) \
257 _debug_printf("%s:%u:%s: error: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
259 #define debug_error(__msg) \
260 _debug_printf("error: %s\n", __msg)
264 * Output a debug log message to the debug info callback.
266 #define pipe_debug_message(cb, type, fmt, ...) do { \
267 static unsigned id = 0; \
268 if ((cb) && (cb)->debug_message) { \
269 _pipe_debug_message(cb, &id, \
270 PIPE_DEBUG_TYPE_ ## type, \
271 fmt, ##__VA_ARGS__); \
275 struct pipe_debug_callback
;
279 struct pipe_debug_callback
*cb
,
281 enum pipe_debug_type type
,
282 const char *fmt
, ...) _util_printf_format(4, 5);
286 * Used by debug_dump_enum and debug_dump_flags to describe symbols.
288 struct debug_named_value
297 * Some C pre-processor magic to simplify creating named values.
301 * static const debug_named_value my_names[] = {
302 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_X),
303 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Y),
304 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Z),
305 * DEBUG_NAMED_VALUE_END
309 * debug_printf("%s = %s\n",
311 * debug_dump_enum(my_names, my_value));
315 #define DEBUG_NAMED_VALUE(__symbol) {#__symbol, (unsigned long)__symbol, NULL}
316 #define DEBUG_NAMED_VALUE_WITH_DESCRIPTION(__symbol, __desc) {#__symbol, (unsigned long)__symbol, __desc}
317 #define DEBUG_NAMED_VALUE_END {NULL, 0, NULL}
321 * Convert a enum value to a string.
324 debug_dump_enum(const struct debug_named_value
*names
,
325 unsigned long value
);
328 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
330 unsigned long value
);
334 * Convert binary flags value to a string.
337 debug_dump_flags(const struct debug_named_value
*names
,
338 unsigned long value
);
342 * Function enter exit loggers
345 int debug_funclog_enter(const char* f
, const int line
, const char* file
);
346 void debug_funclog_exit(const char* f
, const int line
, const char* file
);
347 void debug_funclog_enter_exit(const char* f
, const int line
, const char* file
);
349 #define DEBUG_FUNCLOG_ENTER() \
350 int __debug_decleration_work_around = \
351 debug_funclog_enter(__FUNCTION__, __LINE__, __FILE__)
352 #define DEBUG_FUNCLOG_EXIT() \
354 (void)__debug_decleration_work_around; \
355 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
358 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
360 (void)__debug_decleration_work_around; \
361 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
364 #define DEBUG_FUNCLOG_ENTER_EXIT() \
365 debug_funclog_enter_exit(__FUNCTION__, __LINE__, __FILE__)
368 #define DEBUG_FUNCLOG_ENTER() \
369 int __debug_decleration_work_around
370 #define DEBUG_FUNCLOG_EXIT() \
371 do { (void)__debug_decleration_work_around; return; } while(0)
372 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
373 do { (void)__debug_decleration_work_around; return ret; } while(0)
374 #define DEBUG_FUNCLOG_ENTER_EXIT()
381 * It is an alias for getenv on Linux.
383 * On Windows it reads C:\gallium.cfg, which is a text file with CR+LF line
384 * endings with one option per line as
388 * This file must be terminated with an extra empty line.
391 debug_get_option(const char *name
, const char *dfault
);
394 debug_get_bool_option(const char *name
, bool dfault
);
397 debug_get_num_option(const char *name
, long dfault
);
400 debug_get_flags_option(const char *name
,
401 const struct debug_named_value
*flags
,
404 #define DEBUG_GET_ONCE_OPTION(suffix, name, dfault) \
405 static const char * \
406 debug_get_option_ ## suffix (void) \
408 static bool first = true; \
409 static const char * value; \
412 value = debug_get_option(name, dfault); \
417 #define DEBUG_GET_ONCE_BOOL_OPTION(sufix, name, dfault) \
419 debug_get_option_ ## sufix (void) \
421 static bool first = true; \
425 value = debug_get_bool_option(name, dfault); \
430 #define DEBUG_GET_ONCE_NUM_OPTION(sufix, name, dfault) \
432 debug_get_option_ ## sufix (void) \
434 static bool first = true; \
438 value = debug_get_num_option(name, dfault); \
443 #define DEBUG_GET_ONCE_FLAGS_OPTION(sufix, name, flags, dfault) \
444 static unsigned long \
445 debug_get_option_ ## sufix (void) \
447 static bool first = true; \
448 static unsigned long value; \
451 value = debug_get_flags_option(name, flags, dfault); \
461 #endif /* U_DEBUG_H_ */