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
186 * Return debug state for the context. The debug state will be allocated
187 * and initialized upon the first call.
189 struct gl_debug_state
*
190 _mesa_get_debug_state(struct gl_context
*ctx
)
193 ctx
->Debug
= CALLOC_STRUCT(gl_debug_state
);
195 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
198 struct gl_debug_state
*debug
= ctx
->Debug
;
201 /* Enable all the messages with severity HIGH or MEDIUM by default. */
202 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
], GL_TRUE
,
203 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
]);
204 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
], GL_TRUE
,
205 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
]);
206 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_LOW
], GL_FALSE
,
207 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_LOW
]);
209 /* Initialize state for filtering known debug messages. */
210 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
211 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
212 debug
->Namespaces
[0][s
][t
].IDs
= _mesa_NewHashTable();
213 assert(debug
->Namespaces
[0][s
][t
].IDs
);
215 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
216 make_empty_list(&debug
->Namespaces
[0][s
][t
].Severity
[sev
]);
229 * Returns the state of the given message source/type/ID tuple.
232 should_log(struct gl_context
*ctx
,
233 enum mesa_debug_source source
,
234 enum mesa_debug_type type
,
236 enum mesa_debug_severity severity
)
238 struct gl_debug_state
*debug
;
242 /* no debug state set so far */
246 debug
= _mesa_get_debug_state(ctx
);
248 const GLint gstack
= debug
->GroupStackDepth
;
249 struct gl_debug_namespace
*nspace
=
250 &debug
->Namespaces
[gstack
][source
][type
];
252 if (!debug
->DebugOutput
)
255 /* In addition to not being able to store zero as a value, HashTable also
256 * can't use zero as a key.
259 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
261 state
= nspace
->ZeroID
;
263 /* Only do this once for each ID. This makes sure the ID exists in,
264 * at most, one list, and does not pointlessly appear multiple times.
266 if (!(state
& KNOWN_SEVERITY
)) {
267 struct gl_debug_severity
*entry
;
269 if (state
== NOT_FOUND
) {
270 if (debug
->Defaults
[gstack
][severity
][source
][type
])
276 entry
= malloc(sizeof *entry
);
280 state
|= KNOWN_SEVERITY
;
283 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
285 nspace
->ZeroID
= state
;
288 insert_at_tail(&nspace
->Severity
[severity
], &entry
->link
);
292 return !!(state
& ENABLED_BIT
);
297 * Sets the state of the given message source/type/ID tuple.
300 set_message_state(struct gl_context
*ctx
,
301 enum mesa_debug_source source
,
302 enum mesa_debug_type type
,
303 GLuint id
, GLboolean enabled
)
305 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
308 GLint gstack
= debug
->GroupStackDepth
;
309 struct gl_debug_namespace
*nspace
=
310 &debug
->Namespaces
[gstack
][source
][type
];
313 /* In addition to not being able to store zero as a value, HashTable also
314 * can't use zero as a key.
317 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
319 state
= nspace
->ZeroID
;
321 if (state
== NOT_FOUND
)
322 state
= enabled
? ENABLED
: DISABLED
;
325 state
|= ENABLED_BIT
;
327 state
&= ~ENABLED_BIT
;
331 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
333 nspace
->ZeroID
= state
;
339 store_message_details(struct gl_debug_msg
*emptySlot
,
340 enum mesa_debug_source source
,
341 enum mesa_debug_type type
, GLuint id
,
342 enum mesa_debug_severity severity
, GLint len
,
345 assert(!emptySlot
->message
&& !emptySlot
->length
);
347 emptySlot
->message
= malloc(len
+1);
348 if (emptySlot
->message
) {
349 (void) strncpy(emptySlot
->message
, buf
, (size_t)len
);
350 emptySlot
->message
[len
] = '\0';
352 emptySlot
->length
= len
+1;
353 emptySlot
->source
= source
;
354 emptySlot
->type
= type
;
356 emptySlot
->severity
= severity
;
358 static GLuint oom_msg_id
= 0;
359 debug_get_id(&oom_msg_id
);
362 emptySlot
->message
= out_of_memory
;
363 emptySlot
->length
= strlen(out_of_memory
)+1;
364 emptySlot
->source
= MESA_DEBUG_SOURCE_OTHER
;
365 emptySlot
->type
= MESA_DEBUG_TYPE_ERROR
;
366 emptySlot
->id
= oom_msg_id
;
367 emptySlot
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
373 * 'buf' is not necessarily a null-terminated string. When logging, copy
374 * 'len' characters from it, store them in a new, null-terminated string,
375 * and remember the number of bytes used by that string, *including*
376 * the null terminator this time.
379 log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
380 enum mesa_debug_type type
, GLuint id
,
381 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
383 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
385 struct gl_debug_msg
*emptySlot
;
390 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
392 if (!should_log(ctx
, source
, type
, id
, severity
))
395 if (debug
->Callback
) {
396 GLenum gl_type
= debug_type_enums
[type
];
397 GLenum gl_severity
= debug_severity_enums
[severity
];
399 debug
->Callback(debug_source_enums
[source
], gl_type
, id
, gl_severity
,
400 len
, buf
, debug
->CallbackData
);
404 if (debug
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
407 nextEmpty
= (debug
->NextMsg
+ debug
->NumMessages
)
408 % MAX_DEBUG_LOGGED_MESSAGES
;
409 emptySlot
= &debug
->Log
[nextEmpty
];
411 store_message_details(emptySlot
, source
, type
, id
, severity
, len
, buf
);
413 if (debug
->NumMessages
== 0)
414 debug
->NextMsgLength
= debug
->Log
[debug
->NextMsg
].length
;
416 debug
->NumMessages
++;
421 * Pop the oldest debug message out of the log.
422 * Writes the message string, including the null terminator, into 'buf',
423 * using up to 'bufSize' bytes. If 'bufSize' is too small, or
424 * if 'buf' is NULL, nothing is written.
426 * Returns the number of bytes written on success, or when 'buf' is NULL,
427 * the number that would have been written. A return value of 0
431 get_msg(struct gl_context
*ctx
, GLenum
*source
, GLenum
*type
,
432 GLuint
*id
, GLenum
*severity
, GLsizei bufSize
, char *buf
)
434 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
435 struct gl_debug_msg
*msg
;
438 if (!debug
|| debug
->NumMessages
== 0)
441 msg
= &debug
->Log
[debug
->NextMsg
];
442 length
= msg
->length
;
444 assert(length
> 0 && length
== debug
->NextMsgLength
);
446 if (bufSize
< length
&& buf
!= NULL
)
450 *severity
= debug_severity_enums
[msg
->severity
];
454 *source
= debug_source_enums
[msg
->source
];
458 *type
= debug_type_enums
[msg
->type
];
466 assert(msg
->message
[length
-1] == '\0');
467 (void) strncpy(buf
, msg
->message
, (size_t)length
);
470 if (msg
->message
!= (char*)out_of_memory
)
475 debug
->NumMessages
--;
477 debug
->NextMsg
%= MAX_DEBUG_LOGGED_MESSAGES
;
478 debug
->NextMsgLength
= debug
->Log
[debug
->NextMsg
].length
;
485 * Verify that source, type, and severity are valid enums.
487 * The 'caller' param is used for handling values available
488 * only in glDebugMessageInsert or glDebugMessageControl
491 validate_params(struct gl_context
*ctx
, unsigned caller
,
492 const char *callerstr
, GLenum source
, GLenum type
,
498 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
499 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
501 case GL_DEBUG_SOURCE_API_ARB
:
502 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
503 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
504 case GL_DEBUG_SOURCE_OTHER_ARB
:
505 if (caller
!= INSERT
)
510 if (caller
== CONTROL
)
519 case GL_DEBUG_TYPE_ERROR_ARB
:
520 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
521 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
522 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
523 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
524 case GL_DEBUG_TYPE_OTHER_ARB
:
525 case GL_DEBUG_TYPE_MARKER
:
527 case GL_DEBUG_TYPE_PUSH_GROUP
:
528 case GL_DEBUG_TYPE_POP_GROUP
:
530 if (caller
== CONTROL
)
539 case GL_DEBUG_SEVERITY_HIGH_ARB
:
540 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
541 case GL_DEBUG_SEVERITY_LOW_ARB
:
542 case GL_DEBUG_SEVERITY_NOTIFICATION
:
545 if (caller
== CONTROL
)
555 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
556 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
557 source
, type
, severity
);
564 * Set the state of all message IDs found in the given intersection of
565 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
566 * GL_DONT_CARE (include all messages in the class).
568 * This requires both setting the state of all previously seen message
569 * IDs in the hash table, and setting the default state for all
570 * applicable combinations of source/type/severity, so that all the
571 * yet-unknown message IDs that may be used in the future will be
572 * impacted as if they were already known.
575 control_messages(struct gl_context
*ctx
,
576 enum mesa_debug_source source
,
577 enum mesa_debug_type type
,
578 enum mesa_debug_severity severity
,
581 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
582 int s
, t
, sev
, smax
, tmax
, sevmax
;
583 const GLint gstack
= debug
? debug
->GroupStackDepth
: 0;
588 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
590 smax
= MESA_DEBUG_SOURCE_COUNT
;
595 if (type
== MESA_DEBUG_TYPE_COUNT
) {
597 tmax
= MESA_DEBUG_TYPE_COUNT
;
602 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
604 sevmax
= MESA_DEBUG_SEVERITY_COUNT
;
609 for (sev
= severity
; sev
< sevmax
; sev
++) {
610 for (s
= source
; s
< smax
; s
++) {
611 for (t
= type
; t
< tmax
; t
++) {
612 struct simple_node
*node
;
613 struct gl_debug_severity
*entry
;
615 /* change the default for IDs we've never seen before. */
616 debug
->Defaults
[gstack
][sev
][s
][t
] = enabled
;
618 /* Now change the state of IDs we *have* seen... */
619 foreach(node
, &debug
->Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
620 entry
= (struct gl_debug_severity
*)node
;
621 set_message_state(ctx
, s
, t
, entry
->ID
, enabled
);
630 * Debugging-message namespaces with the source APPLICATION or THIRD_PARTY
631 * require special handling, since the IDs in them are controlled by clients,
632 * not the OpenGL implementation.
634 * 'count' is the length of the array 'ids'. If 'count' is nonzero, all
635 * the given IDs in the namespace defined by 'esource' and 'etype'
638 * If 'count' is zero, this sets the state of all IDs that match
639 * the combination of 'esource', 'etype', and 'eseverity'.
642 control_app_messages(struct gl_context
*ctx
, GLenum esource
, GLenum etype
,
643 GLenum eseverity
, GLsizei count
, const GLuint
*ids
,
647 enum mesa_debug_source source
= gl_enum_to_debug_source(esource
);
648 enum mesa_debug_type type
= gl_enum_to_debug_type(etype
);
649 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(eseverity
);
651 for (i
= 0; i
< count
; i
++)
652 set_message_state(ctx
, source
, type
, ids
[i
], enabled
);
657 control_messages(ctx
, source
, type
, severity
, enabled
);
662 * This is a generic message insert function.
663 * Validation of source, type and severity parameters should be done
664 * before calling this funtion.
667 message_insert(GLenum source
, GLenum type
, GLuint id
,
668 GLenum severity
, GLint length
, const GLchar
*buf
,
669 const char *callerstr
)
671 GET_CURRENT_CONTEXT(ctx
);
674 length
= strlen(buf
);
676 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
677 _mesa_error(ctx
, GL_INVALID_VALUE
,
678 "%s(length=%d, which is not less than "
679 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
680 MAX_DEBUG_MESSAGE_LENGTH
);
685 gl_enum_to_debug_source(source
),
686 gl_enum_to_debug_type(type
), id
,
687 gl_enum_to_debug_severity(severity
), length
, buf
);
692 do_nothing(GLuint key
, void *data
, void *userData
)
698 * Free context state pertaining to error/debug state for the given stack
702 free_errors_data(struct gl_context
*ctx
, GLint gstack
)
704 struct gl_debug_state
*debug
= ctx
->Debug
;
705 enum mesa_debug_type t
;
706 enum mesa_debug_source s
;
707 enum mesa_debug_severity sev
;
711 /* Tear down state for filtering debug messages. */
712 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
713 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
714 _mesa_HashDeleteAll(debug
->Namespaces
[gstack
][s
][t
].IDs
,
716 _mesa_DeleteHashTable(debug
->Namespaces
[gstack
][s
][t
].IDs
);
717 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
718 struct simple_node
*node
, *tmp
;
719 struct gl_debug_severity
*entry
;
722 &debug
->Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
723 entry
= (struct gl_debug_severity
*)node
;
733 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
734 GLenum severity
, GLint length
,
737 const char *callerstr
= "glDebugMessageInsert";
739 GET_CURRENT_CONTEXT(ctx
);
741 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
742 return; /* GL_INVALID_ENUM */
744 message_insert(source
, type
, id
, severity
, length
, buf
, callerstr
);
749 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
750 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
751 GLsizei
*lengths
, GLchar
*messageLog
)
753 GET_CURRENT_CONTEXT(ctx
);
760 _mesa_error(ctx
, GL_INVALID_VALUE
,
761 "glGetDebugMessageLog(logSize=%d : logSize must not be"
762 " negative)", logSize
);
766 for (ret
= 0; ret
< count
; ret
++) {
767 GLsizei written
= get_msg(ctx
, sources
, types
, ids
, severities
,
768 logSize
, messageLog
);
773 messageLog
+= written
;
796 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
797 GLenum gl_severity
, GLsizei count
,
798 const GLuint
*ids
, GLboolean enabled
)
800 const char *callerstr
= "glDebugMessageControl";
802 GET_CURRENT_CONTEXT(ctx
);
805 _mesa_error(ctx
, GL_INVALID_VALUE
,
806 "%s(count=%d : count must not be negative)", callerstr
,
811 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
813 return; /* GL_INVALID_ENUM */
815 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
816 || gl_source
== GL_DONT_CARE
)) {
817 _mesa_error(ctx
, GL_INVALID_OPERATION
,
818 "%s(When passing an array of ids, severity must be"
819 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
824 control_app_messages(ctx
, gl_source
, gl_type
, gl_severity
,
825 count
, ids
, enabled
);
830 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
832 GET_CURRENT_CONTEXT(ctx
);
833 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
835 debug
->Callback
= callback
;
836 debug
->CallbackData
= userParam
;
842 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
843 const GLchar
*message
)
845 GET_CURRENT_CONTEXT(ctx
);
846 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
847 const char *callerstr
= "glPushDebugGroup";
849 GLint prevStackDepth
;
850 GLint currStackDepth
;
851 struct gl_debug_msg
*emptySlot
;
856 if (debug
->GroupStackDepth
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
857 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
862 case GL_DEBUG_SOURCE_APPLICATION
:
863 case GL_DEBUG_SOURCE_THIRD_PARTY
:
866 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
867 "(source=0x%x)", callerstr
, source
);
871 message_insert(source
, GL_DEBUG_TYPE_PUSH_GROUP
, id
,
872 GL_DEBUG_SEVERITY_NOTIFICATION
, length
,
875 prevStackDepth
= debug
->GroupStackDepth
;
876 debug
->GroupStackDepth
++;
877 currStackDepth
= debug
->GroupStackDepth
;
879 /* pop reuses the message details from push so we store this */
881 length
= strlen(message
);
882 emptySlot
= &debug
->DebugGroupMsgs
[debug
->GroupStackDepth
];
883 store_message_details(emptySlot
, gl_enum_to_debug_source(source
),
884 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
886 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
889 /* inherit the control volume of the debug group previously residing on
890 * the top of the debug group stack
892 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
893 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
894 /* copy id settings */
895 debug
->Namespaces
[currStackDepth
][s
][t
].IDs
=
896 _mesa_HashClone(debug
->Namespaces
[prevStackDepth
][s
][t
].IDs
);
898 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
899 struct gl_debug_severity
*entry
, *prevEntry
;
900 struct simple_node
*node
;
902 /* copy default settings for unknown ids */
903 debug
->Defaults
[currStackDepth
][sev
][s
][t
] =
904 debug
->Defaults
[prevStackDepth
][sev
][s
][t
];
906 /* copy known id severity settings */
907 make_empty_list(&debug
->Namespaces
[currStackDepth
][s
][t
].Severity
[sev
]);
908 foreach(node
, &debug
->Namespaces
[prevStackDepth
][s
][t
].Severity
[sev
]) {
909 prevEntry
= (struct gl_debug_severity
*)node
;
910 entry
= malloc(sizeof *entry
);
914 entry
->ID
= prevEntry
->ID
;
915 insert_at_tail(&debug
->Namespaces
[currStackDepth
][s
][t
].Severity
[sev
], &entry
->link
);
924 _mesa_PopDebugGroup(void)
926 GET_CURRENT_CONTEXT(ctx
);
927 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
928 const char *callerstr
= "glPopDebugGroup";
929 struct gl_debug_msg
*gdmessage
;
930 GLint prevStackDepth
;
935 if (debug
->GroupStackDepth
<= 0) {
936 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
940 prevStackDepth
= debug
->GroupStackDepth
;
941 debug
->GroupStackDepth
--;
943 gdmessage
= &debug
->DebugGroupMsgs
[prevStackDepth
];
944 /* using log_msg() directly here as verification of parameters
945 * already done in push
947 log_msg(ctx
, gdmessage
->source
,
948 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
950 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
951 gdmessage
->length
, gdmessage
->message
);
953 if (gdmessage
->message
!= (char*)out_of_memory
)
954 free(gdmessage
->message
);
955 gdmessage
->message
= NULL
;
956 gdmessage
->length
= 0;
958 /* free popped debug group data */
959 free_errors_data(ctx
, prevStackDepth
);
964 _mesa_init_errors(struct gl_context
*ctx
)
971 * Loop through debug group stack tearing down states for
972 * filtering debug messages. Then free debug output state.
975 _mesa_free_errors_data(struct gl_context
*ctx
)
980 for (i
= 0; i
<= ctx
->Debug
->GroupStackDepth
; i
++) {
981 free_errors_data(ctx
, i
);
984 /* set to NULL just in case it is used before context is completely gone. */
990 /**********************************************************************/
991 /** \name Diagnostics */
995 output_if_debug(const char *prefixString
, const char *outputString
,
998 static int debug
= -1;
999 static FILE *fout
= NULL
;
1001 /* Init the local 'debug' var once.
1002 * Note: the _mesa_init_debug() function should have been called
1003 * by now so MESA_DEBUG_FLAGS will be initialized.
1006 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1007 * etc to the named file. Otherwise, output to stderr.
1009 const char *logFile
= _mesa_getenv("MESA_LOG_FILE");
1011 fout
= fopen(logFile
, "w");
1015 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1016 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1021 /* in release builds, be silent unless MESA_DEBUG is set */
1022 debug
= _mesa_getenv("MESA_DEBUG") != NULL
;
1026 /* Now only print the string if we're required to do so. */
1028 fprintf(fout
, "%s: %s", prefixString
, outputString
);
1030 fprintf(fout
, "\n");
1033 #if defined(_WIN32) && !defined(_WIN32_WCE)
1034 /* stderr from windows applications without console is not usually
1035 * visible, so communicate with the debugger instead */
1038 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1039 OutputDebugStringA(buf
);
1047 * When a new type of error is recorded, print a message describing
1048 * previous errors which were accumulated.
1051 flush_delayed_errors( struct gl_context
*ctx
)
1053 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1055 if (ctx
->ErrorDebugCount
) {
1056 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1057 ctx
->ErrorDebugCount
,
1058 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
1060 output_if_debug("Mesa", s
, GL_TRUE
);
1062 ctx
->ErrorDebugCount
= 0;
1068 * Report a warning (a recoverable error condition) to stderr if
1069 * either DEBUG is defined or the MESA_DEBUG env var is set.
1071 * \param ctx GL context.
1072 * \param fmtString printf()-like format string.
1075 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1077 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1079 va_start( args
, fmtString
);
1080 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1084 flush_delayed_errors( ctx
);
1086 output_if_debug("Mesa warning", str
, GL_TRUE
);
1091 * Report an internal implementation problem.
1092 * Prints the message to stderr via fprintf().
1094 * \param ctx GL context.
1095 * \param fmtString problem description string.
1098 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1101 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1102 static int numCalls
= 0;
1106 if (numCalls
< 50) {
1109 va_start( args
, fmtString
);
1110 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1112 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1113 PACKAGE_VERSION
, str
);
1114 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1120 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1122 static GLint debug
= -1;
1124 /* Check debug environment variable only once:
1127 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
1130 if (debugEnv
&& strstr(debugEnv
, "silent"))
1143 if (ctx
->ErrorValue
!= error
||
1144 ctx
->ErrorDebugFmtString
!= fmtString
) {
1145 flush_delayed_errors( ctx
);
1146 ctx
->ErrorDebugFmtString
= fmtString
;
1147 ctx
->ErrorDebugCount
= 0;
1150 ctx
->ErrorDebugCount
++;
1157 _mesa_gl_debug(struct gl_context
*ctx
,
1159 enum mesa_debug_type type
,
1160 enum mesa_debug_severity severity
,
1161 const char *fmtString
, ...)
1163 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1169 va_start(args
, fmtString
);
1170 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1173 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, type
, *id
, severity
, len
, s
);
1178 * Record an OpenGL state error. These usually occur when the user
1179 * passes invalid parameters to a GL function.
1181 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1182 * run-time via the MESA_DEBUG environment variable), report the error with
1185 * \param ctx the GL context.
1186 * \param error the error value.
1187 * \param fmtString printf() style format string, followed by optional args
1190 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1192 GLboolean do_output
, do_log
;
1193 /* Ideally this would be set up by the caller, so that we had proper IDs
1194 * per different message.
1196 static GLuint error_msg_id
= 0;
1198 debug_get_id(&error_msg_id
);
1200 do_output
= should_output(ctx
, error
, fmtString
);
1201 do_log
= should_log(ctx
,
1202 MESA_DEBUG_SOURCE_API
,
1203 MESA_DEBUG_TYPE_ERROR
,
1205 MESA_DEBUG_SEVERITY_HIGH
);
1207 if (do_output
|| do_log
) {
1208 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1212 va_start(args
, fmtString
);
1213 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1216 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1217 /* Too long error message. Whoever calls _mesa_error should use
1224 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1225 _mesa_lookup_enum_by_nr(error
), s
);
1226 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1227 /* Same as above. */
1232 /* Print the error to stderr if needed. */
1234 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1237 /* Log the error via ARB_debug_output if needed.*/
1239 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_ERROR
,
1240 error_msg_id
, MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1244 /* Set the GL context error state for glGetError. */
1245 _mesa_record_error(ctx
, error
);
1250 * Report debug information. Print error message to stderr via fprintf().
1251 * No-op if DEBUG mode not enabled.
1253 * \param ctx GL context.
1254 * \param fmtString printf()-style format string, followed by optional args.
1257 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1260 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1262 va_start(args
, fmtString
);
1263 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1265 output_if_debug("Mesa", s
, GL_FALSE
);
1273 * Report debug information from the shader compiler via GL_ARB_debug_output.
1275 * \param ctx GL context.
1276 * \param type The namespace to which this message belongs.
1277 * \param id The message ID within the given namespace.
1278 * \param msg The message to output. Need not be null-terminated.
1279 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1282 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1283 const char *msg
, int len
)
1285 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1286 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1293 /* Truncate the message if necessary. */
1294 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1295 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1297 log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);