1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
4 * Copyright (c) 2008 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
30 #include "pipe/p_config.h"
32 #include "pipe/p_compiler.h"
33 #include "util/u_debug.h"
34 #include "pipe/p_format.h"
35 #include "pipe/p_state.h"
36 #include "util/u_inlines.h"
37 #include "util/u_string.h"
38 #include "util/u_math.h"
42 #include <limits.h> /* CHAR_BIT */
43 #include <ctype.h> /* isalnum */
52 _debug_vprintf(const char *format
, va_list ap
)
54 static char buf
[4096] = {'\0'};
55 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_EMBEDDED)
56 /* We buffer until we find a newline. */
57 size_t len
= strlen(buf
);
58 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
59 if (ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
64 util_vsnprintf(buf
, sizeof(buf
), format
, ap
);
71 _pipe_debug_message(struct pipe_debug_callback
*cb
,
73 enum pipe_debug_type type
,
78 if (cb
&& cb
->debug_message
)
79 cb
->debug_message(cb
->data
, id
, type
, fmt
, args
);
85 debug_disable_error_message_boxes(void)
88 /* When Windows' error message boxes are disabled for this process (as is
89 * typically the case when running tests in an automated fashion) we disable
90 * CRT message boxes too.
92 UINT uMode
= SetErrorMode(0);
94 if (uMode
& SEM_FAILCRITICALERRORS
) {
95 /* Disable assertion failure message box.
96 * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
98 _set_error_mode(_OUT_TO_STDERR
);
100 /* Disable abort message box.
101 * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
103 _set_abort_behavior(0, _WRITE_ABORT_MSG
| _CALL_REPORTFAULT
);
112 debug_print_blob(const char *name
, const void *blob
, unsigned size
)
114 const unsigned *ublob
= (const unsigned *)blob
;
117 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
118 size
%4 ? "... plus a few bytes" : "");
120 for (i
= 0; i
< size
/4; i
++) {
121 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
128 debug_get_option_should_print(void)
130 static boolean first
= TRUE
;
131 static boolean value
= FALSE
;
136 /* Oh hey this will call into this function,
137 * but its cool since we set first to false
140 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE
);
141 /* XXX should we print this option? Currently it wont */
147 debug_get_option(const char *name
, const char *dfault
)
151 result
= os_get_option(name
);
155 if (debug_get_option_should_print())
156 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
157 result
? result
: "(null)");
164 debug_get_bool_option(const char *name
, boolean dfault
)
166 const char *str
= os_get_option(name
);
171 else if (!util_strcmp(str
, "n"))
173 else if (!util_strcmp(str
, "no"))
175 else if (!util_strcmp(str
, "0"))
177 else if (!util_strcmp(str
, "f"))
179 else if (!util_strcmp(str
, "F"))
181 else if (!util_strcmp(str
, "false"))
183 else if (!util_strcmp(str
, "FALSE"))
188 if (debug_get_option_should_print())
189 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
190 result
? "TRUE" : "FALSE");
197 debug_get_num_option(const char *name
, long dfault
)
202 str
= os_get_option(name
);
208 result
= strtol(str
, &endptr
, 0);
210 /* Restore the default value when no digits were found. */
215 if (debug_get_option_should_print())
216 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
223 str_has_option(const char *str
, const char *name
)
231 if (!util_strcmp(str
, "all")) {
235 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
237 const char *start
= str
;
238 unsigned name_len
= strlen(name
);
240 /* 'start' is the beginning of the currently-parsed word,
241 * we increment 'str' each iteration.
242 * if we find either the end of string or a non-alphanumeric character,
243 * we compare 'start' up to 'str-1' with 'name'. */
246 if (!*str
|| !(isalnum(*str
) || *str
== '_')) {
247 if (str
-start
== name_len
&&
248 !memcmp(start
, name
, name_len
)) {
268 debug_get_flags_option(const char *name
,
269 const struct debug_named_value
*flags
,
274 const struct debug_named_value
*orig
= flags
;
275 unsigned namealign
= 0;
277 str
= os_get_option(name
);
280 else if (!util_strcmp(str
, "help")) {
282 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
283 for (; flags
->name
; ++flags
)
284 namealign
= MAX2(namealign
, strlen(flags
->name
));
285 for (flags
= orig
; flags
->name
; ++flags
)
286 _debug_printf("| %*s [0x%0*"PRIx64
"]%s%s\n", namealign
, flags
->name
,
287 (int)sizeof(uint64_t)*CHAR_BIT
/4, flags
->value
,
288 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
292 while (flags
->name
) {
293 if (str_has_option(str
, flags
->name
))
294 result
|= flags
->value
;
299 if (debug_get_option_should_print()) {
301 debug_printf("%s: %s = 0x%"PRIx64
" (%s)\n",
302 __FUNCTION__
, name
, result
, str
);
304 debug_printf("%s: %s = 0x%"PRIx64
"\n", __FUNCTION__
, name
, result
);
313 _debug_assert_fail(const char *expr
, const char *file
, unsigned line
,
314 const char *function
)
316 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n",
317 file
, line
, function
, expr
);
323 debug_dump_enum(const struct debug_named_value
*names
,
326 static char rest
[64];
328 while (names
->name
) {
329 if (names
->value
== value
)
334 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
340 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
344 static char rest
[64];
346 while (names
->name
) {
347 if (names
->value
== value
) {
348 const char *name
= names
->name
;
349 while (*name
== *prefix
) {
358 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
364 debug_dump_flags(const struct debug_named_value
*names
, unsigned long value
)
366 static char output
[4096];
367 static char rest
[256];
372 while (names
->name
) {
373 if ((names
->value
& value
) == names
->value
) {
375 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
378 util_strncat(output
, names
->name
, sizeof(output
) - strlen(output
) - 1);
379 output
[sizeof(output
) - 1] = '\0';
380 value
&= ~names
->value
;
387 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
391 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
392 util_strncat(output
, rest
, sizeof(output
) - strlen(output
) - 1);
393 output
[sizeof(output
) - 1] = '\0';
406 const char* fl_function
[1024];
409 debug_funclog_enter(const char* f
, UNUSED
const int line
,
410 UNUSED
const char* file
)
414 for (i
= 0; i
< fl_indent
; i
++)
416 debug_printf("%s\n", f
);
418 assert(fl_indent
< 1023);
419 fl_function
[fl_indent
++] = f
;
425 debug_funclog_exit(const char* f
, UNUSED
const int line
,
426 UNUSED
const char* file
)
429 assert(fl_indent
>= 0);
430 assert(fl_function
[fl_indent
] == f
);
434 debug_funclog_enter_exit(const char* f
, UNUSED
const int line
,
435 UNUSED
const char* file
)
438 for (i
= 0; i
< fl_indent
; i
++)
440 debug_printf("%s\n", f
);