3 * Mesa debugging and error handling functions.
7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
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.
40 #define MAXSTRING MAX_DEBUG_MESSAGE_LENGTH
43 static char out_of_memory
[] = "Debugging error: out of memory";
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.
52 _mesa_log_msg(struct gl_context
*ctx
, GLenum source
, GLenum type
,
53 GLuint id
, GLenum severity
, GLint len
, const char *buf
)
56 struct gl_debug_msg
*emptySlot
;
58 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
60 if (ctx
->Debug
.Callback
) {
61 ctx
->Debug
.Callback(source
, type
, id
, severity
,
62 len
, buf
, ctx
->Debug
.CallbackData
);
66 if (ctx
->Debug
.NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
69 nextEmpty
= (ctx
->Debug
.NextMsg
+ ctx
->Debug
.NumMessages
)
70 % MAX_DEBUG_LOGGED_MESSAGES
;
71 emptySlot
= &ctx
->Debug
.Log
[nextEmpty
];
73 assert(!emptySlot
->message
&& !emptySlot
->length
);
75 emptySlot
->message
= MALLOC(len
+1);
76 if (emptySlot
->message
) {
77 (void) strncpy(emptySlot
->message
, buf
, (size_t)len
);
78 emptySlot
->message
[len
] = '\0';
80 emptySlot
->length
= len
+1;
81 emptySlot
->source
= source
;
82 emptySlot
->type
= type
;
84 emptySlot
->severity
= severity
;
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
;
95 if (ctx
->Debug
.NumMessages
== 0)
96 ctx
->Debug
.NextMsgLength
= ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
].length
;
98 ctx
->Debug
.NumMessages
++;
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.
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
112 _mesa_get_msg(struct gl_context
*ctx
, GLenum
*source
, GLenum
*type
,
113 GLuint
*id
, GLenum
*severity
, GLsizei bufSize
, char *buf
)
115 struct gl_debug_msg
*msg
;
118 if (ctx
->Debug
.NumMessages
== 0)
121 msg
= &ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
];
122 length
= msg
->length
;
124 assert(length
> 0 && length
== ctx
->Debug
.NextMsgLength
);
126 if (bufSize
< length
&& buf
!= NULL
)
130 *severity
= msg
->severity
;
132 *source
= msg
->source
;
139 assert(msg
->message
[length
-1] == '\0');
140 (void) strncpy(buf
, msg
->message
, (size_t)length
);
143 if (msg
->message
!= (char*)out_of_memory
)
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
;
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.
163 validate_params(struct gl_context
*ctx
, unsigned caller
,
164 GLenum source
, GLenum type
, GLenum severity
)
169 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
170 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
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
)
179 if (caller
== CONTROL
)
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
:
194 if (caller
== CONTROL
)
201 case GL_DEBUG_SEVERITY_HIGH_ARB
:
202 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
203 case GL_DEBUG_SEVERITY_LOW_ARB
:
206 if (caller
== CONTROL
)
215 const char *callerstr
;
216 if (caller
== INSERT
)
217 callerstr
= "glDebugMessageInsertARB";
218 else if (caller
== CONTROL
)
219 callerstr
= "glDebugMessageControlARB";
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
);
230 static void GLAPIENTRY
231 _mesa_DebugMessageInsertARB(GLenum source
, GLenum type
, GLuint id
,
232 GLenum severity
, GLint length
,
233 const GLcharARB
* buf
)
235 GET_CURRENT_CONTEXT(ctx
);
237 if (!validate_params(ctx
, INSERT
, source
, type
, severity
))
238 return; /* GL_INVALID_ENUM */
241 length
= strlen(buf
);
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
);
251 _mesa_log_msg(ctx
, source
, type
, id
, severity
, length
, buf
);
254 static GLuint GLAPIENTRY
255 _mesa_GetDebugMessageLogARB(GLuint count
, GLsizei logSize
, GLenum
* sources
,
256 GLenum
* types
, GLenum
* ids
, GLenum
* severities
,
257 GLsizei
* lengths
, GLcharARB
* messageLog
)
259 GET_CURRENT_CONTEXT(ctx
);
266 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetDebugMessageLogARB"
267 "(logSize=%d : logSize must not be negative)", logSize
);
271 for (ret
= 0; ret
< count
; ret
++) {
272 GLsizei written
= _mesa_get_msg(ctx
, sources
, types
, ids
, severities
,
273 logSize
, messageLog
);
278 messageLog
+= written
;
299 static void GLAPIENTRY
300 _mesa_DebugMessageCallbackARB(GLvoid
*callback
, GLvoid
*userParam
)
302 GET_CURRENT_CONTEXT(ctx
);
303 ctx
->Debug
.Callback
= (GLDEBUGPROCARB
)callback
;
304 ctx
->Debug
.CallbackData
= userParam
;
308 _mesa_init_errors_dispatch(struct _glapi_table
*disp
)
310 SET_DebugMessageCallbackARB(disp
, _mesa_DebugMessageCallbackARB
);
311 SET_DebugMessageInsertARB(disp
, _mesa_DebugMessageInsertARB
);
312 SET_GetDebugMessageLogARB(disp
, _mesa_GetDebugMessageLogARB
);
316 _mesa_init_errors(struct gl_context
*ctx
)
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;
326 /**********************************************************************/
327 /** \name Diagnostics */
331 output_if_debug(const char *prefixString
, const char *outputString
,
334 static int debug
= -1;
336 /* Check the MESA_DEBUG environment variable if it hasn't
337 * been checked yet. We only have to check it once...
340 char *env
= _mesa_getenv("MESA_DEBUG");
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*.
348 debug
= (env
!= NULL
&& atoi(env
) == 0) ? 0 : 1;
350 debug
= (env
!= NULL
) ? 1 : 0;
354 /* Now only print the string if we're required to do so. */
356 fprintf(stderr
, "%s: %s", prefixString
, outputString
);
358 fprintf(stderr
, "\n");
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 */
365 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
366 OutputDebugStringA(buf
);
374 * Return string version of GL error code.
377 error_string( GLenum 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";
405 * When a new type of error is recorded, print a message describing
406 * previous errors which were accumulated.
409 flush_delayed_errors( struct gl_context
*ctx
)
413 if (ctx
->ErrorDebugCount
) {
414 _mesa_snprintf(s
, MAXSTRING
, "%d similar %s errors",
415 ctx
->ErrorDebugCount
,
416 error_string(ctx
->ErrorValue
));
418 output_if_debug("Mesa", s
, GL_TRUE
);
420 ctx
->ErrorDebugCount
= 0;
426 * Report a warning (a recoverable error condition) to stderr if
427 * either DEBUG is defined or the MESA_DEBUG env var is set.
429 * \param ctx GL context.
430 * \param fmtString printf()-like format string.
433 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
437 va_start( args
, fmtString
);
438 (void) _mesa_vsnprintf( str
, MAXSTRING
, fmtString
, args
);
442 flush_delayed_errors( ctx
);
444 output_if_debug("Mesa warning", str
, GL_TRUE
);
449 * Report an internal implementation problem.
450 * Prints the message to stderr via fprintf().
452 * \param ctx GL context.
453 * \param fmtString problem description string.
456 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
460 static int numCalls
= 0;
467 va_start( args
, fmtString
);
468 _mesa_vsnprintf( str
, MAXSTRING
, fmtString
, 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");
478 * Record an OpenGL state error. These usually occur when the user
479 * passes invalid parameters to a GL function.
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
485 * \param ctx the GL context.
486 * \param error the error value.
487 * \param fmtString printf() style format string, followed by optional args
490 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
492 static GLint debug
= -1;
494 /* Check debug environment variable only once:
497 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
500 if (debugEnv
&& strstr(debugEnv
, "silent"))
513 if (ctx
->ErrorValue
== error
&&
514 ctx
->ErrorDebugFmtString
== fmtString
) {
515 ctx
->ErrorDebugCount
++;
518 char s
[MAXSTRING
], s2
[MAXSTRING
];
521 flush_delayed_errors( ctx
);
523 va_start(args
, fmtString
);
524 _mesa_vsnprintf(s
, MAXSTRING
, fmtString
, args
);
527 _mesa_snprintf(s2
, MAXSTRING
, "%s in %s", error_string(error
), s
);
528 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
530 ctx
->ErrorDebugFmtString
= fmtString
;
531 ctx
->ErrorDebugCount
= 0;
535 _mesa_record_error(ctx
, error
);
540 * Report debug information. Print error message to stderr via fprintf().
541 * No-op if DEBUG mode not enabled.
543 * \param ctx GL context.
544 * \param fmtString printf()-style format string, followed by optional args.
547 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
552 va_start(args
, fmtString
);
553 _mesa_vsnprintf(s
, MAXSTRING
, fmtString
, args
);
555 output_if_debug("Mesa", s
, GL_FALSE
);