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_format.h"
39 #include "util/u_memory.h"
40 #include "util/u_string.h"
41 #include "util/u_math.h"
42 #include "util/u_prim.h"
46 #include <limits.h> /* CHAR_BIT */
47 #include <ctype.h> /* isalnum */
56 _debug_vprintf(const char *format
, va_list ap
)
58 static char buf
[4096] = {'\0'};
59 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_EMBEDDED)
60 /* We buffer until we find a newline. */
61 size_t len
= strlen(buf
);
62 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
63 if (ret
> (int)(sizeof(buf
) - len
- 1) || util_strchr(buf
+ len
, '\n')) {
68 util_vsnprintf(buf
, sizeof(buf
), format
, ap
);
75 _pipe_debug_message(struct pipe_debug_callback
*cb
,
77 enum pipe_debug_type type
,
82 if (cb
&& cb
->debug_message
)
83 cb
->debug_message(cb
->data
, id
, type
, fmt
, args
);
89 debug_disable_error_message_boxes(void)
92 /* When Windows' error message boxes are disabled for this process (as is
93 * typically the case when running tests in an automated fashion) we disable
94 * CRT message boxes too.
96 UINT uMode
= SetErrorMode(0);
98 if (uMode
& SEM_FAILCRITICALERRORS
) {
99 /* Disable assertion failure message box.
100 * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
102 _set_error_mode(_OUT_TO_STDERR
);
104 /* Disable abort message box.
105 * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
107 _set_abort_behavior(0, _WRITE_ABORT_MSG
| _CALL_REPORTFAULT
);
116 debug_print_blob(const char *name
, const void *blob
, unsigned size
)
118 const unsigned *ublob
= (const unsigned *)blob
;
121 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
122 size
%4 ? "... plus a few bytes" : "");
124 for (i
= 0; i
< size
/4; i
++) {
125 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
132 debug_get_option_should_print(void)
134 static boolean first
= TRUE
;
135 static boolean value
= FALSE
;
140 /* Oh hey this will call into this function,
141 * but its cool since we set first to false
144 value
= debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE
);
145 /* XXX should we print this option? Currently it wont */
151 debug_get_option(const char *name
, const char *dfault
)
155 result
= os_get_option(name
);
159 if (debug_get_option_should_print())
160 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
161 result
? result
: "(null)");
168 debug_get_bool_option(const char *name
, boolean dfault
)
170 const char *str
= os_get_option(name
);
175 else if (!util_strcmp(str
, "n"))
177 else if (!util_strcmp(str
, "no"))
179 else if (!util_strcmp(str
, "0"))
181 else if (!util_strcmp(str
, "f"))
183 else if (!util_strcmp(str
, "F"))
185 else if (!util_strcmp(str
, "false"))
187 else if (!util_strcmp(str
, "FALSE"))
192 if (debug_get_option_should_print())
193 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
,
194 result
? "TRUE" : "FALSE");
201 debug_get_num_option(const char *name
, long dfault
)
206 str
= os_get_option(name
);
212 result
= strtol(str
, &endptr
, 0);
214 /* Restore the default value when no digits were found. */
219 if (debug_get_option_should_print())
220 debug_printf("%s: %s = %li\n", __FUNCTION__
, name
, result
);
227 str_has_option(const char *str
, const char *name
)
235 if (!util_strcmp(str
, "all")) {
239 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
241 const char *start
= str
;
242 unsigned name_len
= strlen(name
);
244 /* 'start' is the beginning of the currently-parsed word,
245 * we increment 'str' each iteration.
246 * if we find either the end of string or a non-alphanumeric character,
247 * we compare 'start' up to 'str-1' with 'name'. */
250 if (!*str
|| !(isalnum(*str
) || *str
== '_')) {
251 if (str
-start
== name_len
&&
252 !memcmp(start
, name
, name_len
)) {
272 debug_get_flags_option(const char *name
,
273 const struct debug_named_value
*flags
,
278 const struct debug_named_value
*orig
= flags
;
279 unsigned namealign
= 0;
281 str
= os_get_option(name
);
284 else if (!util_strcmp(str
, "help")) {
286 _debug_printf("%s: help for %s:\n", __FUNCTION__
, name
);
287 for (; flags
->name
; ++flags
)
288 namealign
= MAX2(namealign
, strlen(flags
->name
));
289 for (flags
= orig
; flags
->name
; ++flags
)
290 _debug_printf("| %*s [0x%0*"PRIx64
"]%s%s\n", namealign
, flags
->name
,
291 (int)sizeof(uint64_t)*CHAR_BIT
/4, flags
->value
,
292 flags
->desc
? " " : "", flags
->desc
? flags
->desc
: "");
296 while (flags
->name
) {
297 if (str_has_option(str
, flags
->name
))
298 result
|= flags
->value
;
303 if (debug_get_option_should_print()) {
305 debug_printf("%s: %s = 0x%"PRIx64
" (%s)\n",
306 __FUNCTION__
, name
, result
, str
);
308 debug_printf("%s: %s = 0x%"PRIx64
"\n", __FUNCTION__
, name
, result
);
317 _debug_assert_fail(const char *expr
, const char *file
, unsigned line
,
318 const char *function
)
320 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n",
321 file
, line
, function
, expr
);
327 debug_dump_enum(const struct debug_named_value
*names
,
330 static char rest
[64];
332 while (names
->name
) {
333 if (names
->value
== value
)
338 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
344 debug_dump_enum_noprefix(const struct debug_named_value
*names
,
348 static char rest
[64];
350 while (names
->name
) {
351 if (names
->value
== value
) {
352 const char *name
= names
->name
;
353 while (*name
== *prefix
) {
362 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
368 debug_dump_flags(const struct debug_named_value
*names
, unsigned long value
)
370 static char output
[4096];
371 static char rest
[256];
376 while (names
->name
) {
377 if ((names
->value
& value
) == names
->value
) {
379 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
382 util_strncat(output
, names
->name
, sizeof(output
) - strlen(output
) - 1);
383 output
[sizeof(output
) - 1] = '\0';
384 value
&= ~names
->value
;
391 util_strncat(output
, "|", sizeof(output
) - strlen(output
) - 1);
395 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
396 util_strncat(output
, rest
, sizeof(output
) - strlen(output
) - 1);
397 output
[sizeof(output
) - 1] = '\0';
409 debug_print_format(const char *msg
, unsigned fmt
)
411 debug_printf("%s: %s\n", msg
, util_format_name(fmt
));
416 /** Return string name of given primitive type */
418 u_prim_name(enum pipe_prim_type prim
)
420 static const struct debug_named_value names
[] = {
421 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS
),
422 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES
),
423 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP
),
424 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP
),
425 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES
),
426 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP
),
427 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN
),
428 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS
),
429 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP
),
430 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON
),
431 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES_ADJACENCY
),
432 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP_ADJACENCY
),
433 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES_ADJACENCY
),
434 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
),
435 DEBUG_NAMED_VALUE_END
437 return debug_dump_enum(names
, prim
);
444 const char* fl_function
[1024];
447 debug_funclog_enter(const char* f
, UNUSED
const int line
,
448 UNUSED
const char* file
)
452 for (i
= 0; i
< fl_indent
; i
++)
454 debug_printf("%s\n", f
);
456 assert(fl_indent
< 1023);
457 fl_function
[fl_indent
++] = f
;
463 debug_funclog_exit(const char* f
, UNUSED
const int line
,
464 UNUSED
const char* file
)
467 assert(fl_indent
>= 0);
468 assert(fl_function
[fl_indent
] == f
);
472 debug_funclog_enter_exit(const char* f
, UNUSED
const int line
,
473 UNUSED
const char* file
)
476 for (i
= 0; i
< fl_indent
; i
++)
478 debug_printf("%s\n", f
);
486 * Print PIPE_TRANSFER_x flags with a message.
489 debug_print_transfer_flags(const char *msg
, unsigned usage
)
491 debug_printf("%s: ", msg
);
492 util_dump_transfer_usage(stdout
, usage
);
498 * Print PIPE_BIND_x flags with a message.
501 debug_print_bind_flags(const char *msg
, unsigned usage
)
503 static const struct debug_named_value names
[] = {
504 DEBUG_NAMED_VALUE(PIPE_BIND_DEPTH_STENCIL
),
505 DEBUG_NAMED_VALUE(PIPE_BIND_RENDER_TARGET
),
506 DEBUG_NAMED_VALUE(PIPE_BIND_BLENDABLE
),
507 DEBUG_NAMED_VALUE(PIPE_BIND_SAMPLER_VIEW
),
508 DEBUG_NAMED_VALUE(PIPE_BIND_VERTEX_BUFFER
),
509 DEBUG_NAMED_VALUE(PIPE_BIND_INDEX_BUFFER
),
510 DEBUG_NAMED_VALUE(PIPE_BIND_CONSTANT_BUFFER
),
511 DEBUG_NAMED_VALUE(PIPE_BIND_DISPLAY_TARGET
),
512 DEBUG_NAMED_VALUE(PIPE_BIND_STREAM_OUTPUT
),
513 DEBUG_NAMED_VALUE(PIPE_BIND_CURSOR
),
514 DEBUG_NAMED_VALUE(PIPE_BIND_CUSTOM
),
515 DEBUG_NAMED_VALUE(PIPE_BIND_GLOBAL
),
516 DEBUG_NAMED_VALUE(PIPE_BIND_SHADER_BUFFER
),
517 DEBUG_NAMED_VALUE(PIPE_BIND_SHADER_IMAGE
),
518 DEBUG_NAMED_VALUE(PIPE_BIND_COMPUTE_RESOURCE
),
519 DEBUG_NAMED_VALUE(PIPE_BIND_COMMAND_ARGS_BUFFER
),
520 DEBUG_NAMED_VALUE(PIPE_BIND_SCANOUT
),
521 DEBUG_NAMED_VALUE(PIPE_BIND_SHARED
),
522 DEBUG_NAMED_VALUE(PIPE_BIND_LINEAR
),
523 DEBUG_NAMED_VALUE_END
526 debug_printf("%s: %s\n", msg
, debug_dump_flags(names
, usage
));
531 * Print PIPE_USAGE_x enum values with a message.
534 debug_print_usage_enum(const char *msg
, enum pipe_resource_usage usage
)
536 static const struct debug_named_value names
[] = {
537 DEBUG_NAMED_VALUE(PIPE_USAGE_DEFAULT
),
538 DEBUG_NAMED_VALUE(PIPE_USAGE_IMMUTABLE
),
539 DEBUG_NAMED_VALUE(PIPE_USAGE_DYNAMIC
),
540 DEBUG_NAMED_VALUE(PIPE_USAGE_STREAM
),
541 DEBUG_NAMED_VALUE(PIPE_USAGE_STAGING
),
542 DEBUG_NAMED_VALUE_END
545 debug_printf("%s: %s\n", msg
, debug_dump_enum(names
, usage
));