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_string.h"
37 #include "util/u_math.h"
41 #include <limits.h> /* CHAR_BIT */
42 #include <ctype.h> /* isalnum */
51 _debug_vprintf(const char *format
, va_list ap
)
53 static char buf
[4096] = {'\0'};
54 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_EMBEDDED)
55 /* We buffer until we find a newline. */
56 size_t len
= strlen(buf
);
57 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
58 if (ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
63 util_vsnprintf(buf
, sizeof(buf
), format
, ap
);
70 _pipe_debug_message(struct pipe_debug_callback
*cb
,
72 enum pipe_debug_type type
,
77 if (cb
&& cb
->debug_message
)
78 cb
->debug_message(cb
->data
, id
, type
, fmt
, args
);
84 debug_disable_error_message_boxes(void)
87 /* When Windows' error message boxes are disabled for this process (as is
88 * typically the case when running tests in an automated fashion) we disable
89 * CRT message boxes too.
91 UINT uMode
= SetErrorMode(0);
93 if (uMode
& SEM_FAILCRITICALERRORS
) {
94 /* Disable assertion failure message box.
95 * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
97 _set_error_mode(_OUT_TO_STDERR
);
99 /* Disable abort message box.
100 * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
102 _set_abort_behavior(0, _WRITE_ABORT_MSG
| _CALL_REPORTFAULT
);
111 debug_print_blob(const char *name
, const void *blob
, unsigned size
)
113 const unsigned *ublob
= (const unsigned *)blob
;
116 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
117 size
%4 ? "... plus a few bytes" : "");
119 for (i
= 0; i
< size
/4; i
++) {
120 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
127 debug_get_option_should_print(void)
129 static boolean first
= TRUE
;
130 static boolean value
= FALSE
;
135 /* Oh hey this will call into this function,
136 * but its cool since we set first to false
139 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE
);
140 /* XXX should we print this option? Currently it wont */
146 debug_get_option(const char *name
, const char *dfault
)
150 result
= os_get_option(name
);
154 if (debug_get_option_should_print())
155 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
156 result
? result
: "(null)");
163 debug_get_bool_option(const char *name
, boolean dfault
)
165 const char *str
= os_get_option(name
);
170 else if (!util_strcmp(str
, "n"))
172 else if (!util_strcmp(str
, "no"))
174 else if (!util_strcmp(str
, "0"))
176 else if (!util_strcmp(str
, "f"))
178 else if (!util_strcmp(str
, "F"))
180 else if (!util_strcmp(str
, "false"))
182 else if (!util_strcmp(str
, "FALSE"))
187 if (debug_get_option_should_print())
188 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
189 result
? "TRUE" : "FALSE");
196 debug_get_num_option(const char *name
, long dfault
)
201 str
= os_get_option(name
);
207 result
= strtol(str
, &endptr
, 0);
209 /* Restore the default value when no digits were found. */
214 if (debug_get_option_should_print())
215 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
222 str_has_option(const char *str
, const char *name
)
230 if (!util_strcmp(str
, "all")) {
234 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
236 const char *start
= str
;
237 unsigned name_len
= strlen(name
);
239 /* 'start' is the beginning of the currently-parsed word,
240 * we increment 'str' each iteration.
241 * if we find either the end of string or a non-alphanumeric character,
242 * we compare 'start' up to 'str-1' with 'name'. */
245 if (!*str
|| !(isalnum(*str
) || *str
== '_')) {
246 if (str
-start
== name_len
&&
247 !memcmp(start
, name
, name_len
)) {
267 debug_get_flags_option(const char *name
,
268 const struct debug_named_value
*flags
,
273 const struct debug_named_value
*orig
= flags
;
274 unsigned namealign
= 0;
276 str
= os_get_option(name
);
279 else if (!util_strcmp(str
, "help")) {
281 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
282 for (; flags
->name
; ++flags
)
283 namealign
= MAX2(namealign
, strlen(flags
->name
));
284 for (flags
= orig
; flags
->name
; ++flags
)
285 _debug_printf("| %*s [0x%0*"PRIx64
"]%s%s\n", namealign
, flags
->name
,
286 (int)sizeof(uint64_t)*CHAR_BIT
/4, flags
->value
,
287 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
291 while (flags
->name
) {
292 if (str_has_option(str
, flags
->name
))
293 result
|= flags
->value
;
298 if (debug_get_option_should_print()) {
300 debug_printf("%s: %s = 0x%"PRIx64
" (%s)\n",
301 __FUNCTION__
, name
, result
, str
);
303 debug_printf("%s: %s = 0x%"PRIx64
"\n", __FUNCTION__
, name
, result
);
312 _debug_assert_fail(const char *expr
, const char *file
, unsigned line
,
313 const char *function
)
315 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n",
316 file
, line
, function
, expr
);
322 debug_dump_enum(const struct debug_named_value
*names
,
325 static char rest
[64];
327 while (names
->name
) {
328 if (names
->value
== value
)
333 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
339 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
343 static char rest
[64];
345 while (names
->name
) {
346 if (names
->value
== value
) {
347 const char *name
= names
->name
;
348 while (*name
== *prefix
) {
357 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
363 debug_dump_flags(const struct debug_named_value
*names
, unsigned long value
)
365 static char output
[4096];
366 static char rest
[256];
371 while (names
->name
) {
372 if ((names
->value
& value
) == names
->value
) {
374 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
377 util_strncat(output
, names
->name
, sizeof(output
) - strlen(output
) - 1);
378 output
[sizeof(output
) - 1] = '\0';
379 value
&= ~names
->value
;
386 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
390 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
391 util_strncat(output
, rest
, sizeof(output
) - strlen(output
) - 1);
392 output
[sizeof(output
) - 1] = '\0';
405 const char* fl_function
[1024];
408 debug_funclog_enter(const char* f
, UNUSED
const int line
,
409 UNUSED
const char* file
)
413 for (i
= 0; i
< fl_indent
; i
++)
415 debug_printf("%s\n", f
);
417 assert(fl_indent
< 1023);
418 fl_function
[fl_indent
++] = f
;
424 debug_funclog_exit(const char* f
, UNUSED
const int line
,
425 UNUSED
const char* file
)
428 assert(fl_indent
>= 0);
429 assert(fl_function
[fl_indent
] == f
);
433 debug_funclog_enter_exit(const char* f
, UNUSED
const int line
,
434 UNUSED
const char* file
)
437 for (i
= 0; i
< fl_indent
; i
++)
439 debug_printf("%s\n", f
);