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"
41 static mtx_t DynamicIDMutex
= _MTX_INITIALIZER_NP
;
42 static GLuint NextDynamicID
= 1;
44 struct gl_debug_severity
46 struct simple_node link
;
50 static char out_of_memory
[] = "Debugging error: out of memory";
52 static const GLenum debug_source_enums
[] = {
54 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
55 GL_DEBUG_SOURCE_SHADER_COMPILER
,
56 GL_DEBUG_SOURCE_THIRD_PARTY
,
57 GL_DEBUG_SOURCE_APPLICATION
,
58 GL_DEBUG_SOURCE_OTHER
,
61 static const GLenum debug_type_enums
[] = {
63 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
64 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
65 GL_DEBUG_TYPE_PORTABILITY
,
66 GL_DEBUG_TYPE_PERFORMANCE
,
69 GL_DEBUG_TYPE_PUSH_GROUP
,
70 GL_DEBUG_TYPE_POP_GROUP
,
73 static const GLenum debug_severity_enums
[] = {
74 GL_DEBUG_SEVERITY_LOW
,
75 GL_DEBUG_SEVERITY_MEDIUM
,
76 GL_DEBUG_SEVERITY_HIGH
,
77 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
)
119 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
122 * The GL API has this "ID" mechanism, where the intention is to allow a
123 * client to filter in/out messages based on source, type, and ID. Of course,
124 * building a giant enum list of all debug output messages that Mesa might
125 * generate is ridiculous, so instead we have our caller pass us a pointer to
126 * static storage where the ID should get stored. This ID will be shared
127 * across all contexts for that message (which seems like a desirable
128 * property, even if it's not expected by the spec), but note that it won't be
129 * the same between executions if messages aren't generated in the same order.
132 debug_get_id(GLuint
*id
)
135 mtx_lock(&DynamicIDMutex
);
137 *id
= NextDynamicID
++;
138 mtx_unlock(&DynamicIDMutex
);
144 * We store a bitfield in the hash table, with five possible values total.
146 * The ENABLED_BIT's purpose is self-explanatory.
148 * The FOUND_BIT is needed to differentiate the value of DISABLED from
149 * the value returned by HashTableLookup() when it can't find the given key.
151 * The KNOWN_SEVERITY bit is a bit complicated:
153 * A client may call Control() with an array of IDs, then call Control()
154 * on all message IDs of a certain severity, then Insert() one of the
155 * previously specified IDs, giving us a known severity level, then call
156 * Control() on all message IDs of a certain severity level again.
158 * After the first call, those IDs will have a FOUND_BIT, but will not
159 * exist in any severity-specific list, so the second call will not
160 * impact them. This is undesirable but unavoidable given the API:
161 * The only entrypoint that gives a severity for a client-defined ID
162 * is the Insert() call.
164 * For the sake of Control(), we want to maintain the invariant
165 * that an ID will either appear in none of the three severity lists,
166 * or appear once, to minimize pointless duplication and potential surprises.
168 * Because Insert() is the only place that will learn an ID's severity,
169 * it should insert an ID into the appropriate list, but only if the ID
170 * doesn't exist in it or any other list yet. Because searching all three
171 * lists at O(n) is needlessly expensive, we store KNOWN_SEVERITY.
175 ENABLED_BIT
= 1 << 1,
176 KNOWN_SEVERITY
= 1 << 2,
178 /* HashTable reserves zero as a return value meaning 'not found' */
180 DISABLED
= FOUND_BIT
,
181 ENABLED
= ENABLED_BIT
| FOUND_BIT
185 * Allocate and initialize context debug state.
187 static struct gl_debug_state
*
190 struct gl_debug_state
*debug
;
193 debug
= CALLOC_STRUCT(gl_debug_state
);
197 /* Enable all the messages with severity HIGH or MEDIUM by default. */
198 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
], GL_TRUE
,
199 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
]);
200 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
], GL_TRUE
,
201 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
]);
202 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_LOW
], GL_FALSE
,
203 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_LOW
]);
205 /* Initialize state for filtering known debug messages. */
206 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
207 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
208 debug
->Namespaces
[0][s
][t
].IDs
= _mesa_NewHashTable();
209 assert(debug
->Namespaces
[0][s
][t
].IDs
);
211 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
212 make_empty_list(&debug
->Namespaces
[0][s
][t
].Severity
[sev
]);
221 * Returns if the given message source/type/ID tuple is enabled.
224 debug_is_message_enabled(struct gl_debug_state
*debug
,
225 enum mesa_debug_source source
,
226 enum mesa_debug_type type
,
228 enum mesa_debug_severity severity
)
230 const GLint gstack
= debug
->GroupStackDepth
;
231 struct gl_debug_namespace
*nspace
=
232 &debug
->Namespaces
[gstack
][source
][type
];
235 if (!debug
->DebugOutput
)
238 /* In addition to not being able to store zero as a value, HashTable also
239 * can't use zero as a key.
242 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
244 state
= nspace
->ZeroID
;
246 /* Only do this once for each ID. This makes sure the ID exists in,
247 * at most, one list, and does not pointlessly appear multiple times.
249 if (!(state
& KNOWN_SEVERITY
)) {
250 struct gl_debug_severity
*entry
;
252 if (state
== NOT_FOUND
) {
253 if (debug
->Defaults
[gstack
][severity
][source
][type
])
259 entry
= malloc(sizeof *entry
);
263 state
|= KNOWN_SEVERITY
;
266 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
268 nspace
->ZeroID
= state
;
271 insert_at_tail(&nspace
->Severity
[severity
], &entry
->link
);
274 return (state
& ENABLED_BIT
);
279 * Return debug state for the context. The debug state will be allocated
280 * and initialized upon the first call.
282 struct gl_debug_state
*
283 _mesa_get_debug_state(struct gl_context
*ctx
)
286 ctx
->Debug
= debug_create();
288 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
298 should_log(struct gl_context
*ctx
,
299 enum mesa_debug_source source
,
300 enum mesa_debug_type type
,
302 enum mesa_debug_severity severity
)
304 struct gl_debug_state
*debug
;
307 /* no debug state set so far */
311 debug
= _mesa_get_debug_state(ctx
);
313 return debug_is_message_enabled(debug
, source
, type
, id
, severity
);
320 * Sets the state of the given message source/type/ID tuple.
323 set_message_state(struct gl_context
*ctx
,
324 enum mesa_debug_source source
,
325 enum mesa_debug_type type
,
326 GLuint id
, GLboolean enabled
)
328 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
331 GLint gstack
= debug
->GroupStackDepth
;
332 struct gl_debug_namespace
*nspace
=
333 &debug
->Namespaces
[gstack
][source
][type
];
336 /* In addition to not being able to store zero as a value, HashTable also
337 * can't use zero as a key.
340 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
342 state
= nspace
->ZeroID
;
344 if (state
== NOT_FOUND
)
345 state
= enabled
? ENABLED
: DISABLED
;
348 state
|= ENABLED_BIT
;
350 state
&= ~ENABLED_BIT
;
354 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
356 nspace
->ZeroID
= state
;
362 store_message_details(struct gl_debug_msg
*emptySlot
,
363 enum mesa_debug_source source
,
364 enum mesa_debug_type type
, GLuint id
,
365 enum mesa_debug_severity severity
, GLint len
,
368 assert(!emptySlot
->message
&& !emptySlot
->length
);
370 emptySlot
->message
= malloc(len
+1);
371 if (emptySlot
->message
) {
372 (void) strncpy(emptySlot
->message
, buf
, (size_t)len
);
373 emptySlot
->message
[len
] = '\0';
375 emptySlot
->length
= len
+1;
376 emptySlot
->source
= source
;
377 emptySlot
->type
= type
;
379 emptySlot
->severity
= severity
;
381 static GLuint oom_msg_id
= 0;
382 debug_get_id(&oom_msg_id
);
385 emptySlot
->message
= out_of_memory
;
386 emptySlot
->length
= strlen(out_of_memory
)+1;
387 emptySlot
->source
= MESA_DEBUG_SOURCE_OTHER
;
388 emptySlot
->type
= MESA_DEBUG_TYPE_ERROR
;
389 emptySlot
->id
= oom_msg_id
;
390 emptySlot
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
396 * 'buf' is not necessarily a null-terminated string. When logging, copy
397 * 'len' characters from it, store them in a new, null-terminated string,
398 * and remember the number of bytes used by that string, *including*
399 * the null terminator this time.
402 log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
403 enum mesa_debug_type type
, GLuint id
,
404 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
406 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
408 struct gl_debug_msg
*emptySlot
;
413 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
415 if (!should_log(ctx
, source
, type
, id
, severity
))
418 if (debug
->Callback
) {
419 GLenum gl_type
= debug_type_enums
[type
];
420 GLenum gl_severity
= debug_severity_enums
[severity
];
422 debug
->Callback(debug_source_enums
[source
], gl_type
, id
, gl_severity
,
423 len
, buf
, debug
->CallbackData
);
427 if (debug
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
430 nextEmpty
= (debug
->NextMsg
+ debug
->NumMessages
)
431 % MAX_DEBUG_LOGGED_MESSAGES
;
432 emptySlot
= &debug
->Log
[nextEmpty
];
434 store_message_details(emptySlot
, source
, type
, id
, severity
, len
, buf
);
436 if (debug
->NumMessages
== 0)
437 debug
->NextMsgLength
= debug
->Log
[debug
->NextMsg
].length
;
439 debug
->NumMessages
++;
444 * Pop the oldest debug message out of the log.
445 * Writes the message string, including the null terminator, into 'buf',
446 * using up to 'bufSize' bytes. If 'bufSize' is too small, or
447 * if 'buf' is NULL, nothing is written.
449 * Returns the number of bytes written on success, or when 'buf' is NULL,
450 * the number that would have been written. A return value of 0
454 get_msg(struct gl_context
*ctx
, GLenum
*source
, GLenum
*type
,
455 GLuint
*id
, GLenum
*severity
, GLsizei bufSize
, char *buf
)
457 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
458 struct gl_debug_msg
*msg
;
461 if (!debug
|| debug
->NumMessages
== 0)
464 msg
= &debug
->Log
[debug
->NextMsg
];
465 length
= msg
->length
;
467 assert(length
> 0 && length
== debug
->NextMsgLength
);
469 if (bufSize
< length
&& buf
!= NULL
)
473 *severity
= debug_severity_enums
[msg
->severity
];
477 *source
= debug_source_enums
[msg
->source
];
481 *type
= debug_type_enums
[msg
->type
];
489 assert(msg
->message
[length
-1] == '\0');
490 (void) strncpy(buf
, msg
->message
, (size_t)length
);
493 if (msg
->message
!= (char*)out_of_memory
)
498 debug
->NumMessages
--;
500 debug
->NextMsg
%= MAX_DEBUG_LOGGED_MESSAGES
;
501 debug
->NextMsgLength
= debug
->Log
[debug
->NextMsg
].length
;
508 * Verify that source, type, and severity are valid enums.
510 * The 'caller' param is used for handling values available
511 * only in glDebugMessageInsert or glDebugMessageControl
514 validate_params(struct gl_context
*ctx
, unsigned caller
,
515 const char *callerstr
, GLenum source
, GLenum type
,
521 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
522 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
524 case GL_DEBUG_SOURCE_API_ARB
:
525 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
526 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
527 case GL_DEBUG_SOURCE_OTHER_ARB
:
528 if (caller
!= INSERT
)
533 if (caller
== CONTROL
)
542 case GL_DEBUG_TYPE_ERROR_ARB
:
543 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
544 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
545 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
546 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
547 case GL_DEBUG_TYPE_OTHER_ARB
:
548 case GL_DEBUG_TYPE_MARKER
:
550 case GL_DEBUG_TYPE_PUSH_GROUP
:
551 case GL_DEBUG_TYPE_POP_GROUP
:
553 if (caller
== CONTROL
)
562 case GL_DEBUG_SEVERITY_HIGH_ARB
:
563 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
564 case GL_DEBUG_SEVERITY_LOW_ARB
:
565 case GL_DEBUG_SEVERITY_NOTIFICATION
:
568 if (caller
== CONTROL
)
578 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
579 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
580 source
, type
, severity
);
587 * Set the state of all message IDs found in the given intersection of
588 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
589 * GL_DONT_CARE (include all messages in the class).
591 * This requires both setting the state of all previously seen message
592 * IDs in the hash table, and setting the default state for all
593 * applicable combinations of source/type/severity, so that all the
594 * yet-unknown message IDs that may be used in the future will be
595 * impacted as if they were already known.
598 control_messages(struct gl_context
*ctx
,
599 enum mesa_debug_source source
,
600 enum mesa_debug_type type
,
601 enum mesa_debug_severity severity
,
604 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
605 int s
, t
, sev
, smax
, tmax
, sevmax
;
606 const GLint gstack
= debug
? debug
->GroupStackDepth
: 0;
611 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
613 smax
= MESA_DEBUG_SOURCE_COUNT
;
618 if (type
== MESA_DEBUG_TYPE_COUNT
) {
620 tmax
= MESA_DEBUG_TYPE_COUNT
;
625 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
627 sevmax
= MESA_DEBUG_SEVERITY_COUNT
;
632 for (sev
= severity
; sev
< sevmax
; sev
++) {
633 for (s
= source
; s
< smax
; s
++) {
634 for (t
= type
; t
< tmax
; t
++) {
635 struct simple_node
*node
;
636 struct gl_debug_severity
*entry
;
638 /* change the default for IDs we've never seen before. */
639 debug
->Defaults
[gstack
][sev
][s
][t
] = enabled
;
641 /* Now change the state of IDs we *have* seen... */
642 foreach(node
, &debug
->Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
643 entry
= (struct gl_debug_severity
*)node
;
644 set_message_state(ctx
, s
, t
, entry
->ID
, enabled
);
653 * Debugging-message namespaces with the source APPLICATION or THIRD_PARTY
654 * require special handling, since the IDs in them are controlled by clients,
655 * not the OpenGL implementation.
657 * 'count' is the length of the array 'ids'. If 'count' is nonzero, all
658 * the given IDs in the namespace defined by 'esource' and 'etype'
661 * If 'count' is zero, this sets the state of all IDs that match
662 * the combination of 'esource', 'etype', and 'eseverity'.
665 control_app_messages(struct gl_context
*ctx
, GLenum esource
, GLenum etype
,
666 GLenum eseverity
, GLsizei count
, const GLuint
*ids
,
670 enum mesa_debug_source source
= gl_enum_to_debug_source(esource
);
671 enum mesa_debug_type type
= gl_enum_to_debug_type(etype
);
672 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(eseverity
);
674 for (i
= 0; i
< count
; i
++)
675 set_message_state(ctx
, source
, type
, ids
[i
], enabled
);
680 control_messages(ctx
, source
, type
, severity
, enabled
);
685 * This is a generic message insert function.
686 * Validation of source, type and severity parameters should be done
687 * before calling this funtion.
690 message_insert(GLenum source
, GLenum type
, GLuint id
,
691 GLenum severity
, GLint length
, const GLchar
*buf
,
692 const char *callerstr
)
694 GET_CURRENT_CONTEXT(ctx
);
697 length
= strlen(buf
);
699 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
700 _mesa_error(ctx
, GL_INVALID_VALUE
,
701 "%s(length=%d, which is not less than "
702 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
703 MAX_DEBUG_MESSAGE_LENGTH
);
708 gl_enum_to_debug_source(source
),
709 gl_enum_to_debug_type(type
), id
,
710 gl_enum_to_debug_severity(severity
), length
, buf
);
715 do_nothing(GLuint key
, void *data
, void *userData
)
721 * Free context state pertaining to error/debug state for the given stack
725 free_errors_data(struct gl_context
*ctx
, GLint gstack
)
727 struct gl_debug_state
*debug
= ctx
->Debug
;
728 enum mesa_debug_type t
;
729 enum mesa_debug_source s
;
730 enum mesa_debug_severity sev
;
734 /* Tear down state for filtering debug messages. */
735 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
736 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
737 _mesa_HashDeleteAll(debug
->Namespaces
[gstack
][s
][t
].IDs
,
739 _mesa_DeleteHashTable(debug
->Namespaces
[gstack
][s
][t
].IDs
);
740 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
741 struct simple_node
*node
, *tmp
;
742 struct gl_debug_severity
*entry
;
745 &debug
->Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
746 entry
= (struct gl_debug_severity
*)node
;
756 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
757 GLenum severity
, GLint length
,
760 const char *callerstr
= "glDebugMessageInsert";
762 GET_CURRENT_CONTEXT(ctx
);
764 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
765 return; /* GL_INVALID_ENUM */
767 message_insert(source
, type
, id
, severity
, length
, buf
, callerstr
);
772 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
773 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
774 GLsizei
*lengths
, GLchar
*messageLog
)
776 GET_CURRENT_CONTEXT(ctx
);
783 _mesa_error(ctx
, GL_INVALID_VALUE
,
784 "glGetDebugMessageLog(logSize=%d : logSize must not be"
785 " negative)", logSize
);
789 for (ret
= 0; ret
< count
; ret
++) {
790 GLsizei written
= get_msg(ctx
, sources
, types
, ids
, severities
,
791 logSize
, messageLog
);
796 messageLog
+= written
;
819 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
820 GLenum gl_severity
, GLsizei count
,
821 const GLuint
*ids
, GLboolean enabled
)
823 const char *callerstr
= "glDebugMessageControl";
825 GET_CURRENT_CONTEXT(ctx
);
828 _mesa_error(ctx
, GL_INVALID_VALUE
,
829 "%s(count=%d : count must not be negative)", callerstr
,
834 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
836 return; /* GL_INVALID_ENUM */
838 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
839 || gl_source
== GL_DONT_CARE
)) {
840 _mesa_error(ctx
, GL_INVALID_OPERATION
,
841 "%s(When passing an array of ids, severity must be"
842 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
847 control_app_messages(ctx
, gl_source
, gl_type
, gl_severity
,
848 count
, ids
, enabled
);
853 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
855 GET_CURRENT_CONTEXT(ctx
);
856 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
858 debug
->Callback
= callback
;
859 debug
->CallbackData
= userParam
;
865 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
866 const GLchar
*message
)
868 GET_CURRENT_CONTEXT(ctx
);
869 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
870 const char *callerstr
= "glPushDebugGroup";
872 GLint prevStackDepth
;
873 GLint currStackDepth
;
874 struct gl_debug_msg
*emptySlot
;
879 if (debug
->GroupStackDepth
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
880 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
885 case GL_DEBUG_SOURCE_APPLICATION
:
886 case GL_DEBUG_SOURCE_THIRD_PARTY
:
889 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
890 "(source=0x%x)", callerstr
, source
);
894 message_insert(source
, GL_DEBUG_TYPE_PUSH_GROUP
, id
,
895 GL_DEBUG_SEVERITY_NOTIFICATION
, length
,
898 prevStackDepth
= debug
->GroupStackDepth
;
899 debug
->GroupStackDepth
++;
900 currStackDepth
= debug
->GroupStackDepth
;
902 /* pop reuses the message details from push so we store this */
904 length
= strlen(message
);
905 emptySlot
= &debug
->DebugGroupMsgs
[debug
->GroupStackDepth
];
906 store_message_details(emptySlot
, gl_enum_to_debug_source(source
),
907 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
909 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
912 /* inherit the control volume of the debug group previously residing on
913 * the top of the debug group stack
915 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
916 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
917 /* copy id settings */
918 debug
->Namespaces
[currStackDepth
][s
][t
].IDs
=
919 _mesa_HashClone(debug
->Namespaces
[prevStackDepth
][s
][t
].IDs
);
921 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
922 struct gl_debug_severity
*entry
, *prevEntry
;
923 struct simple_node
*node
;
925 /* copy default settings for unknown ids */
926 debug
->Defaults
[currStackDepth
][sev
][s
][t
] =
927 debug
->Defaults
[prevStackDepth
][sev
][s
][t
];
929 /* copy known id severity settings */
930 make_empty_list(&debug
->Namespaces
[currStackDepth
][s
][t
].Severity
[sev
]);
931 foreach(node
, &debug
->Namespaces
[prevStackDepth
][s
][t
].Severity
[sev
]) {
932 prevEntry
= (struct gl_debug_severity
*)node
;
933 entry
= malloc(sizeof *entry
);
937 entry
->ID
= prevEntry
->ID
;
938 insert_at_tail(&debug
->Namespaces
[currStackDepth
][s
][t
].Severity
[sev
], &entry
->link
);
947 _mesa_PopDebugGroup(void)
949 GET_CURRENT_CONTEXT(ctx
);
950 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
951 const char *callerstr
= "glPopDebugGroup";
952 struct gl_debug_msg
*gdmessage
;
953 GLint prevStackDepth
;
958 if (debug
->GroupStackDepth
<= 0) {
959 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
963 prevStackDepth
= debug
->GroupStackDepth
;
964 debug
->GroupStackDepth
--;
966 gdmessage
= &debug
->DebugGroupMsgs
[prevStackDepth
];
967 /* using log_msg() directly here as verification of parameters
968 * already done in push
970 log_msg(ctx
, gdmessage
->source
,
971 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
973 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
974 gdmessage
->length
, gdmessage
->message
);
976 if (gdmessage
->message
!= (char*)out_of_memory
)
977 free(gdmessage
->message
);
978 gdmessage
->message
= NULL
;
979 gdmessage
->length
= 0;
981 /* free popped debug group data */
982 free_errors_data(ctx
, prevStackDepth
);
987 _mesa_init_errors(struct gl_context
*ctx
)
994 * Loop through debug group stack tearing down states for
995 * filtering debug messages. Then free debug output state.
998 _mesa_free_errors_data(struct gl_context
*ctx
)
1003 for (i
= 0; i
<= ctx
->Debug
->GroupStackDepth
; i
++) {
1004 free_errors_data(ctx
, i
);
1007 /* set to NULL just in case it is used before context is completely gone. */
1013 /**********************************************************************/
1014 /** \name Diagnostics */
1018 output_if_debug(const char *prefixString
, const char *outputString
,
1021 static int debug
= -1;
1022 static FILE *fout
= NULL
;
1024 /* Init the local 'debug' var once.
1025 * Note: the _mesa_init_debug() function should have been called
1026 * by now so MESA_DEBUG_FLAGS will be initialized.
1029 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1030 * etc to the named file. Otherwise, output to stderr.
1032 const char *logFile
= _mesa_getenv("MESA_LOG_FILE");
1034 fout
= fopen(logFile
, "w");
1038 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1039 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1044 /* in release builds, be silent unless MESA_DEBUG is set */
1045 debug
= _mesa_getenv("MESA_DEBUG") != NULL
;
1049 /* Now only print the string if we're required to do so. */
1051 fprintf(fout
, "%s: %s", prefixString
, outputString
);
1053 fprintf(fout
, "\n");
1056 #if defined(_WIN32) && !defined(_WIN32_WCE)
1057 /* stderr from windows applications without console is not usually
1058 * visible, so communicate with the debugger instead */
1061 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1062 OutputDebugStringA(buf
);
1070 * When a new type of error is recorded, print a message describing
1071 * previous errors which were accumulated.
1074 flush_delayed_errors( struct gl_context
*ctx
)
1076 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1078 if (ctx
->ErrorDebugCount
) {
1079 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1080 ctx
->ErrorDebugCount
,
1081 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
1083 output_if_debug("Mesa", s
, GL_TRUE
);
1085 ctx
->ErrorDebugCount
= 0;
1091 * Report a warning (a recoverable error condition) to stderr if
1092 * either DEBUG is defined or the MESA_DEBUG env var is set.
1094 * \param ctx GL context.
1095 * \param fmtString printf()-like format string.
1098 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1100 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1102 va_start( args
, fmtString
);
1103 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1107 flush_delayed_errors( ctx
);
1109 output_if_debug("Mesa warning", str
, GL_TRUE
);
1114 * Report an internal implementation problem.
1115 * Prints the message to stderr via fprintf().
1117 * \param ctx GL context.
1118 * \param fmtString problem description string.
1121 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1124 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1125 static int numCalls
= 0;
1129 if (numCalls
< 50) {
1132 va_start( args
, fmtString
);
1133 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1135 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1136 PACKAGE_VERSION
, str
);
1137 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1143 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1145 static GLint debug
= -1;
1147 /* Check debug environment variable only once:
1150 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
1153 if (debugEnv
&& strstr(debugEnv
, "silent"))
1166 if (ctx
->ErrorValue
!= error
||
1167 ctx
->ErrorDebugFmtString
!= fmtString
) {
1168 flush_delayed_errors( ctx
);
1169 ctx
->ErrorDebugFmtString
= fmtString
;
1170 ctx
->ErrorDebugCount
= 0;
1173 ctx
->ErrorDebugCount
++;
1180 _mesa_gl_debug(struct gl_context
*ctx
,
1182 enum mesa_debug_type type
,
1183 enum mesa_debug_severity severity
,
1184 const char *fmtString
, ...)
1186 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1192 va_start(args
, fmtString
);
1193 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1196 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, type
, *id
, severity
, len
, s
);
1201 * Record an OpenGL state error. These usually occur when the user
1202 * passes invalid parameters to a GL function.
1204 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1205 * run-time via the MESA_DEBUG environment variable), report the error with
1208 * \param ctx the GL context.
1209 * \param error the error value.
1210 * \param fmtString printf() style format string, followed by optional args
1213 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1215 GLboolean do_output
, do_log
;
1216 /* Ideally this would be set up by the caller, so that we had proper IDs
1217 * per different message.
1219 static GLuint error_msg_id
= 0;
1221 debug_get_id(&error_msg_id
);
1223 do_output
= should_output(ctx
, error
, fmtString
);
1224 do_log
= should_log(ctx
,
1225 MESA_DEBUG_SOURCE_API
,
1226 MESA_DEBUG_TYPE_ERROR
,
1228 MESA_DEBUG_SEVERITY_HIGH
);
1230 if (do_output
|| do_log
) {
1231 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1235 va_start(args
, fmtString
);
1236 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1239 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1240 /* Too long error message. Whoever calls _mesa_error should use
1247 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1248 _mesa_lookup_enum_by_nr(error
), s
);
1249 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1250 /* Same as above. */
1255 /* Print the error to stderr if needed. */
1257 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1260 /* Log the error via ARB_debug_output if needed.*/
1262 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_ERROR
,
1263 error_msg_id
, MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1267 /* Set the GL context error state for glGetError. */
1268 _mesa_record_error(ctx
, error
);
1273 * Report debug information. Print error message to stderr via fprintf().
1274 * No-op if DEBUG mode not enabled.
1276 * \param ctx GL context.
1277 * \param fmtString printf()-style format string, followed by optional args.
1280 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1283 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1285 va_start(args
, fmtString
);
1286 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1288 output_if_debug("Mesa", s
, GL_FALSE
);
1296 * Report debug information from the shader compiler via GL_ARB_debug_output.
1298 * \param ctx GL context.
1299 * \param type The namespace to which this message belongs.
1300 * \param id The message ID within the given namespace.
1301 * \param msg The message to output. Need not be null-terminated.
1302 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1305 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1306 const char *msg
, int len
)
1308 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1309 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1316 /* Truncate the message if necessary. */
1317 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1318 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1320 log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);