u_debug.c revision 01e04c3f
101e04c3fSmrg/************************************************************************** 201e04c3fSmrg * 301e04c3fSmrg * Copyright 2008 VMware, Inc. 401e04c3fSmrg * Copyright (c) 2008 VMware, Inc. 501e04c3fSmrg * All Rights Reserved. 601e04c3fSmrg * 701e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 801e04c3fSmrg * copy of this software and associated documentation files (the 901e04c3fSmrg * "Software"), to deal in the Software without restriction, including 1001e04c3fSmrg * without limitation the rights to use, copy, modify, merge, publish, 1101e04c3fSmrg * distribute, sub license, and/or sell copies of the Software, and to 1201e04c3fSmrg * permit persons to whom the Software is furnished to do so, subject to 1301e04c3fSmrg * the following conditions: 1401e04c3fSmrg * 1501e04c3fSmrg * The above copyright notice and this permission notice (including the 1601e04c3fSmrg * next paragraph) shall be included in all copies or substantial portions 1701e04c3fSmrg * of the Software. 1801e04c3fSmrg * 1901e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 2001e04c3fSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2101e04c3fSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 2201e04c3fSmrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 2301e04c3fSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 2401e04c3fSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 2501e04c3fSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2601e04c3fSmrg * 2701e04c3fSmrg **************************************************************************/ 2801e04c3fSmrg 2901e04c3fSmrg 3001e04c3fSmrg#include "pipe/p_config.h" 3101e04c3fSmrg 3201e04c3fSmrg#include "pipe/p_compiler.h" 3301e04c3fSmrg#include "util/u_debug.h" 3401e04c3fSmrg#include "pipe/p_format.h" 3501e04c3fSmrg#include "pipe/p_state.h" 3601e04c3fSmrg#include "util/u_string.h" 3701e04c3fSmrg#include "util/u_math.h" 3801e04c3fSmrg#include <inttypes.h> 3901e04c3fSmrg 4001e04c3fSmrg#include <stdio.h> 4101e04c3fSmrg#include <limits.h> /* CHAR_BIT */ 4201e04c3fSmrg#include <ctype.h> /* isalnum */ 4301e04c3fSmrg 4401e04c3fSmrg#ifdef _WIN32 4501e04c3fSmrg#include <windows.h> 4601e04c3fSmrg#include <stdlib.h> 4701e04c3fSmrg#endif 4801e04c3fSmrg 4901e04c3fSmrg 5001e04c3fSmrgvoid 5101e04c3fSmrg_debug_vprintf(const char *format, va_list ap) 5201e04c3fSmrg{ 5301e04c3fSmrg static char buf[4096] = {'\0'}; 5401e04c3fSmrg#if defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_EMBEDDED) 5501e04c3fSmrg /* We buffer until we find a newline. */ 5601e04c3fSmrg size_t len = strlen(buf); 5701e04c3fSmrg int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap); 5801e04c3fSmrg if (ret > (int)(sizeof(buf) - len - 1) || util_strchr(buf + len, '\n')) { 5901e04c3fSmrg os_log_message(buf); 6001e04c3fSmrg buf[0] = '\0'; 6101e04c3fSmrg } 6201e04c3fSmrg#else 6301e04c3fSmrg util_vsnprintf(buf, sizeof(buf), format, ap); 6401e04c3fSmrg os_log_message(buf); 6501e04c3fSmrg#endif 6601e04c3fSmrg} 6701e04c3fSmrg 6801e04c3fSmrg 6901e04c3fSmrgvoid 7001e04c3fSmrg_pipe_debug_message(struct pipe_debug_callback *cb, 7101e04c3fSmrg unsigned *id, 7201e04c3fSmrg enum pipe_debug_type type, 7301e04c3fSmrg const char *fmt, ...) 7401e04c3fSmrg{ 7501e04c3fSmrg va_list args; 7601e04c3fSmrg va_start(args, fmt); 7701e04c3fSmrg if (cb && cb->debug_message) 7801e04c3fSmrg cb->debug_message(cb->data, id, type, fmt, args); 7901e04c3fSmrg va_end(args); 8001e04c3fSmrg} 8101e04c3fSmrg 8201e04c3fSmrg 8301e04c3fSmrgvoid 8401e04c3fSmrgdebug_disable_error_message_boxes(void) 8501e04c3fSmrg{ 8601e04c3fSmrg#ifdef _WIN32 8701e04c3fSmrg /* When Windows' error message boxes are disabled for this process (as is 8801e04c3fSmrg * typically the case when running tests in an automated fashion) we disable 8901e04c3fSmrg * CRT message boxes too. 9001e04c3fSmrg */ 9101e04c3fSmrg UINT uMode = SetErrorMode(0); 9201e04c3fSmrg SetErrorMode(uMode); 9301e04c3fSmrg if (uMode & SEM_FAILCRITICALERRORS) { 9401e04c3fSmrg /* Disable assertion failure message box. 9501e04c3fSmrg * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx 9601e04c3fSmrg */ 9701e04c3fSmrg _set_error_mode(_OUT_TO_STDERR); 9801e04c3fSmrg#ifdef _MSC_VER 9901e04c3fSmrg /* Disable abort message box. 10001e04c3fSmrg * http://msdn.microsoft.com/en-us/library/e631wekh.aspx 10101e04c3fSmrg */ 10201e04c3fSmrg _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); 10301e04c3fSmrg#endif 10401e04c3fSmrg } 10501e04c3fSmrg#endif /* _WIN32 */ 10601e04c3fSmrg} 10701e04c3fSmrg 10801e04c3fSmrg 10901e04c3fSmrg#ifdef DEBUG 11001e04c3fSmrgvoid 11101e04c3fSmrgdebug_print_blob(const char *name, const void *blob, unsigned size) 11201e04c3fSmrg{ 11301e04c3fSmrg const unsigned *ublob = (const unsigned *)blob; 11401e04c3fSmrg unsigned i; 11501e04c3fSmrg 11601e04c3fSmrg debug_printf("%s (%d dwords%s)\n", name, size/4, 11701e04c3fSmrg size%4 ? "... plus a few bytes" : ""); 11801e04c3fSmrg 11901e04c3fSmrg for (i = 0; i < size/4; i++) { 12001e04c3fSmrg debug_printf("%d:\t%08x\n", i, ublob[i]); 12101e04c3fSmrg } 12201e04c3fSmrg} 12301e04c3fSmrg#endif 12401e04c3fSmrg 12501e04c3fSmrg 12601e04c3fSmrgstatic boolean 12701e04c3fSmrgdebug_get_option_should_print(void) 12801e04c3fSmrg{ 12901e04c3fSmrg static boolean first = TRUE; 13001e04c3fSmrg static boolean value = FALSE; 13101e04c3fSmrg 13201e04c3fSmrg if (!first) 13301e04c3fSmrg return value; 13401e04c3fSmrg 13501e04c3fSmrg /* Oh hey this will call into this function, 13601e04c3fSmrg * but its cool since we set first to false 13701e04c3fSmrg */ 13801e04c3fSmrg first = FALSE; 13901e04c3fSmrg value = debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE); 14001e04c3fSmrg /* XXX should we print this option? Currently it wont */ 14101e04c3fSmrg return value; 14201e04c3fSmrg} 14301e04c3fSmrg 14401e04c3fSmrg 14501e04c3fSmrgconst char * 14601e04c3fSmrgdebug_get_option(const char *name, const char *dfault) 14701e04c3fSmrg{ 14801e04c3fSmrg const char *result; 14901e04c3fSmrg 15001e04c3fSmrg result = os_get_option(name); 15101e04c3fSmrg if (!result) 15201e04c3fSmrg result = dfault; 15301e04c3fSmrg 15401e04c3fSmrg if (debug_get_option_should_print()) 15501e04c3fSmrg debug_printf("%s: %s = %s\n", __FUNCTION__, name, 15601e04c3fSmrg result ? result : "(null)"); 15701e04c3fSmrg 15801e04c3fSmrg return result; 15901e04c3fSmrg} 16001e04c3fSmrg 16101e04c3fSmrg 16201e04c3fSmrgboolean 16301e04c3fSmrgdebug_get_bool_option(const char *name, boolean dfault) 16401e04c3fSmrg{ 16501e04c3fSmrg const char *str = os_get_option(name); 16601e04c3fSmrg boolean result; 16701e04c3fSmrg 16801e04c3fSmrg if (str == NULL) 16901e04c3fSmrg result = dfault; 17001e04c3fSmrg else if (!util_strcmp(str, "n")) 17101e04c3fSmrg result = FALSE; 17201e04c3fSmrg else if (!util_strcmp(str, "no")) 17301e04c3fSmrg result = FALSE; 17401e04c3fSmrg else if (!util_strcmp(str, "0")) 17501e04c3fSmrg result = FALSE; 17601e04c3fSmrg else if (!util_strcmp(str, "f")) 17701e04c3fSmrg result = FALSE; 17801e04c3fSmrg else if (!util_strcmp(str, "F")) 17901e04c3fSmrg result = FALSE; 18001e04c3fSmrg else if (!util_strcmp(str, "false")) 18101e04c3fSmrg result = FALSE; 18201e04c3fSmrg else if (!util_strcmp(str, "FALSE")) 18301e04c3fSmrg result = FALSE; 18401e04c3fSmrg else 18501e04c3fSmrg result = TRUE; 18601e04c3fSmrg 18701e04c3fSmrg if (debug_get_option_should_print()) 18801e04c3fSmrg debug_printf("%s: %s = %s\n", __FUNCTION__, name, 18901e04c3fSmrg result ? "TRUE" : "FALSE"); 19001e04c3fSmrg 19101e04c3fSmrg return result; 19201e04c3fSmrg} 19301e04c3fSmrg 19401e04c3fSmrg 19501e04c3fSmrglong 19601e04c3fSmrgdebug_get_num_option(const char *name, long dfault) 19701e04c3fSmrg{ 19801e04c3fSmrg long result; 19901e04c3fSmrg const char *str; 20001e04c3fSmrg 20101e04c3fSmrg str = os_get_option(name); 20201e04c3fSmrg if (!str) { 20301e04c3fSmrg result = dfault; 20401e04c3fSmrg } else { 20501e04c3fSmrg char *endptr; 20601e04c3fSmrg 20701e04c3fSmrg result = strtol(str, &endptr, 0); 20801e04c3fSmrg if (str == endptr) { 20901e04c3fSmrg /* Restore the default value when no digits were found. */ 21001e04c3fSmrg result = dfault; 21101e04c3fSmrg } 21201e04c3fSmrg } 21301e04c3fSmrg 21401e04c3fSmrg if (debug_get_option_should_print()) 21501e04c3fSmrg debug_printf("%s: %s = %li\n", __FUNCTION__, name, result); 21601e04c3fSmrg 21701e04c3fSmrg return result; 21801e04c3fSmrg} 21901e04c3fSmrg 22001e04c3fSmrg 22101e04c3fSmrgstatic boolean 22201e04c3fSmrgstr_has_option(const char *str, const char *name) 22301e04c3fSmrg{ 22401e04c3fSmrg /* Empty string. */ 22501e04c3fSmrg if (!*str) { 22601e04c3fSmrg return FALSE; 22701e04c3fSmrg } 22801e04c3fSmrg 22901e04c3fSmrg /* OPTION=all */ 23001e04c3fSmrg if (!util_strcmp(str, "all")) { 23101e04c3fSmrg return TRUE; 23201e04c3fSmrg } 23301e04c3fSmrg 23401e04c3fSmrg /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */ 23501e04c3fSmrg { 23601e04c3fSmrg const char *start = str; 23701e04c3fSmrg unsigned name_len = strlen(name); 23801e04c3fSmrg 23901e04c3fSmrg /* 'start' is the beginning of the currently-parsed word, 24001e04c3fSmrg * we increment 'str' each iteration. 24101e04c3fSmrg * if we find either the end of string or a non-alphanumeric character, 24201e04c3fSmrg * we compare 'start' up to 'str-1' with 'name'. */ 24301e04c3fSmrg 24401e04c3fSmrg while (1) { 24501e04c3fSmrg if (!*str || !(isalnum(*str) || *str == '_')) { 24601e04c3fSmrg if (str-start == name_len && 24701e04c3fSmrg !memcmp(start, name, name_len)) { 24801e04c3fSmrg return TRUE; 24901e04c3fSmrg } 25001e04c3fSmrg 25101e04c3fSmrg if (!*str) { 25201e04c3fSmrg return FALSE; 25301e04c3fSmrg } 25401e04c3fSmrg 25501e04c3fSmrg start = str+1; 25601e04c3fSmrg } 25701e04c3fSmrg 25801e04c3fSmrg str++; 25901e04c3fSmrg } 26001e04c3fSmrg } 26101e04c3fSmrg 26201e04c3fSmrg return FALSE; 26301e04c3fSmrg} 26401e04c3fSmrg 26501e04c3fSmrg 26601e04c3fSmrguint64_t 26701e04c3fSmrgdebug_get_flags_option(const char *name, 26801e04c3fSmrg const struct debug_named_value *flags, 26901e04c3fSmrg uint64_t dfault) 27001e04c3fSmrg{ 27101e04c3fSmrg uint64_t result; 27201e04c3fSmrg const char *str; 27301e04c3fSmrg const struct debug_named_value *orig = flags; 27401e04c3fSmrg unsigned namealign = 0; 27501e04c3fSmrg 27601e04c3fSmrg str = os_get_option(name); 27701e04c3fSmrg if (!str) 27801e04c3fSmrg result = dfault; 27901e04c3fSmrg else if (!util_strcmp(str, "help")) { 28001e04c3fSmrg result = dfault; 28101e04c3fSmrg _debug_printf("%s: help for %s:\n", __FUNCTION__, name); 28201e04c3fSmrg for (; flags->name; ++flags) 28301e04c3fSmrg namealign = MAX2(namealign, strlen(flags->name)); 28401e04c3fSmrg for (flags = orig; flags->name; ++flags) 28501e04c3fSmrg _debug_printf("| %*s [0x%0*"PRIx64"]%s%s\n", namealign, flags->name, 28601e04c3fSmrg (int)sizeof(uint64_t)*CHAR_BIT/4, flags->value, 28701e04c3fSmrg flags->desc ? " " : "", flags->desc ? flags->desc : ""); 28801e04c3fSmrg } 28901e04c3fSmrg else { 29001e04c3fSmrg result = 0; 29101e04c3fSmrg while (flags->name) { 29201e04c3fSmrg if (str_has_option(str, flags->name)) 29301e04c3fSmrg result |= flags->value; 29401e04c3fSmrg ++flags; 29501e04c3fSmrg } 29601e04c3fSmrg } 29701e04c3fSmrg 29801e04c3fSmrg if (debug_get_option_should_print()) { 29901e04c3fSmrg if (str) { 30001e04c3fSmrg debug_printf("%s: %s = 0x%"PRIx64" (%s)\n", 30101e04c3fSmrg __FUNCTION__, name, result, str); 30201e04c3fSmrg } else { 30301e04c3fSmrg debug_printf("%s: %s = 0x%"PRIx64"\n", __FUNCTION__, name, result); 30401e04c3fSmrg } 30501e04c3fSmrg } 30601e04c3fSmrg 30701e04c3fSmrg return result; 30801e04c3fSmrg} 30901e04c3fSmrg 31001e04c3fSmrg 31101e04c3fSmrgvoid 31201e04c3fSmrg_debug_assert_fail(const char *expr, const char *file, unsigned line, 31301e04c3fSmrg const char *function) 31401e04c3fSmrg{ 31501e04c3fSmrg _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", 31601e04c3fSmrg file, line, function, expr); 31701e04c3fSmrg os_abort(); 31801e04c3fSmrg} 31901e04c3fSmrg 32001e04c3fSmrg 32101e04c3fSmrgconst char * 32201e04c3fSmrgdebug_dump_enum(const struct debug_named_value *names, 32301e04c3fSmrg unsigned long value) 32401e04c3fSmrg{ 32501e04c3fSmrg static char rest[64]; 32601e04c3fSmrg 32701e04c3fSmrg while (names->name) { 32801e04c3fSmrg if (names->value == value) 32901e04c3fSmrg return names->name; 33001e04c3fSmrg ++names; 33101e04c3fSmrg } 33201e04c3fSmrg 33301e04c3fSmrg util_snprintf(rest, sizeof(rest), "0x%08lx", value); 33401e04c3fSmrg return rest; 33501e04c3fSmrg} 33601e04c3fSmrg 33701e04c3fSmrg 33801e04c3fSmrgconst char * 33901e04c3fSmrgdebug_dump_enum_noprefix(const struct debug_named_value *names, 34001e04c3fSmrg const char *prefix, 34101e04c3fSmrg unsigned long value) 34201e04c3fSmrg{ 34301e04c3fSmrg static char rest[64]; 34401e04c3fSmrg 34501e04c3fSmrg while (names->name) { 34601e04c3fSmrg if (names->value == value) { 34701e04c3fSmrg const char *name = names->name; 34801e04c3fSmrg while (*name == *prefix) { 34901e04c3fSmrg name++; 35001e04c3fSmrg prefix++; 35101e04c3fSmrg } 35201e04c3fSmrg return name; 35301e04c3fSmrg } 35401e04c3fSmrg ++names; 35501e04c3fSmrg } 35601e04c3fSmrg 35701e04c3fSmrg util_snprintf(rest, sizeof(rest), "0x%08lx", value); 35801e04c3fSmrg return rest; 35901e04c3fSmrg} 36001e04c3fSmrg 36101e04c3fSmrg 36201e04c3fSmrgconst char * 36301e04c3fSmrgdebug_dump_flags(const struct debug_named_value *names, unsigned long value) 36401e04c3fSmrg{ 36501e04c3fSmrg static char output[4096]; 36601e04c3fSmrg static char rest[256]; 36701e04c3fSmrg int first = 1; 36801e04c3fSmrg 36901e04c3fSmrg output[0] = '\0'; 37001e04c3fSmrg 37101e04c3fSmrg while (names->name) { 37201e04c3fSmrg if ((names->value & value) == names->value) { 37301e04c3fSmrg if (!first) 37401e04c3fSmrg util_strncat(output, "|", sizeof(output) - strlen(output) - 1); 37501e04c3fSmrg else 37601e04c3fSmrg first = 0; 37701e04c3fSmrg util_strncat(output, names->name, sizeof(output) - strlen(output) - 1); 37801e04c3fSmrg output[sizeof(output) - 1] = '\0'; 37901e04c3fSmrg value &= ~names->value; 38001e04c3fSmrg } 38101e04c3fSmrg ++names; 38201e04c3fSmrg } 38301e04c3fSmrg 38401e04c3fSmrg if (value) { 38501e04c3fSmrg if (!first) 38601e04c3fSmrg util_strncat(output, "|", sizeof(output) - strlen(output) - 1); 38701e04c3fSmrg else 38801e04c3fSmrg first = 0; 38901e04c3fSmrg 39001e04c3fSmrg util_snprintf(rest, sizeof(rest), "0x%08lx", value); 39101e04c3fSmrg util_strncat(output, rest, sizeof(output) - strlen(output) - 1); 39201e04c3fSmrg output[sizeof(output) - 1] = '\0'; 39301e04c3fSmrg } 39401e04c3fSmrg 39501e04c3fSmrg if (first) 39601e04c3fSmrg return "0"; 39701e04c3fSmrg 39801e04c3fSmrg return output; 39901e04c3fSmrg} 40001e04c3fSmrg 40101e04c3fSmrg 40201e04c3fSmrg 40301e04c3fSmrg#ifdef DEBUG 40401e04c3fSmrgint fl_indent = 0; 40501e04c3fSmrgconst char* fl_function[1024]; 40601e04c3fSmrg 40701e04c3fSmrgint 40801e04c3fSmrgdebug_funclog_enter(const char* f, UNUSED const int line, 40901e04c3fSmrg UNUSED const char* file) 41001e04c3fSmrg{ 41101e04c3fSmrg int i; 41201e04c3fSmrg 41301e04c3fSmrg for (i = 0; i < fl_indent; i++) 41401e04c3fSmrg debug_printf(" "); 41501e04c3fSmrg debug_printf("%s\n", f); 41601e04c3fSmrg 41701e04c3fSmrg assert(fl_indent < 1023); 41801e04c3fSmrg fl_function[fl_indent++] = f; 41901e04c3fSmrg 42001e04c3fSmrg return 0; 42101e04c3fSmrg} 42201e04c3fSmrg 42301e04c3fSmrgvoid 42401e04c3fSmrgdebug_funclog_exit(const char* f, UNUSED const int line, 42501e04c3fSmrg UNUSED const char* file) 42601e04c3fSmrg{ 42701e04c3fSmrg --fl_indent; 42801e04c3fSmrg assert(fl_indent >= 0); 42901e04c3fSmrg assert(fl_function[fl_indent] == f); 43001e04c3fSmrg} 43101e04c3fSmrg 43201e04c3fSmrgvoid 43301e04c3fSmrgdebug_funclog_enter_exit(const char* f, UNUSED const int line, 43401e04c3fSmrg UNUSED const char* file) 43501e04c3fSmrg{ 43601e04c3fSmrg int i; 43701e04c3fSmrg for (i = 0; i < fl_indent; i++) 43801e04c3fSmrg debug_printf(" "); 43901e04c3fSmrg debug_printf("%s\n", f); 44001e04c3fSmrg} 44101e04c3fSmrg#endif 442