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"
43 #define MESSAGE_LOG_ARB 2
45 _glthread_DECLARE_STATIC_MUTEX(DynamicIDMutex
);
46 static GLuint NextDynamicID
= 1;
48 struct gl_debug_severity
50 struct simple_node link
;
54 static char out_of_memory
[] = "Debugging error: out of memory";
56 static const GLenum debug_source_enums
[] = {
58 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
59 GL_DEBUG_SOURCE_SHADER_COMPILER
,
60 GL_DEBUG_SOURCE_THIRD_PARTY
,
61 GL_DEBUG_SOURCE_APPLICATION
,
62 GL_DEBUG_SOURCE_OTHER
,
65 static const GLenum debug_type_enums
[] = {
67 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
68 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
69 GL_DEBUG_TYPE_PORTABILITY
,
70 GL_DEBUG_TYPE_PERFORMANCE
,
73 GL_DEBUG_TYPE_PUSH_GROUP
,
74 GL_DEBUG_TYPE_POP_GROUP
,
77 static const GLenum debug_severity_enums
[] = {
78 GL_DEBUG_SEVERITY_LOW
,
79 GL_DEBUG_SEVERITY_MEDIUM
,
80 GL_DEBUG_SEVERITY_HIGH
,
81 GL_DEBUG_SEVERITY_NOTIFICATION
,
85 static enum mesa_debug_source
86 gl_enum_to_debug_source(GLenum e
)
90 for (i
= 0; i
< Elements(debug_source_enums
); i
++) {
91 if (debug_source_enums
[i
] == e
)
97 static enum mesa_debug_type
98 gl_enum_to_debug_type(GLenum e
)
102 for (i
= 0; i
< Elements(debug_type_enums
); i
++) {
103 if (debug_type_enums
[i
] == e
)
109 static enum mesa_debug_severity
110 gl_enum_to_debug_severity(GLenum e
)
114 for (i
= 0; i
< Elements(debug_severity_enums
); i
++) {
115 if (debug_severity_enums
[i
] == e
)
123 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
126 * The GL API has this "ID" mechanism, where the intention is to allow a
127 * client to filter in/out messages based on source, type, and ID. Of course,
128 * building a giant enum list of all debug output messages that Mesa might
129 * generate is ridiculous, so instead we have our caller pass us a pointer to
130 * static storage where the ID should get stored. This ID will be shared
131 * across all contexts for that message (which seems like a desirable
132 * property, even if it's not expected by the spec), but note that it won't be
133 * the same between executions if messages aren't generated in the same order.
136 debug_get_id(GLuint
*id
)
139 _glthread_LOCK_MUTEX(DynamicIDMutex
);
141 *id
= NextDynamicID
++;
142 _glthread_UNLOCK_MUTEX(DynamicIDMutex
);
148 * We store a bitfield in the hash table, with five possible values total.
150 * The ENABLED_BIT's purpose is self-explanatory.
152 * The FOUND_BIT is needed to differentiate the value of DISABLED from
153 * the value returned by HashTableLookup() when it can't find the given key.
155 * The KNOWN_SEVERITY bit is a bit complicated:
157 * A client may call Control() with an array of IDs, then call Control()
158 * on all message IDs of a certain severity, then Insert() one of the
159 * previously specified IDs, giving us a known severity level, then call
160 * Control() on all message IDs of a certain severity level again.
162 * After the first call, those IDs will have a FOUND_BIT, but will not
163 * exist in any severity-specific list, so the second call will not
164 * impact them. This is undesirable but unavoidable given the API:
165 * The only entrypoint that gives a severity for a client-defined ID
166 * is the Insert() call.
168 * For the sake of Control(), we want to maintain the invariant
169 * that an ID will either appear in none of the three severity lists,
170 * or appear once, to minimize pointless duplication and potential surprises.
172 * Because Insert() is the only place that will learn an ID's severity,
173 * it should insert an ID into the appropriate list, but only if the ID
174 * doesn't exist in it or any other list yet. Because searching all three
175 * lists at O(n) is needlessly expensive, we store KNOWN_SEVERITY.
179 ENABLED_BIT
= 1 << 1,
180 KNOWN_SEVERITY
= 1 << 2,
182 /* HashTable reserves zero as a return value meaning 'not found' */
184 DISABLED
= FOUND_BIT
,
185 ENABLED
= ENABLED_BIT
| FOUND_BIT
190 * Return debug state for the context. The debug state will be allocated
191 * and initialized upon the first call.
193 struct gl_debug_state
*
194 _mesa_get_debug_state(struct gl_context
*ctx
)
197 ctx
->Debug
= CALLOC_STRUCT(gl_debug_state
);
199 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
202 struct gl_debug_state
*debug
= ctx
->Debug
;
205 /* Enable all the messages with severity HIGH or MEDIUM by default. */
206 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
], GL_TRUE
,
207 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
]);
208 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
], GL_TRUE
,
209 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
]);
210 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_LOW
], GL_FALSE
,
211 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_LOW
]);
213 /* Initialize state for filtering known debug messages. */
214 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
215 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
216 debug
->Namespaces
[0][s
][t
].IDs
= _mesa_NewHashTable();
217 assert(debug
->Namespaces
[0][s
][t
].IDs
);
219 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
220 make_empty_list(&debug
->Namespaces
[0][s
][t
].Severity
[sev
]);
233 * Returns the state of the given message source/type/ID tuple.
236 should_log(struct gl_context
*ctx
,
237 enum mesa_debug_source source
,
238 enum mesa_debug_type type
,
240 enum mesa_debug_severity severity
)
242 struct gl_debug_state
*debug
;
246 /* no debug state set so far */
250 debug
= _mesa_get_debug_state(ctx
);
252 const GLint gstack
= debug
->GroupStackDepth
;
253 struct gl_debug_namespace
*nspace
=
254 &debug
->Namespaces
[gstack
][source
][type
];
256 if (!debug
->DebugOutput
)
259 /* In addition to not being able to store zero as a value, HashTable also
260 * can't use zero as a key.
263 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
265 state
= nspace
->ZeroID
;
267 /* Only do this once for each ID. This makes sure the ID exists in,
268 * at most, one list, and does not pointlessly appear multiple times.
270 if (!(state
& KNOWN_SEVERITY
)) {
271 struct gl_debug_severity
*entry
;
273 if (state
== NOT_FOUND
) {
274 if (debug
->Defaults
[gstack
][severity
][source
][type
])
280 entry
= malloc(sizeof *entry
);
284 state
|= KNOWN_SEVERITY
;
287 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
289 nspace
->ZeroID
= state
;
292 insert_at_tail(&nspace
->Severity
[severity
], &entry
->link
);
296 return !!(state
& ENABLED_BIT
);
301 * Sets the state of the given message source/type/ID tuple.
304 set_message_state(struct gl_context
*ctx
,
305 enum mesa_debug_source source
,
306 enum mesa_debug_type type
,
307 GLuint id
, GLboolean enabled
)
309 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
312 GLint gstack
= debug
->GroupStackDepth
;
313 struct gl_debug_namespace
*nspace
=
314 &debug
->Namespaces
[gstack
][source
][type
];
317 /* In addition to not being able to store zero as a value, HashTable also
318 * can't use zero as a key.
321 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
323 state
= nspace
->ZeroID
;
325 if (state
== NOT_FOUND
)
326 state
= enabled
? ENABLED
: DISABLED
;
329 state
|= ENABLED_BIT
;
331 state
&= ~ENABLED_BIT
;
335 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
337 nspace
->ZeroID
= state
;
343 store_message_details(struct gl_debug_msg
*emptySlot
,
344 enum mesa_debug_source source
,
345 enum mesa_debug_type type
, GLuint id
,
346 enum mesa_debug_severity severity
, GLint len
,
349 assert(!emptySlot
->message
&& !emptySlot
->length
);
351 emptySlot
->message
= malloc(len
+1);
352 if (emptySlot
->message
) {
353 (void) strncpy(emptySlot
->message
, buf
, (size_t)len
);
354 emptySlot
->message
[len
] = '\0';
356 emptySlot
->length
= len
+1;
357 emptySlot
->source
= source
;
358 emptySlot
->type
= type
;
360 emptySlot
->severity
= severity
;
362 static GLuint oom_msg_id
= 0;
363 debug_get_id(&oom_msg_id
);
366 emptySlot
->message
= out_of_memory
;
367 emptySlot
->length
= strlen(out_of_memory
)+1;
368 emptySlot
->source
= MESA_DEBUG_SOURCE_OTHER
;
369 emptySlot
->type
= MESA_DEBUG_TYPE_ERROR
;
370 emptySlot
->id
= oom_msg_id
;
371 emptySlot
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
377 * Remap any type exclusive to KHR_debug to something suitable
378 * for ARB_debug_output
381 remap_type(GLenum type
) {
384 case GL_DEBUG_TYPE_MARKER
:
385 case GL_DEBUG_TYPE_PUSH_GROUP
:
386 case GL_DEBUG_TYPE_POP_GROUP
:
387 type
= GL_DEBUG_TYPE_OTHER
;
397 * Remap severity exclusive to KHR_debug to something suitable
398 * for ARB_debug_output
401 remap_severity(GLenum severity
) {
403 if (GL_DEBUG_SEVERITY_NOTIFICATION
== severity
)
404 severity
= GL_DEBUG_SEVERITY_LOW
;
411 * 'buf' is not necessarily a null-terminated string. When logging, copy
412 * 'len' characters from it, store them in a new, null-terminated string,
413 * and remember the number of bytes used by that string, *including*
414 * the null terminator this time.
417 log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
418 enum mesa_debug_type type
, GLuint id
,
419 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
421 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
423 struct gl_debug_msg
*emptySlot
;
428 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
430 if (!should_log(ctx
, source
, type
, id
, severity
))
433 if (debug
->Callback
) {
434 GLenum gl_type
= debug_type_enums
[type
];
435 GLenum gl_severity
= debug_severity_enums
[severity
];
437 if (debug
->ARBCallback
) {
438 gl_severity
= remap_severity(gl_severity
);
439 gl_type
= remap_type(gl_type
);
441 debug
->Callback(debug_source_enums
[source
], gl_type
, id
, gl_severity
,
442 len
, buf
, debug
->CallbackData
);
446 if (debug
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
449 nextEmpty
= (debug
->NextMsg
+ debug
->NumMessages
)
450 % MAX_DEBUG_LOGGED_MESSAGES
;
451 emptySlot
= &debug
->Log
[nextEmpty
];
453 store_message_details(emptySlot
, source
, type
, id
, severity
, len
, buf
);
455 if (debug
->NumMessages
== 0)
456 debug
->NextMsgLength
= debug
->Log
[debug
->NextMsg
].length
;
458 debug
->NumMessages
++;
463 * Pop the oldest debug message out of the log.
464 * Writes the message string, including the null terminator, into 'buf',
465 * using up to 'bufSize' bytes. If 'bufSize' is too small, or
466 * if 'buf' is NULL, nothing is written.
468 * Returns the number of bytes written on success, or when 'buf' is NULL,
469 * the number that would have been written. A return value of 0
473 get_msg(struct gl_context
*ctx
, GLenum
*source
, GLenum
*type
,
474 GLuint
*id
, GLenum
*severity
, GLsizei bufSize
, char *buf
,
477 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
478 struct gl_debug_msg
*msg
;
481 if (!debug
|| debug
->NumMessages
== 0)
484 msg
= &debug
->Log
[debug
->NextMsg
];
485 length
= msg
->length
;
487 assert(length
> 0 && length
== debug
->NextMsgLength
);
489 if (bufSize
< length
&& buf
!= NULL
)
493 *severity
= debug_severity_enums
[msg
->severity
];
494 if (caller
== MESSAGE_LOG_ARB
)
495 *severity
= remap_severity(*severity
);
499 *source
= debug_source_enums
[msg
->source
];
503 *type
= debug_type_enums
[msg
->type
];
504 if (caller
== MESSAGE_LOG_ARB
)
505 *type
= remap_type(*type
);
513 assert(msg
->message
[length
-1] == '\0');
514 (void) strncpy(buf
, msg
->message
, (size_t)length
);
517 if (msg
->message
!= (char*)out_of_memory
)
522 debug
->NumMessages
--;
524 debug
->NextMsg
%= MAX_DEBUG_LOGGED_MESSAGES
;
525 debug
->NextMsgLength
= debug
->Log
[debug
->NextMsg
].length
;
532 * Verify that source, type, and severity are valid enums.
533 * glDebugMessageInsertARB only accepts two values for 'source',
534 * and glDebugMessageControlARB will additionally accept GL_DONT_CARE
535 * in any parameter, so handle those cases specially.
537 * There is also special cases for handling values available in
538 * GL_KHR_debug that are not avaliable in GL_ARB_debug_output
541 validate_params(struct gl_context
*ctx
, unsigned caller
,
542 const char *callerstr
, GLenum source
, GLenum type
,
548 #define CONTROL_ARB 4
550 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
551 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
553 case GL_DEBUG_SOURCE_API_ARB
:
554 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
555 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
556 case GL_DEBUG_SOURCE_OTHER_ARB
:
557 if (caller
!= INSERT
|| caller
== INSERT_ARB
)
560 if (caller
== CONTROL
|| caller
== CONTROL_ARB
)
567 case GL_DEBUG_TYPE_ERROR_ARB
:
568 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
569 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
570 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
571 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
572 case GL_DEBUG_TYPE_OTHER_ARB
:
574 case GL_DEBUG_TYPE_MARKER
:
575 /* this value is only valid for GL_KHR_debug functions */
576 if (caller
== CONTROL
|| caller
== INSERT
)
579 if (caller
== CONTROL
|| caller
== CONTROL_ARB
)
586 case GL_DEBUG_SEVERITY_HIGH_ARB
:
587 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
588 case GL_DEBUG_SEVERITY_LOW_ARB
:
590 case GL_DEBUG_SEVERITY_NOTIFICATION
:
591 /* this value is only valid for GL_KHR_debug functions */
592 if (caller
== CONTROL
|| caller
== INSERT
)
595 if (caller
== CONTROL
|| caller
== CONTROL_ARB
)
603 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
604 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
605 source
, type
, severity
);
612 * Set the state of all message IDs found in the given intersection of
613 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
614 * GL_DONT_CARE (include all messages in the class).
616 * This requires both setting the state of all previously seen message
617 * IDs in the hash table, and setting the default state for all
618 * applicable combinations of source/type/severity, so that all the
619 * yet-unknown message IDs that may be used in the future will be
620 * impacted as if they were already known.
623 control_messages(struct gl_context
*ctx
,
624 enum mesa_debug_source source
,
625 enum mesa_debug_type type
,
626 enum mesa_debug_severity severity
,
629 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
630 int s
, t
, sev
, smax
, tmax
, sevmax
;
631 const GLint gstack
= debug
? debug
->GroupStackDepth
: 0;
636 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
638 smax
= MESA_DEBUG_SOURCE_COUNT
;
643 if (type
== MESA_DEBUG_TYPE_COUNT
) {
645 tmax
= MESA_DEBUG_TYPE_COUNT
;
650 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
652 sevmax
= MESA_DEBUG_SEVERITY_COUNT
;
657 for (sev
= severity
; sev
< sevmax
; sev
++) {
658 for (s
= source
; s
< smax
; s
++) {
659 for (t
= type
; t
< tmax
; t
++) {
660 struct simple_node
*node
;
661 struct gl_debug_severity
*entry
;
663 /* change the default for IDs we've never seen before. */
664 debug
->Defaults
[gstack
][sev
][s
][t
] = enabled
;
666 /* Now change the state of IDs we *have* seen... */
667 foreach(node
, &debug
->Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
668 entry
= (struct gl_debug_severity
*)node
;
669 set_message_state(ctx
, s
, t
, entry
->ID
, enabled
);
678 * Debugging-message namespaces with the source APPLICATION or THIRD_PARTY
679 * require special handling, since the IDs in them are controlled by clients,
680 * not the OpenGL implementation.
682 * 'count' is the length of the array 'ids'. If 'count' is nonzero, all
683 * the given IDs in the namespace defined by 'esource' and 'etype'
686 * If 'count' is zero, this sets the state of all IDs that match
687 * the combination of 'esource', 'etype', and 'eseverity'.
690 control_app_messages(struct gl_context
*ctx
, GLenum esource
, GLenum etype
,
691 GLenum eseverity
, GLsizei count
, const GLuint
*ids
,
695 enum mesa_debug_source source
= gl_enum_to_debug_source(esource
);
696 enum mesa_debug_type type
= gl_enum_to_debug_type(etype
);
697 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(eseverity
);
699 for (i
= 0; i
< count
; i
++)
700 set_message_state(ctx
, source
, type
, ids
[i
], enabled
);
705 control_messages(ctx
, source
, type
, severity
, enabled
);
710 * This is a generic message control function for use by both
711 * glDebugMessageControlARB and glDebugMessageControl.
714 message_control(GLenum gl_source
, GLenum gl_type
,
716 GLsizei count
, const GLuint
*ids
,
718 unsigned caller
, const char *callerstr
)
720 GET_CURRENT_CONTEXT(ctx
);
723 _mesa_error(ctx
, GL_INVALID_VALUE
,
724 "%s(count=%d : count must not be negative)", callerstr
,
729 if (!validate_params(ctx
, caller
, callerstr
, gl_source
, gl_type
,
731 return; /* GL_INVALID_ENUM */
733 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
734 || gl_source
== GL_DONT_CARE
)) {
735 _mesa_error(ctx
, GL_INVALID_OPERATION
,
736 "%s(When passing an array of ids, severity must be"
737 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
742 control_app_messages(ctx
, gl_source
, gl_type
, gl_severity
,
743 count
, ids
, enabled
);
748 * This is a generic message insert function.
749 * Validation of source, type and severity parameters should be done
750 * before calling this funtion.
753 message_insert(GLenum source
, GLenum type
, GLuint id
,
754 GLenum severity
, GLint length
, const GLchar
*buf
,
755 const char *callerstr
)
757 GET_CURRENT_CONTEXT(ctx
);
760 length
= strlen(buf
);
762 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
763 _mesa_error(ctx
, GL_INVALID_VALUE
,
764 "%s(length=%d, which is not less than "
765 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
766 MAX_DEBUG_MESSAGE_LENGTH
);
771 gl_enum_to_debug_source(source
),
772 gl_enum_to_debug_type(type
), id
,
773 gl_enum_to_debug_severity(severity
), length
, buf
);
778 * This is a generic message insert function for use by both
779 * glGetDebugMessageLogARB and glGetDebugMessageLog.
782 get_message_log(GLuint count
, GLsizei logSize
, GLenum
*sources
,
783 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
784 GLsizei
*lengths
, GLchar
*messageLog
,
785 unsigned caller
, const char *callerstr
)
787 GET_CURRENT_CONTEXT(ctx
);
794 _mesa_error(ctx
, GL_INVALID_VALUE
,
795 "%s(logSize=%d : logSize must not be negative)", callerstr
,
800 for (ret
= 0; ret
< count
; ret
++) {
801 GLsizei written
= get_msg(ctx
, sources
, types
, ids
, severities
,
802 logSize
, messageLog
, caller
);
807 messageLog
+= written
;
830 do_nothing(GLuint key
, void *data
, void *userData
)
836 * Free context state pertaining to error/debug state for the given stack
840 free_errors_data(struct gl_context
*ctx
, GLint gstack
)
842 struct gl_debug_state
*debug
= ctx
->Debug
;
843 enum mesa_debug_type t
;
844 enum mesa_debug_source s
;
845 enum mesa_debug_severity sev
;
849 /* Tear down state for filtering debug messages. */
850 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
851 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
852 _mesa_HashDeleteAll(debug
->Namespaces
[gstack
][s
][t
].IDs
,
854 _mesa_DeleteHashTable(debug
->Namespaces
[gstack
][s
][t
].IDs
);
855 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
856 struct simple_node
*node
, *tmp
;
857 struct gl_debug_severity
*entry
;
860 &debug
->Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
861 entry
= (struct gl_debug_severity
*)node
;
871 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
872 GLenum severity
, GLint length
,
875 const char *callerstr
= "glDebugMessageInsert";
877 GET_CURRENT_CONTEXT(ctx
);
879 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
880 return; /* GL_INVALID_ENUM */
882 message_insert(source
, type
, id
, severity
, length
, buf
, callerstr
);
887 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
888 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
889 GLsizei
*lengths
, GLchar
*messageLog
)
891 const char *callerstr
= "glGetDebugMessageLog";
893 return get_message_log(count
, logSize
, sources
, types
, ids
, severities
,
894 lengths
, messageLog
, MESSAGE_LOG
, callerstr
);
899 _mesa_DebugMessageControl(GLenum source
, GLenum type
, GLenum severity
,
900 GLsizei count
, const GLuint
*ids
,
903 const char *callerstr
= "glDebugMessageControl";
905 message_control(source
, type
, severity
, count
, ids
,
906 enabled
, CONTROL
, callerstr
);
911 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
913 GET_CURRENT_CONTEXT(ctx
);
914 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
916 debug
->Callback
= callback
;
917 debug
->CallbackData
= userParam
;
918 debug
->ARBCallback
= GL_FALSE
;
924 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
925 const GLchar
*message
)
927 GET_CURRENT_CONTEXT(ctx
);
928 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
929 const char *callerstr
= "glPushDebugGroup";
931 GLint prevStackDepth
;
932 GLint currStackDepth
;
933 struct gl_debug_msg
*emptySlot
;
938 if (debug
->GroupStackDepth
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
939 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
944 case GL_DEBUG_SOURCE_APPLICATION
:
945 case GL_DEBUG_SOURCE_THIRD_PARTY
:
948 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
949 "(source=0x%x)", callerstr
, source
);
953 message_insert(source
, GL_DEBUG_TYPE_PUSH_GROUP
, id
,
954 GL_DEBUG_SEVERITY_NOTIFICATION
, length
,
957 prevStackDepth
= debug
->GroupStackDepth
;
958 debug
->GroupStackDepth
++;
959 currStackDepth
= debug
->GroupStackDepth
;
961 /* pop reuses the message details from push so we store this */
963 length
= strlen(message
);
964 emptySlot
= &debug
->DebugGroupMsgs
[debug
->GroupStackDepth
];
965 store_message_details(emptySlot
, gl_enum_to_debug_source(source
),
966 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
968 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
971 /* inherit the control volume of the debug group previously residing on
972 * the top of the debug group stack
974 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
975 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
976 /* copy id settings */
977 debug
->Namespaces
[currStackDepth
][s
][t
].IDs
=
978 _mesa_HashClone(debug
->Namespaces
[prevStackDepth
][s
][t
].IDs
);
980 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
981 struct gl_debug_severity
*entry
, *prevEntry
;
982 struct simple_node
*node
;
984 /* copy default settings for unknown ids */
985 debug
->Defaults
[currStackDepth
][sev
][s
][t
] =
986 debug
->Defaults
[prevStackDepth
][sev
][s
][t
];
988 /* copy known id severity settings */
989 make_empty_list(&debug
->Namespaces
[currStackDepth
][s
][t
].Severity
[sev
]);
990 foreach(node
, &debug
->Namespaces
[prevStackDepth
][s
][t
].Severity
[sev
]) {
991 prevEntry
= (struct gl_debug_severity
*)node
;
992 entry
= malloc(sizeof *entry
);
996 entry
->ID
= prevEntry
->ID
;
997 insert_at_tail(&debug
->Namespaces
[currStackDepth
][s
][t
].Severity
[sev
], &entry
->link
);
1006 _mesa_PopDebugGroup(void)
1008 GET_CURRENT_CONTEXT(ctx
);
1009 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
1010 const char *callerstr
= "glPopDebugGroup";
1011 struct gl_debug_msg
*gdmessage
;
1012 GLint prevStackDepth
;
1017 if (debug
->GroupStackDepth
<= 0) {
1018 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
1022 prevStackDepth
= debug
->GroupStackDepth
;
1023 debug
->GroupStackDepth
--;
1025 gdmessage
= &debug
->DebugGroupMsgs
[prevStackDepth
];
1026 /* using log_msg() directly here as verification of parameters
1027 * already done in push
1029 log_msg(ctx
, gdmessage
->source
,
1030 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
1032 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1033 gdmessage
->length
, gdmessage
->message
);
1035 if (gdmessage
->message
!= (char*)out_of_memory
)
1036 free(gdmessage
->message
);
1037 gdmessage
->message
= NULL
;
1038 gdmessage
->length
= 0;
1040 /* free popped debug group data */
1041 free_errors_data(ctx
, prevStackDepth
);
1046 _mesa_DebugMessageInsertARB(GLenum source
, GLenum type
, GLuint id
,
1047 GLenum severity
, GLint length
,
1048 const GLcharARB
*buf
)
1050 const char *callerstr
= "glDebugMessageInsertARB";
1052 GET_CURRENT_CONTEXT(ctx
);
1054 if (!validate_params(ctx
, INSERT_ARB
, callerstr
, source
, type
, severity
))
1055 return; /* GL_INVALID_ENUM */
1057 message_insert(source
, type
, id
, severity
, length
, buf
, callerstr
);
1062 _mesa_GetDebugMessageLogARB(GLuint count
, GLsizei logSize
, GLenum
*sources
,
1063 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
1064 GLsizei
*lengths
, GLcharARB
*messageLog
)
1066 const char *callerstr
= "glGetDebugMessageLogARB";
1068 return get_message_log(count
, logSize
, sources
, types
, ids
, severities
,
1069 lengths
, messageLog
, MESSAGE_LOG_ARB
, callerstr
);
1074 _mesa_DebugMessageControlARB(GLenum gl_source
, GLenum gl_type
,
1076 GLsizei count
, const GLuint
*ids
,
1079 const char *callerstr
= "glDebugMessageControlARB";
1081 message_control(gl_source
, gl_type
, gl_severity
, count
, ids
,
1082 enabled
, CONTROL_ARB
, callerstr
);
1087 _mesa_DebugMessageCallbackARB(GLDEBUGPROCARB callback
, const void *userParam
)
1089 GET_CURRENT_CONTEXT(ctx
);
1090 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
1092 debug
->Callback
= callback
;
1093 debug
->CallbackData
= userParam
;
1094 debug
->ARBCallback
= GL_TRUE
;
1100 _mesa_init_errors(struct gl_context
*ctx
)
1107 * Loop through debug group stack tearing down states for
1108 * filtering debug messages.
1111 _mesa_free_errors_data(struct gl_context
*ctx
)
1116 for (i
= 0; i
<= ctx
->Debug
->GroupStackDepth
; i
++) {
1117 free_errors_data(ctx
, i
);
1123 /**********************************************************************/
1124 /** \name Diagnostics */
1128 output_if_debug(const char *prefixString
, const char *outputString
,
1131 static int debug
= -1;
1132 static FILE *fout
= NULL
;
1134 /* Init the local 'debug' var once.
1135 * Note: the _mesa_init_debug() function should have been called
1136 * by now so MESA_DEBUG_FLAGS will be initialized.
1139 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1140 * etc to the named file. Otherwise, output to stderr.
1142 const char *logFile
= _mesa_getenv("MESA_LOG_FILE");
1144 fout
= fopen(logFile
, "w");
1148 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1149 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1154 /* in release builds, be silent unless MESA_DEBUG is set */
1155 debug
= _mesa_getenv("MESA_DEBUG") != NULL
;
1159 /* Now only print the string if we're required to do so. */
1161 fprintf(fout
, "%s: %s", prefixString
, outputString
);
1163 fprintf(fout
, "\n");
1166 #if defined(_WIN32) && !defined(_WIN32_WCE)
1167 /* stderr from windows applications without console is not usually
1168 * visible, so communicate with the debugger instead */
1171 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1172 OutputDebugStringA(buf
);
1180 * When a new type of error is recorded, print a message describing
1181 * previous errors which were accumulated.
1184 flush_delayed_errors( struct gl_context
*ctx
)
1186 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1188 if (ctx
->ErrorDebugCount
) {
1189 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1190 ctx
->ErrorDebugCount
,
1191 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
1193 output_if_debug("Mesa", s
, GL_TRUE
);
1195 ctx
->ErrorDebugCount
= 0;
1201 * Report a warning (a recoverable error condition) to stderr if
1202 * either DEBUG is defined or the MESA_DEBUG env var is set.
1204 * \param ctx GL context.
1205 * \param fmtString printf()-like format string.
1208 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1210 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1212 va_start( args
, fmtString
);
1213 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1217 flush_delayed_errors( ctx
);
1219 output_if_debug("Mesa warning", str
, GL_TRUE
);
1224 * Report an internal implementation problem.
1225 * Prints the message to stderr via fprintf().
1227 * \param ctx GL context.
1228 * \param fmtString problem description string.
1231 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1234 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1235 static int numCalls
= 0;
1239 if (numCalls
< 50) {
1242 va_start( args
, fmtString
);
1243 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1245 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1246 PACKAGE_VERSION
, str
);
1247 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1253 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1255 static GLint debug
= -1;
1257 /* Check debug environment variable only once:
1260 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
1263 if (debugEnv
&& strstr(debugEnv
, "silent"))
1276 if (ctx
->ErrorValue
!= error
||
1277 ctx
->ErrorDebugFmtString
!= fmtString
) {
1278 flush_delayed_errors( ctx
);
1279 ctx
->ErrorDebugFmtString
= fmtString
;
1280 ctx
->ErrorDebugCount
= 0;
1283 ctx
->ErrorDebugCount
++;
1290 _mesa_gl_debug(struct gl_context
*ctx
,
1292 enum mesa_debug_type type
,
1293 enum mesa_debug_severity severity
,
1294 const char *fmtString
, ...)
1296 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1302 va_start(args
, fmtString
);
1303 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1306 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, type
, *id
, severity
, len
, s
);
1311 * Record an OpenGL state error. These usually occur when the user
1312 * passes invalid parameters to a GL function.
1314 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1315 * run-time via the MESA_DEBUG environment variable), report the error with
1318 * \param ctx the GL context.
1319 * \param error the error value.
1320 * \param fmtString printf() style format string, followed by optional args
1323 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1325 GLboolean do_output
, do_log
;
1326 /* Ideally this would be set up by the caller, so that we had proper IDs
1327 * per different message.
1329 static GLuint error_msg_id
= 0;
1331 debug_get_id(&error_msg_id
);
1333 do_output
= should_output(ctx
, error
, fmtString
);
1334 do_log
= should_log(ctx
,
1335 MESA_DEBUG_SOURCE_API
,
1336 MESA_DEBUG_TYPE_ERROR
,
1338 MESA_DEBUG_SEVERITY_HIGH
);
1340 if (do_output
|| do_log
) {
1341 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1345 va_start(args
, fmtString
);
1346 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1349 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1350 /* Too long error message. Whoever calls _mesa_error should use
1357 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1358 _mesa_lookup_enum_by_nr(error
), s
);
1359 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1360 /* Same as above. */
1365 /* Print the error to stderr if needed. */
1367 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1370 /* Log the error via ARB_debug_output if needed.*/
1372 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_ERROR
,
1373 error_msg_id
, MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1377 /* Set the GL context error state for glGetError. */
1378 _mesa_record_error(ctx
, error
);
1383 * Report debug information. Print error message to stderr via fprintf().
1384 * No-op if DEBUG mode not enabled.
1386 * \param ctx GL context.
1387 * \param fmtString printf()-style format string, followed by optional args.
1390 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1393 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1395 va_start(args
, fmtString
);
1396 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1398 output_if_debug("Mesa", s
, GL_FALSE
);
1406 * Report debug information from the shader compiler via GL_ARB_debug_output.
1408 * \param ctx GL context.
1409 * \param type The namespace to which this message belongs.
1410 * \param id The message ID within the given namespace.
1411 * \param msg The message to output. Need not be null-terminated.
1412 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1415 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1416 const char *msg
, int len
)
1418 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1419 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1426 /* Truncate the message if necessary. */
1427 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1428 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1430 log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);