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 #if defined(PIPE_OS_HAIKU)
45 /* Haiku provides debug_printf in libroot with OS.h */
49 #include "pipe/p_format.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)
86 #if !defined(PIPE_OS_HAIKU)
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 boolean 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 /* Print a message along with a prettified format string
137 void debug_print_format(const char *msg
, unsigned fmt
);
139 #define debug_print_blob(_name, _blob, _size) ((void)0)
140 #define debug_print_format(_msg, _fmt) ((void)0)
145 * Disable interactive error message boxes.
147 * Should be called as soon as possible for effectiveness.
150 debug_disable_error_message_boxes(void);
154 * Hard-coded breakpoint.
157 #define debug_break() os_break()
159 #define debug_break() ((void)0)
164 debug_get_num_option(const char *name
, long dfault
);
169 void _debug_assert_fail(const char *expr
,
172 const char *function
)
174 __attribute__((__noreturn__
))
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 && (expr))
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\n", __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 * Emit a warning message, but only once.
242 #define debug_warn_once(__msg) \
244 static bool warned = FALSE; \
246 _debug_printf("%s:%u:%s: one time warning: %s\n", \
247 __FILE__, __LINE__, __FUNCTION__, __msg); \
252 #define debug_warn_once(__msg) \
258 * Output an error message. Not muted on release version.
261 #define debug_error(__msg) \
262 _debug_printf("%s:%u:%s: error: %s\n", __FILE__, __LINE__, __FUNCTION__, __msg)
264 #define debug_error(__msg) \
265 _debug_printf("error: %s\n", __msg)
269 * Output a debug log message to the debug info callback.
271 #define pipe_debug_message(cb, type, fmt, ...) do { \
272 static unsigned id = 0; \
273 if ((cb) && (cb)->debug_message) { \
274 _pipe_debug_message(cb, &id, \
275 PIPE_DEBUG_TYPE_ ## type, \
276 fmt, ##__VA_ARGS__); \
280 struct pipe_debug_callback
;
284 struct pipe_debug_callback
*cb
,
286 enum pipe_debug_type type
,
287 const char *fmt
, ...) _util_printf_format(4, 5);
291 * Used by debug_dump_enum and debug_dump_flags to describe symbols.
293 struct debug_named_value
302 * Some C pre-processor magic to simplify creating named values.
306 * static const debug_named_value my_names[] = {
307 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_X),
308 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Y),
309 * DEBUG_NAMED_VALUE(MY_ENUM_VALUE_Z),
310 * DEBUG_NAMED_VALUE_END
314 * debug_printf("%s = %s\n",
316 * debug_dump_enum(my_names, my_value));
320 #define DEBUG_NAMED_VALUE(__symbol) {#__symbol, (unsigned long)__symbol, NULL}
321 #define DEBUG_NAMED_VALUE_WITH_DESCRIPTION(__symbol, __desc) {#__symbol, (unsigned long)__symbol, __desc}
322 #define DEBUG_NAMED_VALUE_END {NULL, 0, NULL}
326 * Convert a enum value to a string.
329 debug_dump_enum(const struct debug_named_value
*names
,
330 unsigned long value
);
333 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
335 unsigned long value
);
339 * Convert binary flags value to a string.
342 debug_dump_flags(const struct debug_named_value
*names
,
343 unsigned long value
);
347 * Function enter exit loggers
350 int debug_funclog_enter(const char* f
, const int line
, const char* file
);
351 void debug_funclog_exit(const char* f
, const int line
, const char* file
);
352 void debug_funclog_enter_exit(const char* f
, const int line
, const char* file
);
354 #define DEBUG_FUNCLOG_ENTER() \
355 int __debug_decleration_work_around = \
356 debug_funclog_enter(__FUNCTION__, __LINE__, __FILE__)
357 #define DEBUG_FUNCLOG_EXIT() \
359 (void)__debug_decleration_work_around; \
360 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
363 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
365 (void)__debug_decleration_work_around; \
366 debug_funclog_exit(__FUNCTION__, __LINE__, __FILE__); \
369 #define DEBUG_FUNCLOG_ENTER_EXIT() \
370 debug_funclog_enter_exit(__FUNCTION__, __LINE__, __FILE__)
373 #define DEBUG_FUNCLOG_ENTER() \
374 int __debug_decleration_work_around
375 #define DEBUG_FUNCLOG_EXIT() \
376 do { (void)__debug_decleration_work_around; return; } while(0)
377 #define DEBUG_FUNCLOG_EXIT_RET(ret) \
378 do { (void)__debug_decleration_work_around; return ret; } while(0)
379 #define DEBUG_FUNCLOG_ENTER_EXIT()
386 * It is an alias for getenv on Linux.
388 * On Windows it reads C:\gallium.cfg, which is a text file with CR+LF line
389 * endings with one option per line as
393 * This file must be terminated with an extra empty line.
396 debug_get_option(const char *name
, const char *dfault
);
399 debug_get_bool_option(const char *name
, boolean dfault
);
402 debug_get_num_option(const char *name
, long dfault
);
405 debug_get_flags_option(const char *name
,
406 const struct debug_named_value
*flags
,
409 #define DEBUG_GET_ONCE_OPTION(suffix, name, dfault) \
410 static const char * \
411 debug_get_option_ ## suffix (void) \
413 static boolean first = TRUE; \
414 static const char * value; \
417 value = debug_get_option(name, dfault); \
422 #define DEBUG_GET_ONCE_BOOL_OPTION(sufix, name, dfault) \
424 debug_get_option_ ## sufix (void) \
426 static boolean first = TRUE; \
427 static boolean value; \
430 value = debug_get_bool_option(name, dfault); \
435 #define DEBUG_GET_ONCE_NUM_OPTION(sufix, name, dfault) \
437 debug_get_option_ ## sufix (void) \
439 static boolean first = TRUE; \
443 value = debug_get_num_option(name, dfault); \
448 #define DEBUG_GET_ONCE_FLAGS_OPTION(sufix, name, flags, dfault) \
449 static unsigned long \
450 debug_get_option_ ## sufix (void) \
452 static boolean first = TRUE; \
453 static unsigned long value; \
456 value = debug_get_flags_option(name, flags, dfault); \
463 debug_memory_begin(void);
466 debug_memory_end(unsigned long beginning
);
470 debug_print_transfer_flags(const char *msg
, unsigned usage
);
473 debug_print_bind_flags(const char *msg
, unsigned usage
);
476 debug_print_usage_enum(const char *msg
, enum pipe_resource_usage usage
);
483 #endif /* U_DEBUG_H_ */