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 debug_message_clear(struct gl_debug_msg
*msg
)
187 if (msg
->message
!= (char*)out_of_memory
)
194 debug_message_store(struct gl_debug_msg
*msg
,
195 enum mesa_debug_source source
,
196 enum mesa_debug_type type
, GLuint id
,
197 enum mesa_debug_severity severity
,
198 GLsizei len
, const char *buf
)
200 assert(!msg
->message
&& !msg
->length
);
202 msg
->message
= malloc(len
+1);
204 (void) strncpy(msg
->message
, buf
, (size_t)len
);
205 msg
->message
[len
] = '\0';
208 msg
->source
= source
;
211 msg
->severity
= severity
;
213 static GLuint oom_msg_id
= 0;
214 debug_get_id(&oom_msg_id
);
217 msg
->message
= out_of_memory
;
218 msg
->length
= strlen(out_of_memory
)+1;
219 msg
->source
= MESA_DEBUG_SOURCE_OTHER
;
220 msg
->type
= MESA_DEBUG_TYPE_ERROR
;
221 msg
->id
= oom_msg_id
;
222 msg
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
227 * Allocate and initialize context debug state.
229 static struct gl_debug_state
*
232 struct gl_debug_state
*debug
;
235 debug
= CALLOC_STRUCT(gl_debug_state
);
239 /* Enable all the messages with severity HIGH or MEDIUM by default. */
240 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
], GL_TRUE
,
241 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
]);
242 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
], GL_TRUE
,
243 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
]);
244 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_LOW
], GL_FALSE
,
245 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_LOW
]);
247 /* Initialize state for filtering known debug messages. */
248 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
249 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
250 debug
->Namespaces
[0][s
][t
].IDs
= _mesa_NewHashTable();
251 assert(debug
->Namespaces
[0][s
][t
].IDs
);
253 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
254 make_empty_list(&debug
->Namespaces
[0][s
][t
].Severity
[sev
]);
263 debug_clear_group_cb(GLuint key
, void *data
, void *userData
)
268 * Free debug state for the given stack depth.
271 debug_clear_group(struct gl_debug_state
*debug
, GLint gstack
)
273 enum mesa_debug_type t
;
274 enum mesa_debug_source s
;
275 enum mesa_debug_severity sev
;
277 /* Tear down state for filtering debug messages. */
278 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
279 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
280 struct gl_debug_namespace
*nspace
= &debug
->Namespaces
[gstack
][s
][t
];
282 _mesa_HashDeleteAll(nspace
->IDs
, debug_clear_group_cb
, NULL
);
283 _mesa_DeleteHashTable(nspace
->IDs
);
284 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
285 struct simple_node
*node
, *tmp
;
286 struct gl_debug_severity
*entry
;
288 foreach_s(node
, tmp
, &nspace
->Severity
[sev
]) {
289 entry
= (struct gl_debug_severity
*)node
;
298 * Loop through debug group stack tearing down states for
299 * filtering debug messages. Then free debug output state.
302 debug_destroy(struct gl_debug_state
*debug
)
306 for (i
= 0; i
<= debug
->GroupStackDepth
; i
++)
307 debug_clear_group(debug
, i
);
313 * Sets the state of the given message source/type/ID tuple.
316 debug_set_message_enable(struct gl_debug_state
*debug
,
317 enum mesa_debug_source source
,
318 enum mesa_debug_type type
,
319 GLuint id
, GLboolean enabled
)
321 GLint gstack
= debug
->GroupStackDepth
;
322 struct gl_debug_namespace
*nspace
=
323 &debug
->Namespaces
[gstack
][source
][type
];
326 /* In addition to not being able to store zero as a value, HashTable also
327 * can't use zero as a key.
330 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
332 state
= nspace
->ZeroID
;
334 if (state
== NOT_FOUND
)
335 state
= enabled
? ENABLED
: DISABLED
;
338 state
|= ENABLED_BIT
;
340 state
&= ~ENABLED_BIT
;
344 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
346 nspace
->ZeroID
= state
;
350 * Set the state of all message IDs found in the given intersection of
351 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
352 * GL_DONT_CARE (include all messages in the class).
354 * This requires both setting the state of all previously seen message
355 * IDs in the hash table, and setting the default state for all
356 * applicable combinations of source/type/severity, so that all the
357 * yet-unknown message IDs that may be used in the future will be
358 * impacted as if they were already known.
361 debug_set_message_enable_all(struct gl_debug_state
*debug
,
362 enum mesa_debug_source source
,
363 enum mesa_debug_type type
,
364 enum mesa_debug_severity severity
,
367 const GLint gstack
= debug
->GroupStackDepth
;
368 int s
, t
, sev
, smax
, tmax
, sevmax
;
370 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
372 smax
= MESA_DEBUG_SOURCE_COUNT
;
377 if (type
== MESA_DEBUG_TYPE_COUNT
) {
379 tmax
= MESA_DEBUG_TYPE_COUNT
;
384 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
386 sevmax
= MESA_DEBUG_SEVERITY_COUNT
;
391 for (sev
= severity
; sev
< sevmax
; sev
++) {
392 for (s
= source
; s
< smax
; s
++) {
393 for (t
= type
; t
< tmax
; t
++) {
394 struct simple_node
*node
;
395 struct gl_debug_severity
*entry
;
397 /* change the default for IDs we've never seen before. */
398 debug
->Defaults
[gstack
][sev
][s
][t
] = enabled
;
400 /* Now change the state of IDs we *have* seen... */
401 foreach(node
, &debug
->Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
402 entry
= (struct gl_debug_severity
*)node
;
403 debug_set_message_enable(debug
, s
, t
, entry
->ID
, enabled
);
411 * Returns if the given message source/type/ID tuple is enabled.
414 debug_is_message_enabled(struct gl_debug_state
*debug
,
415 enum mesa_debug_source source
,
416 enum mesa_debug_type type
,
418 enum mesa_debug_severity severity
)
420 const GLint gstack
= debug
->GroupStackDepth
;
421 struct gl_debug_namespace
*nspace
=
422 &debug
->Namespaces
[gstack
][source
][type
];
425 if (!debug
->DebugOutput
)
428 /* In addition to not being able to store zero as a value, HashTable also
429 * can't use zero as a key.
432 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
434 state
= nspace
->ZeroID
;
436 /* Only do this once for each ID. This makes sure the ID exists in,
437 * at most, one list, and does not pointlessly appear multiple times.
439 if (!(state
& KNOWN_SEVERITY
)) {
440 struct gl_debug_severity
*entry
;
442 if (state
== NOT_FOUND
) {
443 if (debug
->Defaults
[gstack
][severity
][source
][type
])
449 entry
= malloc(sizeof *entry
);
453 state
|= KNOWN_SEVERITY
;
456 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
458 nspace
->ZeroID
= state
;
461 insert_at_tail(&nspace
->Severity
[severity
], &entry
->link
);
464 return (state
& ENABLED_BIT
);
468 * 'buf' is not necessarily a null-terminated string. When logging, copy
469 * 'len' characters from it, store them in a new, null-terminated string,
470 * and remember the number of bytes used by that string, *including*
471 * the null terminator this time.
474 debug_log_message(struct gl_debug_state
*debug
,
475 enum mesa_debug_source source
,
476 enum mesa_debug_type type
, GLuint id
,
477 enum mesa_debug_severity severity
,
478 GLsizei len
, const char *buf
)
481 struct gl_debug_msg
*emptySlot
;
483 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
485 if (debug
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
488 nextEmpty
= (debug
->NextMsg
+ debug
->NumMessages
)
489 % MAX_DEBUG_LOGGED_MESSAGES
;
490 emptySlot
= &debug
->Log
[nextEmpty
];
492 debug_message_store(emptySlot
, source
, type
,
493 id
, severity
, len
, buf
);
495 if (debug
->NumMessages
== 0)
496 debug
->NextMsgLength
= debug
->Log
[debug
->NextMsg
].length
;
498 debug
->NumMessages
++;
502 * Return the oldest debug message out of the log.
504 static const struct gl_debug_msg
*
505 debug_fetch_message(const struct gl_debug_state
*debug
)
507 return (debug
->NumMessages
) ? &debug
->Log
[debug
->NextMsg
] : NULL
;
511 * Delete the oldest debug messages out of the log.
514 debug_delete_messages(struct gl_debug_state
*debug
, unsigned count
)
516 if (count
> debug
->NumMessages
)
517 count
= debug
->NumMessages
;
520 struct gl_debug_msg
*msg
= &debug
->Log
[debug
->NextMsg
];
522 assert(msg
->length
> 0 && msg
->length
== debug
->NextMsgLength
);
523 debug_message_clear(msg
);
525 debug
->NumMessages
--;
527 debug
->NextMsg
%= MAX_DEBUG_LOGGED_MESSAGES
;
528 debug
->NextMsgLength
= debug
->Log
[debug
->NextMsg
].length
;
532 static struct gl_debug_msg
*
533 debug_get_group_message(struct gl_debug_state
*debug
)
535 return &debug
->DebugGroupMsgs
[debug
->GroupStackDepth
];
539 debug_push_group(struct gl_debug_state
*debug
)
541 const GLint gstack
= debug
->GroupStackDepth
;
544 /* inherit the control volume of the debug group previously residing on
545 * the top of the debug group stack
547 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
548 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
549 const struct gl_debug_namespace
*nspace
=
550 &debug
->Namespaces
[gstack
][s
][t
];
551 struct gl_debug_namespace
*next
=
552 &debug
->Namespaces
[gstack
+ 1][s
][t
];
554 /* copy id settings */
555 next
->IDs
= _mesa_HashClone(nspace
->IDs
);
557 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
558 struct simple_node
*node
;
560 /* copy default settings for unknown ids */
561 debug
->Defaults
[gstack
+ 1][sev
][s
][t
] =
562 debug
->Defaults
[gstack
][sev
][s
][t
];
564 /* copy known id severity settings */
565 make_empty_list(&next
->Severity
[sev
]);
566 foreach(node
, &nspace
->Severity
[sev
]) {
567 const struct gl_debug_severity
*entry
=
568 (const struct gl_debug_severity
*) node
;
569 struct gl_debug_severity
*copy
;
571 copy
= malloc(sizeof *entry
);
575 copy
->ID
= entry
->ID
;
576 insert_at_tail(&next
->Severity
[sev
], ©
->link
);
583 debug
->GroupStackDepth
++;
587 debug_pop_group(struct gl_debug_state
*debug
)
589 const GLint gstack
= debug
->GroupStackDepth
;
591 debug
->GroupStackDepth
--;
592 debug_clear_group(debug
, gstack
);
597 * Return debug state for the context. The debug state will be allocated
598 * and initialized upon the first call.
600 struct gl_debug_state
*
601 _mesa_get_debug_state(struct gl_context
*ctx
)
604 ctx
->Debug
= debug_create();
606 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
615 * Log a client or driver debug message.
618 log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
619 enum mesa_debug_type type
, GLuint id
,
620 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
622 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
627 if (!debug_is_message_enabled(debug
, source
, type
, id
, severity
))
630 if (debug
->Callback
) {
631 GLenum gl_type
= debug_type_enums
[type
];
632 GLenum gl_severity
= debug_severity_enums
[severity
];
634 debug
->Callback(debug_source_enums
[source
], gl_type
, id
, gl_severity
,
635 len
, buf
, debug
->CallbackData
);
639 debug_log_message(debug
, source
, type
, id
, severity
, len
, buf
);
644 * Verify that source, type, and severity are valid enums.
646 * The 'caller' param is used for handling values available
647 * only in glDebugMessageInsert or glDebugMessageControl
650 validate_params(struct gl_context
*ctx
, unsigned caller
,
651 const char *callerstr
, GLenum source
, GLenum type
,
657 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
658 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
660 case GL_DEBUG_SOURCE_API_ARB
:
661 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
662 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
663 case GL_DEBUG_SOURCE_OTHER_ARB
:
664 if (caller
!= INSERT
)
669 if (caller
== CONTROL
)
678 case GL_DEBUG_TYPE_ERROR_ARB
:
679 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
680 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
681 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
682 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
683 case GL_DEBUG_TYPE_OTHER_ARB
:
684 case GL_DEBUG_TYPE_MARKER
:
686 case GL_DEBUG_TYPE_PUSH_GROUP
:
687 case GL_DEBUG_TYPE_POP_GROUP
:
689 if (caller
== CONTROL
)
698 case GL_DEBUG_SEVERITY_HIGH_ARB
:
699 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
700 case GL_DEBUG_SEVERITY_LOW_ARB
:
701 case GL_DEBUG_SEVERITY_NOTIFICATION
:
704 if (caller
== CONTROL
)
714 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
715 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
716 source
, type
, severity
);
723 * This is a generic message insert function.
724 * Validation of source, type and severity parameters should be done
725 * before calling this funtion.
728 message_insert(GLenum source
, GLenum type
, GLuint id
,
729 GLenum severity
, GLint length
, const GLchar
*buf
,
730 const char *callerstr
)
732 GET_CURRENT_CONTEXT(ctx
);
735 length
= strlen(buf
);
737 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
738 _mesa_error(ctx
, GL_INVALID_VALUE
,
739 "%s(length=%d, which is not less than "
740 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
741 MAX_DEBUG_MESSAGE_LENGTH
);
746 gl_enum_to_debug_source(source
),
747 gl_enum_to_debug_type(type
), id
,
748 gl_enum_to_debug_severity(severity
), length
, buf
);
753 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
754 GLenum severity
, GLint length
,
757 const char *callerstr
= "glDebugMessageInsert";
759 GET_CURRENT_CONTEXT(ctx
);
761 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
762 return; /* GL_INVALID_ENUM */
764 message_insert(source
, type
, id
, severity
, length
, buf
, callerstr
);
769 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
770 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
771 GLsizei
*lengths
, GLchar
*messageLog
)
773 GET_CURRENT_CONTEXT(ctx
);
774 struct gl_debug_state
*debug
;
781 _mesa_error(ctx
, GL_INVALID_VALUE
,
782 "glGetDebugMessageLog(logSize=%d : logSize must not be"
783 " negative)", logSize
);
787 debug
= _mesa_get_debug_state(ctx
);
791 for (ret
= 0; ret
< count
; ret
++) {
792 const struct gl_debug_msg
*msg
= debug_fetch_message(debug
);
797 assert(msg
->length
> 0 && msg
->length
== debug
->NextMsgLength
);
799 if (logSize
< msg
->length
&& messageLog
!= NULL
)
803 assert(msg
->message
[msg
->length
-1] == '\0');
804 (void) strncpy(messageLog
, msg
->message
, (size_t)msg
->length
);
806 messageLog
+= msg
->length
;
807 logSize
-= msg
->length
;
811 *lengths
++ = msg
->length
;
813 *severities
++ = debug_severity_enums
[msg
->severity
];
815 *sources
++ = debug_source_enums
[msg
->source
];
817 *types
++ = debug_type_enums
[msg
->type
];
821 debug_delete_messages(debug
, 1);
829 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
830 GLenum gl_severity
, GLsizei count
,
831 const GLuint
*ids
, GLboolean enabled
)
833 GET_CURRENT_CONTEXT(ctx
);
834 enum mesa_debug_source source
= gl_enum_to_debug_source(gl_source
);
835 enum mesa_debug_type type
= gl_enum_to_debug_type(gl_type
);
836 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(gl_severity
);
837 const char *callerstr
= "glDebugMessageControl";
838 struct gl_debug_state
*debug
;
841 _mesa_error(ctx
, GL_INVALID_VALUE
,
842 "%s(count=%d : count must not be negative)", callerstr
,
847 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
849 return; /* GL_INVALID_ENUM */
851 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
852 || gl_source
== GL_DONT_CARE
)) {
853 _mesa_error(ctx
, GL_INVALID_OPERATION
,
854 "%s(When passing an array of ids, severity must be"
855 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
860 debug
= _mesa_get_debug_state(ctx
);
866 for (i
= 0; i
< count
; i
++)
867 debug_set_message_enable(debug
, source
, type
, ids
[i
], enabled
);
870 debug_set_message_enable_all(debug
, source
, type
, severity
, enabled
);
876 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
878 GET_CURRENT_CONTEXT(ctx
);
879 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
881 debug
->Callback
= callback
;
882 debug
->CallbackData
= userParam
;
888 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
889 const GLchar
*message
)
891 GET_CURRENT_CONTEXT(ctx
);
892 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
893 const char *callerstr
= "glPushDebugGroup";
894 struct gl_debug_msg
*emptySlot
;
899 if (debug
->GroupStackDepth
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
900 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
905 case GL_DEBUG_SOURCE_APPLICATION
:
906 case GL_DEBUG_SOURCE_THIRD_PARTY
:
909 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
910 "(source=0x%x)", callerstr
, source
);
915 length
= strlen(message
);
917 message_insert(source
, GL_DEBUG_TYPE_PUSH_GROUP
, id
,
918 GL_DEBUG_SEVERITY_NOTIFICATION
, length
,
921 /* pop reuses the message details from push so we store this */
922 emptySlot
= debug_get_group_message(debug
);
923 debug_message_store(emptySlot
,
924 gl_enum_to_debug_source(source
),
925 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
927 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
930 debug_push_group(debug
);
935 _mesa_PopDebugGroup(void)
937 GET_CURRENT_CONTEXT(ctx
);
938 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
939 const char *callerstr
= "glPopDebugGroup";
940 struct gl_debug_msg
*gdmessage
;
945 if (debug
->GroupStackDepth
<= 0) {
946 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
950 debug_pop_group(debug
);
952 gdmessage
= debug_get_group_message(debug
);
953 /* using log_msg() directly here as verification of parameters
954 * already done in push
956 log_msg(ctx
, gdmessage
->source
,
957 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
959 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
960 gdmessage
->length
, gdmessage
->message
);
962 debug_message_clear(gdmessage
);
967 _mesa_init_errors(struct gl_context
*ctx
)
974 _mesa_free_errors_data(struct gl_context
*ctx
)
977 debug_destroy(ctx
->Debug
);
978 /* set to NULL just in case it is used before context is completely gone. */
984 /**********************************************************************/
985 /** \name Diagnostics */
989 output_if_debug(const char *prefixString
, const char *outputString
,
992 static int debug
= -1;
993 static FILE *fout
= NULL
;
995 /* Init the local 'debug' var once.
996 * Note: the _mesa_init_debug() function should have been called
997 * by now so MESA_DEBUG_FLAGS will be initialized.
1000 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1001 * etc to the named file. Otherwise, output to stderr.
1003 const char *logFile
= _mesa_getenv("MESA_LOG_FILE");
1005 fout
= fopen(logFile
, "w");
1009 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1010 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1015 /* in release builds, be silent unless MESA_DEBUG is set */
1016 debug
= _mesa_getenv("MESA_DEBUG") != NULL
;
1020 /* Now only print the string if we're required to do so. */
1022 fprintf(fout
, "%s: %s", prefixString
, outputString
);
1024 fprintf(fout
, "\n");
1027 #if defined(_WIN32) && !defined(_WIN32_WCE)
1028 /* stderr from windows applications without console is not usually
1029 * visible, so communicate with the debugger instead */
1032 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1033 OutputDebugStringA(buf
);
1041 * When a new type of error is recorded, print a message describing
1042 * previous errors which were accumulated.
1045 flush_delayed_errors( struct gl_context
*ctx
)
1047 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1049 if (ctx
->ErrorDebugCount
) {
1050 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1051 ctx
->ErrorDebugCount
,
1052 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
1054 output_if_debug("Mesa", s
, GL_TRUE
);
1056 ctx
->ErrorDebugCount
= 0;
1062 * Report a warning (a recoverable error condition) to stderr if
1063 * either DEBUG is defined or the MESA_DEBUG env var is set.
1065 * \param ctx GL context.
1066 * \param fmtString printf()-like format string.
1069 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1071 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1073 va_start( args
, fmtString
);
1074 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1078 flush_delayed_errors( ctx
);
1080 output_if_debug("Mesa warning", str
, GL_TRUE
);
1085 * Report an internal implementation problem.
1086 * Prints the message to stderr via fprintf().
1088 * \param ctx GL context.
1089 * \param fmtString problem description string.
1092 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1095 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1096 static int numCalls
= 0;
1100 if (numCalls
< 50) {
1103 va_start( args
, fmtString
);
1104 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1106 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1107 PACKAGE_VERSION
, str
);
1108 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1114 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1116 static GLint debug
= -1;
1118 /* Check debug environment variable only once:
1121 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
1124 if (debugEnv
&& strstr(debugEnv
, "silent"))
1137 if (ctx
->ErrorValue
!= error
||
1138 ctx
->ErrorDebugFmtString
!= fmtString
) {
1139 flush_delayed_errors( ctx
);
1140 ctx
->ErrorDebugFmtString
= fmtString
;
1141 ctx
->ErrorDebugCount
= 0;
1144 ctx
->ErrorDebugCount
++;
1151 _mesa_gl_debug(struct gl_context
*ctx
,
1153 enum mesa_debug_type type
,
1154 enum mesa_debug_severity severity
,
1155 const char *fmtString
, ...)
1157 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1163 va_start(args
, fmtString
);
1164 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1167 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, type
, *id
, severity
, len
, s
);
1172 * Record an OpenGL state error. These usually occur when the user
1173 * passes invalid parameters to a GL function.
1175 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1176 * run-time via the MESA_DEBUG environment variable), report the error with
1179 * \param ctx the GL context.
1180 * \param error the error value.
1181 * \param fmtString printf() style format string, followed by optional args
1184 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1186 GLboolean do_output
, do_log
;
1187 /* Ideally this would be set up by the caller, so that we had proper IDs
1188 * per different message.
1190 static GLuint error_msg_id
= 0;
1192 debug_get_id(&error_msg_id
);
1194 do_output
= should_output(ctx
, error
, fmtString
);
1196 do_log
= debug_is_message_enabled(ctx
->Debug
,
1197 MESA_DEBUG_SOURCE_API
,
1198 MESA_DEBUG_TYPE_ERROR
,
1200 MESA_DEBUG_SEVERITY_HIGH
);
1206 if (do_output
|| do_log
) {
1207 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1211 va_start(args
, fmtString
);
1212 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1215 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1216 /* Too long error message. Whoever calls _mesa_error should use
1223 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1224 _mesa_lookup_enum_by_nr(error
), s
);
1225 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1226 /* Same as above. */
1231 /* Print the error to stderr if needed. */
1233 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1236 /* Log the error via ARB_debug_output if needed.*/
1238 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_ERROR
,
1239 error_msg_id
, MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1243 /* Set the GL context error state for glGetError. */
1244 _mesa_record_error(ctx
, error
);
1249 * Report debug information. Print error message to stderr via fprintf().
1250 * No-op if DEBUG mode not enabled.
1252 * \param ctx GL context.
1253 * \param fmtString printf()-style format string, followed by optional args.
1256 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1259 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1261 va_start(args
, fmtString
);
1262 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1264 output_if_debug("Mesa", s
, GL_FALSE
);
1272 * Report debug information from the shader compiler via GL_ARB_debug_output.
1274 * \param ctx GL context.
1275 * \param type The namespace to which this message belongs.
1276 * \param id The message ID within the given namespace.
1277 * \param msg The message to output. Need not be null-terminated.
1278 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1281 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1282 const char *msg
, int len
)
1284 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1285 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1292 /* Truncate the message if necessary. */
1293 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1294 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1296 log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);