1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright (c) 2008 VMware, Inc.
5 * All Rights Reserved.
6 *
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:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
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.
26 *
27 **************************************************************************/
28
29
30 /* (virglrenderer) removed includes for p_format.h and p_state.h */
31 #include "pipe/p_compiler.h"
32 #include "pipe/p_config.h"
33
34 #include "util/u_debug.h"
35 #include "util/u_string.h"
36 #include <inttypes.h>
37
38 #include <stdio.h>
39 #include <limits.h> /* CHAR_BIT */
40 #include <ctype.h> /* isalnum */
41
42 #ifdef _WIN32
43 #include <windows.h>
44 #include <stdlib.h>
45 #endif
46
47
48 void
_debug_vprintf(const char * format,va_list ap)49 _debug_vprintf(const char *format, va_list ap)
50 {
51 static char buf[4096] = {'\0'};
52 #if DETECT_OS_WINDOWS || defined(EMBEDDED_DEVICE)
53 /* We buffer until we find a newline. */
54 size_t len = strlen(buf);
55 int ret = vsnprintf(buf + len, sizeof(buf) - len, format, ap);
56 if (ret > (int)(sizeof(buf) - len - 1) || strchr(buf + len, '\n')) {
57 os_log_message(buf);
58 buf[0] = '\0';
59 }
60 #else
61 vsnprintf(buf, sizeof(buf), format, ap);
62 os_log_message(buf);
63 #endif
64 }
65
66
67 /* (virglrenderer) removed _pipe_debug_message */
68
69
70 void
debug_disable_error_message_boxes(void)71 debug_disable_error_message_boxes(void)
72 {
73 #ifdef _WIN32
74 /* When Windows' error message boxes are disabled for this process (as is
75 * typically the case when running tests in an automated fashion) we disable
76 * CRT message boxes too.
77 */
78 UINT uMode = SetErrorMode(0);
79 SetErrorMode(uMode);
80 if (uMode & SEM_FAILCRITICALERRORS) {
81 /* Disable assertion failure message box.
82 * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
83 */
84 _set_error_mode(_OUT_TO_STDERR);
85 #ifdef _MSC_VER
86 /* Disable abort message box.
87 * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
88 */
89 _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
90 #endif
91 }
92 #endif /* _WIN32 */
93 }
94
95
96 #ifdef DEBUG
97 void
debug_print_blob(const char * name,const void * blob,unsigned size)98 debug_print_blob(const char *name, const void *blob, unsigned size)
99 {
100 const unsigned *ublob = (const unsigned *)blob;
101 unsigned i;
102
103 debug_printf("%s (%d dwords%s)\n", name, size/4,
104 size%4 ? "... plus a few bytes" : "");
105
106 for (i = 0; i < size/4; i++) {
107 debug_printf("%d:\t%08x\n", i, ublob[i]);
108 }
109 }
110 #endif
111
112
113 static bool
debug_get_option_should_print(void)114 debug_get_option_should_print(void)
115 {
116 static bool first = true;
117 static bool value = false;
118
119 if (!first)
120 return value;
121
122 /* Oh hey this will call into this function,
123 * but its cool since we set first to false
124 */
125 first = false;
126 value = debug_get_bool_option("GALLIUM_PRINT_OPTIONS", false);
127 /* XXX should we print this option? Currently it wont */
128 return value;
129 }
130
131
132 const char *
debug_get_option(const char * name,const char * dfault)133 debug_get_option(const char *name, const char *dfault)
134 {
135 const char *result;
136
137 result = os_get_option(name);
138 if (!result)
139 result = dfault;
140
141 if (debug_get_option_should_print())
142 debug_printf("%s: %s = %s\n", __FUNCTION__, name,
143 result ? result : "(null)");
144
145 return result;
146 }
147
148
149 bool
debug_get_bool_option(const char * name,bool dfault)150 debug_get_bool_option(const char *name, bool dfault)
151 {
152 const char *str = os_get_option(name);
153 bool result;
154
155 if (str == NULL)
156 result = dfault;
157 else if (!strcmp(str, "n"))
158 result = false;
159 else if (!strcmp(str, "no"))
160 result = false;
161 else if (!strcmp(str, "0"))
162 result = false;
163 else if (!strcmp(str, "f"))
164 result = false;
165 else if (!strcmp(str, "F"))
166 result = false;
167 else if (!strcmp(str, "false"))
168 result = false;
169 else if (!strcmp(str, "FALSE"))
170 result = false;
171 else
172 result = true;
173
174 if (debug_get_option_should_print())
175 debug_printf("%s: %s = %s\n", __FUNCTION__, name,
176 result ? "TRUE" : "FALSE");
177
178 return result;
179 }
180
181
182 long
debug_get_num_option(const char * name,long dfault)183 debug_get_num_option(const char *name, long dfault)
184 {
185 long result;
186 const char *str;
187
188 str = os_get_option(name);
189 if (!str) {
190 result = dfault;
191 } else {
192 char *endptr;
193
194 result = strtol(str, &endptr, 0);
195 if (str == endptr) {
196 /* Restore the default value when no digits were found. */
197 result = dfault;
198 }
199 }
200
201 if (debug_get_option_should_print())
202 debug_printf("%s: %s = %li\n", __FUNCTION__, name, result);
203
204 return result;
205 }
206
207 void
debug_get_version_option(const char * name,unsigned * major,unsigned * minor)208 debug_get_version_option(const char *name, unsigned *major, unsigned *minor)
209 {
210 const char *str;
211
212 str = os_get_option(name);
213 if (str) {
214 unsigned v_maj, v_min;
215 int n;
216
217 n = sscanf(str, "%u.%u", &v_maj, &v_min);
218 if (n != 2) {
219 debug_printf("Illegal version specified for %s : %s\n", name, str);
220 return;
221 }
222 *major = v_maj;
223 *minor = v_min;
224 }
225
226 if (debug_get_option_should_print())
227 debug_printf("%s: %s = %u.%u\n", __FUNCTION__, name, *major, *minor);
228
229 return;
230 }
231
232 static bool
str_has_option(const char * str,const char * name)233 str_has_option(const char *str, const char *name)
234 {
235 /* Empty string. */
236 if (!*str) {
237 return false;
238 }
239
240 /* OPTION=all */
241 if (!strcmp(str, "all")) {
242 return true;
243 }
244
245 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
246 {
247 const char *start = str;
248 unsigned name_len = strlen(name);
249
250 /* 'start' is the beginning of the currently-parsed word,
251 * we increment 'str' each iteration.
252 * if we find either the end of string or a non-alphanumeric character,
253 * we compare 'start' up to 'str-1' with 'name'. */
254
255 while (1) {
256 if (!*str || !(isalnum(*str) || *str == '_')) {
257 if (str-start == name_len &&
258 !memcmp(start, name, name_len)) {
259 return true;
260 }
261
262 if (!*str) {
263 return false;
264 }
265
266 start = str+1;
267 }
268
269 str++;
270 }
271 }
272
273 return false;
274 }
275
276
277 uint64_t
debug_get_flags_option(const char * name,const struct debug_named_value * flags,uint64_t dfault)278 debug_get_flags_option(const char *name,
279 const struct debug_named_value *flags,
280 uint64_t dfault)
281 {
282 uint64_t result;
283 const char *str;
284 const struct debug_named_value *orig = flags;
285 unsigned namealign = 0;
286
287 str = os_get_option(name);
288 if (!str)
289 result = dfault;
290 else if (!strcmp(str, "help")) {
291 result = dfault;
292 _debug_printf("%s: help for %s:\n", __FUNCTION__, name);
293 for (; flags->name; ++flags)
294 namealign = MAX2(namealign, strlen(flags->name));
295 for (flags = orig; flags->name; ++flags)
296 _debug_printf("| %*s [0x%0*"PRIx64"]%s%s\n", namealign, flags->name,
297 (int)sizeof(uint64_t)*CHAR_BIT/4, flags->value,
298 flags->desc ? " " : "", flags->desc ? flags->desc : "");
299 }
300 else {
301 result = 0;
302 while (flags->name) {
303 if (str_has_option(str, flags->name))
304 result |= flags->value;
305 ++flags;
306 }
307 }
308
309 if (debug_get_option_should_print()) {
310 if (str) {
311 debug_printf("%s: %s = 0x%"PRIx64" (%s)\n",
312 __FUNCTION__, name, result, str);
313 } else {
314 debug_printf("%s: %s = 0x%"PRIx64"\n", __FUNCTION__, name, result);
315 }
316 }
317
318 return result;
319 }
320
321
322 void
_debug_assert_fail(const char * expr,const char * file,unsigned line,const char * function)323 _debug_assert_fail(const char *expr, const char *file, unsigned line,
324 const char *function)
325 {
326 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n",
327 file, line, function, expr);
328 os_abort();
329 }
330
331
332 const char *
debug_dump_enum(const struct debug_named_value * names,unsigned long value)333 debug_dump_enum(const struct debug_named_value *names,
334 unsigned long value)
335 {
336 static char rest[64];
337
338 while (names->name) {
339 if (names->value == value)
340 return names->name;
341 ++names;
342 }
343
344 snprintf(rest, sizeof(rest), "0x%08lx", value);
345 return rest;
346 }
347
348
349 const char *
debug_dump_enum_noprefix(const struct debug_named_value * names,const char * prefix,unsigned long value)350 debug_dump_enum_noprefix(const struct debug_named_value *names,
351 const char *prefix,
352 unsigned long value)
353 {
354 static char rest[64];
355
356 while (names->name) {
357 if (names->value == value) {
358 const char *name = names->name;
359 while (*name == *prefix) {
360 name++;
361 prefix++;
362 }
363 return name;
364 }
365 ++names;
366 }
367
368 snprintf(rest, sizeof(rest), "0x%08lx", value);
369 return rest;
370 }
371
372
373 const char *
debug_dump_flags(const struct debug_named_value * names,unsigned long value)374 debug_dump_flags(const struct debug_named_value *names, unsigned long value)
375 {
376 static char output[4096];
377 static char rest[256];
378 int first = 1;
379
380 output[0] = '\0';
381
382 while (names->name) {
383 if ((names->value & value) == names->value) {
384 if (!first)
385 strncat(output, "|", sizeof(output) - strlen(output) - 1);
386 else
387 first = 0;
388 strncat(output, names->name, sizeof(output) - strlen(output) - 1);
389 output[sizeof(output) - 1] = '\0';
390 value &= ~names->value;
391 }
392 ++names;
393 }
394
395 if (value) {
396 if (!first)
397 strncat(output, "|", sizeof(output) - strlen(output) - 1);
398 else
399 first = 0;
400
401 snprintf(rest, sizeof(rest), "0x%08lx", value);
402 strncat(output, rest, sizeof(output) - strlen(output) - 1);
403 output[sizeof(output) - 1] = '\0';
404 }
405
406 if (first)
407 return "0";
408
409 return output;
410 }
411
412
413
414 #ifdef DEBUG
415 int fl_indent = 0;
416 const char* fl_function[1024];
417
418 int
debug_funclog_enter(const char * f,UNUSED const int line,UNUSED const char * file)419 debug_funclog_enter(const char* f, UNUSED const int line,
420 UNUSED const char* file)
421 {
422 int i;
423
424 for (i = 0; i < fl_indent; i++)
425 debug_printf(" ");
426 debug_printf("%s\n", f);
427
428 assert(fl_indent < 1023);
429 fl_function[fl_indent++] = f;
430
431 return 0;
432 }
433
434 void
debug_funclog_exit(const char * f,UNUSED const int line,UNUSED const char * file)435 debug_funclog_exit(const char* f, UNUSED const int line,
436 UNUSED const char* file)
437 {
438 --fl_indent;
439 assert(fl_indent >= 0);
440 assert(fl_function[fl_indent] == f);
441 }
442
443 void
debug_funclog_enter_exit(const char * f,UNUSED const int line,UNUSED const char * file)444 debug_funclog_enter_exit(const char* f, UNUSED const int line,
445 UNUSED const char* file)
446 {
447 int i;
448 for (i = 0; i < fl_indent; i++)
449 debug_printf(" ");
450 debug_printf("%s\n", f);
451 }
452 #endif
453