1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 #include "pipe/p_config.h"
33 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
41 #include "pipe/p_compiler.h"
42 #include "pipe/p_util.h"
43 #include "pipe/p_debug.h"
44 #include "pipe/p_format.h"
45 #include "util/u_string.h"
48 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
50 _EngDebugPrint(const char *format
, ...)
54 EngDebugPrint("", (PCHAR
)format
, ap
);
60 void _debug_vprintf(const char *format
, va_list ap
)
62 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
64 /* EngDebugPrint does not handle float point arguments, so we need to use
65 * our own vsnprintf implementation. It is also very slow, so buffer until
66 * we find a newline. */
67 static char buf
[512 + 1] = {'\0'};
68 size_t len
= strlen(buf
);
69 int ret
= util_vsnprintf(buf
+ len
, sizeof(buf
) - len
, format
, ap
);
70 if(ret
> (int)(sizeof(buf
) - len
- 1) || strchr(buf
+ len
, '\n')) {
71 _EngDebugPrint("%s", buf
);
75 /* TODO: Implement debug print for WINCE */
78 vfprintf(stderr
, format
, ap
);
84 void debug_print_blob( const char *name
,
88 const unsigned *ublob
= (const unsigned *)blob
;
91 debug_printf("%s (%d dwords%s)\n", name
, size
/4,
92 size
%4 ? "... plus a few bytes" : "");
94 for (i
= 0; i
< size
/4; i
++) {
95 debug_printf("%d:\t%08x\n", i
, ublob
[i
]);
101 void _debug_break(void)
103 #if (defined(__i386__) || defined(__386__)) && defined(__GNUC__)
105 #elif (defined(__i386__) || defined(__386__)) && defined(__MSC__)
107 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) && !defined(WINCE)
115 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
117 find(const char *start
, const char *end
, char c
)
120 for(p
= start
; !end
|| p
!= end
; ++p
) {
130 compare(const char *start
, const char *end
, const char *s
)
133 for(p
= start
, q
= s
; p
!= end
&& *q
!= '\0'; ++p
, ++q
) {
137 return p
== end
&& *q
== '\0';
141 copy(char *dst
, const char *start
, const char *end
, size_t n
)
145 for(p
= start
, q
= dst
, n
= n
- 1; p
!= end
&& n
; ++p
, ++q
, --n
)
153 debug_get_option(const char *name
, const char *dfault
)
156 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
158 const void *pMap
= NULL
;
159 const char *sol
, *eol
, *sep
;
160 static char output
[1024];
163 /* XXX: this creates the file if it does not exists, so it must either be
164 * disabled on release versions, or put in a less conspicuous place.
166 pMap
= EngMapFile(L
"\\??\\c:\\gallium.cfg", 0, &iFile
);
168 sol
= (const char *)pMap
;
170 /* TODO: handle LF line endings */
171 eol
= find(sol
, NULL
, '\r');
172 if(!eol
|| eol
== sol
)
174 sep
= find(sol
, eol
, '=');
177 if(compare(sol
, sep
, name
)) {
178 copy(output
, sep
+ 1, eol
, sizeof(output
));
188 result
= getenv(name
);
193 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? result
: "(null)");
199 debug_get_bool_option(const char *name
, boolean dfault
)
201 const char *str
= debug_get_option(name
, NULL
);
206 else if(!strcmp(str
, "n"))
208 else if(!strcmp(str
, "no"))
210 else if(!strcmp(str
, "0"))
212 else if(!strcmp(str
, "f"))
214 else if(!strcmp(str
, "false"))
219 debug_printf("%s: %s = %s\n", __FUNCTION__
, name
, result
? "TRUE" : "FALSE");
226 debug_get_num_option(const char *name
, long dfault
)
234 debug_get_flags_option(const char *name
,
235 const struct debug_named_value
*flags
,
236 unsigned long dfault
)
238 unsigned long result
;
241 str
= debug_get_option(name
, NULL
);
246 while( flags
->name
) {
247 if (!strcmp(str
, "all") || strstr(str
, flags
->name
))
248 result
|= flags
->value
;
253 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__
, name
, result
);
259 void _debug_assert_fail(const char *expr
,
262 const char *function
)
264 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file
, line
, function
, expr
);
265 if (debug_get_bool_option("GALLIUM_ABORT_ON_ASSERT", TRUE
))
268 _debug_printf("continuing...\n");
273 debug_dump_enum(const struct debug_named_value
*names
,
276 static char rest
[256];
279 if(names
->value
== value
)
284 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
290 debug_dump_flags(const struct debug_named_value
*names
,
293 static char output
[4096];
294 static char rest
[256];
300 if((names
->value
& value
) == names
->value
) {
302 strncat(output
, "|", sizeof(output
));
305 strncat(output
, names
->name
, sizeof(output
));
306 value
&= ~names
->value
;
313 strncat(output
, "|", sizeof(output
));
317 util_snprintf(rest
, sizeof(rest
), "0x%08lx", value
);
318 strncat(output
, rest
, sizeof(output
));
332 char *pf_sprint_name( char *str
, enum pipe_format format
)
334 strcpy( str
, "PIPE_FORMAT_" );
335 switch (pf_layout( format
)) {
336 case PIPE_FORMAT_LAYOUT_RGBAZS
:
338 pipe_format_rgbazs_t rgbazs
= (pipe_format_rgbazs_t
) format
;
340 uint scale
= 1 << (pf_exp8( rgbazs
) * 3);
342 for (i
= 0; i
< 4; i
++) {
343 uint size
= pf_size_xyzw( rgbazs
, i
);
348 switch (pf_swizzle_xyzw( rgbazs
, i
)) {
349 case PIPE_FORMAT_COMP_R
:
352 case PIPE_FORMAT_COMP_G
:
355 case PIPE_FORMAT_COMP_B
:
358 case PIPE_FORMAT_COMP_A
:
361 case PIPE_FORMAT_COMP_0
:
364 case PIPE_FORMAT_COMP_1
:
367 case PIPE_FORMAT_COMP_Z
:
370 case PIPE_FORMAT_COMP_S
:
374 util_snprintf( &str
[strlen( str
)], 32, "%u", size
* scale
);
379 switch (pf_type( rgbazs
)) {
380 case PIPE_FORMAT_TYPE_UNKNOWN
:
381 strcat( str
, "NONE" );
383 case PIPE_FORMAT_TYPE_FLOAT
:
384 strcat( str
, "FLOAT" );
386 case PIPE_FORMAT_TYPE_UNORM
:
387 strcat( str
, "UNORM" );
389 case PIPE_FORMAT_TYPE_SNORM
:
390 strcat( str
, "SNORM" );
392 case PIPE_FORMAT_TYPE_USCALED
:
393 strcat( str
, "USCALED" );
395 case PIPE_FORMAT_TYPE_SSCALED
:
396 strcat( str
, "SSCALED" );
401 case PIPE_FORMAT_LAYOUT_YCBCR
:
403 pipe_format_ycbcr_t ycbcr
= (pipe_format_ycbcr_t
) format
;
405 strcat( str
, "YCBCR" );
406 if (pf_rev( ycbcr
)) {
407 strcat( str
, "_REV" );
416 void debug_print_format(const char *msg
, enum pipe_format fmt
)
420 pf_sprint_name(fmtstr
, fmt
);
422 debug_printf("%s: %s\n", msg
, fmtstr
);