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 * Sets the state of the given message source/type/ID tuple.
224 debug_set_message_enable(struct gl_debug_state
*debug
,
225 enum mesa_debug_source source
,
226 enum mesa_debug_type type
,
227 GLuint id
, GLboolean enabled
)
229 GLint gstack
= debug
->GroupStackDepth
;
230 struct gl_debug_namespace
*nspace
=
231 &debug
->Namespaces
[gstack
][source
][type
];
234 /* In addition to not being able to store zero as a value, HashTable also
235 * can't use zero as a key.
238 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
240 state
= nspace
->ZeroID
;
242 if (state
== NOT_FOUND
)
243 state
= enabled
? ENABLED
: DISABLED
;
246 state
|= ENABLED_BIT
;
248 state
&= ~ENABLED_BIT
;
252 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
254 nspace
->ZeroID
= state
;
258 * Returns if the given message source/type/ID tuple is enabled.
261 debug_is_message_enabled(struct gl_debug_state
*debug
,
262 enum mesa_debug_source source
,
263 enum mesa_debug_type type
,
265 enum mesa_debug_severity severity
)
267 const GLint gstack
= debug
->GroupStackDepth
;
268 struct gl_debug_namespace
*nspace
=
269 &debug
->Namespaces
[gstack
][source
][type
];
272 if (!debug
->DebugOutput
)
275 /* In addition to not being able to store zero as a value, HashTable also
276 * can't use zero as a key.
279 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
281 state
= nspace
->ZeroID
;
283 /* Only do this once for each ID. This makes sure the ID exists in,
284 * at most, one list, and does not pointlessly appear multiple times.
286 if (!(state
& KNOWN_SEVERITY
)) {
287 struct gl_debug_severity
*entry
;
289 if (state
== NOT_FOUND
) {
290 if (debug
->Defaults
[gstack
][severity
][source
][type
])
296 entry
= malloc(sizeof *entry
);
300 state
|= KNOWN_SEVERITY
;
303 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
305 nspace
->ZeroID
= state
;
308 insert_at_tail(&nspace
->Severity
[severity
], &entry
->link
);
311 return (state
& ENABLED_BIT
);
316 * Return debug state for the context. The debug state will be allocated
317 * and initialized upon the first call.
319 struct gl_debug_state
*
320 _mesa_get_debug_state(struct gl_context
*ctx
)
323 ctx
->Debug
= debug_create();
325 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
335 should_log(struct gl_context
*ctx
,
336 enum mesa_debug_source source
,
337 enum mesa_debug_type type
,
339 enum mesa_debug_severity severity
)
341 struct gl_debug_state
*debug
;
344 /* no debug state set so far */
348 debug
= _mesa_get_debug_state(ctx
);
350 return debug_is_message_enabled(debug
, source
, type
, id
, severity
);
357 set_message_state(struct gl_context
*ctx
,
358 enum mesa_debug_source source
,
359 enum mesa_debug_type type
,
360 GLuint id
, GLboolean enabled
)
362 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
365 debug_set_message_enable(debug
, source
, type
, id
, enabled
);
370 store_message_details(struct gl_debug_msg
*emptySlot
,
371 enum mesa_debug_source source
,
372 enum mesa_debug_type type
, GLuint id
,
373 enum mesa_debug_severity severity
, GLint len
,
376 assert(!emptySlot
->message
&& !emptySlot
->length
);
378 emptySlot
->message
= malloc(len
+1);
379 if (emptySlot
->message
) {
380 (void) strncpy(emptySlot
->message
, buf
, (size_t)len
);
381 emptySlot
->message
[len
] = '\0';
383 emptySlot
->length
= len
+1;
384 emptySlot
->source
= source
;
385 emptySlot
->type
= type
;
387 emptySlot
->severity
= severity
;
389 static GLuint oom_msg_id
= 0;
390 debug_get_id(&oom_msg_id
);
393 emptySlot
->message
= out_of_memory
;
394 emptySlot
->length
= strlen(out_of_memory
)+1;
395 emptySlot
->source
= MESA_DEBUG_SOURCE_OTHER
;
396 emptySlot
->type
= MESA_DEBUG_TYPE_ERROR
;
397 emptySlot
->id
= oom_msg_id
;
398 emptySlot
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
404 * 'buf' is not necessarily a null-terminated string. When logging, copy
405 * 'len' characters from it, store them in a new, null-terminated string,
406 * and remember the number of bytes used by that string, *including*
407 * the null terminator this time.
410 log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
411 enum mesa_debug_type type
, GLuint id
,
412 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
414 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
416 struct gl_debug_msg
*emptySlot
;
421 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
423 if (!should_log(ctx
, source
, type
, id
, severity
))
426 if (debug
->Callback
) {
427 GLenum gl_type
= debug_type_enums
[type
];
428 GLenum gl_severity
= debug_severity_enums
[severity
];
430 debug
->Callback(debug_source_enums
[source
], gl_type
, id
, gl_severity
,
431 len
, buf
, debug
->CallbackData
);
435 if (debug
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
438 nextEmpty
= (debug
->NextMsg
+ debug
->NumMessages
)
439 % MAX_DEBUG_LOGGED_MESSAGES
;
440 emptySlot
= &debug
->Log
[nextEmpty
];
442 store_message_details(emptySlot
, source
, type
, id
, severity
, len
, buf
);
444 if (debug
->NumMessages
== 0)
445 debug
->NextMsgLength
= debug
->Log
[debug
->NextMsg
].length
;
447 debug
->NumMessages
++;
452 * Pop the oldest debug message out of the log.
453 * Writes the message string, including the null terminator, into 'buf',
454 * using up to 'bufSize' bytes. If 'bufSize' is too small, or
455 * if 'buf' is NULL, nothing is written.
457 * Returns the number of bytes written on success, or when 'buf' is NULL,
458 * the number that would have been written. A return value of 0
462 get_msg(struct gl_context
*ctx
, GLenum
*source
, GLenum
*type
,
463 GLuint
*id
, GLenum
*severity
, GLsizei bufSize
, char *buf
)
465 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
466 struct gl_debug_msg
*msg
;
469 if (!debug
|| debug
->NumMessages
== 0)
472 msg
= &debug
->Log
[debug
->NextMsg
];
473 length
= msg
->length
;
475 assert(length
> 0 && length
== debug
->NextMsgLength
);
477 if (bufSize
< length
&& buf
!= NULL
)
481 *severity
= debug_severity_enums
[msg
->severity
];
485 *source
= debug_source_enums
[msg
->source
];
489 *type
= debug_type_enums
[msg
->type
];
497 assert(msg
->message
[length
-1] == '\0');
498 (void) strncpy(buf
, msg
->message
, (size_t)length
);
501 if (msg
->message
!= (char*)out_of_memory
)
506 debug
->NumMessages
--;
508 debug
->NextMsg
%= MAX_DEBUG_LOGGED_MESSAGES
;
509 debug
->NextMsgLength
= debug
->Log
[debug
->NextMsg
].length
;
516 * Verify that source, type, and severity are valid enums.
518 * The 'caller' param is used for handling values available
519 * only in glDebugMessageInsert or glDebugMessageControl
522 validate_params(struct gl_context
*ctx
, unsigned caller
,
523 const char *callerstr
, GLenum source
, GLenum type
,
529 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
530 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
532 case GL_DEBUG_SOURCE_API_ARB
:
533 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
534 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
535 case GL_DEBUG_SOURCE_OTHER_ARB
:
536 if (caller
!= INSERT
)
541 if (caller
== CONTROL
)
550 case GL_DEBUG_TYPE_ERROR_ARB
:
551 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
552 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
553 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
554 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
555 case GL_DEBUG_TYPE_OTHER_ARB
:
556 case GL_DEBUG_TYPE_MARKER
:
558 case GL_DEBUG_TYPE_PUSH_GROUP
:
559 case GL_DEBUG_TYPE_POP_GROUP
:
561 if (caller
== CONTROL
)
570 case GL_DEBUG_SEVERITY_HIGH_ARB
:
571 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
572 case GL_DEBUG_SEVERITY_LOW_ARB
:
573 case GL_DEBUG_SEVERITY_NOTIFICATION
:
576 if (caller
== CONTROL
)
586 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
587 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
588 source
, type
, severity
);
595 * Set the state of all message IDs found in the given intersection of
596 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
597 * GL_DONT_CARE (include all messages in the class).
599 * This requires both setting the state of all previously seen message
600 * IDs in the hash table, and setting the default state for all
601 * applicable combinations of source/type/severity, so that all the
602 * yet-unknown message IDs that may be used in the future will be
603 * impacted as if they were already known.
606 control_messages(struct gl_context
*ctx
,
607 enum mesa_debug_source source
,
608 enum mesa_debug_type type
,
609 enum mesa_debug_severity severity
,
612 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
613 int s
, t
, sev
, smax
, tmax
, sevmax
;
614 const GLint gstack
= debug
? debug
->GroupStackDepth
: 0;
619 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
621 smax
= MESA_DEBUG_SOURCE_COUNT
;
626 if (type
== MESA_DEBUG_TYPE_COUNT
) {
628 tmax
= MESA_DEBUG_TYPE_COUNT
;
633 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
635 sevmax
= MESA_DEBUG_SEVERITY_COUNT
;
640 for (sev
= severity
; sev
< sevmax
; sev
++) {
641 for (s
= source
; s
< smax
; s
++) {
642 for (t
= type
; t
< tmax
; t
++) {
643 struct simple_node
*node
;
644 struct gl_debug_severity
*entry
;
646 /* change the default for IDs we've never seen before. */
647 debug
->Defaults
[gstack
][sev
][s
][t
] = enabled
;
649 /* Now change the state of IDs we *have* seen... */
650 foreach(node
, &debug
->Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
651 entry
= (struct gl_debug_severity
*)node
;
652 set_message_state(ctx
, s
, t
, entry
->ID
, enabled
);
661 * Debugging-message namespaces with the source APPLICATION or THIRD_PARTY
662 * require special handling, since the IDs in them are controlled by clients,
663 * not the OpenGL implementation.
665 * 'count' is the length of the array 'ids'. If 'count' is nonzero, all
666 * the given IDs in the namespace defined by 'esource' and 'etype'
669 * If 'count' is zero, this sets the state of all IDs that match
670 * the combination of 'esource', 'etype', and 'eseverity'.
673 control_app_messages(struct gl_context
*ctx
, GLenum esource
, GLenum etype
,
674 GLenum eseverity
, GLsizei count
, const GLuint
*ids
,
678 enum mesa_debug_source source
= gl_enum_to_debug_source(esource
);
679 enum mesa_debug_type type
= gl_enum_to_debug_type(etype
);
680 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(eseverity
);
682 for (i
= 0; i
< count
; i
++)
683 set_message_state(ctx
, source
, type
, ids
[i
], enabled
);
688 control_messages(ctx
, source
, type
, severity
, enabled
);
693 * This is a generic message insert function.
694 * Validation of source, type and severity parameters should be done
695 * before calling this funtion.
698 message_insert(GLenum source
, GLenum type
, GLuint id
,
699 GLenum severity
, GLint length
, const GLchar
*buf
,
700 const char *callerstr
)
702 GET_CURRENT_CONTEXT(ctx
);
705 length
= strlen(buf
);
707 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
708 _mesa_error(ctx
, GL_INVALID_VALUE
,
709 "%s(length=%d, which is not less than "
710 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
711 MAX_DEBUG_MESSAGE_LENGTH
);
716 gl_enum_to_debug_source(source
),
717 gl_enum_to_debug_type(type
), id
,
718 gl_enum_to_debug_severity(severity
), length
, buf
);
723 do_nothing(GLuint key
, void *data
, void *userData
)
729 * Free context state pertaining to error/debug state for the given stack
733 free_errors_data(struct gl_context
*ctx
, GLint gstack
)
735 struct gl_debug_state
*debug
= ctx
->Debug
;
736 enum mesa_debug_type t
;
737 enum mesa_debug_source s
;
738 enum mesa_debug_severity sev
;
742 /* Tear down state for filtering debug messages. */
743 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
744 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
745 _mesa_HashDeleteAll(debug
->Namespaces
[gstack
][s
][t
].IDs
,
747 _mesa_DeleteHashTable(debug
->Namespaces
[gstack
][s
][t
].IDs
);
748 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
749 struct simple_node
*node
, *tmp
;
750 struct gl_debug_severity
*entry
;
753 &debug
->Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
754 entry
= (struct gl_debug_severity
*)node
;
764 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
765 GLenum severity
, GLint length
,
768 const char *callerstr
= "glDebugMessageInsert";
770 GET_CURRENT_CONTEXT(ctx
);
772 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
773 return; /* GL_INVALID_ENUM */
775 message_insert(source
, type
, id
, severity
, length
, buf
, callerstr
);
780 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
781 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
782 GLsizei
*lengths
, GLchar
*messageLog
)
784 GET_CURRENT_CONTEXT(ctx
);
791 _mesa_error(ctx
, GL_INVALID_VALUE
,
792 "glGetDebugMessageLog(logSize=%d : logSize must not be"
793 " negative)", logSize
);
797 for (ret
= 0; ret
< count
; ret
++) {
798 GLsizei written
= get_msg(ctx
, sources
, types
, ids
, severities
,
799 logSize
, messageLog
);
804 messageLog
+= written
;
827 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
828 GLenum gl_severity
, GLsizei count
,
829 const GLuint
*ids
, GLboolean enabled
)
831 const char *callerstr
= "glDebugMessageControl";
833 GET_CURRENT_CONTEXT(ctx
);
836 _mesa_error(ctx
, GL_INVALID_VALUE
,
837 "%s(count=%d : count must not be negative)", callerstr
,
842 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
844 return; /* GL_INVALID_ENUM */
846 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
847 || gl_source
== GL_DONT_CARE
)) {
848 _mesa_error(ctx
, GL_INVALID_OPERATION
,
849 "%s(When passing an array of ids, severity must be"
850 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
855 control_app_messages(ctx
, gl_source
, gl_type
, gl_severity
,
856 count
, ids
, enabled
);
861 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
863 GET_CURRENT_CONTEXT(ctx
);
864 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
866 debug
->Callback
= callback
;
867 debug
->CallbackData
= userParam
;
873 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
874 const GLchar
*message
)
876 GET_CURRENT_CONTEXT(ctx
);
877 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
878 const char *callerstr
= "glPushDebugGroup";
880 GLint prevStackDepth
;
881 GLint currStackDepth
;
882 struct gl_debug_msg
*emptySlot
;
887 if (debug
->GroupStackDepth
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
888 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
893 case GL_DEBUG_SOURCE_APPLICATION
:
894 case GL_DEBUG_SOURCE_THIRD_PARTY
:
897 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
898 "(source=0x%x)", callerstr
, source
);
902 message_insert(source
, GL_DEBUG_TYPE_PUSH_GROUP
, id
,
903 GL_DEBUG_SEVERITY_NOTIFICATION
, length
,
906 prevStackDepth
= debug
->GroupStackDepth
;
907 debug
->GroupStackDepth
++;
908 currStackDepth
= debug
->GroupStackDepth
;
910 /* pop reuses the message details from push so we store this */
912 length
= strlen(message
);
913 emptySlot
= &debug
->DebugGroupMsgs
[debug
->GroupStackDepth
];
914 store_message_details(emptySlot
, gl_enum_to_debug_source(source
),
915 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
917 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
920 /* inherit the control volume of the debug group previously residing on
921 * the top of the debug group stack
923 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
924 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
925 /* copy id settings */
926 debug
->Namespaces
[currStackDepth
][s
][t
].IDs
=
927 _mesa_HashClone(debug
->Namespaces
[prevStackDepth
][s
][t
].IDs
);
929 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
930 struct gl_debug_severity
*entry
, *prevEntry
;
931 struct simple_node
*node
;
933 /* copy default settings for unknown ids */
934 debug
->Defaults
[currStackDepth
][sev
][s
][t
] =
935 debug
->Defaults
[prevStackDepth
][sev
][s
][t
];
937 /* copy known id severity settings */
938 make_empty_list(&debug
->Namespaces
[currStackDepth
][s
][t
].Severity
[sev
]);
939 foreach(node
, &debug
->Namespaces
[prevStackDepth
][s
][t
].Severity
[sev
]) {
940 prevEntry
= (struct gl_debug_severity
*)node
;
941 entry
= malloc(sizeof *entry
);
945 entry
->ID
= prevEntry
->ID
;
946 insert_at_tail(&debug
->Namespaces
[currStackDepth
][s
][t
].Severity
[sev
], &entry
->link
);
955 _mesa_PopDebugGroup(void)
957 GET_CURRENT_CONTEXT(ctx
);
958 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
959 const char *callerstr
= "glPopDebugGroup";
960 struct gl_debug_msg
*gdmessage
;
961 GLint prevStackDepth
;
966 if (debug
->GroupStackDepth
<= 0) {
967 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
971 prevStackDepth
= debug
->GroupStackDepth
;
972 debug
->GroupStackDepth
--;
974 gdmessage
= &debug
->DebugGroupMsgs
[prevStackDepth
];
975 /* using log_msg() directly here as verification of parameters
976 * already done in push
978 log_msg(ctx
, gdmessage
->source
,
979 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
981 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
982 gdmessage
->length
, gdmessage
->message
);
984 if (gdmessage
->message
!= (char*)out_of_memory
)
985 free(gdmessage
->message
);
986 gdmessage
->message
= NULL
;
987 gdmessage
->length
= 0;
989 /* free popped debug group data */
990 free_errors_data(ctx
, prevStackDepth
);
995 _mesa_init_errors(struct gl_context
*ctx
)
1002 * Loop through debug group stack tearing down states for
1003 * filtering debug messages. Then free debug output state.
1006 _mesa_free_errors_data(struct gl_context
*ctx
)
1011 for (i
= 0; i
<= ctx
->Debug
->GroupStackDepth
; i
++) {
1012 free_errors_data(ctx
, i
);
1015 /* set to NULL just in case it is used before context is completely gone. */
1021 /**********************************************************************/
1022 /** \name Diagnostics */
1026 output_if_debug(const char *prefixString
, const char *outputString
,
1029 static int debug
= -1;
1030 static FILE *fout
= NULL
;
1032 /* Init the local 'debug' var once.
1033 * Note: the _mesa_init_debug() function should have been called
1034 * by now so MESA_DEBUG_FLAGS will be initialized.
1037 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1038 * etc to the named file. Otherwise, output to stderr.
1040 const char *logFile
= _mesa_getenv("MESA_LOG_FILE");
1042 fout
= fopen(logFile
, "w");
1046 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1047 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1052 /* in release builds, be silent unless MESA_DEBUG is set */
1053 debug
= _mesa_getenv("MESA_DEBUG") != NULL
;
1057 /* Now only print the string if we're required to do so. */
1059 fprintf(fout
, "%s: %s", prefixString
, outputString
);
1061 fprintf(fout
, "\n");
1064 #if defined(_WIN32) && !defined(_WIN32_WCE)
1065 /* stderr from windows applications without console is not usually
1066 * visible, so communicate with the debugger instead */
1069 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1070 OutputDebugStringA(buf
);
1078 * When a new type of error is recorded, print a message describing
1079 * previous errors which were accumulated.
1082 flush_delayed_errors( struct gl_context
*ctx
)
1084 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1086 if (ctx
->ErrorDebugCount
) {
1087 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1088 ctx
->ErrorDebugCount
,
1089 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
1091 output_if_debug("Mesa", s
, GL_TRUE
);
1093 ctx
->ErrorDebugCount
= 0;
1099 * Report a warning (a recoverable error condition) to stderr if
1100 * either DEBUG is defined or the MESA_DEBUG env var is set.
1102 * \param ctx GL context.
1103 * \param fmtString printf()-like format string.
1106 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1108 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1110 va_start( args
, fmtString
);
1111 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1115 flush_delayed_errors( ctx
);
1117 output_if_debug("Mesa warning", str
, GL_TRUE
);
1122 * Report an internal implementation problem.
1123 * Prints the message to stderr via fprintf().
1125 * \param ctx GL context.
1126 * \param fmtString problem description string.
1129 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1132 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1133 static int numCalls
= 0;
1137 if (numCalls
< 50) {
1140 va_start( args
, fmtString
);
1141 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1143 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1144 PACKAGE_VERSION
, str
);
1145 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1151 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1153 static GLint debug
= -1;
1155 /* Check debug environment variable only once:
1158 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
1161 if (debugEnv
&& strstr(debugEnv
, "silent"))
1174 if (ctx
->ErrorValue
!= error
||
1175 ctx
->ErrorDebugFmtString
!= fmtString
) {
1176 flush_delayed_errors( ctx
);
1177 ctx
->ErrorDebugFmtString
= fmtString
;
1178 ctx
->ErrorDebugCount
= 0;
1181 ctx
->ErrorDebugCount
++;
1188 _mesa_gl_debug(struct gl_context
*ctx
,
1190 enum mesa_debug_type type
,
1191 enum mesa_debug_severity severity
,
1192 const char *fmtString
, ...)
1194 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1200 va_start(args
, fmtString
);
1201 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1204 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, type
, *id
, severity
, len
, s
);
1209 * Record an OpenGL state error. These usually occur when the user
1210 * passes invalid parameters to a GL function.
1212 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1213 * run-time via the MESA_DEBUG environment variable), report the error with
1216 * \param ctx the GL context.
1217 * \param error the error value.
1218 * \param fmtString printf() style format string, followed by optional args
1221 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1223 GLboolean do_output
, do_log
;
1224 /* Ideally this would be set up by the caller, so that we had proper IDs
1225 * per different message.
1227 static GLuint error_msg_id
= 0;
1229 debug_get_id(&error_msg_id
);
1231 do_output
= should_output(ctx
, error
, fmtString
);
1232 do_log
= should_log(ctx
,
1233 MESA_DEBUG_SOURCE_API
,
1234 MESA_DEBUG_TYPE_ERROR
,
1236 MESA_DEBUG_SEVERITY_HIGH
);
1238 if (do_output
|| do_log
) {
1239 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1243 va_start(args
, fmtString
);
1244 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1247 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1248 /* Too long error message. Whoever calls _mesa_error should use
1255 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1256 _mesa_lookup_enum_by_nr(error
), s
);
1257 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1258 /* Same as above. */
1263 /* Print the error to stderr if needed. */
1265 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1268 /* Log the error via ARB_debug_output if needed.*/
1270 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_ERROR
,
1271 error_msg_id
, MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1275 /* Set the GL context error state for glGetError. */
1276 _mesa_record_error(ctx
, error
);
1281 * Report debug information. Print error message to stderr via fprintf().
1282 * No-op if DEBUG mode not enabled.
1284 * \param ctx GL context.
1285 * \param fmtString printf()-style format string, followed by optional args.
1288 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1291 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1293 va_start(args
, fmtString
);
1294 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1296 output_if_debug("Mesa", s
, GL_FALSE
);
1304 * Report debug information from the shader compiler via GL_ARB_debug_output.
1306 * \param ctx GL context.
1307 * \param type The namespace to which this message belongs.
1308 * \param id The message ID within the given namespace.
1309 * \param msg The message to output. Need not be null-terminated.
1310 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1313 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1314 const char *msg
, int len
)
1316 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1317 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1324 /* Truncate the message if necessary. */
1325 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1326 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1328 log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);