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.
42 struct gl_debug_severity
44 struct simple_node link
;
48 static char out_of_memory
[] = "Debugging error: out of memory";
50 static const GLenum debug_source_enums
[] = {
52 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
53 GL_DEBUG_SOURCE_SHADER_COMPILER
,
54 GL_DEBUG_SOURCE_THIRD_PARTY
,
55 GL_DEBUG_SOURCE_APPLICATION
,
56 GL_DEBUG_SOURCE_OTHER
,
59 static const GLenum debug_type_enums
[] = {
61 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
62 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
63 GL_DEBUG_TYPE_PORTABILITY
,
64 GL_DEBUG_TYPE_PERFORMANCE
,
68 static const GLenum debug_severity_enums
[] = {
69 GL_DEBUG_SEVERITY_LOW
,
70 GL_DEBUG_SEVERITY_MEDIUM
,
71 GL_DEBUG_SEVERITY_HIGH
,
74 static enum mesa_debug_source
75 gl_enum_to_debug_source(GLenum e
)
79 for (i
= 0; i
< Elements(debug_source_enums
); i
++) {
80 if (debug_source_enums
[i
] == e
)
86 static enum mesa_debug_type
87 gl_enum_to_debug_type(GLenum e
)
91 for (i
= 0; i
< Elements(debug_type_enums
); i
++) {
92 if (debug_type_enums
[i
] == e
)
98 static enum mesa_debug_severity
99 gl_enum_to_debug_severity(GLenum e
)
103 for (i
= 0; i
< Elements(debug_severity_enums
); i
++) {
104 if (debug_severity_enums
[i
] == e
)
111 * We store a bitfield in the hash table, with five possible values total.
113 * The ENABLED_BIT's purpose is self-explanatory.
115 * The FOUND_BIT is needed to differentiate the value of DISABLED from
116 * the value returned by HashTableLookup() when it can't find the given key.
118 * The KNOWN_SEVERITY bit is a bit complicated:
120 * A client may call Control() with an array of IDs, then call Control()
121 * on all message IDs of a certain severity, then Insert() one of the
122 * previously specified IDs, giving us a known severity level, then call
123 * Control() on all message IDs of a certain severity level again.
125 * After the first call, those IDs will have a FOUND_BIT, but will not
126 * exist in any severity-specific list, so the second call will not
127 * impact them. This is undesirable but unavoidable given the API:
128 * The only entrypoint that gives a severity for a client-defined ID
129 * is the Insert() call.
131 * For the sake of Control(), we want to maintain the invariant
132 * that an ID will either appear in none of the three severity lists,
133 * or appear once, to minimize pointless duplication and potential surprises.
135 * Because Insert() is the only place that will learn an ID's severity,
136 * it should insert an ID into the appropriate list, but only if the ID
137 * doesn't exist in it or any other list yet. Because searching all three
138 * lists at O(n) is needlessly expensive, we store KNOWN_SEVERITY.
142 ENABLED_BIT
= 1 << 1,
143 KNOWN_SEVERITY
= 1 << 2,
145 /* HashTable reserves zero as a return value meaning 'not found' */
147 DISABLED
= FOUND_BIT
,
148 ENABLED
= ENABLED_BIT
| FOUND_BIT
152 * Returns the state of the given message source/type/ID tuple.
155 should_log(struct gl_context
*ctx
,
156 enum mesa_debug_source source
,
157 enum mesa_debug_type type
,
159 enum mesa_debug_severity severity
)
161 struct gl_debug_namespace
*nspace
=
162 &ctx
->Debug
.Namespaces
[source
][type
];
165 /* In addition to not being able to store zero as a value, HashTable also
166 can't use zero as a key. */
168 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
170 state
= nspace
->ZeroID
;
172 /* Only do this once for each ID. This makes sure the ID exists in,
173 at most, one list, and does not pointlessly appear multiple times. */
174 if (!(state
& KNOWN_SEVERITY
)) {
175 struct gl_debug_severity
*entry
;
177 if (state
== NOT_FOUND
) {
178 if (ctx
->Debug
.Defaults
[severity
][source
][type
])
184 entry
= malloc(sizeof *entry
);
188 state
|= KNOWN_SEVERITY
;
191 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
193 nspace
->ZeroID
= state
;
196 insert_at_tail(&nspace
->Severity
[severity
], &entry
->link
);
200 return !!(state
& ENABLED_BIT
);
204 * Sets the state of the given message source/type/ID tuple.
207 set_message_state(struct gl_context
*ctx
,
208 enum mesa_debug_source source
,
209 enum mesa_debug_type type
,
210 GLuint id
, GLboolean enabled
)
212 struct gl_debug_namespace
*nspace
=
213 &ctx
->Debug
.Namespaces
[source
][type
];
216 /* In addition to not being able to store zero as a value, HashTable also
217 can't use zero as a key. */
219 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
221 state
= nspace
->ZeroID
;
223 if (state
== NOT_FOUND
)
224 state
= enabled
? ENABLED
: DISABLED
;
227 state
|= ENABLED_BIT
;
229 state
&= ~ENABLED_BIT
;
233 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
235 nspace
->ZeroID
= state
;
239 * 'buf' is not necessarily a null-terminated string. When logging, copy
240 * 'len' characters from it, store them in a new, null-terminated string,
241 * and remember the number of bytes used by that string, *including*
242 * the null terminator this time.
245 _mesa_log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
246 enum mesa_debug_type type
, GLuint id
,
247 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
250 struct gl_debug_msg
*emptySlot
;
252 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
254 if (!should_log(ctx
, source
, type
, id
, severity
))
257 if (ctx
->Debug
.Callback
) {
258 ctx
->Debug
.Callback(debug_source_enums
[source
],
259 debug_type_enums
[type
],
261 debug_severity_enums
[severity
],
262 len
, buf
, ctx
->Debug
.CallbackData
);
266 if (ctx
->Debug
.NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
269 nextEmpty
= (ctx
->Debug
.NextMsg
+ ctx
->Debug
.NumMessages
)
270 % MAX_DEBUG_LOGGED_MESSAGES
;
271 emptySlot
= &ctx
->Debug
.Log
[nextEmpty
];
273 assert(!emptySlot
->message
&& !emptySlot
->length
);
275 emptySlot
->message
= malloc(len
+1);
276 if (emptySlot
->message
) {
277 (void) strncpy(emptySlot
->message
, buf
, (size_t)len
);
278 emptySlot
->message
[len
] = '\0';
280 emptySlot
->length
= len
+1;
281 emptySlot
->source
= source
;
282 emptySlot
->type
= type
;
284 emptySlot
->severity
= severity
;
287 emptySlot
->message
= out_of_memory
;
288 emptySlot
->length
= strlen(out_of_memory
)+1;
289 emptySlot
->source
= MESA_DEBUG_SOURCE_OTHER
;
290 emptySlot
->type
= MESA_DEBUG_TYPE_ERROR
;
291 emptySlot
->id
= OTHER_ERROR_OUT_OF_MEMORY
;
292 emptySlot
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
295 if (ctx
->Debug
.NumMessages
== 0)
296 ctx
->Debug
.NextMsgLength
= ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
].length
;
298 ctx
->Debug
.NumMessages
++;
302 * Pop the oldest debug message out of the log.
303 * Writes the message string, including the null terminator, into 'buf',
304 * using up to 'bufSize' bytes. If 'bufSize' is too small, or
305 * if 'buf' is NULL, nothing is written.
307 * Returns the number of bytes written on success, or when 'buf' is NULL,
308 * the number that would have been written. A return value of 0
312 _mesa_get_msg(struct gl_context
*ctx
, GLenum
*source
, GLenum
*type
,
313 GLuint
*id
, GLenum
*severity
, GLsizei bufSize
, char *buf
)
315 struct gl_debug_msg
*msg
;
318 if (ctx
->Debug
.NumMessages
== 0)
321 msg
= &ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
];
322 length
= msg
->length
;
324 assert(length
> 0 && length
== ctx
->Debug
.NextMsgLength
);
326 if (bufSize
< length
&& buf
!= NULL
)
330 *severity
= debug_severity_enums
[msg
->severity
];
332 *source
= debug_source_enums
[msg
->source
];
334 *type
= debug_type_enums
[msg
->type
];
339 assert(msg
->message
[length
-1] == '\0');
340 (void) strncpy(buf
, msg
->message
, (size_t)length
);
343 if (msg
->message
!= (char*)out_of_memory
)
348 ctx
->Debug
.NumMessages
--;
349 ctx
->Debug
.NextMsg
++;
350 ctx
->Debug
.NextMsg
%= MAX_DEBUG_LOGGED_MESSAGES
;
351 ctx
->Debug
.NextMsgLength
= ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
].length
;
357 * Verify that source, type, and severity are valid enums.
358 * glDebugMessageInsertARB only accepts two values for 'source',
359 * and glDebugMessageControlARB will additionally accept GL_DONT_CARE
360 * in any parameter, so handle those cases specially.
363 validate_params(struct gl_context
*ctx
, unsigned caller
,
364 GLenum source
, GLenum type
, GLenum severity
)
369 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
370 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
372 case GL_DEBUG_SOURCE_API_ARB
:
373 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
374 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
375 case GL_DEBUG_SOURCE_OTHER_ARB
:
376 if (caller
!= INSERT
)
379 if (caller
== CONTROL
)
386 case GL_DEBUG_TYPE_ERROR_ARB
:
387 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
388 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
389 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
390 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
391 case GL_DEBUG_TYPE_OTHER_ARB
:
394 if (caller
== CONTROL
)
401 case GL_DEBUG_SEVERITY_HIGH_ARB
:
402 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
403 case GL_DEBUG_SEVERITY_LOW_ARB
:
406 if (caller
== CONTROL
)
415 const char *callerstr
;
416 if (caller
== INSERT
)
417 callerstr
= "glDebugMessageInsertARB";
418 else if (caller
== CONTROL
)
419 callerstr
= "glDebugMessageControlARB";
423 _mesa_error( ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
424 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
425 source
, type
, severity
);
431 _mesa_DebugMessageInsertARB(GLenum source
, GLenum type
, GLuint id
,
432 GLenum severity
, GLint length
,
433 const GLcharARB
* buf
)
435 GET_CURRENT_CONTEXT(ctx
);
437 if (!validate_params(ctx
, INSERT
, source
, type
, severity
))
438 return; /* GL_INVALID_ENUM */
441 length
= strlen(buf
);
443 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
444 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDebugMessageInsertARB"
445 "(length=%d, which is not less than "
446 "GL_MAX_DEBUG_MESSAGE_LENGTH_ARB=%d)", length
,
447 MAX_DEBUG_MESSAGE_LENGTH
);
452 gl_enum_to_debug_source(source
),
453 gl_enum_to_debug_type(type
), id
,
454 gl_enum_to_debug_severity(severity
), length
, buf
);
458 _mesa_GetDebugMessageLogARB(GLuint count
, GLsizei logSize
, GLenum
* sources
,
459 GLenum
* types
, GLenum
* ids
, GLenum
* severities
,
460 GLsizei
* lengths
, GLcharARB
* messageLog
)
462 GET_CURRENT_CONTEXT(ctx
);
469 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetDebugMessageLogARB"
470 "(logSize=%d : logSize must not be negative)", logSize
);
474 for (ret
= 0; ret
< count
; ret
++) {
475 GLsizei written
= _mesa_get_msg(ctx
, sources
, types
, ids
, severities
,
476 logSize
, messageLog
);
481 messageLog
+= written
;
503 * Set the state of all message IDs found in the given intersection of
504 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
505 * GL_DONT_CARE (include all messages in the class).
507 * This requires both setting the state of all previously seen message
508 * IDs in the hash table, and setting the default state for all
509 * applicable combinations of source/type/severity, so that all the
510 * yet-unknown message IDs that may be used in the future will be
511 * impacted as if they were already known.
514 control_messages(struct gl_context
*ctx
,
515 enum mesa_debug_source source
,
516 enum mesa_debug_type type
,
517 enum mesa_debug_severity severity
,
520 int s
, t
, sev
, smax
, tmax
, sevmax
;
522 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
524 smax
= MESA_DEBUG_SOURCE_COUNT
;
529 if (type
== MESA_DEBUG_TYPE_COUNT
) {
531 tmax
= MESA_DEBUG_TYPE_COUNT
;
536 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
538 sevmax
= MESA_DEBUG_SEVERITY_COUNT
;
543 for (sev
= severity
; sev
< sevmax
; sev
++)
544 for (s
= source
; s
< smax
; s
++)
545 for (t
= type
; t
< tmax
; t
++) {
546 struct simple_node
*node
;
547 struct gl_debug_severity
*entry
;
549 /* change the default for IDs we've never seen before. */
550 ctx
->Debug
.Defaults
[sev
][s
][t
] = enabled
;
552 /* Now change the state of IDs we *have* seen... */
553 foreach(node
, &ctx
->Debug
.Namespaces
[s
][t
].Severity
[sev
]) {
554 entry
= (struct gl_debug_severity
*)node
;
555 set_message_state(ctx
, s
, t
, entry
->ID
, enabled
);
561 * Debugging-message namespaces with the source APPLICATION or THIRD_PARTY
562 * require special handling, since the IDs in them are controlled by clients,
563 * not the OpenGL implementation.
565 * 'count' is the length of the array 'ids'. If 'count' is nonzero, all
566 * the given IDs in the namespace defined by 'esource' and 'etype'
569 * If 'count' is zero, this sets the state of all IDs that match
570 * the combination of 'esource', 'etype', and 'eseverity'.
573 control_app_messages(struct gl_context
*ctx
, GLenum esource
, GLenum etype
,
574 GLenum eseverity
, GLsizei count
, const GLuint
*ids
,
578 enum mesa_debug_source source
= gl_enum_to_debug_source(esource
);
579 enum mesa_debug_type type
= gl_enum_to_debug_type(etype
);
580 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(eseverity
);
583 assert(severity
== MESA_DEBUG_SEVERITY_COUNT
584 && type
!= MESA_DEBUG_TYPE_COUNT
585 && source
!= MESA_DEBUG_SOURCE_COUNT
);
587 for (i
= 0; i
< count
; i
++)
588 set_message_state(ctx
, source
, type
, ids
[i
], enabled
);
593 control_messages(ctx
, source
, type
, severity
, enabled
);
597 _mesa_DebugMessageControlARB(GLenum gl_source
, GLenum gl_type
,
599 GLsizei count
, const GLuint
*ids
,
602 enum mesa_debug_source source
;
603 enum mesa_debug_type type
;
604 enum mesa_debug_severity severity
;
605 GET_CURRENT_CONTEXT(ctx
);
608 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDebugMessageControlARB"
609 "(count=%d : count must not be negative)", count
);
613 if (!validate_params(ctx
, CONTROL
, gl_source
, gl_type
, gl_severity
))
614 return; /* GL_INVALID_ENUM */
616 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
617 || gl_source
== GL_DONT_CARE
)) {
618 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDebugMessageControlARB"
619 "(When passing an array of ids, severity must be"
620 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.");
624 source
= gl_enum_to_debug_severity(gl_source
);
625 type
= gl_enum_to_debug_severity(gl_type
);
626 severity
= gl_enum_to_debug_severity(gl_severity
);
628 control_app_messages(ctx
, source
, type
, severity
, count
, ids
, enabled
);
632 _mesa_DebugMessageCallbackARB(GLDEBUGPROCARB callback
, const GLvoid
*userParam
)
634 GET_CURRENT_CONTEXT(ctx
);
635 ctx
->Debug
.Callback
= callback
;
636 ctx
->Debug
.CallbackData
= (void *) userParam
;
640 _mesa_init_errors(struct gl_context
*ctx
)
644 ctx
->Debug
.Callback
= NULL
;
645 ctx
->Debug
.SyncOutput
= GL_FALSE
;
646 ctx
->Debug
.Log
[0].length
= 0;
647 ctx
->Debug
.NumMessages
= 0;
648 ctx
->Debug
.NextMsg
= 0;
649 ctx
->Debug
.NextMsgLength
= 0;
651 /* Enable all the messages with severity HIGH or MEDIUM by default. */
652 memset(ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_HIGH
], GL_TRUE
,
653 sizeof ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_HIGH
]);
654 memset(ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_MEDIUM
], GL_TRUE
,
655 sizeof ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_MEDIUM
]);
656 memset(ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_LOW
], GL_FALSE
,
657 sizeof ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_LOW
]);
659 /* Initialize state for filtering known debug messages. */
660 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++)
661 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
662 ctx
->Debug
.Namespaces
[s
][t
].IDs
= _mesa_NewHashTable();
663 assert(ctx
->Debug
.Namespaces
[s
][t
].IDs
);
665 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++)
666 make_empty_list(&ctx
->Debug
.Namespaces
[s
][t
].Severity
[sev
]);
671 do_nothing(GLuint key
, void *data
, void *userData
)
676 _mesa_free_errors_data(struct gl_context
*ctx
)
678 enum mesa_debug_type t
;
679 enum mesa_debug_source s
;
680 enum mesa_debug_severity sev
;
682 /* Tear down state for filtering debug messages. */
683 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++)
684 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
685 _mesa_HashDeleteAll(ClientIDs
->Namespaces
[s
][t
].IDs
, do_nothing
, NULL
);
686 _mesa_DeleteHashTable(ClientIDs
->Namespaces
[s
][t
].IDs
);
687 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
688 struct simple_node
*node
, *tmp
;
689 struct gl_debug_severity
*entry
;
691 foreach_s(node
, tmp
, &ctx
->Debug
.Namespaces
[s
][t
].Severity
[sev
]) {
692 entry
= (struct gl_debug_severity
*)node
;
699 /**********************************************************************/
700 /** \name Diagnostics */
704 output_if_debug(const char *prefixString
, const char *outputString
,
707 static int debug
= -1;
708 static FILE *fout
= NULL
;
710 /* Init the local 'debug' var once.
711 * Note: the _mesa_init_debug() function should have been called
712 * by now so MESA_DEBUG_FLAGS will be initialized.
715 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
716 * etc to the named file. Otherwise, output to stderr.
718 const char *logFile
= _mesa_getenv("MESA_LOG_FILE");
720 fout
= fopen(logFile
, "w");
724 /* in debug builds, print messages unless MESA_DEBUG="silent" */
725 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
730 /* in release builds, be silent unless MESA_DEBUG is set */
731 debug
= _mesa_getenv("MESA_DEBUG") != NULL
;
735 /* Now only print the string if we're required to do so. */
737 fprintf(fout
, "%s: %s", prefixString
, outputString
);
742 #if defined(_WIN32) && !defined(_WIN32_WCE)
743 /* stderr from windows applications without console is not usually
744 * visible, so communicate with the debugger instead */
747 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
748 OutputDebugStringA(buf
);
755 * When a new type of error is recorded, print a message describing
756 * previous errors which were accumulated.
759 flush_delayed_errors( struct gl_context
*ctx
)
761 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
763 if (ctx
->ErrorDebugCount
) {
764 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
765 ctx
->ErrorDebugCount
,
766 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
768 output_if_debug("Mesa", s
, GL_TRUE
);
770 ctx
->ErrorDebugCount
= 0;
776 * Report a warning (a recoverable error condition) to stderr if
777 * either DEBUG is defined or the MESA_DEBUG env var is set.
779 * \param ctx GL context.
780 * \param fmtString printf()-like format string.
783 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
785 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
787 va_start( args
, fmtString
);
788 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
792 flush_delayed_errors( ctx
);
794 output_if_debug("Mesa warning", str
, GL_TRUE
);
799 * Report an internal implementation problem.
800 * Prints the message to stderr via fprintf().
802 * \param ctx GL context.
803 * \param fmtString problem description string.
806 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
809 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
810 static int numCalls
= 0;
817 va_start( args
, fmtString
);
818 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
820 fprintf(stderr
, "Mesa %s implementation error: %s\n",
821 MESA_VERSION_STRING
, str
);
822 fprintf(stderr
, "Please report at bugs.freedesktop.org\n");
827 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
829 static GLint debug
= -1;
831 /* Check debug environment variable only once:
834 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
837 if (debugEnv
&& strstr(debugEnv
, "silent"))
850 if (ctx
->ErrorValue
!= error
||
851 ctx
->ErrorDebugFmtString
!= fmtString
) {
852 flush_delayed_errors( ctx
);
853 ctx
->ErrorDebugFmtString
= fmtString
;
854 ctx
->ErrorDebugCount
= 0;
857 ctx
->ErrorDebugCount
++;
864 * Record an OpenGL state error. These usually occur when the user
865 * passes invalid parameters to a GL function.
867 * If debugging is enabled (either at compile-time via the DEBUG macro, or
868 * run-time via the MESA_DEBUG environment variable), report the error with
871 * \param ctx the GL context.
872 * \param error the error value.
873 * \param fmtString printf() style format string, followed by optional args
876 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
878 GLboolean do_output
, do_log
;
880 do_output
= should_output(ctx
, error
, fmtString
);
881 do_log
= should_log(ctx
,
882 MESA_DEBUG_SOURCE_API
,
883 MESA_DEBUG_TYPE_ERROR
,
885 MESA_DEBUG_SEVERITY_HIGH
);
887 if (do_output
|| do_log
) {
888 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
892 va_start(args
, fmtString
);
893 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
896 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
897 /* Too long error message. Whoever calls _mesa_error should use
898 * shorter strings. */
903 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
904 _mesa_lookup_enum_by_nr(error
), s
);
905 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
911 /* Print the error to stderr if needed. */
913 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
916 /* Log the error via ARB_debug_output if needed.*/
919 MESA_DEBUG_SOURCE_API
,
920 MESA_DEBUG_TYPE_ERROR
,
922 MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
926 /* Set the GL context error state for glGetError. */
927 _mesa_record_error(ctx
, error
);
932 * Report debug information. Print error message to stderr via fprintf().
933 * No-op if DEBUG mode not enabled.
935 * \param ctx GL context.
936 * \param fmtString printf()-style format string, followed by optional args.
939 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
942 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
944 va_start(args
, fmtString
);
945 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
947 output_if_debug("Mesa", s
, GL_FALSE
);
955 * Report debug information from the shader compiler via GL_ARB_debug_output.
957 * \param ctx GL context.
958 * \param type The namespace to which this message belongs.
959 * \param id The message ID within the given namespace.
960 * \param msg The message to output. Need not be null-terminated.
961 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
964 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint id
,
965 const char *msg
, int len
)
967 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
968 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
973 /* Truncate the message if necessary. */
974 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
975 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
977 _mesa_log_msg(ctx
, source
, type
, id
, severity
, len
, msg
);