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 "util/u_debug.h"
33 #include "pipe/p_format.h"
34 #include "pipe/p_state.h"
35 #include "util/u_string.h"
36 #include "util/u_math.h"
40 #include <limits.h> /* CHAR_BIT */
41 #include <ctype.h> /* isalnum */
50 _debug_vprintf(const char *format
, va_list ap
)
52 static char buf
[4096] = {'\0'};
53 #if DETECT_OS_WINDOWS || defined(EMBEDDED_DEVICE)
54 /* We buffer until we find a newline. */
55 size_t len
= strlen(buf
);
56 int ret
= vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
57 if (ret
> (int)(sizeof(buf
) - len
- 1) || strchr(buf
+ len
, '\n')) {
62 vsnprintf(buf
, sizeof(buf
), format
, ap
);
69 _pipe_debug_message(struct pipe_debug_callback
*cb
,
71 enum pipe_debug_type type
,
76 if (cb
&& cb
->debug_message
)
77 cb
->debug_message(cb
->data
, id
, type
, fmt
, args
);
83 debug_disable_error_message_boxes(void)
86 /* When Windows' error message boxes are disabled for this process (as is
87 * typically the case when running tests in an automated fashion) we disable
88 * CRT message boxes too.
90 UINT uMode
= SetErrorMode(0);
92 if (uMode
& SEM_FAILCRITICALERRORS
) {
93 /* Disable assertion failure message box.
94 * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
96 _set_error_mode(_OUT_TO_STDERR
);
98 /* Disable abort message box.
99 * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
101 _set_abort_behavior(0, _WRITE_ABORT_MSG
| _CALL_REPORTFAULT
);
110 debug_print_blob(const char *name
, const void *blob
, unsigned size
)
112 const unsigned *ublob
= (const unsigned *)blob
;
115 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
116 size
%4 ? "... plus a few bytes" : "");
118 for (i
= 0; i
< size
/4; i
++) {
119 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
126 debug_get_option_should_print(void)
128 static bool first
= true;
129 static bool value
= false;
134 /* Oh hey this will call into this function,
135 * but its cool since we set first to false
138 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", false);
139 /* XXX should we print this option? Currently it wont */
145 debug_get_option(const char *name
, const char *dfault
)
149 result
= os_get_option(name
);
153 if (debug_get_option_should_print())
154 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
155 result
? result
: "(null)");
162 debug_get_bool_option(const char *name
, bool dfault
)
164 const char *str
= os_get_option(name
);
169 else if (!strcmp(str
, "n"))
171 else if (!strcmp(str
, "no"))
173 else if (!strcmp(str
, "0"))
175 else if (!strcmp(str
, "f"))
177 else if (!strcmp(str
, "F"))
179 else if (!strcmp(str
, "false"))
181 else if (!strcmp(str
, "FALSE"))
186 if (debug_get_option_should_print())
187 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
188 result
? "TRUE" : "FALSE");
195 debug_get_num_option(const char *name
, long dfault
)
200 str
= os_get_option(name
);
206 result
= strtol(str
, &endptr
, 0);
208 /* Restore the default value when no digits were found. */
213 if (debug_get_option_should_print())
214 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
221 str_has_option(const char *str
, const char *name
)
229 if (!strcmp(str
, "all")) {
233 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
235 const char *start
= str
;
236 unsigned name_len
= strlen(name
);
238 /* 'start' is the beginning of the currently-parsed word,
239 * we increment 'str' each iteration.
240 * if we find either the end of string or a non-alphanumeric character,
241 * we compare 'start' up to 'str-1' with 'name'. */
244 if (!*str
|| !(isalnum(*str
) || *str
== '_')) {
245 if (str
-start
== name_len
&&
246 !memcmp(start
, name
, name_len
)) {
266 debug_get_flags_option(const char *name
,
267 const struct debug_named_value
*flags
,
272 const struct debug_named_value
*orig
= flags
;
273 unsigned namealign
= 0;
275 str
= os_get_option(name
);
278 else if (!strcmp(str
, "help")) {
280 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
281 for (; flags
->name
; ++flags
)
282 namealign
= MAX2(namealign
, strlen(flags
->name
));
283 for (flags
= orig
; flags
->name
; ++flags
)
284 _debug_printf("| %*s [0x%0*"PRIx64
"]%s%s\n", namealign
, flags
->name
,
285 (int)sizeof(uint64_t)*CHAR_BIT
/4, flags
->value
,
286 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
290 while (flags
->name
) {
291 if (str_has_option(str
, flags
->name
))
292 result
|= flags
->value
;
297 if (debug_get_option_should_print()) {
299 debug_printf("%s: %s = 0x%"PRIx64
" (%s)\n",
300 __FUNCTION__
, name
, result
, str
);
302 debug_printf("%s: %s = 0x%"PRIx64
"\n", __FUNCTION__
, name
, result
);
311 _debug_assert_fail(const char *expr
, const char *file
, unsigned line
,
312 const char *function
)
314 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n",
315 file
, line
, function
, expr
);
321 debug_dump_enum(const struct debug_named_value
*names
,
324 static char rest
[64];
326 while (names
->name
) {
327 if (names
->value
== value
)
332 snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
338 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
342 static char rest
[64];
344 while (names
->name
) {
345 if (names
->value
== value
) {
346 const char *name
= names
->name
;
347 while (*name
== *prefix
) {
356 snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
362 debug_dump_flags(const struct debug_named_value
*names
, unsigned long value
)
364 static char output
[4096];
365 static char rest
[256];
370 while (names
->name
) {
371 if ((names
->value
& value
) == names
->value
) {
373 strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
376 strncat(output
, names
->name
, sizeof(output
) - strlen(output
) - 1);
377 output
[sizeof(output
) - 1] = '\0';
378 value
&= ~names
->value
;
385 strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
389 snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
390 strncat(output
, rest
, sizeof(output
) - strlen(output
) - 1);
391 output
[sizeof(output
) - 1] = '\0';
404 const char* fl_function
[1024];
407 debug_funclog_enter(const char* f
, UNUSED
const int line
,
408 UNUSED
const char* file
)
412 for (i
= 0; i
< fl_indent
; i
++)
414 debug_printf("%s\n", f
);
416 assert(fl_indent
< 1023);
417 fl_function
[fl_indent
++] = f
;
423 debug_funclog_exit(const char* f
, UNUSED
const int line
,
424 UNUSED
const char* file
)
427 assert(fl_indent
>= 0);
428 assert(fl_function
[fl_indent
] == f
);
432 debug_funclog_enter_exit(const char* f
, UNUSED
const int line
,
433 UNUSED
const char* file
)
436 for (i
= 0; i
< fl_indent
; i
++)
438 debug_printf("%s\n", f
);