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