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"
42 #define MESSAGE_LOG_ARB 2
44 static mtx_t DynamicIDMutex
= _MTX_INITIALIZER_NP
;
45 static GLuint NextDynamicID
= 1;
47 struct gl_debug_severity
49 struct simple_node link
;
53 static char out_of_memory
[] = "Debugging error: out of memory";
55 static const GLenum debug_source_enums
[] = {
57 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
58 GL_DEBUG_SOURCE_SHADER_COMPILER
,
59 GL_DEBUG_SOURCE_THIRD_PARTY
,
60 GL_DEBUG_SOURCE_APPLICATION
,
61 GL_DEBUG_SOURCE_OTHER
,
64 static const GLenum debug_type_enums
[] = {
66 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
67 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
68 GL_DEBUG_TYPE_PORTABILITY
,
69 GL_DEBUG_TYPE_PERFORMANCE
,
72 GL_DEBUG_TYPE_PUSH_GROUP
,
73 GL_DEBUG_TYPE_POP_GROUP
,
76 static const GLenum debug_severity_enums
[] = {
77 GL_DEBUG_SEVERITY_LOW
,
78 GL_DEBUG_SEVERITY_MEDIUM
,
79 GL_DEBUG_SEVERITY_HIGH
,
80 GL_DEBUG_SEVERITY_NOTIFICATION
,
84 static enum mesa_debug_source
85 gl_enum_to_debug_source(GLenum e
)
89 for (i
= 0; i
< Elements(debug_source_enums
); i
++) {
90 if (debug_source_enums
[i
] == e
)
96 static enum mesa_debug_type
97 gl_enum_to_debug_type(GLenum e
)
101 for (i
= 0; i
< Elements(debug_type_enums
); i
++) {
102 if (debug_type_enums
[i
] == e
)
108 static enum mesa_debug_severity
109 gl_enum_to_debug_severity(GLenum e
)
113 for (i
= 0; i
< Elements(debug_severity_enums
); i
++) {
114 if (debug_severity_enums
[i
] == e
)
122 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
125 * The GL API has this "ID" mechanism, where the intention is to allow a
126 * client to filter in/out messages based on source, type, and ID. Of course,
127 * building a giant enum list of all debug output messages that Mesa might
128 * generate is ridiculous, so instead we have our caller pass us a pointer to
129 * static storage where the ID should get stored. This ID will be shared
130 * across all contexts for that message (which seems like a desirable
131 * property, even if it's not expected by the spec), but note that it won't be
132 * the same between executions if messages aren't generated in the same order.
135 debug_get_id(GLuint
*id
)
138 mtx_lock(&DynamicIDMutex
);
140 *id
= NextDynamicID
++;
141 mtx_unlock(&DynamicIDMutex
);
147 * We store a bitfield in the hash table, with five possible values total.
149 * The ENABLED_BIT's purpose is self-explanatory.
151 * The FOUND_BIT is needed to differentiate the value of DISABLED from
152 * the value returned by HashTableLookup() when it can't find the given key.
154 * The KNOWN_SEVERITY bit is a bit complicated:
156 * A client may call Control() with an array of IDs, then call Control()
157 * on all message IDs of a certain severity, then Insert() one of the
158 * previously specified IDs, giving us a known severity level, then call
159 * Control() on all message IDs of a certain severity level again.
161 * After the first call, those IDs will have a FOUND_BIT, but will not
162 * exist in any severity-specific list, so the second call will not
163 * impact them. This is undesirable but unavoidable given the API:
164 * The only entrypoint that gives a severity for a client-defined ID
165 * is the Insert() call.
167 * For the sake of Control(), we want to maintain the invariant
168 * that an ID will either appear in none of the three severity lists,
169 * or appear once, to minimize pointless duplication and potential surprises.
171 * Because Insert() is the only place that will learn an ID's severity,
172 * it should insert an ID into the appropriate list, but only if the ID
173 * doesn't exist in it or any other list yet. Because searching all three
174 * lists at O(n) is needlessly expensive, we store KNOWN_SEVERITY.
178 ENABLED_BIT
= 1 << 1,
179 KNOWN_SEVERITY
= 1 << 2,
181 /* HashTable reserves zero as a return value meaning 'not found' */
183 DISABLED
= FOUND_BIT
,
184 ENABLED
= ENABLED_BIT
| FOUND_BIT
189 * Return debug state for the context. The debug state will be allocated
190 * and initialized upon the first call.
192 struct gl_debug_state
*
193 _mesa_get_debug_state(struct gl_context
*ctx
)
196 ctx
->Debug
= CALLOC_STRUCT(gl_debug_state
);
198 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
201 struct gl_debug_state
*debug
= ctx
->Debug
;
204 /* Enable all the messages with severity HIGH or MEDIUM by default. */
205 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
], GL_TRUE
,
206 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
]);
207 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
], GL_TRUE
,
208 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
]);
209 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_LOW
], GL_FALSE
,
210 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_LOW
]);
212 /* Initialize state for filtering known debug messages. */
213 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
214 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
215 debug
->Namespaces
[0][s
][t
].IDs
= _mesa_NewHashTable();
216 assert(debug
->Namespaces
[0][s
][t
].IDs
);
218 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
219 make_empty_list(&debug
->Namespaces
[0][s
][t
].Severity
[sev
]);
232 * Returns the state of the given message source/type/ID tuple.
235 should_log(struct gl_context
*ctx
,
236 enum mesa_debug_source source
,
237 enum mesa_debug_type type
,
239 enum mesa_debug_severity severity
)
241 struct gl_debug_state
*debug
;
245 /* no debug state set so far */
249 debug
= _mesa_get_debug_state(ctx
);
251 const GLint gstack
= debug
->GroupStackDepth
;
252 struct gl_debug_namespace
*nspace
=
253 &debug
->Namespaces
[gstack
][source
][type
];
255 if (!debug
->DebugOutput
)
258 /* In addition to not being able to store zero as a value, HashTable also
259 * can't use zero as a key.
262 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
264 state
= nspace
->ZeroID
;
266 /* Only do this once for each ID. This makes sure the ID exists in,
267 * at most, one list, and does not pointlessly appear multiple times.
269 if (!(state
& KNOWN_SEVERITY
)) {
270 struct gl_debug_severity
*entry
;
272 if (state
== NOT_FOUND
) {
273 if (debug
->Defaults
[gstack
][severity
][source
][type
])
279 entry
= malloc(sizeof *entry
);
283 state
|= KNOWN_SEVERITY
;
286 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
288 nspace
->ZeroID
= state
;
291 insert_at_tail(&nspace
->Severity
[severity
], &entry
->link
);
295 return !!(state
& ENABLED_BIT
);
300 * Sets the state of the given message source/type/ID tuple.
303 set_message_state(struct gl_context
*ctx
,
304 enum mesa_debug_source source
,
305 enum mesa_debug_type type
,
306 GLuint id
, GLboolean enabled
)
308 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
311 GLint gstack
= debug
->GroupStackDepth
;
312 struct gl_debug_namespace
*nspace
=
313 &debug
->Namespaces
[gstack
][source
][type
];
316 /* In addition to not being able to store zero as a value, HashTable also
317 * can't use zero as a key.
320 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
322 state
= nspace
->ZeroID
;
324 if (state
== NOT_FOUND
)
325 state
= enabled
? ENABLED
: DISABLED
;
328 state
|= ENABLED_BIT
;
330 state
&= ~ENABLED_BIT
;
334 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
336 nspace
->ZeroID
= state
;
342 store_message_details(struct gl_debug_msg
*emptySlot
,
343 enum mesa_debug_source source
,
344 enum mesa_debug_type type
, GLuint id
,
345 enum mesa_debug_severity severity
, GLint len
,
348 assert(!emptySlot
->message
&& !emptySlot
->length
);
350 emptySlot
->message
= malloc(len
+1);
351 if (emptySlot
->message
) {
352 (void) strncpy(emptySlot
->message
, buf
, (size_t)len
);
353 emptySlot
->message
[len
] = '\0';
355 emptySlot
->length
= len
+1;
356 emptySlot
->source
= source
;
357 emptySlot
->type
= type
;
359 emptySlot
->severity
= severity
;
361 static GLuint oom_msg_id
= 0;
362 debug_get_id(&oom_msg_id
);
365 emptySlot
->message
= out_of_memory
;
366 emptySlot
->length
= strlen(out_of_memory
)+1;
367 emptySlot
->source
= MESA_DEBUG_SOURCE_OTHER
;
368 emptySlot
->type
= MESA_DEBUG_TYPE_ERROR
;
369 emptySlot
->id
= oom_msg_id
;
370 emptySlot
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
376 * Remap any type exclusive to KHR_debug to something suitable
377 * for ARB_debug_output
380 remap_type(GLenum type
) {
383 case GL_DEBUG_TYPE_MARKER
:
384 case GL_DEBUG_TYPE_PUSH_GROUP
:
385 case GL_DEBUG_TYPE_POP_GROUP
:
386 type
= GL_DEBUG_TYPE_OTHER
;
396 * Remap severity exclusive to KHR_debug to something suitable
397 * for ARB_debug_output
400 remap_severity(GLenum severity
) {
402 if (GL_DEBUG_SEVERITY_NOTIFICATION
== severity
)
403 severity
= GL_DEBUG_SEVERITY_LOW
;
410 * 'buf' is not necessarily a null-terminated string. When logging, copy
411 * 'len' characters from it, store them in a new, null-terminated string,
412 * and remember the number of bytes used by that string, *including*
413 * the null terminator this time.
416 log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
417 enum mesa_debug_type type
, GLuint id
,
418 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
420 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
422 struct gl_debug_msg
*emptySlot
;
427 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
429 if (!should_log(ctx
, source
, type
, id
, severity
))
432 if (debug
->Callback
) {
433 GLenum gl_type
= debug_type_enums
[type
];
434 GLenum gl_severity
= debug_severity_enums
[severity
];
436 if (debug
->ARBCallback
) {
437 gl_severity
= remap_severity(gl_severity
);
438 gl_type
= remap_type(gl_type
);
440 debug
->Callback(debug_source_enums
[source
], gl_type
, id
, gl_severity
,
441 len
, buf
, debug
->CallbackData
);
445 if (debug
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
448 nextEmpty
= (debug
->NextMsg
+ debug
->NumMessages
)
449 % MAX_DEBUG_LOGGED_MESSAGES
;
450 emptySlot
= &debug
->Log
[nextEmpty
];
452 store_message_details(emptySlot
, source
, type
, id
, severity
, len
, buf
);
454 if (debug
->NumMessages
== 0)
455 debug
->NextMsgLength
= debug
->Log
[debug
->NextMsg
].length
;
457 debug
->NumMessages
++;
462 * Pop the oldest debug message out of the log.
463 * Writes the message string, including the null terminator, into 'buf',
464 * using up to 'bufSize' bytes. If 'bufSize' is too small, or
465 * if 'buf' is NULL, nothing is written.
467 * Returns the number of bytes written on success, or when 'buf' is NULL,
468 * the number that would have been written. A return value of 0
472 get_msg(struct gl_context
*ctx
, GLenum
*source
, GLenum
*type
,
473 GLuint
*id
, GLenum
*severity
, GLsizei bufSize
, char *buf
,
476 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
477 struct gl_debug_msg
*msg
;
480 if (!debug
|| debug
->NumMessages
== 0)
483 msg
= &debug
->Log
[debug
->NextMsg
];
484 length
= msg
->length
;
486 assert(length
> 0 && length
== debug
->NextMsgLength
);
488 if (bufSize
< length
&& buf
!= NULL
)
492 *severity
= debug_severity_enums
[msg
->severity
];
493 if (caller
== MESSAGE_LOG_ARB
)
494 *severity
= remap_severity(*severity
);
498 *source
= debug_source_enums
[msg
->source
];
502 *type
= debug_type_enums
[msg
->type
];
503 if (caller
== MESSAGE_LOG_ARB
)
504 *type
= remap_type(*type
);
512 assert(msg
->message
[length
-1] == '\0');
513 (void) strncpy(buf
, msg
->message
, (size_t)length
);
516 if (msg
->message
!= (char*)out_of_memory
)
521 debug
->NumMessages
--;
523 debug
->NextMsg
%= MAX_DEBUG_LOGGED_MESSAGES
;
524 debug
->NextMsgLength
= debug
->Log
[debug
->NextMsg
].length
;
531 * Verify that source, type, and severity are valid enums.
532 * glDebugMessageInsertARB only accepts two values for 'source',
533 * and glDebugMessageControlARB will additionally accept GL_DONT_CARE
534 * in any parameter, so handle those cases specially.
536 * There is also special cases for handling values available in
537 * GL_KHR_debug that are not avaliable in GL_ARB_debug_output
540 validate_params(struct gl_context
*ctx
, unsigned caller
,
541 const char *callerstr
, GLenum source
, GLenum type
,
547 #define CONTROL_ARB 4
549 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
550 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
552 case GL_DEBUG_SOURCE_API_ARB
:
553 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
554 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
555 case GL_DEBUG_SOURCE_OTHER_ARB
:
556 if (caller
!= INSERT
|| caller
== INSERT_ARB
)
559 if (caller
== CONTROL
|| caller
== CONTROL_ARB
)
566 case GL_DEBUG_TYPE_ERROR_ARB
:
567 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
568 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
569 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
570 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
571 case GL_DEBUG_TYPE_OTHER_ARB
:
573 case GL_DEBUG_TYPE_MARKER
:
574 /* this value is only valid for GL_KHR_debug functions */
575 if (caller
== CONTROL
|| caller
== INSERT
)
578 if (caller
== CONTROL
|| caller
== CONTROL_ARB
)
585 case GL_DEBUG_SEVERITY_HIGH_ARB
:
586 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
587 case GL_DEBUG_SEVERITY_LOW_ARB
:
589 case GL_DEBUG_SEVERITY_NOTIFICATION
:
590 /* this value is only valid for GL_KHR_debug functions */
591 if (caller
== CONTROL
|| caller
== INSERT
)
594 if (caller
== CONTROL
|| caller
== CONTROL_ARB
)
602 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
603 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
604 source
, type
, severity
);
611 * Set the state of all message IDs found in the given intersection of
612 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
613 * GL_DONT_CARE (include all messages in the class).
615 * This requires both setting the state of all previously seen message
616 * IDs in the hash table, and setting the default state for all
617 * applicable combinations of source/type/severity, so that all the
618 * yet-unknown message IDs that may be used in the future will be
619 * impacted as if they were already known.
622 control_messages(struct gl_context
*ctx
,
623 enum mesa_debug_source source
,
624 enum mesa_debug_type type
,
625 enum mesa_debug_severity severity
,
628 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
629 int s
, t
, sev
, smax
, tmax
, sevmax
;
630 const GLint gstack
= debug
? debug
->GroupStackDepth
: 0;
635 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
637 smax
= MESA_DEBUG_SOURCE_COUNT
;
642 if (type
== MESA_DEBUG_TYPE_COUNT
) {
644 tmax
= MESA_DEBUG_TYPE_COUNT
;
649 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
651 sevmax
= MESA_DEBUG_SEVERITY_COUNT
;
656 for (sev
= severity
; sev
< sevmax
; sev
++) {
657 for (s
= source
; s
< smax
; s
++) {
658 for (t
= type
; t
< tmax
; t
++) {
659 struct simple_node
*node
;
660 struct gl_debug_severity
*entry
;
662 /* change the default for IDs we've never seen before. */
663 debug
->Defaults
[gstack
][sev
][s
][t
] = enabled
;
665 /* Now change the state of IDs we *have* seen... */
666 foreach(node
, &debug
->Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
667 entry
= (struct gl_debug_severity
*)node
;
668 set_message_state(ctx
, s
, t
, entry
->ID
, enabled
);
677 * Debugging-message namespaces with the source APPLICATION or THIRD_PARTY
678 * require special handling, since the IDs in them are controlled by clients,
679 * not the OpenGL implementation.
681 * 'count' is the length of the array 'ids'. If 'count' is nonzero, all
682 * the given IDs in the namespace defined by 'esource' and 'etype'
685 * If 'count' is zero, this sets the state of all IDs that match
686 * the combination of 'esource', 'etype', and 'eseverity'.
689 control_app_messages(struct gl_context
*ctx
, GLenum esource
, GLenum etype
,
690 GLenum eseverity
, GLsizei count
, const GLuint
*ids
,
694 enum mesa_debug_source source
= gl_enum_to_debug_source(esource
);
695 enum mesa_debug_type type
= gl_enum_to_debug_type(etype
);
696 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(eseverity
);
698 for (i
= 0; i
< count
; i
++)
699 set_message_state(ctx
, source
, type
, ids
[i
], enabled
);
704 control_messages(ctx
, source
, type
, severity
, enabled
);
709 * This is a generic message control function for use by both
710 * glDebugMessageControlARB and glDebugMessageControl.
713 message_control(GLenum gl_source
, GLenum gl_type
,
715 GLsizei count
, const GLuint
*ids
,
717 unsigned caller
, const char *callerstr
)
719 GET_CURRENT_CONTEXT(ctx
);
722 _mesa_error(ctx
, GL_INVALID_VALUE
,
723 "%s(count=%d : count must not be negative)", callerstr
,
728 if (!validate_params(ctx
, caller
, callerstr
, gl_source
, gl_type
,
730 return; /* GL_INVALID_ENUM */
732 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
733 || gl_source
== GL_DONT_CARE
)) {
734 _mesa_error(ctx
, GL_INVALID_OPERATION
,
735 "%s(When passing an array of ids, severity must be"
736 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
741 control_app_messages(ctx
, gl_source
, gl_type
, gl_severity
,
742 count
, ids
, enabled
);
747 * This is a generic message insert function.
748 * Validation of source, type and severity parameters should be done
749 * before calling this funtion.
752 message_insert(GLenum source
, GLenum type
, GLuint id
,
753 GLenum severity
, GLint length
, const GLchar
*buf
,
754 const char *callerstr
)
756 GET_CURRENT_CONTEXT(ctx
);
759 length
= strlen(buf
);
761 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
762 _mesa_error(ctx
, GL_INVALID_VALUE
,
763 "%s(length=%d, which is not less than "
764 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
765 MAX_DEBUG_MESSAGE_LENGTH
);
770 gl_enum_to_debug_source(source
),
771 gl_enum_to_debug_type(type
), id
,
772 gl_enum_to_debug_severity(severity
), length
, buf
);
777 * This is a generic message insert function for use by both
778 * glGetDebugMessageLogARB and glGetDebugMessageLog.
781 get_message_log(GLuint count
, GLsizei logSize
, GLenum
*sources
,
782 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
783 GLsizei
*lengths
, GLchar
*messageLog
,
784 unsigned caller
, const char *callerstr
)
786 GET_CURRENT_CONTEXT(ctx
);
793 _mesa_error(ctx
, GL_INVALID_VALUE
,
794 "%s(logSize=%d : logSize must not be negative)", callerstr
,
799 for (ret
= 0; ret
< count
; ret
++) {
800 GLsizei written
= get_msg(ctx
, sources
, types
, ids
, severities
,
801 logSize
, messageLog
, caller
);
806 messageLog
+= written
;
829 do_nothing(GLuint key
, void *data
, void *userData
)
835 * Free context state pertaining to error/debug state for the given stack
839 free_errors_data(struct gl_context
*ctx
, GLint gstack
)
841 struct gl_debug_state
*debug
= ctx
->Debug
;
842 enum mesa_debug_type t
;
843 enum mesa_debug_source s
;
844 enum mesa_debug_severity sev
;
848 /* Tear down state for filtering debug messages. */
849 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
850 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
851 _mesa_HashDeleteAll(debug
->Namespaces
[gstack
][s
][t
].IDs
,
853 _mesa_DeleteHashTable(debug
->Namespaces
[gstack
][s
][t
].IDs
);
854 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
855 struct simple_node
*node
, *tmp
;
856 struct gl_debug_severity
*entry
;
859 &debug
->Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
860 entry
= (struct gl_debug_severity
*)node
;
870 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
871 GLenum severity
, GLint length
,
874 const char *callerstr
= "glDebugMessageInsert";
876 GET_CURRENT_CONTEXT(ctx
);
878 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
879 return; /* GL_INVALID_ENUM */
881 message_insert(source
, type
, id
, severity
, length
, buf
, callerstr
);
886 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
887 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
888 GLsizei
*lengths
, GLchar
*messageLog
)
890 const char *callerstr
= "glGetDebugMessageLog";
892 return get_message_log(count
, logSize
, sources
, types
, ids
, severities
,
893 lengths
, messageLog
, MESSAGE_LOG
, callerstr
);
898 _mesa_DebugMessageControl(GLenum source
, GLenum type
, GLenum severity
,
899 GLsizei count
, const GLuint
*ids
,
902 const char *callerstr
= "glDebugMessageControl";
904 message_control(source
, type
, severity
, count
, ids
,
905 enabled
, CONTROL
, callerstr
);
910 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
912 GET_CURRENT_CONTEXT(ctx
);
913 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
915 debug
->Callback
= callback
;
916 debug
->CallbackData
= userParam
;
917 debug
->ARBCallback
= GL_FALSE
;
923 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
924 const GLchar
*message
)
926 GET_CURRENT_CONTEXT(ctx
);
927 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
928 const char *callerstr
= "glPushDebugGroup";
930 GLint prevStackDepth
;
931 GLint currStackDepth
;
932 struct gl_debug_msg
*emptySlot
;
937 if (debug
->GroupStackDepth
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
938 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
943 case GL_DEBUG_SOURCE_APPLICATION
:
944 case GL_DEBUG_SOURCE_THIRD_PARTY
:
947 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
948 "(source=0x%x)", callerstr
, source
);
952 message_insert(source
, GL_DEBUG_TYPE_PUSH_GROUP
, id
,
953 GL_DEBUG_SEVERITY_NOTIFICATION
, length
,
956 prevStackDepth
= debug
->GroupStackDepth
;
957 debug
->GroupStackDepth
++;
958 currStackDepth
= debug
->GroupStackDepth
;
960 /* pop reuses the message details from push so we store this */
962 length
= strlen(message
);
963 emptySlot
= &debug
->DebugGroupMsgs
[debug
->GroupStackDepth
];
964 store_message_details(emptySlot
, gl_enum_to_debug_source(source
),
965 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
967 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
970 /* inherit the control volume of the debug group previously residing on
971 * the top of the debug group stack
973 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
974 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
975 /* copy id settings */
976 debug
->Namespaces
[currStackDepth
][s
][t
].IDs
=
977 _mesa_HashClone(debug
->Namespaces
[prevStackDepth
][s
][t
].IDs
);
979 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
980 struct gl_debug_severity
*entry
, *prevEntry
;
981 struct simple_node
*node
;
983 /* copy default settings for unknown ids */
984 debug
->Defaults
[currStackDepth
][sev
][s
][t
] =
985 debug
->Defaults
[prevStackDepth
][sev
][s
][t
];
987 /* copy known id severity settings */
988 make_empty_list(&debug
->Namespaces
[currStackDepth
][s
][t
].Severity
[sev
]);
989 foreach(node
, &debug
->Namespaces
[prevStackDepth
][s
][t
].Severity
[sev
]) {
990 prevEntry
= (struct gl_debug_severity
*)node
;
991 entry
= malloc(sizeof *entry
);
995 entry
->ID
= prevEntry
->ID
;
996 insert_at_tail(&debug
->Namespaces
[currStackDepth
][s
][t
].Severity
[sev
], &entry
->link
);
1005 _mesa_PopDebugGroup(void)
1007 GET_CURRENT_CONTEXT(ctx
);
1008 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
1009 const char *callerstr
= "glPopDebugGroup";
1010 struct gl_debug_msg
*gdmessage
;
1011 GLint prevStackDepth
;
1016 if (debug
->GroupStackDepth
<= 0) {
1017 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
1021 prevStackDepth
= debug
->GroupStackDepth
;
1022 debug
->GroupStackDepth
--;
1024 gdmessage
= &debug
->DebugGroupMsgs
[prevStackDepth
];
1025 /* using log_msg() directly here as verification of parameters
1026 * already done in push
1028 log_msg(ctx
, gdmessage
->source
,
1029 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
1031 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1032 gdmessage
->length
, gdmessage
->message
);
1034 if (gdmessage
->message
!= (char*)out_of_memory
)
1035 free(gdmessage
->message
);
1036 gdmessage
->message
= NULL
;
1037 gdmessage
->length
= 0;
1039 /* free popped debug group data */
1040 free_errors_data(ctx
, prevStackDepth
);
1045 _mesa_DebugMessageInsertARB(GLenum source
, GLenum type
, GLuint id
,
1046 GLenum severity
, GLint length
,
1047 const GLcharARB
*buf
)
1049 const char *callerstr
= "glDebugMessageInsertARB";
1051 GET_CURRENT_CONTEXT(ctx
);
1053 if (!validate_params(ctx
, INSERT_ARB
, callerstr
, source
, type
, severity
))
1054 return; /* GL_INVALID_ENUM */
1056 message_insert(source
, type
, id
, severity
, length
, buf
, callerstr
);
1061 _mesa_GetDebugMessageLogARB(GLuint count
, GLsizei logSize
, GLenum
*sources
,
1062 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
1063 GLsizei
*lengths
, GLcharARB
*messageLog
)
1065 const char *callerstr
= "glGetDebugMessageLogARB";
1067 return get_message_log(count
, logSize
, sources
, types
, ids
, severities
,
1068 lengths
, messageLog
, MESSAGE_LOG_ARB
, callerstr
);
1073 _mesa_DebugMessageControlARB(GLenum gl_source
, GLenum gl_type
,
1075 GLsizei count
, const GLuint
*ids
,
1078 const char *callerstr
= "glDebugMessageControlARB";
1080 message_control(gl_source
, gl_type
, gl_severity
, count
, ids
,
1081 enabled
, CONTROL_ARB
, callerstr
);
1086 _mesa_DebugMessageCallbackARB(GLDEBUGPROCARB callback
, const void *userParam
)
1088 GET_CURRENT_CONTEXT(ctx
);
1089 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
1091 debug
->Callback
= callback
;
1092 debug
->CallbackData
= userParam
;
1093 debug
->ARBCallback
= GL_TRUE
;
1099 _mesa_init_errors(struct gl_context
*ctx
)
1106 * Loop through debug group stack tearing down states for
1107 * filtering debug messages.
1110 _mesa_free_errors_data(struct gl_context
*ctx
)
1115 for (i
= 0; i
<= ctx
->Debug
->GroupStackDepth
; i
++) {
1116 free_errors_data(ctx
, i
);
1122 /**********************************************************************/
1123 /** \name Diagnostics */
1127 output_if_debug(const char *prefixString
, const char *outputString
,
1130 static int debug
= -1;
1131 static FILE *fout
= NULL
;
1133 /* Init the local 'debug' var once.
1134 * Note: the _mesa_init_debug() function should have been called
1135 * by now so MESA_DEBUG_FLAGS will be initialized.
1138 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1139 * etc to the named file. Otherwise, output to stderr.
1141 const char *logFile
= _mesa_getenv("MESA_LOG_FILE");
1143 fout
= fopen(logFile
, "w");
1147 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1148 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1153 /* in release builds, be silent unless MESA_DEBUG is set */
1154 debug
= _mesa_getenv("MESA_DEBUG") != NULL
;
1158 /* Now only print the string if we're required to do so. */
1160 fprintf(fout
, "%s: %s", prefixString
, outputString
);
1162 fprintf(fout
, "\n");
1165 #if defined(_WIN32) && !defined(_WIN32_WCE)
1166 /* stderr from windows applications without console is not usually
1167 * visible, so communicate with the debugger instead */
1170 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1171 OutputDebugStringA(buf
);
1179 * When a new type of error is recorded, print a message describing
1180 * previous errors which were accumulated.
1183 flush_delayed_errors( struct gl_context
*ctx
)
1185 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1187 if (ctx
->ErrorDebugCount
) {
1188 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1189 ctx
->ErrorDebugCount
,
1190 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
1192 output_if_debug("Mesa", s
, GL_TRUE
);
1194 ctx
->ErrorDebugCount
= 0;
1200 * Report a warning (a recoverable error condition) to stderr if
1201 * either DEBUG is defined or the MESA_DEBUG env var is set.
1203 * \param ctx GL context.
1204 * \param fmtString printf()-like format string.
1207 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1209 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1211 va_start( args
, fmtString
);
1212 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1216 flush_delayed_errors( ctx
);
1218 output_if_debug("Mesa warning", str
, GL_TRUE
);
1223 * Report an internal implementation problem.
1224 * Prints the message to stderr via fprintf().
1226 * \param ctx GL context.
1227 * \param fmtString problem description string.
1230 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1233 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1234 static int numCalls
= 0;
1238 if (numCalls
< 50) {
1241 va_start( args
, fmtString
);
1242 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1244 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1245 PACKAGE_VERSION
, str
);
1246 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1252 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1254 static GLint debug
= -1;
1256 /* Check debug environment variable only once:
1259 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
1262 if (debugEnv
&& strstr(debugEnv
, "silent"))
1275 if (ctx
->ErrorValue
!= error
||
1276 ctx
->ErrorDebugFmtString
!= fmtString
) {
1277 flush_delayed_errors( ctx
);
1278 ctx
->ErrorDebugFmtString
= fmtString
;
1279 ctx
->ErrorDebugCount
= 0;
1282 ctx
->ErrorDebugCount
++;
1289 _mesa_gl_debug(struct gl_context
*ctx
,
1291 enum mesa_debug_type type
,
1292 enum mesa_debug_severity severity
,
1293 const char *fmtString
, ...)
1295 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1301 va_start(args
, fmtString
);
1302 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1305 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, type
, *id
, severity
, len
, s
);
1310 * Record an OpenGL state error. These usually occur when the user
1311 * passes invalid parameters to a GL function.
1313 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1314 * run-time via the MESA_DEBUG environment variable), report the error with
1317 * \param ctx the GL context.
1318 * \param error the error value.
1319 * \param fmtString printf() style format string, followed by optional args
1322 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1324 GLboolean do_output
, do_log
;
1325 /* Ideally this would be set up by the caller, so that we had proper IDs
1326 * per different message.
1328 static GLuint error_msg_id
= 0;
1330 debug_get_id(&error_msg_id
);
1332 do_output
= should_output(ctx
, error
, fmtString
);
1333 do_log
= should_log(ctx
,
1334 MESA_DEBUG_SOURCE_API
,
1335 MESA_DEBUG_TYPE_ERROR
,
1337 MESA_DEBUG_SEVERITY_HIGH
);
1339 if (do_output
|| do_log
) {
1340 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1344 va_start(args
, fmtString
);
1345 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1348 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1349 /* Too long error message. Whoever calls _mesa_error should use
1356 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1357 _mesa_lookup_enum_by_nr(error
), s
);
1358 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1359 /* Same as above. */
1364 /* Print the error to stderr if needed. */
1366 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1369 /* Log the error via ARB_debug_output if needed.*/
1371 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_ERROR
,
1372 error_msg_id
, MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1376 /* Set the GL context error state for glGetError. */
1377 _mesa_record_error(ctx
, error
);
1382 * Report debug information. Print error message to stderr via fprintf().
1383 * No-op if DEBUG mode not enabled.
1385 * \param ctx GL context.
1386 * \param fmtString printf()-style format string, followed by optional args.
1389 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1392 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1394 va_start(args
, fmtString
);
1395 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1397 output_if_debug("Mesa", s
, GL_FALSE
);
1405 * Report debug information from the shader compiler via GL_ARB_debug_output.
1407 * \param ctx GL context.
1408 * \param type The namespace to which this message belongs.
1409 * \param id The message ID within the given namespace.
1410 * \param msg The message to output. Need not be null-terminated.
1411 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1414 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1415 const char *msg
, int len
)
1417 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1418 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1425 /* Truncate the message if necessary. */
1426 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1427 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1429 log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);