3 * Mesa debugging and error handling functions.
7 * Mesa 3-D graphics library
9 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
39 #include "hash_table.h"
40 #include "glapi/glthread.h"
42 _glthread_DECLARE_STATIC_MUTEX(DynamicIDMutex
);
43 static GLuint NextDynamicID
= 1;
45 struct gl_debug_severity
47 struct simple_node link
;
51 static char out_of_memory
[] = "Debugging error: out of memory";
53 static const GLenum debug_source_enums
[] = {
55 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
56 GL_DEBUG_SOURCE_SHADER_COMPILER
,
57 GL_DEBUG_SOURCE_THIRD_PARTY
,
58 GL_DEBUG_SOURCE_APPLICATION
,
59 GL_DEBUG_SOURCE_OTHER
,
62 static const GLenum debug_type_enums
[] = {
64 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
65 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
66 GL_DEBUG_TYPE_PORTABILITY
,
67 GL_DEBUG_TYPE_PERFORMANCE
,
70 GL_DEBUG_TYPE_PUSH_GROUP
,
71 GL_DEBUG_TYPE_POP_GROUP
,
74 static const GLenum debug_severity_enums
[] = {
75 GL_DEBUG_SEVERITY_LOW
,
76 GL_DEBUG_SEVERITY_MEDIUM
,
77 GL_DEBUG_SEVERITY_HIGH
,
78 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
)
118 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
121 * The GL API has this "ID" mechanism, where the intention is to allow a
122 * client to filter in/out messages based on source, type, and ID. Of course,
123 * building a giant enum list of all debug output messages that Mesa might
124 * generate is ridiculous, so instead we have our caller pass us a pointer to
125 * static storage where the ID should get stored. This ID will be shared
126 * across all contexts for that message (which seems like a desirable
127 * property, even if it's not expected by the spec), but note that it won't be
128 * the same between executions if messages aren't generated in the same order.
131 debug_get_id(GLuint
*id
)
134 _glthread_LOCK_MUTEX(DynamicIDMutex
);
136 *id
= NextDynamicID
++;
137 _glthread_UNLOCK_MUTEX(DynamicIDMutex
);
142 * We store a bitfield in the hash table, with five possible values total.
144 * The ENABLED_BIT's purpose is self-explanatory.
146 * The FOUND_BIT is needed to differentiate the value of DISABLED from
147 * the value returned by HashTableLookup() when it can't find the given key.
149 * The KNOWN_SEVERITY bit is a bit complicated:
151 * A client may call Control() with an array of IDs, then call Control()
152 * on all message IDs of a certain severity, then Insert() one of the
153 * previously specified IDs, giving us a known severity level, then call
154 * Control() on all message IDs of a certain severity level again.
156 * After the first call, those IDs will have a FOUND_BIT, but will not
157 * exist in any severity-specific list, so the second call will not
158 * impact them. This is undesirable but unavoidable given the API:
159 * The only entrypoint that gives a severity for a client-defined ID
160 * is the Insert() call.
162 * For the sake of Control(), we want to maintain the invariant
163 * that an ID will either appear in none of the three severity lists,
164 * or appear once, to minimize pointless duplication and potential surprises.
166 * Because Insert() is the only place that will learn an ID's severity,
167 * it should insert an ID into the appropriate list, but only if the ID
168 * doesn't exist in it or any other list yet. Because searching all three
169 * lists at O(n) is needlessly expensive, we store KNOWN_SEVERITY.
173 ENABLED_BIT
= 1 << 1,
174 KNOWN_SEVERITY
= 1 << 2,
176 /* HashTable reserves zero as a return value meaning 'not found' */
178 DISABLED
= FOUND_BIT
,
179 ENABLED
= ENABLED_BIT
| FOUND_BIT
183 * Returns the state of the given message source/type/ID tuple.
186 should_log(struct gl_context
*ctx
,
187 enum mesa_debug_source source
,
188 enum mesa_debug_type type
,
190 enum mesa_debug_severity severity
)
192 GLint gstack
= ctx
->Debug
.GroupStackDepth
;
193 struct gl_debug_namespace
*nspace
=
194 &ctx
->Debug
.Namespaces
[gstack
][source
][type
];
197 if (!ctx
->Debug
.DebugOutput
)
200 /* In addition to not being able to store zero as a value, HashTable also
201 can't use zero as a key. */
203 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
205 state
= nspace
->ZeroID
;
207 /* Only do this once for each ID. This makes sure the ID exists in,
208 at most, one list, and does not pointlessly appear multiple times. */
209 if (!(state
& KNOWN_SEVERITY
)) {
210 struct gl_debug_severity
*entry
;
212 if (state
== NOT_FOUND
) {
213 if (ctx
->Debug
.Defaults
[gstack
][severity
][source
][type
])
219 entry
= malloc(sizeof *entry
);
223 state
|= KNOWN_SEVERITY
;
226 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
228 nspace
->ZeroID
= state
;
231 insert_at_tail(&nspace
->Severity
[severity
], &entry
->link
);
235 return !!(state
& ENABLED_BIT
);
239 * Sets the state of the given message source/type/ID tuple.
242 set_message_state(struct gl_context
*ctx
,
243 enum mesa_debug_source source
,
244 enum mesa_debug_type type
,
245 GLuint id
, GLboolean enabled
)
247 GLint gstack
= ctx
->Debug
.GroupStackDepth
;
248 struct gl_debug_namespace
*nspace
=
249 &ctx
->Debug
.Namespaces
[gstack
][source
][type
];
252 /* In addition to not being able to store zero as a value, HashTable also
253 can't use zero as a key. */
255 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
257 state
= nspace
->ZeroID
;
259 if (state
== NOT_FOUND
)
260 state
= enabled
? ENABLED
: DISABLED
;
263 state
|= ENABLED_BIT
;
265 state
&= ~ENABLED_BIT
;
269 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
271 nspace
->ZeroID
= state
;
275 store_message_details(struct gl_debug_msg
*emptySlot
,
276 enum mesa_debug_source source
,
277 enum mesa_debug_type type
, GLuint id
,
278 enum mesa_debug_severity severity
, GLint len
,
281 assert(!emptySlot
->message
&& !emptySlot
->length
);
283 emptySlot
->message
= malloc(len
+1);
284 if (emptySlot
->message
) {
285 (void) strncpy(emptySlot
->message
, buf
, (size_t)len
);
286 emptySlot
->message
[len
] = '\0';
288 emptySlot
->length
= len
+1;
289 emptySlot
->source
= source
;
290 emptySlot
->type
= type
;
292 emptySlot
->severity
= severity
;
294 static GLuint oom_msg_id
= 0;
295 debug_get_id(&oom_msg_id
);
298 emptySlot
->message
= out_of_memory
;
299 emptySlot
->length
= strlen(out_of_memory
)+1;
300 emptySlot
->source
= MESA_DEBUG_SOURCE_OTHER
;
301 emptySlot
->type
= MESA_DEBUG_TYPE_ERROR
;
302 emptySlot
->id
= oom_msg_id
;
303 emptySlot
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
308 * 'buf' is not necessarily a null-terminated string. When logging, copy
309 * 'len' characters from it, store them in a new, null-terminated string,
310 * and remember the number of bytes used by that string, *including*
311 * the null terminator this time.
314 _mesa_log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
315 enum mesa_debug_type type
, GLuint id
,
316 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
319 struct gl_debug_msg
*emptySlot
;
321 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
323 if (!should_log(ctx
, source
, type
, id
, severity
))
326 if (ctx
->Debug
.Callback
) {
327 ctx
->Debug
.Callback(debug_source_enums
[source
],
328 debug_type_enums
[type
],
330 debug_severity_enums
[severity
],
331 len
, buf
, ctx
->Debug
.CallbackData
);
335 if (ctx
->Debug
.NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
338 nextEmpty
= (ctx
->Debug
.NextMsg
+ ctx
->Debug
.NumMessages
)
339 % MAX_DEBUG_LOGGED_MESSAGES
;
340 emptySlot
= &ctx
->Debug
.Log
[nextEmpty
];
342 store_message_details(emptySlot
, source
, type
, id
, severity
, len
, buf
);
344 if (ctx
->Debug
.NumMessages
== 0)
345 ctx
->Debug
.NextMsgLength
= ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
].length
;
347 ctx
->Debug
.NumMessages
++;
351 * Pop the oldest debug message out of the log.
352 * Writes the message string, including the null terminator, into 'buf',
353 * using up to 'bufSize' bytes. If 'bufSize' is too small, or
354 * if 'buf' is NULL, nothing is written.
356 * Returns the number of bytes written on success, or when 'buf' is NULL,
357 * the number that would have been written. A return value of 0
361 _mesa_get_msg(struct gl_context
*ctx
, GLenum
*source
, GLenum
*type
,
362 GLuint
*id
, GLenum
*severity
, GLsizei bufSize
, char *buf
)
364 struct gl_debug_msg
*msg
;
367 if (ctx
->Debug
.NumMessages
== 0)
370 msg
= &ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
];
371 length
= msg
->length
;
373 assert(length
> 0 && length
== ctx
->Debug
.NextMsgLength
);
375 if (bufSize
< length
&& buf
!= NULL
)
379 *severity
= debug_severity_enums
[msg
->severity
];
381 *source
= debug_source_enums
[msg
->source
];
383 *type
= debug_type_enums
[msg
->type
];
388 assert(msg
->message
[length
-1] == '\0');
389 (void) strncpy(buf
, msg
->message
, (size_t)length
);
392 if (msg
->message
!= (char*)out_of_memory
)
397 ctx
->Debug
.NumMessages
--;
398 ctx
->Debug
.NextMsg
++;
399 ctx
->Debug
.NextMsg
%= MAX_DEBUG_LOGGED_MESSAGES
;
400 ctx
->Debug
.NextMsgLength
= ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
].length
;
406 * Verify that source, type, and severity are valid enums.
407 * glDebugMessageInsertARB only accepts two values for 'source',
408 * and glDebugMessageControlARB will additionally accept GL_DONT_CARE
409 * in any parameter, so handle those cases specially.
411 * There is also special cases for handling values available in
412 * GL_KHR_debug that are not avaliable in GL_ARB_debug_output
415 validate_params(struct gl_context
*ctx
, unsigned caller
,
416 const char *callerstr
, GLenum source
, GLenum type
,
422 #define CONTROL_ARB 4
424 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
425 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
427 case GL_DEBUG_SOURCE_API_ARB
:
428 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
429 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
430 case GL_DEBUG_SOURCE_OTHER_ARB
:
431 if (caller
!= INSERT
|| caller
== INSERT_ARB
)
434 if (caller
== CONTROL
|| caller
== CONTROL_ARB
)
441 case GL_DEBUG_TYPE_ERROR_ARB
:
442 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
443 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
444 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
445 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
446 case GL_DEBUG_TYPE_OTHER_ARB
:
448 case GL_DEBUG_TYPE_MARKER
:
449 /* this value is only valid for GL_KHR_debug functions */
450 if (caller
== CONTROL
|| caller
== INSERT
)
453 if (caller
== CONTROL
|| caller
== CONTROL_ARB
)
460 case GL_DEBUG_SEVERITY_HIGH_ARB
:
461 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
462 case GL_DEBUG_SEVERITY_LOW_ARB
:
464 case GL_DEBUG_SEVERITY_NOTIFICATION
:
465 /* this value is only valid for GL_KHR_debug functions */
466 if (caller
== CONTROL
|| caller
== INSERT
)
469 if (caller
== CONTROL
|| caller
== CONTROL_ARB
)
478 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
479 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
480 source
, type
, severity
);
486 * Set the state of all message IDs found in the given intersection of
487 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
488 * GL_DONT_CARE (include all messages in the class).
490 * This requires both setting the state of all previously seen message
491 * IDs in the hash table, and setting the default state for all
492 * applicable combinations of source/type/severity, so that all the
493 * yet-unknown message IDs that may be used in the future will be
494 * impacted as if they were already known.
497 control_messages(struct gl_context
*ctx
,
498 enum mesa_debug_source source
,
499 enum mesa_debug_type type
,
500 enum mesa_debug_severity severity
,
503 int s
, t
, sev
, smax
, tmax
, sevmax
;
504 GLint gstack
= ctx
->Debug
.GroupStackDepth
;
506 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
508 smax
= MESA_DEBUG_SOURCE_COUNT
;
513 if (type
== MESA_DEBUG_TYPE_COUNT
) {
515 tmax
= MESA_DEBUG_TYPE_COUNT
;
520 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
522 sevmax
= MESA_DEBUG_SEVERITY_COUNT
;
527 for (sev
= severity
; sev
< sevmax
; sev
++)
528 for (s
= source
; s
< smax
; s
++)
529 for (t
= type
; t
< tmax
; t
++) {
530 struct simple_node
*node
;
531 struct gl_debug_severity
*entry
;
533 /* change the default for IDs we've never seen before. */
534 ctx
->Debug
.Defaults
[gstack
][sev
][s
][t
] = enabled
;
536 /* Now change the state of IDs we *have* seen... */
537 foreach(node
, &ctx
->Debug
.Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
538 entry
= (struct gl_debug_severity
*)node
;
539 set_message_state(ctx
, s
, t
, entry
->ID
, enabled
);
545 * Debugging-message namespaces with the source APPLICATION or THIRD_PARTY
546 * require special handling, since the IDs in them are controlled by clients,
547 * not the OpenGL implementation.
549 * 'count' is the length of the array 'ids'. If 'count' is nonzero, all
550 * the given IDs in the namespace defined by 'esource' and 'etype'
553 * If 'count' is zero, this sets the state of all IDs that match
554 * the combination of 'esource', 'etype', and 'eseverity'.
557 control_app_messages(struct gl_context
*ctx
, GLenum esource
, GLenum etype
,
558 GLenum eseverity
, GLsizei count
, const GLuint
*ids
,
562 enum mesa_debug_source source
= gl_enum_to_debug_source(esource
);
563 enum mesa_debug_type type
= gl_enum_to_debug_type(etype
);
564 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(eseverity
);
566 for (i
= 0; i
< count
; i
++)
567 set_message_state(ctx
, source
, type
, ids
[i
], enabled
);
572 control_messages(ctx
, source
, type
, severity
, enabled
);
576 * This is a generic message control function for use by both
577 * glDebugMessageControlARB and glDebugMessageControl.
580 message_control(GLenum gl_source
, GLenum gl_type
,
582 GLsizei count
, const GLuint
*ids
,
584 unsigned caller
, const char *callerstr
)
586 GET_CURRENT_CONTEXT(ctx
);
589 _mesa_error(ctx
, GL_INVALID_VALUE
,
590 "%s(count=%d : count must not be negative)", callerstr
,
595 if (!validate_params(ctx
, caller
, callerstr
, gl_source
, gl_type
,
597 return; /* GL_INVALID_ENUM */
599 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
600 || gl_source
== GL_DONT_CARE
)) {
601 _mesa_error(ctx
, GL_INVALID_OPERATION
,
602 "%s(When passing an array of ids, severity must be"
603 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
608 control_app_messages(ctx
, gl_source
, gl_type
, gl_severity
,
609 count
, ids
, enabled
);
613 * This is a generic message insert function.
614 * Validation of source, type and severity parameters should be done
615 * before calling this funtion.
618 message_insert(GLenum source
, GLenum type
, GLuint id
,
619 GLenum severity
, GLint length
, const GLchar
* buf
,
620 const char *callerstr
)
622 GET_CURRENT_CONTEXT(ctx
);
625 length
= strlen(buf
);
627 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
628 _mesa_error(ctx
, GL_INVALID_VALUE
,
629 "%s(length=%d, which is not less than "
630 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
631 MAX_DEBUG_MESSAGE_LENGTH
);
636 gl_enum_to_debug_source(source
),
637 gl_enum_to_debug_type(type
), id
,
638 gl_enum_to_debug_severity(severity
), length
, buf
);
642 * This is a generic message insert function for use by both
643 * glGetDebugMessageLogARB and glGetDebugMessageLog.
646 get_message_log(GLuint count
, GLsizei logSize
, GLenum
* sources
,
647 GLenum
* types
, GLenum
* ids
, GLenum
* severities
,
648 GLsizei
* lengths
, GLchar
* messageLog
,
649 unsigned caller
, const char *callerstr
)
651 #define MESSAGE_LOG 1
652 #define MESSAGE_LOG_ARB 2
653 GET_CURRENT_CONTEXT(ctx
);
660 _mesa_error(ctx
, GL_INVALID_VALUE
,
661 "%s(logSize=%d : logSize must not be negative)", callerstr
,
666 for (ret
= 0; ret
< count
; ret
++) {
667 GLsizei written
= _mesa_get_msg(ctx
, sources
, types
, ids
, severities
,
668 logSize
, messageLog
);
673 messageLog
+= written
;
695 do_nothing(GLuint key
, void *data
, void *userData
)
700 free_errors_data(struct gl_context
*ctx
, GLint gstack
)
702 enum mesa_debug_type t
;
703 enum mesa_debug_source s
;
704 enum mesa_debug_severity sev
;
706 /* Tear down state for filtering debug messages. */
707 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++)
708 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
709 _mesa_HashDeleteAll(ctx
->Debug
.Namespaces
[gstack
][s
][t
].IDs
,
711 _mesa_DeleteHashTable(ctx
->Debug
.Namespaces
[gstack
][s
][t
].IDs
);
712 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
713 struct simple_node
*node
, *tmp
;
714 struct gl_debug_severity
*entry
;
717 &ctx
->Debug
.Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
718 entry
= (struct gl_debug_severity
*)node
;
726 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
727 GLenum severity
, GLint length
,
730 const char *callerstr
= "glDebugMessageInsert";
732 GET_CURRENT_CONTEXT(ctx
);
734 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
735 return; /* GL_INVALID_ENUM */
737 message_insert(source
, type
, id
, severity
, length
, buf
,
742 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
* sources
,
743 GLenum
* types
, GLenum
* ids
, GLenum
* severities
,
744 GLsizei
* lengths
, GLchar
* messageLog
)
746 const char *callerstr
= "glGetDebugMessageLog";
748 return get_message_log(count
, logSize
, sources
, types
, ids
, severities
,
749 lengths
, messageLog
, MESSAGE_LOG
, callerstr
);
753 _mesa_DebugMessageControl(GLenum source
, GLenum type
, GLenum severity
,
754 GLsizei count
, const GLuint
*ids
,
757 const char *callerstr
= "glDebugMessageControl";
759 message_control(source
, type
, severity
, count
, ids
,
760 enabled
, CONTROL
, callerstr
);
764 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
766 GET_CURRENT_CONTEXT(ctx
);
767 ctx
->Debug
.Callback
= callback
;
768 ctx
->Debug
.CallbackData
= userParam
;
772 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
773 const GLchar
*message
)
775 const char *callerstr
= "glPushDebugGroup";
777 GLint prevStackDepth
;
778 GLint currStackDepth
;
779 struct gl_debug_msg
*emptySlot
;
781 GET_CURRENT_CONTEXT(ctx
);
783 if (ctx
->Debug
.GroupStackDepth
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
784 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
789 case GL_DEBUG_SOURCE_APPLICATION
:
790 case GL_DEBUG_SOURCE_THIRD_PARTY
:
793 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
794 "(source=0x%x)", callerstr
, source
);
798 message_insert(source
, GL_DEBUG_TYPE_PUSH_GROUP
, id
,
799 GL_DEBUG_SEVERITY_NOTIFICATION
, length
,
802 prevStackDepth
= ctx
->Debug
.GroupStackDepth
;
803 ctx
->Debug
.GroupStackDepth
++;
804 currStackDepth
= ctx
->Debug
.GroupStackDepth
;
806 /* pop reuses the message details from push so we store this */
808 length
= strlen(message
);
809 emptySlot
= &ctx
->Debug
.DebugGroupMsgs
[ctx
->Debug
.GroupStackDepth
];
810 store_message_details(emptySlot
, gl_enum_to_debug_source(source
),
811 gl_enum_to_debug_source(GL_DEBUG_TYPE_PUSH_GROUP
),
813 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
816 /* inherit the control volume of the debug group previously residing on
817 * the top of the debug group stack
819 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++)
820 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
821 /* copy id settings */
822 ctx
->Debug
.Namespaces
[currStackDepth
][s
][t
].IDs
=
823 _mesa_HashClone(ctx
->Debug
.Namespaces
[prevStackDepth
][s
][t
].IDs
);
825 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
826 struct gl_debug_severity
*entry
, *prevEntry
;
827 struct simple_node
*node
;
829 /* copy default settings for unknown ids */
830 ctx
->Debug
.Defaults
[currStackDepth
][sev
][s
][t
] = ctx
->Debug
.Defaults
[prevStackDepth
][sev
][s
][t
];
832 /* copy known id severity settings */
833 make_empty_list(&ctx
->Debug
.Namespaces
[currStackDepth
][s
][t
].Severity
[sev
]);
834 foreach(node
, &ctx
->Debug
.Namespaces
[prevStackDepth
][s
][t
].Severity
[sev
]) {
835 prevEntry
= (struct gl_debug_severity
*)node
;
836 entry
= malloc(sizeof *entry
);
840 entry
->ID
= prevEntry
->ID
;
841 insert_at_tail(&ctx
->Debug
.Namespaces
[currStackDepth
][s
][t
].Severity
[sev
], &entry
->link
);
848 _mesa_PopDebugGroup()
850 const char *callerstr
= "glPopDebugGroup";
851 struct gl_debug_msg
*gdmessage
;
852 GLint prevStackDepth
;
854 GET_CURRENT_CONTEXT(ctx
);
856 if (ctx
->Debug
.GroupStackDepth
<= 0) {
857 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
861 prevStackDepth
= ctx
->Debug
.GroupStackDepth
;
862 ctx
->Debug
.GroupStackDepth
--;
864 gdmessage
= &ctx
->Debug
.DebugGroupMsgs
[prevStackDepth
];
865 /* using _mesa_log_msg() directly here as verification of parameters
866 * already done in push
868 _mesa_log_msg(ctx
, gdmessage
->source
,
869 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
871 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
872 gdmessage
->length
, gdmessage
->message
);
874 if (gdmessage
->message
!= (char*)out_of_memory
)
875 free(gdmessage
->message
);
876 gdmessage
->message
= NULL
;
877 gdmessage
->length
= 0;
879 /* free popped debug group data */
880 free_errors_data(ctx
, prevStackDepth
);
884 _mesa_DebugMessageInsertARB(GLenum source
, GLenum type
, GLuint id
,
885 GLenum severity
, GLint length
,
886 const GLcharARB
* buf
)
888 const char *callerstr
= "glDebugMessageInsertARB";
890 GET_CURRENT_CONTEXT(ctx
);
892 if (!validate_params(ctx
, INSERT_ARB
, callerstr
, source
, type
, severity
))
893 return; /* GL_INVALID_ENUM */
895 message_insert(source
, type
, id
, severity
, length
, buf
,
900 _mesa_GetDebugMessageLogARB(GLuint count
, GLsizei logSize
, GLenum
* sources
,
901 GLenum
* types
, GLenum
* ids
, GLenum
* severities
,
902 GLsizei
* lengths
, GLcharARB
* messageLog
)
904 const char *callerstr
= "glGetDebugMessageLogARB";
906 return get_message_log(count
, logSize
, sources
, types
, ids
, severities
,
907 lengths
, messageLog
, MESSAGE_LOG_ARB
, callerstr
);
911 _mesa_DebugMessageControlARB(GLenum gl_source
, GLenum gl_type
,
913 GLsizei count
, const GLuint
*ids
,
916 const char *callerstr
= "glDebugMessageControlARB";
918 message_control(gl_source
, gl_type
, gl_severity
, count
, ids
,
919 enabled
, CONTROL_ARB
, callerstr
);
923 _mesa_DebugMessageCallbackARB(GLDEBUGPROCARB callback
, const void *userParam
)
925 GET_CURRENT_CONTEXT(ctx
);
926 ctx
->Debug
.Callback
= callback
;
927 ctx
->Debug
.CallbackData
= userParam
;
931 _mesa_init_errors(struct gl_context
*ctx
)
935 ctx
->Debug
.Callback
= NULL
;
936 ctx
->Debug
.SyncOutput
= GL_FALSE
;
937 ctx
->Debug
.Log
[0].length
= 0;
938 ctx
->Debug
.NumMessages
= 0;
939 ctx
->Debug
.NextMsg
= 0;
940 ctx
->Debug
.NextMsgLength
= 0;
941 ctx
->Debug
.GroupStackDepth
= 0;
943 /* Enable all the messages with severity HIGH or MEDIUM by default. */
944 memset(ctx
->Debug
.Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
], GL_TRUE
,
945 sizeof ctx
->Debug
.Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
]);
946 memset(ctx
->Debug
.Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
], GL_TRUE
,
947 sizeof ctx
->Debug
.Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
]);
948 memset(ctx
->Debug
.Defaults
[0][MESA_DEBUG_SEVERITY_LOW
], GL_FALSE
,
949 sizeof ctx
->Debug
.Defaults
[0][MESA_DEBUG_SEVERITY_LOW
]);
951 /* Initialize state for filtering known debug messages. */
952 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++)
953 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
954 ctx
->Debug
.Namespaces
[0][s
][t
].IDs
= _mesa_NewHashTable();
955 assert(ctx
->Debug
.Namespaces
[0][s
][t
].IDs
);
957 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++)
958 make_empty_list(&ctx
->Debug
.Namespaces
[0][s
][t
].Severity
[sev
]);
963 * Loop through debug group stack tearing down states for
964 * filtering debug messages.
967 _mesa_free_errors_data(struct gl_context
*ctx
)
971 for (i
= 0; i
<= ctx
->Debug
.GroupStackDepth
; i
++) {
972 free_errors_data(ctx
, i
);
976 /**********************************************************************/
977 /** \name Diagnostics */
981 output_if_debug(const char *prefixString
, const char *outputString
,
984 static int debug
= -1;
985 static FILE *fout
= NULL
;
987 /* Init the local 'debug' var once.
988 * Note: the _mesa_init_debug() function should have been called
989 * by now so MESA_DEBUG_FLAGS will be initialized.
992 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
993 * etc to the named file. Otherwise, output to stderr.
995 const char *logFile
= _mesa_getenv("MESA_LOG_FILE");
997 fout
= fopen(logFile
, "w");
1001 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1002 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1007 /* in release builds, be silent unless MESA_DEBUG is set */
1008 debug
= _mesa_getenv("MESA_DEBUG") != NULL
;
1012 /* Now only print the string if we're required to do so. */
1014 fprintf(fout
, "%s: %s", prefixString
, outputString
);
1016 fprintf(fout
, "\n");
1019 #if defined(_WIN32) && !defined(_WIN32_WCE)
1020 /* stderr from windows applications without console is not usually
1021 * visible, so communicate with the debugger instead */
1024 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1025 OutputDebugStringA(buf
);
1032 * When a new type of error is recorded, print a message describing
1033 * previous errors which were accumulated.
1036 flush_delayed_errors( struct gl_context
*ctx
)
1038 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1040 if (ctx
->ErrorDebugCount
) {
1041 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1042 ctx
->ErrorDebugCount
,
1043 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
1045 output_if_debug("Mesa", s
, GL_TRUE
);
1047 ctx
->ErrorDebugCount
= 0;
1053 * Report a warning (a recoverable error condition) to stderr if
1054 * either DEBUG is defined or the MESA_DEBUG env var is set.
1056 * \param ctx GL context.
1057 * \param fmtString printf()-like format string.
1060 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1062 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1064 va_start( args
, fmtString
);
1065 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1069 flush_delayed_errors( ctx
);
1071 output_if_debug("Mesa warning", str
, GL_TRUE
);
1076 * Report an internal implementation problem.
1077 * Prints the message to stderr via fprintf().
1079 * \param ctx GL context.
1080 * \param fmtString problem description string.
1083 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1086 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1087 static int numCalls
= 0;
1091 if (numCalls
< 50) {
1094 va_start( args
, fmtString
);
1095 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1097 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1098 PACKAGE_VERSION
, str
);
1099 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1104 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1106 static GLint debug
= -1;
1108 /* Check debug environment variable only once:
1111 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
1114 if (debugEnv
&& strstr(debugEnv
, "silent"))
1127 if (ctx
->ErrorValue
!= error
||
1128 ctx
->ErrorDebugFmtString
!= fmtString
) {
1129 flush_delayed_errors( ctx
);
1130 ctx
->ErrorDebugFmtString
= fmtString
;
1131 ctx
->ErrorDebugCount
= 0;
1134 ctx
->ErrorDebugCount
++;
1140 _mesa_gl_debug(struct gl_context
*ctx
,
1142 enum mesa_debug_type type
,
1143 enum mesa_debug_severity severity
,
1144 const char *fmtString
, ...)
1146 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1152 va_start(args
, fmtString
);
1153 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1156 _mesa_log_msg(ctx
, MESA_DEBUG_SOURCE_API
, type
,
1157 *id
, severity
, len
, s
);
1162 * Record an OpenGL state error. These usually occur when the user
1163 * passes invalid parameters to a GL function.
1165 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1166 * run-time via the MESA_DEBUG environment variable), report the error with
1169 * \param ctx the GL context.
1170 * \param error the error value.
1171 * \param fmtString printf() style format string, followed by optional args
1174 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1176 GLboolean do_output
, do_log
;
1177 /* Ideally this would be set up by the caller, so that we had proper IDs
1178 * per different message.
1180 static GLuint error_msg_id
= 0;
1182 debug_get_id(&error_msg_id
);
1184 do_output
= should_output(ctx
, error
, fmtString
);
1185 do_log
= should_log(ctx
,
1186 MESA_DEBUG_SOURCE_API
,
1187 MESA_DEBUG_TYPE_ERROR
,
1189 MESA_DEBUG_SEVERITY_HIGH
);
1191 if (do_output
|| do_log
) {
1192 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1196 va_start(args
, fmtString
);
1197 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1200 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1201 /* Too long error message. Whoever calls _mesa_error should use
1202 * shorter strings. */
1207 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1208 _mesa_lookup_enum_by_nr(error
), s
);
1209 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1210 /* Same as above. */
1215 /* Print the error to stderr if needed. */
1217 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1220 /* Log the error via ARB_debug_output if needed.*/
1223 MESA_DEBUG_SOURCE_API
,
1224 MESA_DEBUG_TYPE_ERROR
,
1226 MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1230 /* Set the GL context error state for glGetError. */
1231 _mesa_record_error(ctx
, error
);
1236 * Report debug information. Print error message to stderr via fprintf().
1237 * No-op if DEBUG mode not enabled.
1239 * \param ctx GL context.
1240 * \param fmtString printf()-style format string, followed by optional args.
1243 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1246 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1248 va_start(args
, fmtString
);
1249 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1251 output_if_debug("Mesa", s
, GL_FALSE
);
1259 * Report debug information from the shader compiler via GL_ARB_debug_output.
1261 * \param ctx GL context.
1262 * \param type The namespace to which this message belongs.
1263 * \param id The message ID within the given namespace.
1264 * \param msg The message to output. Need not be null-terminated.
1265 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1268 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1269 const char *msg
, int len
)
1271 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1272 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1279 /* Truncate the message if necessary. */
1280 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1281 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1283 _mesa_log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);