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 "util/u_dump.h"
35 #include "pipe/p_format.h"
36 #include "pipe/p_state.h"
37 #include "util/u_inlines.h"
38 #include "util/u_memory.h"
39 #include "util/u_string.h"
40 #include "util/u_math.h"
44 #include <limits.h> /* CHAR_BIT */
45 #include <ctype.h> /* isalnum */
54 _debug_vprintf(const char *format
, va_list ap
)
56 static char buf
[4096] = {'\0'};
57 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_EMBEDDED)
58 /* We buffer until we find a newline. */
59 size_t len
= strlen(buf
);
60 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
61 if (ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
66 util_vsnprintf(buf
, sizeof(buf
), format
, ap
);
73 _pipe_debug_message(struct pipe_debug_callback
*cb
,
75 enum pipe_debug_type type
,
80 if (cb
&& cb
->debug_message
)
81 cb
->debug_message(cb
->data
, id
, type
, fmt
, args
);
87 debug_disable_error_message_boxes(void)
90 /* When Windows' error message boxes are disabled for this process (as is
91 * typically the case when running tests in an automated fashion) we disable
92 * CRT message boxes too.
94 UINT uMode
= SetErrorMode(0);
96 if (uMode
& SEM_FAILCRITICALERRORS
) {
97 /* Disable assertion failure message box.
98 * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
100 _set_error_mode(_OUT_TO_STDERR
);
102 /* Disable abort message box.
103 * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
105 _set_abort_behavior(0, _WRITE_ABORT_MSG
| _CALL_REPORTFAULT
);
114 debug_print_blob(const char *name
, const void *blob
, unsigned size
)
116 const unsigned *ublob
= (const unsigned *)blob
;
119 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
120 size
%4 ? "... plus a few bytes" : "");
122 for (i
= 0; i
< size
/4; i
++) {
123 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
130 debug_get_option_should_print(void)
132 static boolean first
= TRUE
;
133 static boolean value
= FALSE
;
138 /* Oh hey this will call into this function,
139 * but its cool since we set first to false
142 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE
);
143 /* XXX should we print this option? Currently it wont */
149 debug_get_option(const char *name
, const char *dfault
)
153 result
= os_get_option(name
);
157 if (debug_get_option_should_print())
158 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
159 result
? result
: "(null)");
166 debug_get_bool_option(const char *name
, boolean dfault
)
168 const char *str
= os_get_option(name
);
173 else if (!util_strcmp(str
, "n"))
175 else if (!util_strcmp(str
, "no"))
177 else if (!util_strcmp(str
, "0"))
179 else if (!util_strcmp(str
, "f"))
181 else if (!util_strcmp(str
, "F"))
183 else if (!util_strcmp(str
, "false"))
185 else if (!util_strcmp(str
, "FALSE"))
190 if (debug_get_option_should_print())
191 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
192 result
? "TRUE" : "FALSE");
199 debug_get_num_option(const char *name
, long dfault
)
204 str
= os_get_option(name
);
210 result
= strtol(str
, &endptr
, 0);
212 /* Restore the default value when no digits were found. */
217 if (debug_get_option_should_print())
218 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
225 str_has_option(const char *str
, const char *name
)
233 if (!util_strcmp(str
, "all")) {
237 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
239 const char *start
= str
;
240 unsigned name_len
= strlen(name
);
242 /* 'start' is the beginning of the currently-parsed word,
243 * we increment 'str' each iteration.
244 * if we find either the end of string or a non-alphanumeric character,
245 * we compare 'start' up to 'str-1' with 'name'. */
248 if (!*str
|| !(isalnum(*str
) || *str
== '_')) {
249 if (str
-start
== name_len
&&
250 !memcmp(start
, name
, name_len
)) {
270 debug_get_flags_option(const char *name
,
271 const struct debug_named_value
*flags
,
276 const struct debug_named_value
*orig
= flags
;
277 unsigned namealign
= 0;
279 str
= os_get_option(name
);
282 else if (!util_strcmp(str
, "help")) {
284 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
285 for (; flags
->name
; ++flags
)
286 namealign
= MAX2(namealign
, strlen(flags
->name
));
287 for (flags
= orig
; flags
->name
; ++flags
)
288 _debug_printf("| %*s [0x%0*"PRIx64
"]%s%s\n", namealign
, flags
->name
,
289 (int)sizeof(uint64_t)*CHAR_BIT
/4, flags
->value
,
290 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
294 while (flags
->name
) {
295 if (str_has_option(str
, flags
->name
))
296 result
|= flags
->value
;
301 if (debug_get_option_should_print()) {
303 debug_printf("%s: %s = 0x%"PRIx64
" (%s)\n",
304 __FUNCTION__
, name
, result
, str
);
306 debug_printf("%s: %s = 0x%"PRIx64
"\n", __FUNCTION__
, name
, result
);
315 _debug_assert_fail(const char *expr
, const char *file
, unsigned line
,
316 const char *function
)
318 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n",
319 file
, line
, function
, expr
);
325 debug_dump_enum(const struct debug_named_value
*names
,
328 static char rest
[64];
330 while (names
->name
) {
331 if (names
->value
== value
)
336 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
342 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
346 static char rest
[64];
348 while (names
->name
) {
349 if (names
->value
== value
) {
350 const char *name
= names
->name
;
351 while (*name
== *prefix
) {
360 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
366 debug_dump_flags(const struct debug_named_value
*names
, unsigned long value
)
368 static char output
[4096];
369 static char rest
[256];
374 while (names
->name
) {
375 if ((names
->value
& value
) == names
->value
) {
377 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
380 util_strncat(output
, names
->name
, sizeof(output
) - strlen(output
) - 1);
381 output
[sizeof(output
) - 1] = '\0';
382 value
&= ~names
->value
;
389 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
393 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
394 util_strncat(output
, rest
, sizeof(output
) - strlen(output
) - 1);
395 output
[sizeof(output
) - 1] = '\0';
408 const char* fl_function
[1024];
411 debug_funclog_enter(const char* f
, UNUSED
const int line
,
412 UNUSED
const char* file
)
416 for (i
= 0; i
< fl_indent
; i
++)
418 debug_printf("%s\n", f
);
420 assert(fl_indent
< 1023);
421 fl_function
[fl_indent
++] = f
;
427 debug_funclog_exit(const char* f
, UNUSED
const int line
,
428 UNUSED
const char* file
)
431 assert(fl_indent
>= 0);
432 assert(fl_function
[fl_indent
] == f
);
436 debug_funclog_enter_exit(const char* f
, UNUSED
const int line
,
437 UNUSED
const char* file
)
440 for (i
= 0; i
< fl_indent
; i
++)
442 debug_printf("%s\n", f
);
450 * Print PIPE_TRANSFER_x flags with a message.
453 debug_print_transfer_flags(const char *msg
, unsigned usage
)
455 debug_printf("%s: ", msg
);
456 util_dump_transfer_usage(stdout
, usage
);
462 * Print PIPE_BIND_x flags with a message.
465 debug_print_bind_flags(const char *msg
, unsigned usage
)
467 static const struct debug_named_value names
[] = {
468 DEBUG_NAMED_VALUE(PIPE_BIND_DEPTH_STENCIL
),
469 DEBUG_NAMED_VALUE(PIPE_BIND_RENDER_TARGET
),
470 DEBUG_NAMED_VALUE(PIPE_BIND_BLENDABLE
),
471 DEBUG_NAMED_VALUE(PIPE_BIND_SAMPLER_VIEW
),
472 DEBUG_NAMED_VALUE(PIPE_BIND_VERTEX_BUFFER
),
473 DEBUG_NAMED_VALUE(PIPE_BIND_INDEX_BUFFER
),
474 DEBUG_NAMED_VALUE(PIPE_BIND_CONSTANT_BUFFER
),
475 DEBUG_NAMED_VALUE(PIPE_BIND_DISPLAY_TARGET
),
476 DEBUG_NAMED_VALUE(PIPE_BIND_STREAM_OUTPUT
),
477 DEBUG_NAMED_VALUE(PIPE_BIND_CURSOR
),
478 DEBUG_NAMED_VALUE(PIPE_BIND_CUSTOM
),
479 DEBUG_NAMED_VALUE(PIPE_BIND_GLOBAL
),
480 DEBUG_NAMED_VALUE(PIPE_BIND_SHADER_BUFFER
),
481 DEBUG_NAMED_VALUE(PIPE_BIND_SHADER_IMAGE
),
482 DEBUG_NAMED_VALUE(PIPE_BIND_COMPUTE_RESOURCE
),
483 DEBUG_NAMED_VALUE(PIPE_BIND_COMMAND_ARGS_BUFFER
),
484 DEBUG_NAMED_VALUE(PIPE_BIND_SCANOUT
),
485 DEBUG_NAMED_VALUE(PIPE_BIND_SHARED
),
486 DEBUG_NAMED_VALUE(PIPE_BIND_LINEAR
),
487 DEBUG_NAMED_VALUE_END
490 debug_printf("%s: %s\n", msg
, debug_dump_flags(names
, usage
));