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_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_string.h"
40 #include "util/u_math.h"
41 #include "util/u_prim.h"
45 #include <limits.h> /* CHAR_BIT */
46 #include <ctype.h> /* isalnum */
55 _debug_vprintf(const char *format
, va_list ap
)
57 static char buf
[4096] = {'\0'};
58 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_EMBEDDED)
59 /* We buffer until we find a newline. */
60 size_t len
= strlen(buf
);
61 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
62 if (ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
67 util_vsnprintf(buf
, sizeof(buf
), format
, ap
);
74 _pipe_debug_message(struct pipe_debug_callback
*cb
,
76 enum pipe_debug_type type
,
81 if (cb
&& cb
->debug_message
)
82 cb
->debug_message(cb
->data
, id
, type
, fmt
, args
);
88 debug_disable_error_message_boxes(void)
91 /* When Windows' error message boxes are disabled for this process (as is
92 * typically the case when running tests in an automated fashion) we disable
93 * CRT message boxes too.
95 UINT uMode
= SetErrorMode(0);
97 if (uMode
& SEM_FAILCRITICALERRORS
) {
98 /* Disable assertion failure message box.
99 * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
101 _set_error_mode(_OUT_TO_STDERR
);
103 /* Disable abort message box.
104 * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
106 _set_abort_behavior(0, _WRITE_ABORT_MSG
| _CALL_REPORTFAULT
);
115 debug_print_blob(const char *name
, const void *blob
, unsigned size
)
117 const unsigned *ublob
= (const unsigned *)blob
;
120 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
121 size
%4 ? "... plus a few bytes" : "");
123 for (i
= 0; i
< size
/4; i
++) {
124 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
131 debug_get_option_should_print(void)
133 static boolean first
= TRUE
;
134 static boolean value
= FALSE
;
139 /* Oh hey this will call into this function,
140 * but its cool since we set first to false
143 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE
);
144 /* XXX should we print this option? Currently it wont */
150 debug_get_option(const char *name
, const char *dfault
)
154 result
= os_get_option(name
);
158 if (debug_get_option_should_print())
159 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
160 result
? result
: "(null)");
167 debug_get_bool_option(const char *name
, boolean dfault
)
169 const char *str
= os_get_option(name
);
174 else if (!util_strcmp(str
, "n"))
176 else if (!util_strcmp(str
, "no"))
178 else if (!util_strcmp(str
, "0"))
180 else if (!util_strcmp(str
, "f"))
182 else if (!util_strcmp(str
, "F"))
184 else if (!util_strcmp(str
, "false"))
186 else if (!util_strcmp(str
, "FALSE"))
191 if (debug_get_option_should_print())
192 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
193 result
? "TRUE" : "FALSE");
200 debug_get_num_option(const char *name
, long dfault
)
205 str
= os_get_option(name
);
220 while ('0' <= c
&& c
<= '9') {
221 result
= result
*10 + (c
- '0');
227 if (debug_get_option_should_print())
228 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
235 str_has_option(const char *str
, const char *name
)
243 if (!util_strcmp(str
, "all")) {
247 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
249 const char *start
= str
;
250 unsigned name_len
= strlen(name
);
252 /* 'start' is the beginning of the currently-parsed word,
253 * we increment 'str' each iteration.
254 * if we find either the end of string or a non-alphanumeric character,
255 * we compare 'start' up to 'str-1' with 'name'. */
258 if (!*str
|| !(isalnum(*str
) || *str
== '_')) {
259 if (str
-start
== name_len
&&
260 !memcmp(start
, name
, name_len
)) {
280 debug_get_flags_option(const char *name
,
281 const struct debug_named_value
*flags
,
286 const struct debug_named_value
*orig
= flags
;
287 unsigned namealign
= 0;
289 str
= os_get_option(name
);
292 else if (!util_strcmp(str
, "help")) {
294 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
295 for (; flags
->name
; ++flags
)
296 namealign
= MAX2(namealign
, strlen(flags
->name
));
297 for (flags
= orig
; flags
->name
; ++flags
)
298 _debug_printf("| %*s [0x%0*"PRIx64
"]%s%s\n", namealign
, flags
->name
,
299 (int)sizeof(uint64_t)*CHAR_BIT
/4, flags
->value
,
300 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
304 while (flags
->name
) {
305 if (str_has_option(str
, flags
->name
))
306 result
|= flags
->value
;
311 if (debug_get_option_should_print()) {
313 debug_printf("%s: %s = 0x%"PRIx64
" (%s)\n",
314 __FUNCTION__
, name
, result
, str
);
316 debug_printf("%s: %s = 0x%"PRIx64
"\n", __FUNCTION__
, name
, result
);
325 _debug_assert_fail(const char *expr
, const char *file
, unsigned line
,
326 const char *function
)
328 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n",
329 file
, line
, function
, expr
);
335 debug_dump_enum(const struct debug_named_value
*names
,
338 static char rest
[64];
340 while (names
->name
) {
341 if (names
->value
== value
)
346 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
352 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
356 static char rest
[64];
358 while (names
->name
) {
359 if (names
->value
== value
) {
360 const char *name
= names
->name
;
361 while (*name
== *prefix
) {
370 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
376 debug_dump_flags(const struct debug_named_value
*names
, unsigned long value
)
378 static char output
[4096];
379 static char rest
[256];
384 while (names
->name
) {
385 if ((names
->value
& value
) == names
->value
) {
387 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
390 util_strncat(output
, names
->name
, sizeof(output
) - strlen(output
) - 1);
391 output
[sizeof(output
) - 1] = '\0';
392 value
&= ~names
->value
;
399 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
403 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
404 util_strncat(output
, rest
, sizeof(output
) - strlen(output
) - 1);
405 output
[sizeof(output
) - 1] = '\0';
417 debug_print_format(const char *msg
, unsigned fmt
)
419 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
424 /** Return string name of given primitive type */
426 u_prim_name(unsigned prim
)
428 static const struct debug_named_value names
[] = {
429 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
430 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
431 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
432 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
433 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
434 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
435 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
436 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
437 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
438 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
439 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES_ADJACENCY
),
440 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP_ADJACENCY
),
441 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES_ADJACENCY
),
442 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
),
443 DEBUG_NAMED_VALUE_END
445 return debug_dump_enum(names
, prim
);
452 const char* fl_function
[1024];
455 debug_funclog_enter(const char* f
, const int line
, const char* file
)
459 for (i
= 0; i
< fl_indent
; i
++)
461 debug_printf("%s\n", f
);
463 assert(fl_indent
< 1023);
464 fl_function
[fl_indent
++] = f
;
470 debug_funclog_exit(const char* f
, const int line
, const char* file
)
473 assert(fl_indent
>= 0);
474 assert(fl_function
[fl_indent
] == f
);
478 debug_funclog_enter_exit(const char* f
, const int line
, const char* file
)
481 for (i
= 0; i
< fl_indent
; i
++)
483 debug_printf("%s\n", f
);
491 * Print PIPE_TRANSFER_x flags with a message.
494 debug_print_transfer_flags(const char *msg
, unsigned usage
)
496 static const struct debug_named_value names
[] = {
497 DEBUG_NAMED_VALUE(PIPE_TRANSFER_READ
),
498 DEBUG_NAMED_VALUE(PIPE_TRANSFER_WRITE
),
499 DEBUG_NAMED_VALUE(PIPE_TRANSFER_MAP_DIRECTLY
),
500 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DISCARD_RANGE
),
501 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DONTBLOCK
),
502 DEBUG_NAMED_VALUE(PIPE_TRANSFER_UNSYNCHRONIZED
),
503 DEBUG_NAMED_VALUE(PIPE_TRANSFER_FLUSH_EXPLICIT
),
504 DEBUG_NAMED_VALUE(PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
),
505 DEBUG_NAMED_VALUE(PIPE_TRANSFER_PERSISTENT
),
506 DEBUG_NAMED_VALUE(PIPE_TRANSFER_COHERENT
),
507 DEBUG_NAMED_VALUE_END
510 debug_printf("%s: %s\n", msg
, debug_dump_flags(names
, usage
));
515 * Print PIPE_BIND_x flags with a message.
518 debug_print_bind_flags(const char *msg
, unsigned usage
)
520 static const struct debug_named_value names
[] = {
521 DEBUG_NAMED_VALUE(PIPE_BIND_DEPTH_STENCIL
),
522 DEBUG_NAMED_VALUE(PIPE_BIND_RENDER_TARGET
),
523 DEBUG_NAMED_VALUE(PIPE_BIND_BLENDABLE
),
524 DEBUG_NAMED_VALUE(PIPE_BIND_SAMPLER_VIEW
),
525 DEBUG_NAMED_VALUE(PIPE_BIND_VERTEX_BUFFER
),
526 DEBUG_NAMED_VALUE(PIPE_BIND_INDEX_BUFFER
),
527 DEBUG_NAMED_VALUE(PIPE_BIND_CONSTANT_BUFFER
),
528 DEBUG_NAMED_VALUE(PIPE_BIND_DISPLAY_TARGET
),
529 DEBUG_NAMED_VALUE(PIPE_BIND_TRANSFER_WRITE
),
530 DEBUG_NAMED_VALUE(PIPE_BIND_TRANSFER_READ
),
531 DEBUG_NAMED_VALUE(PIPE_BIND_STREAM_OUTPUT
),
532 DEBUG_NAMED_VALUE(PIPE_BIND_CURSOR
),
533 DEBUG_NAMED_VALUE(PIPE_BIND_CUSTOM
),
534 DEBUG_NAMED_VALUE(PIPE_BIND_GLOBAL
),
535 DEBUG_NAMED_VALUE(PIPE_BIND_SHADER_BUFFER
),
536 DEBUG_NAMED_VALUE(PIPE_BIND_SHADER_IMAGE
),
537 DEBUG_NAMED_VALUE(PIPE_BIND_COMPUTE_RESOURCE
),
538 DEBUG_NAMED_VALUE(PIPE_BIND_COMMAND_ARGS_BUFFER
),
539 DEBUG_NAMED_VALUE(PIPE_BIND_SCANOUT
),
540 DEBUG_NAMED_VALUE(PIPE_BIND_SHARED
),
541 DEBUG_NAMED_VALUE(PIPE_BIND_LINEAR
),
542 DEBUG_NAMED_VALUE_END
545 debug_printf("%s: %s\n", msg
, debug_dump_flags(names
, usage
));
550 * Print PIPE_USAGE_x enum values with a message.
553 debug_print_usage_enum(const char *msg
, unsigned usage
)
555 static const struct debug_named_value names
[] = {
556 DEBUG_NAMED_VALUE(PIPE_USAGE_DEFAULT
),
557 DEBUG_NAMED_VALUE(PIPE_USAGE_IMMUTABLE
),
558 DEBUG_NAMED_VALUE(PIPE_USAGE_DYNAMIC
),
559 DEBUG_NAMED_VALUE(PIPE_USAGE_STREAM
),
560 DEBUG_NAMED_VALUE(PIPE_USAGE_STAGING
),
561 DEBUG_NAMED_VALUE_END
564 debug_printf("%s: %s\n", msg
, debug_dump_enum(names
, usage
));