errors.c revision 5dd750d685d3f80b3783b1205f6727cb867c049a
1/** 2 * \file errors.c 3 * Mesa debugging and error handling functions. 4 */ 5 6/* 7 * Mesa 3-D graphics library 8 * Version: 7.1 9 * 10 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. 11 * 12 * Permission is hereby granted, free of charge, to any person obtaining a 13 * copy of this software and associated documentation files (the "Software"), 14 * to deal in the Software without restriction, including without limitation 15 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 16 * and/or sell copies of the Software, and to permit persons to whom the 17 * Software is furnished to do so, subject to the following conditions: 18 * 19 * The above copyright notice and this permission notice shall be included 20 * in all copies or substantial portions of the Software. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 */ 29 30 31#include "errors.h" 32 33#include "imports.h" 34#include "context.h" 35#include "dispatch.h" 36#include "mtypes.h" 37#include "version.h" 38 39 40#define MAXSTRING MAX_DEBUG_MESSAGE_LENGTH 41 42 43static char out_of_memory[] = "Debugging error: out of memory"; 44 45/** 46 * 'buf' is not necessarily a null-terminated string. When logging, copy 47 * 'len' characters from it, store them in a new, null-terminated string, 48 * and remember the number of bytes used by that string, *including* 49 * the null terminator this time. 50 */ 51static void 52_mesa_log_msg(struct gl_context *ctx, GLenum source, GLenum type, 53 GLuint id, GLenum severity, GLint len, const char *buf) 54{ 55 GLint nextEmpty; 56 struct gl_debug_msg *emptySlot; 57 58 assert(len >= 0 && len < MAX_DEBUG_MESSAGE_LENGTH); 59 60 if (ctx->Debug.Callback) { 61 ctx->Debug.Callback(source, type, id, severity, 62 len, buf, ctx->Debug.CallbackData); 63 return; 64 } 65 66 if (ctx->Debug.NumMessages == MAX_DEBUG_LOGGED_MESSAGES) 67 return; 68 69 nextEmpty = (ctx->Debug.NextMsg + ctx->Debug.NumMessages) 70 % MAX_DEBUG_LOGGED_MESSAGES; 71 emptySlot = &ctx->Debug.Log[nextEmpty]; 72 73 assert(!emptySlot->message && !emptySlot->length); 74 75 emptySlot->message = MALLOC(len+1); 76 if (emptySlot->message) { 77 (void) strncpy(emptySlot->message, buf, (size_t)len); 78 emptySlot->message[len] = '\0'; 79 80 emptySlot->length = len+1; 81 emptySlot->source = source; 82 emptySlot->type = type; 83 emptySlot->id = id; 84 emptySlot->severity = severity; 85 } else { 86 /* malloc failed! */ 87 emptySlot->message = out_of_memory; 88 emptySlot->length = strlen(out_of_memory)+1; 89 emptySlot->source = GL_DEBUG_SOURCE_OTHER_ARB; 90 emptySlot->type = GL_DEBUG_TYPE_ERROR_ARB; 91 emptySlot->id = 1; /* TODO: proper id namespace */ 92 emptySlot->severity = GL_DEBUG_SEVERITY_HIGH_ARB; 93 } 94 95 if (ctx->Debug.NumMessages == 0) 96 ctx->Debug.NextMsgLength = ctx->Debug.Log[ctx->Debug.NextMsg].length; 97 98 ctx->Debug.NumMessages++; 99} 100 101/** 102 * Pop the oldest debug message out of the log. 103 * Writes the message string, including the null terminator, into 'buf', 104 * using up to 'bufSize' bytes. If 'bufSize' is too small, or 105 * if 'buf' is NULL, nothing is written. 106 * 107 * Returns the number of bytes written on success, or when 'buf' is NULL, 108 * the number that would have been written. A return value of 0 109 * indicates failure. 110 */ 111static GLsizei 112_mesa_get_msg(struct gl_context *ctx, GLenum *source, GLenum *type, 113 GLuint *id, GLenum *severity, GLsizei bufSize, char *buf) 114{ 115 struct gl_debug_msg *msg; 116 GLsizei length; 117 118 if (ctx->Debug.NumMessages == 0) 119 return 0; 120 121 msg = &ctx->Debug.Log[ctx->Debug.NextMsg]; 122 length = msg->length; 123 124 assert(length > 0 && length == ctx->Debug.NextMsgLength); 125 126 if (bufSize < length && buf != NULL) 127 return 0; 128 129 if (severity) 130 *severity = msg->severity; 131 if (source) 132 *source = msg->source; 133 if (type) 134 *type = msg->type; 135 if (id) 136 *id = msg->id; 137 138 if (buf) { 139 assert(msg->message[length-1] == '\0'); 140 (void) strncpy(buf, msg->message, (size_t)length); 141 } 142 143 if (msg->message != (char*)out_of_memory) 144 FREE(msg->message); 145 msg->message = NULL; 146 msg->length = 0; 147 148 ctx->Debug.NumMessages--; 149 ctx->Debug.NextMsg++; 150 ctx->Debug.NextMsg %= MAX_DEBUG_LOGGED_MESSAGES; 151 ctx->Debug.NextMsgLength = ctx->Debug.Log[ctx->Debug.NextMsg].length; 152 153 return length; 154} 155 156/** 157 * Verify that source, type, and severity are valid enums. 158 * glDebugMessageInsertARB only accepts two values for 'source', 159 * and glDebugMessageControlARB will additionally accept GL_DONT_CARE 160 * in any parameter, so handle those cases specially. 161 */ 162static GLboolean 163validate_params(struct gl_context *ctx, unsigned caller, 164 GLenum source, GLenum type, GLenum severity) 165{ 166#define INSERT 1 167#define CONTROL 2 168 switch(source) { 169 case GL_DEBUG_SOURCE_APPLICATION_ARB: 170 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB: 171 break; 172 case GL_DEBUG_SOURCE_API_ARB: 173 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB: 174 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB: 175 case GL_DEBUG_SOURCE_OTHER_ARB: 176 if (caller != INSERT) 177 break; 178 case GL_DONT_CARE: 179 if (caller == CONTROL) 180 break; 181 default: 182 goto error; 183 } 184 185 switch(type) { 186 case GL_DEBUG_TYPE_ERROR_ARB: 187 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB: 188 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB: 189 case GL_DEBUG_TYPE_PERFORMANCE_ARB: 190 case GL_DEBUG_TYPE_PORTABILITY_ARB: 191 case GL_DEBUG_TYPE_OTHER_ARB: 192 break; 193 case GL_DONT_CARE: 194 if (caller == CONTROL) 195 break; 196 default: 197 goto error; 198 } 199 200 switch(severity) { 201 case GL_DEBUG_SEVERITY_HIGH_ARB: 202 case GL_DEBUG_SEVERITY_MEDIUM_ARB: 203 case GL_DEBUG_SEVERITY_LOW_ARB: 204 break; 205 case GL_DONT_CARE: 206 if (caller == CONTROL) 207 break; 208 default: 209 goto error; 210 } 211 return GL_TRUE; 212 213error: 214 { 215 const char *callerstr; 216 if (caller == INSERT) 217 callerstr = "glDebugMessageInsertARB"; 218 else if (caller == CONTROL) 219 callerstr = "glDebugMessageControlARB"; 220 else 221 return GL_FALSE; 222 223 _mesa_error( ctx, GL_INVALID_ENUM, "bad values passed to %s" 224 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr, 225 source, type, severity); 226 } 227 return GL_FALSE; 228} 229 230static void GLAPIENTRY 231_mesa_DebugMessageInsertARB(GLenum source, GLenum type, GLuint id, 232 GLenum severity, GLint length, 233 const GLcharARB* buf) 234{ 235 GET_CURRENT_CONTEXT(ctx); 236 237 if (!validate_params(ctx, INSERT, source, type, severity)) 238 return; /* GL_INVALID_ENUM */ 239 240 if (length < 0) 241 length = strlen(buf); 242 243 if (length >= MAX_DEBUG_MESSAGE_LENGTH) { 244 _mesa_error(ctx, GL_INVALID_VALUE, "glDebugMessageInsertARB" 245 "(length=%d, which is not less than " 246 "GL_MAX_DEBUG_MESSAGE_LENGTH_ARB=%d)", length, 247 MAX_DEBUG_MESSAGE_LENGTH); 248 return; 249 } 250 251 _mesa_log_msg(ctx, source, type, id, severity, length, buf); 252} 253 254static GLuint GLAPIENTRY 255_mesa_GetDebugMessageLogARB(GLuint count, GLsizei logSize, GLenum* sources, 256 GLenum* types, GLenum* ids, GLenum* severities, 257 GLsizei* lengths, GLcharARB* messageLog) 258{ 259 GET_CURRENT_CONTEXT(ctx); 260 GLuint ret; 261 262 if (!messageLog) 263 logSize = 0; 264 265 if (logSize < 0) { 266 _mesa_error(ctx, GL_INVALID_VALUE, "glGetDebugMessageLogARB" 267 "(logSize=%d : logSize must not be negative)", logSize); 268 return 0; 269 } 270 271 for (ret = 0; ret < count; ret++) { 272 GLsizei written = _mesa_get_msg(ctx, sources, types, ids, severities, 273 logSize, messageLog); 274 if (!written) 275 break; 276 277 if (messageLog) { 278 messageLog += written; 279 logSize -= written; 280 } 281 if (lengths) { 282 *lengths = written; 283 lengths++; 284 } 285 286 if (severities) 287 severities++; 288 if (sources) 289 sources++; 290 if (types) 291 types++; 292 if (ids) 293 ids++; 294 } 295 296 return ret; 297} 298 299static void GLAPIENTRY 300_mesa_DebugMessageCallbackARB(GLvoid *callback, GLvoid *userParam) 301{ 302 GET_CURRENT_CONTEXT(ctx); 303 ctx->Debug.Callback = (GLDEBUGPROCARB)callback; 304 ctx->Debug.CallbackData = userParam; 305} 306 307void 308_mesa_init_errors_dispatch(struct _glapi_table *disp) 309{ 310 SET_DebugMessageCallbackARB(disp, _mesa_DebugMessageCallbackARB); 311 SET_DebugMessageInsertARB(disp, _mesa_DebugMessageInsertARB); 312 SET_GetDebugMessageLogARB(disp, _mesa_GetDebugMessageLogARB); 313} 314 315void 316_mesa_init_errors(struct gl_context *ctx) 317{ 318 ctx->Debug.Callback = NULL; 319 ctx->Debug.SyncOutput = GL_FALSE; 320 ctx->Debug.Log[0].length = 0; 321 ctx->Debug.NumMessages = 0; 322 ctx->Debug.NextMsg = 0; 323 ctx->Debug.NextMsgLength = 0; 324} 325 326/**********************************************************************/ 327/** \name Diagnostics */ 328/*@{*/ 329 330static void 331output_if_debug(const char *prefixString, const char *outputString, 332 GLboolean newline) 333{ 334 static int debug = -1; 335 336 /* Check the MESA_DEBUG environment variable if it hasn't 337 * been checked yet. We only have to check it once... 338 */ 339 if (debug == -1) { 340 char *env = _mesa_getenv("MESA_DEBUG"); 341 342 /* In a debug build, we print warning messages *unless* 343 * MESA_DEBUG is 0. In a non-debug build, we don't 344 * print warning messages *unless* MESA_DEBUG is 345 * set *to any value*. 346 */ 347#ifdef DEBUG 348 debug = (env != NULL && atoi(env) == 0) ? 0 : 1; 349#else 350 debug = (env != NULL) ? 1 : 0; 351#endif 352 } 353 354 /* Now only print the string if we're required to do so. */ 355 if (debug) { 356 fprintf(stderr, "%s: %s", prefixString, outputString); 357 if (newline) 358 fprintf(stderr, "\n"); 359 360#if defined(_WIN32) && !defined(_WIN32_WCE) 361 /* stderr from windows applications without console is not usually 362 * visible, so communicate with the debugger instead */ 363 { 364 char buf[4096]; 365 _mesa_snprintf(buf, sizeof(buf), "%s: %s%s", prefixString, outputString, newline ? "\n" : ""); 366 OutputDebugStringA(buf); 367 } 368#endif 369 } 370} 371 372 373/** 374 * Return string version of GL error code. 375 */ 376static const char * 377error_string( GLenum error ) 378{ 379 switch (error) { 380 case GL_NO_ERROR: 381 return "GL_NO_ERROR"; 382 case GL_INVALID_VALUE: 383 return "GL_INVALID_VALUE"; 384 case GL_INVALID_ENUM: 385 return "GL_INVALID_ENUM"; 386 case GL_INVALID_OPERATION: 387 return "GL_INVALID_OPERATION"; 388 case GL_STACK_OVERFLOW: 389 return "GL_STACK_OVERFLOW"; 390 case GL_STACK_UNDERFLOW: 391 return "GL_STACK_UNDERFLOW"; 392 case GL_OUT_OF_MEMORY: 393 return "GL_OUT_OF_MEMORY"; 394 case GL_TABLE_TOO_LARGE: 395 return "GL_TABLE_TOO_LARGE"; 396 case GL_INVALID_FRAMEBUFFER_OPERATION_EXT: 397 return "GL_INVALID_FRAMEBUFFER_OPERATION"; 398 default: 399 return "unknown"; 400 } 401} 402 403 404/** 405 * When a new type of error is recorded, print a message describing 406 * previous errors which were accumulated. 407 */ 408static void 409flush_delayed_errors( struct gl_context *ctx ) 410{ 411 char s[MAXSTRING]; 412 413 if (ctx->ErrorDebugCount) { 414 _mesa_snprintf(s, MAXSTRING, "%d similar %s errors", 415 ctx->ErrorDebugCount, 416 error_string(ctx->ErrorValue)); 417 418 output_if_debug("Mesa", s, GL_TRUE); 419 420 ctx->ErrorDebugCount = 0; 421 } 422} 423 424 425/** 426 * Report a warning (a recoverable error condition) to stderr if 427 * either DEBUG is defined or the MESA_DEBUG env var is set. 428 * 429 * \param ctx GL context. 430 * \param fmtString printf()-like format string. 431 */ 432void 433_mesa_warning( struct gl_context *ctx, const char *fmtString, ... ) 434{ 435 char str[MAXSTRING]; 436 va_list args; 437 va_start( args, fmtString ); 438 (void) _mesa_vsnprintf( str, MAXSTRING, fmtString, args ); 439 va_end( args ); 440 441 if (ctx) 442 flush_delayed_errors( ctx ); 443 444 output_if_debug("Mesa warning", str, GL_TRUE); 445} 446 447 448/** 449 * Report an internal implementation problem. 450 * Prints the message to stderr via fprintf(). 451 * 452 * \param ctx GL context. 453 * \param fmtString problem description string. 454 */ 455void 456_mesa_problem( const struct gl_context *ctx, const char *fmtString, ... ) 457{ 458 va_list args; 459 char str[MAXSTRING]; 460 static int numCalls = 0; 461 462 (void) ctx; 463 464 if (numCalls < 50) { 465 numCalls++; 466 467 va_start( args, fmtString ); 468 _mesa_vsnprintf( str, MAXSTRING, fmtString, args ); 469 va_end( args ); 470 fprintf(stderr, "Mesa %s implementation error: %s\n", 471 MESA_VERSION_STRING, str); 472 fprintf(stderr, "Please report at bugs.freedesktop.org\n"); 473 } 474} 475 476 477/** 478 * Record an OpenGL state error. These usually occur when the user 479 * passes invalid parameters to a GL function. 480 * 481 * If debugging is enabled (either at compile-time via the DEBUG macro, or 482 * run-time via the MESA_DEBUG environment variable), report the error with 483 * _mesa_debug(). 484 * 485 * \param ctx the GL context. 486 * \param error the error value. 487 * \param fmtString printf() style format string, followed by optional args 488 */ 489void 490_mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... ) 491{ 492 static GLint debug = -1; 493 494 /* Check debug environment variable only once: 495 */ 496 if (debug == -1) { 497 const char *debugEnv = _mesa_getenv("MESA_DEBUG"); 498 499#ifdef DEBUG 500 if (debugEnv && strstr(debugEnv, "silent")) 501 debug = GL_FALSE; 502 else 503 debug = GL_TRUE; 504#else 505 if (debugEnv) 506 debug = GL_TRUE; 507 else 508 debug = GL_FALSE; 509#endif 510 } 511 512 if (debug) { 513 if (ctx->ErrorValue == error && 514 ctx->ErrorDebugFmtString == fmtString) { 515 ctx->ErrorDebugCount++; 516 } 517 else { 518 char s[MAXSTRING], s2[MAXSTRING]; 519 va_list args; 520 521 flush_delayed_errors( ctx ); 522 523 va_start(args, fmtString); 524 _mesa_vsnprintf(s, MAXSTRING, fmtString, args); 525 va_end(args); 526 527 _mesa_snprintf(s2, MAXSTRING, "%s in %s", error_string(error), s); 528 output_if_debug("Mesa: User error", s2, GL_TRUE); 529 530 ctx->ErrorDebugFmtString = fmtString; 531 ctx->ErrorDebugCount = 0; 532 } 533 } 534 535 _mesa_record_error(ctx, error); 536} 537 538 539/** 540 * Report debug information. Print error message to stderr via fprintf(). 541 * No-op if DEBUG mode not enabled. 542 * 543 * \param ctx GL context. 544 * \param fmtString printf()-style format string, followed by optional args. 545 */ 546void 547_mesa_debug( const struct gl_context *ctx, const char *fmtString, ... ) 548{ 549#ifdef DEBUG 550 char s[MAXSTRING]; 551 va_list args; 552 va_start(args, fmtString); 553 _mesa_vsnprintf(s, MAXSTRING, fmtString, args); 554 va_end(args); 555 output_if_debug("Mesa", s, GL_FALSE); 556#endif /* DEBUG */ 557 (void) ctx; 558 (void) fmtString; 559} 560 561/*@}*/ 562