3 * Mesa debugging and error handling functions.
7 * Mesa 3-D graphics library
9 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
39 #include "hash_table.h"
40 #include "glapi/glthread.h"
42 _glthread_DECLARE_STATIC_MUTEX(DynamicIDMutex
);
43 static GLuint NextDynamicID
= 1;
45 struct gl_debug_severity
47 struct simple_node link
;
51 static char out_of_memory
[] = "Debugging error: out of memory";
53 static const GLenum debug_source_enums
[] = {
55 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
56 GL_DEBUG_SOURCE_SHADER_COMPILER
,
57 GL_DEBUG_SOURCE_THIRD_PARTY
,
58 GL_DEBUG_SOURCE_APPLICATION
,
59 GL_DEBUG_SOURCE_OTHER
,
62 static const GLenum debug_type_enums
[] = {
64 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
65 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
66 GL_DEBUG_TYPE_PORTABILITY
,
67 GL_DEBUG_TYPE_PERFORMANCE
,
70 GL_DEBUG_TYPE_PUSH_GROUP
,
71 GL_DEBUG_TYPE_POP_GROUP
,
74 static const GLenum debug_severity_enums
[] = {
75 GL_DEBUG_SEVERITY_LOW
,
76 GL_DEBUG_SEVERITY_MEDIUM
,
77 GL_DEBUG_SEVERITY_HIGH
,
78 GL_DEBUG_SEVERITY_NOTIFICATION
,
81 static enum mesa_debug_source
82 gl_enum_to_debug_source(GLenum e
)
86 for (i
= 0; i
< Elements(debug_source_enums
); i
++) {
87 if (debug_source_enums
[i
] == e
)
93 static enum mesa_debug_type
94 gl_enum_to_debug_type(GLenum e
)
98 for (i
= 0; i
< Elements(debug_type_enums
); i
++) {
99 if (debug_type_enums
[i
] == e
)
105 static enum mesa_debug_severity
106 gl_enum_to_debug_severity(GLenum e
)
110 for (i
= 0; i
< Elements(debug_severity_enums
); i
++) {
111 if (debug_severity_enums
[i
] == e
)
118 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
121 * The GL API has this "ID" mechanism, where the intention is to allow a
122 * client to filter in/out messages based on source, type, and ID. Of course,
123 * building a giant enum list of all debug output messages that Mesa might
124 * generate is ridiculous, so instead we have our caller pass us a pointer to
125 * static storage where the ID should get stored. This ID will be shared
126 * across all contexts for that message (which seems like a desirable
127 * property, even if it's not expected by the spec), but note that it won't be
128 * the same between executions if messages aren't generated in the same order.
131 debug_get_id(GLuint
*id
)
134 _glthread_LOCK_MUTEX(DynamicIDMutex
);
136 *id
= NextDynamicID
++;
137 _glthread_UNLOCK_MUTEX(DynamicIDMutex
);
142 * We store a bitfield in the hash table, with five possible values total.
144 * The ENABLED_BIT's purpose is self-explanatory.
146 * The FOUND_BIT is needed to differentiate the value of DISABLED from
147 * the value returned by HashTableLookup() when it can't find the given key.
149 * The KNOWN_SEVERITY bit is a bit complicated:
151 * A client may call Control() with an array of IDs, then call Control()
152 * on all message IDs of a certain severity, then Insert() one of the
153 * previously specified IDs, giving us a known severity level, then call
154 * Control() on all message IDs of a certain severity level again.
156 * After the first call, those IDs will have a FOUND_BIT, but will not
157 * exist in any severity-specific list, so the second call will not
158 * impact them. This is undesirable but unavoidable given the API:
159 * The only entrypoint that gives a severity for a client-defined ID
160 * is the Insert() call.
162 * For the sake of Control(), we want to maintain the invariant
163 * that an ID will either appear in none of the three severity lists,
164 * or appear once, to minimize pointless duplication and potential surprises.
166 * Because Insert() is the only place that will learn an ID's severity,
167 * it should insert an ID into the appropriate list, but only if the ID
168 * doesn't exist in it or any other list yet. Because searching all three
169 * lists at O(n) is needlessly expensive, we store KNOWN_SEVERITY.
173 ENABLED_BIT
= 1 << 1,
174 KNOWN_SEVERITY
= 1 << 2,
176 /* HashTable reserves zero as a return value meaning 'not found' */
178 DISABLED
= FOUND_BIT
,
179 ENABLED
= ENABLED_BIT
| FOUND_BIT
183 * Returns the state of the given message source/type/ID tuple.
186 should_log(struct gl_context
*ctx
,
187 enum mesa_debug_source source
,
188 enum mesa_debug_type type
,
190 enum mesa_debug_severity severity
)
192 struct gl_debug_namespace
*nspace
=
193 &ctx
->Debug
.Namespaces
[source
][type
];
196 /* In addition to not being able to store zero as a value, HashTable also
197 can't use zero as a key. */
199 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
201 state
= nspace
->ZeroID
;
203 /* Only do this once for each ID. This makes sure the ID exists in,
204 at most, one list, and does not pointlessly appear multiple times. */
205 if (!(state
& KNOWN_SEVERITY
)) {
206 struct gl_debug_severity
*entry
;
208 if (state
== NOT_FOUND
) {
209 if (ctx
->Debug
.Defaults
[severity
][source
][type
])
215 entry
= malloc(sizeof *entry
);
219 state
|= KNOWN_SEVERITY
;
222 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
224 nspace
->ZeroID
= state
;
227 insert_at_tail(&nspace
->Severity
[severity
], &entry
->link
);
231 return !!(state
& ENABLED_BIT
);
235 * Sets the state of the given message source/type/ID tuple.
238 set_message_state(struct gl_context
*ctx
,
239 enum mesa_debug_source source
,
240 enum mesa_debug_type type
,
241 GLuint id
, GLboolean enabled
)
243 struct gl_debug_namespace
*nspace
=
244 &ctx
->Debug
.Namespaces
[source
][type
];
247 /* In addition to not being able to store zero as a value, HashTable also
248 can't use zero as a key. */
250 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
252 state
= nspace
->ZeroID
;
254 if (state
== NOT_FOUND
)
255 state
= enabled
? ENABLED
: DISABLED
;
258 state
|= ENABLED_BIT
;
260 state
&= ~ENABLED_BIT
;
264 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
266 nspace
->ZeroID
= state
;
270 * 'buf' is not necessarily a null-terminated string. When logging, copy
271 * 'len' characters from it, store them in a new, null-terminated string,
272 * and remember the number of bytes used by that string, *including*
273 * the null terminator this time.
276 _mesa_log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
277 enum mesa_debug_type type
, GLuint id
,
278 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
281 struct gl_debug_msg
*emptySlot
;
283 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
285 if (!should_log(ctx
, source
, type
, id
, severity
))
288 if (ctx
->Debug
.Callback
) {
289 ctx
->Debug
.Callback(debug_source_enums
[source
],
290 debug_type_enums
[type
],
292 debug_severity_enums
[severity
],
293 len
, buf
, ctx
->Debug
.CallbackData
);
297 if (ctx
->Debug
.NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
300 nextEmpty
= (ctx
->Debug
.NextMsg
+ ctx
->Debug
.NumMessages
)
301 % MAX_DEBUG_LOGGED_MESSAGES
;
302 emptySlot
= &ctx
->Debug
.Log
[nextEmpty
];
304 assert(!emptySlot
->message
&& !emptySlot
->length
);
306 emptySlot
->message
= malloc(len
+1);
307 if (emptySlot
->message
) {
308 (void) strncpy(emptySlot
->message
, buf
, (size_t)len
);
309 emptySlot
->message
[len
] = '\0';
311 emptySlot
->length
= len
+1;
312 emptySlot
->source
= source
;
313 emptySlot
->type
= type
;
315 emptySlot
->severity
= severity
;
317 static GLuint oom_msg_id
= 0;
318 debug_get_id(&oom_msg_id
);
321 emptySlot
->message
= out_of_memory
;
322 emptySlot
->length
= strlen(out_of_memory
)+1;
323 emptySlot
->source
= MESA_DEBUG_SOURCE_OTHER
;
324 emptySlot
->type
= MESA_DEBUG_TYPE_ERROR
;
325 emptySlot
->id
= oom_msg_id
;
326 emptySlot
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
329 if (ctx
->Debug
.NumMessages
== 0)
330 ctx
->Debug
.NextMsgLength
= ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
].length
;
332 ctx
->Debug
.NumMessages
++;
336 * Pop the oldest debug message out of the log.
337 * Writes the message string, including the null terminator, into 'buf',
338 * using up to 'bufSize' bytes. If 'bufSize' is too small, or
339 * if 'buf' is NULL, nothing is written.
341 * Returns the number of bytes written on success, or when 'buf' is NULL,
342 * the number that would have been written. A return value of 0
346 _mesa_get_msg(struct gl_context
*ctx
, GLenum
*source
, GLenum
*type
,
347 GLuint
*id
, GLenum
*severity
, GLsizei bufSize
, char *buf
)
349 struct gl_debug_msg
*msg
;
352 if (ctx
->Debug
.NumMessages
== 0)
355 msg
= &ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
];
356 length
= msg
->length
;
358 assert(length
> 0 && length
== ctx
->Debug
.NextMsgLength
);
360 if (bufSize
< length
&& buf
!= NULL
)
364 *severity
= debug_severity_enums
[msg
->severity
];
366 *source
= debug_source_enums
[msg
->source
];
368 *type
= debug_type_enums
[msg
->type
];
373 assert(msg
->message
[length
-1] == '\0');
374 (void) strncpy(buf
, msg
->message
, (size_t)length
);
377 if (msg
->message
!= (char*)out_of_memory
)
382 ctx
->Debug
.NumMessages
--;
383 ctx
->Debug
.NextMsg
++;
384 ctx
->Debug
.NextMsg
%= MAX_DEBUG_LOGGED_MESSAGES
;
385 ctx
->Debug
.NextMsgLength
= ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
].length
;
391 * Verify that source, type, and severity are valid enums.
392 * glDebugMessageInsertARB only accepts two values for 'source',
393 * and glDebugMessageControlARB will additionally accept GL_DONT_CARE
394 * in any parameter, so handle those cases specially.
397 validate_params(struct gl_context
*ctx
, unsigned caller
,
398 GLenum source
, GLenum type
, GLenum severity
)
403 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
404 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
406 case GL_DEBUG_SOURCE_API_ARB
:
407 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
408 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
409 case GL_DEBUG_SOURCE_OTHER_ARB
:
410 if (caller
!= INSERT
)
413 if (caller
== CONTROL
)
420 case GL_DEBUG_TYPE_ERROR_ARB
:
421 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
422 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
423 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
424 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
425 case GL_DEBUG_TYPE_OTHER_ARB
:
428 if (caller
== CONTROL
)
435 case GL_DEBUG_SEVERITY_HIGH_ARB
:
436 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
437 case GL_DEBUG_SEVERITY_LOW_ARB
:
440 if (caller
== CONTROL
)
449 const char *callerstr
;
450 if (caller
== INSERT
)
451 callerstr
= "glDebugMessageInsertARB";
452 else if (caller
== CONTROL
)
453 callerstr
= "glDebugMessageControlARB";
457 _mesa_error( ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
458 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
459 source
, type
, severity
);
465 _mesa_DebugMessageInsertARB(GLenum source
, GLenum type
, GLuint id
,
466 GLenum severity
, GLint length
,
467 const GLcharARB
* buf
)
469 GET_CURRENT_CONTEXT(ctx
);
471 if (!validate_params(ctx
, INSERT
, source
, type
, severity
))
472 return; /* GL_INVALID_ENUM */
475 length
= strlen(buf
);
477 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
478 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDebugMessageInsertARB"
479 "(length=%d, which is not less than "
480 "GL_MAX_DEBUG_MESSAGE_LENGTH_ARB=%d)", length
,
481 MAX_DEBUG_MESSAGE_LENGTH
);
486 gl_enum_to_debug_source(source
),
487 gl_enum_to_debug_type(type
), id
,
488 gl_enum_to_debug_severity(severity
), length
, buf
);
492 _mesa_GetDebugMessageLogARB(GLuint count
, GLsizei logSize
, GLenum
* sources
,
493 GLenum
* types
, GLenum
* ids
, GLenum
* severities
,
494 GLsizei
* lengths
, GLcharARB
* messageLog
)
496 GET_CURRENT_CONTEXT(ctx
);
503 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetDebugMessageLogARB"
504 "(logSize=%d : logSize must not be negative)", logSize
);
508 for (ret
= 0; ret
< count
; ret
++) {
509 GLsizei written
= _mesa_get_msg(ctx
, sources
, types
, ids
, severities
,
510 logSize
, messageLog
);
515 messageLog
+= written
;
537 * Set the state of all message IDs found in the given intersection of
538 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
539 * GL_DONT_CARE (include all messages in the class).
541 * This requires both setting the state of all previously seen message
542 * IDs in the hash table, and setting the default state for all
543 * applicable combinations of source/type/severity, so that all the
544 * yet-unknown message IDs that may be used in the future will be
545 * impacted as if they were already known.
548 control_messages(struct gl_context
*ctx
,
549 enum mesa_debug_source source
,
550 enum mesa_debug_type type
,
551 enum mesa_debug_severity severity
,
554 int s
, t
, sev
, smax
, tmax
, sevmax
;
556 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
558 smax
= MESA_DEBUG_SOURCE_COUNT
;
563 if (type
== MESA_DEBUG_TYPE_COUNT
) {
565 tmax
= MESA_DEBUG_TYPE_COUNT
;
570 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
572 sevmax
= MESA_DEBUG_SEVERITY_COUNT
;
577 for (sev
= severity
; sev
< sevmax
; sev
++)
578 for (s
= source
; s
< smax
; s
++)
579 for (t
= type
; t
< tmax
; t
++) {
580 struct simple_node
*node
;
581 struct gl_debug_severity
*entry
;
583 /* change the default for IDs we've never seen before. */
584 ctx
->Debug
.Defaults
[sev
][s
][t
] = enabled
;
586 /* Now change the state of IDs we *have* seen... */
587 foreach(node
, &ctx
->Debug
.Namespaces
[s
][t
].Severity
[sev
]) {
588 entry
= (struct gl_debug_severity
*)node
;
589 set_message_state(ctx
, s
, t
, entry
->ID
, enabled
);
595 * Debugging-message namespaces with the source APPLICATION or THIRD_PARTY
596 * require special handling, since the IDs in them are controlled by clients,
597 * not the OpenGL implementation.
599 * 'count' is the length of the array 'ids'. If 'count' is nonzero, all
600 * the given IDs in the namespace defined by 'esource' and 'etype'
603 * If 'count' is zero, this sets the state of all IDs that match
604 * the combination of 'esource', 'etype', and 'eseverity'.
607 control_app_messages(struct gl_context
*ctx
, GLenum esource
, GLenum etype
,
608 GLenum eseverity
, GLsizei count
, const GLuint
*ids
,
612 enum mesa_debug_source source
= gl_enum_to_debug_source(esource
);
613 enum mesa_debug_type type
= gl_enum_to_debug_type(etype
);
614 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(eseverity
);
616 for (i
= 0; i
< count
; i
++)
617 set_message_state(ctx
, source
, type
, ids
[i
], enabled
);
622 control_messages(ctx
, source
, type
, severity
, enabled
);
626 _mesa_DebugMessageControlARB(GLenum gl_source
, GLenum gl_type
,
628 GLsizei count
, const GLuint
*ids
,
631 GET_CURRENT_CONTEXT(ctx
);
634 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDebugMessageControlARB"
635 "(count=%d : count must not be negative)", count
);
639 if (!validate_params(ctx
, CONTROL
, gl_source
, gl_type
, gl_severity
))
640 return; /* GL_INVALID_ENUM */
642 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
643 || gl_source
== GL_DONT_CARE
)) {
644 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDebugMessageControlARB"
645 "(When passing an array of ids, severity must be"
646 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.");
650 control_app_messages(ctx
, gl_source
, gl_type
, gl_severity
,
651 count
, ids
, enabled
);
655 _mesa_DebugMessageCallbackARB(GLDEBUGPROCARB callback
, const void *userParam
)
657 GET_CURRENT_CONTEXT(ctx
);
658 ctx
->Debug
.Callback
= callback
;
659 ctx
->Debug
.CallbackData
= userParam
;
663 _mesa_init_errors(struct gl_context
*ctx
)
667 ctx
->Debug
.Callback
= NULL
;
668 ctx
->Debug
.SyncOutput
= GL_FALSE
;
669 ctx
->Debug
.Log
[0].length
= 0;
670 ctx
->Debug
.NumMessages
= 0;
671 ctx
->Debug
.NextMsg
= 0;
672 ctx
->Debug
.NextMsgLength
= 0;
674 /* Enable all the messages with severity HIGH or MEDIUM by default. */
675 memset(ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_HIGH
], GL_TRUE
,
676 sizeof ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_HIGH
]);
677 memset(ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_MEDIUM
], GL_TRUE
,
678 sizeof ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_MEDIUM
]);
679 memset(ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_LOW
], GL_FALSE
,
680 sizeof ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_LOW
]);
682 /* Initialize state for filtering known debug messages. */
683 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++)
684 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
685 ctx
->Debug
.Namespaces
[s
][t
].IDs
= _mesa_NewHashTable();
686 assert(ctx
->Debug
.Namespaces
[s
][t
].IDs
);
688 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++)
689 make_empty_list(&ctx
->Debug
.Namespaces
[s
][t
].Severity
[sev
]);
694 do_nothing(GLuint key
, void *data
, void *userData
)
699 _mesa_free_errors_data(struct gl_context
*ctx
)
701 enum mesa_debug_type t
;
702 enum mesa_debug_source s
;
703 enum mesa_debug_severity sev
;
705 /* Tear down state for filtering debug messages. */
706 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++)
707 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
708 _mesa_HashDeleteAll(ctx
->Debug
.Namespaces
[s
][t
].IDs
, do_nothing
, NULL
);
709 _mesa_DeleteHashTable(ctx
->Debug
.Namespaces
[s
][t
].IDs
);
710 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
711 struct simple_node
*node
, *tmp
;
712 struct gl_debug_severity
*entry
;
714 foreach_s(node
, tmp
, &ctx
->Debug
.Namespaces
[s
][t
].Severity
[sev
]) {
715 entry
= (struct gl_debug_severity
*)node
;
722 /**********************************************************************/
723 /** \name Diagnostics */
727 output_if_debug(const char *prefixString
, const char *outputString
,
730 static int debug
= -1;
731 static FILE *fout
= NULL
;
733 /* Init the local 'debug' var once.
734 * Note: the _mesa_init_debug() function should have been called
735 * by now so MESA_DEBUG_FLAGS will be initialized.
738 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
739 * etc to the named file. Otherwise, output to stderr.
741 const char *logFile
= _mesa_getenv("MESA_LOG_FILE");
743 fout
= fopen(logFile
, "w");
747 /* in debug builds, print messages unless MESA_DEBUG="silent" */
748 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
753 /* in release builds, be silent unless MESA_DEBUG is set */
754 debug
= _mesa_getenv("MESA_DEBUG") != NULL
;
758 /* Now only print the string if we're required to do so. */
760 fprintf(fout
, "%s: %s", prefixString
, outputString
);
765 #if defined(_WIN32) && !defined(_WIN32_WCE)
766 /* stderr from windows applications without console is not usually
767 * visible, so communicate with the debugger instead */
770 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
771 OutputDebugStringA(buf
);
778 * When a new type of error is recorded, print a message describing
779 * previous errors which were accumulated.
782 flush_delayed_errors( struct gl_context
*ctx
)
784 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
786 if (ctx
->ErrorDebugCount
) {
787 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
788 ctx
->ErrorDebugCount
,
789 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
791 output_if_debug("Mesa", s
, GL_TRUE
);
793 ctx
->ErrorDebugCount
= 0;
799 * Report a warning (a recoverable error condition) to stderr if
800 * either DEBUG is defined or the MESA_DEBUG env var is set.
802 * \param ctx GL context.
803 * \param fmtString printf()-like format string.
806 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
808 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
810 va_start( args
, fmtString
);
811 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
815 flush_delayed_errors( ctx
);
817 output_if_debug("Mesa warning", str
, GL_TRUE
);
822 * Report an internal implementation problem.
823 * Prints the message to stderr via fprintf().
825 * \param ctx GL context.
826 * \param fmtString problem description string.
829 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
832 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
833 static int numCalls
= 0;
840 va_start( args
, fmtString
);
841 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
843 fprintf(stderr
, "Mesa %s implementation error: %s\n",
844 PACKAGE_VERSION
, str
);
845 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
850 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
852 static GLint debug
= -1;
854 /* Check debug environment variable only once:
857 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
860 if (debugEnv
&& strstr(debugEnv
, "silent"))
873 if (ctx
->ErrorValue
!= error
||
874 ctx
->ErrorDebugFmtString
!= fmtString
) {
875 flush_delayed_errors( ctx
);
876 ctx
->ErrorDebugFmtString
= fmtString
;
877 ctx
->ErrorDebugCount
= 0;
880 ctx
->ErrorDebugCount
++;
886 _mesa_gl_debug(struct gl_context
*ctx
,
888 enum mesa_debug_type type
,
889 enum mesa_debug_severity severity
,
890 const char *fmtString
, ...)
892 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
898 va_start(args
, fmtString
);
899 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
902 _mesa_log_msg(ctx
, MESA_DEBUG_SOURCE_API
, type
,
903 *id
, severity
, len
, s
);
908 * Record an OpenGL state error. These usually occur when the user
909 * passes invalid parameters to a GL function.
911 * If debugging is enabled (either at compile-time via the DEBUG macro, or
912 * run-time via the MESA_DEBUG environment variable), report the error with
915 * \param ctx the GL context.
916 * \param error the error value.
917 * \param fmtString printf() style format string, followed by optional args
920 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
922 GLboolean do_output
, do_log
;
923 /* Ideally this would be set up by the caller, so that we had proper IDs
924 * per different message.
926 static GLuint error_msg_id
= 0;
928 debug_get_id(&error_msg_id
);
930 do_output
= should_output(ctx
, error
, fmtString
);
931 do_log
= should_log(ctx
,
932 MESA_DEBUG_SOURCE_API
,
933 MESA_DEBUG_TYPE_ERROR
,
935 MESA_DEBUG_SEVERITY_HIGH
);
937 if (do_output
|| do_log
) {
938 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
942 va_start(args
, fmtString
);
943 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
946 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
947 /* Too long error message. Whoever calls _mesa_error should use
948 * shorter strings. */
953 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
954 _mesa_lookup_enum_by_nr(error
), s
);
955 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
961 /* Print the error to stderr if needed. */
963 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
966 /* Log the error via ARB_debug_output if needed.*/
969 MESA_DEBUG_SOURCE_API
,
970 MESA_DEBUG_TYPE_ERROR
,
972 MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
976 /* Set the GL context error state for glGetError. */
977 _mesa_record_error(ctx
, error
);
982 * Report debug information. Print error message to stderr via fprintf().
983 * No-op if DEBUG mode not enabled.
985 * \param ctx GL context.
986 * \param fmtString printf()-style format string, followed by optional args.
989 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
992 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
994 va_start(args
, fmtString
);
995 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
997 output_if_debug("Mesa", s
, GL_FALSE
);
1005 * Report debug information from the shader compiler via GL_ARB_debug_output.
1007 * \param ctx GL context.
1008 * \param type The namespace to which this message belongs.
1009 * \param id The message ID within the given namespace.
1010 * \param msg The message to output. Need not be null-terminated.
1011 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1014 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1015 const char *msg
, int len
)
1017 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1018 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1025 /* Truncate the message if necessary. */
1026 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1027 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1029 _mesa_log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);