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 struct gl_debug_namespace
52 struct _mesa_HashTable
*IDs
;
53 unsigned ZeroID
; /* a HashTable won't take zero, so store its state here */
54 /** lists of IDs in the hash table at each severity */
55 struct simple_node Severity
[MESA_DEBUG_SEVERITY_COUNT
];
57 GLboolean Defaults
[MESA_DEBUG_SEVERITY_COUNT
];
60 struct gl_debug_group
{
61 struct gl_debug_namespace Namespaces
[MESA_DEBUG_SOURCE_COUNT
][MESA_DEBUG_TYPE_COUNT
];
65 * An error, warning, or other piece of debug information for an application
66 * to consume via GL_ARB_debug_output/GL_KHR_debug.
68 struct gl_debug_message
70 enum mesa_debug_source source
;
71 enum mesa_debug_type type
;
73 enum mesa_debug_severity severity
;
79 * Debug message log. It works like a ring buffer.
82 struct gl_debug_message Messages
[MAX_DEBUG_LOGGED_MESSAGES
];
90 const void *CallbackData
;
92 GLboolean DebugOutput
;
94 struct gl_debug_group Groups
[MAX_DEBUG_GROUP_STACK_DEPTH
];
95 struct gl_debug_message GroupMessages
[MAX_DEBUG_GROUP_STACK_DEPTH
];
96 GLint GroupStackDepth
;
98 struct gl_debug_log Log
;
101 static char out_of_memory
[] = "Debugging error: out of memory";
103 static const GLenum debug_source_enums
[] = {
105 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
106 GL_DEBUG_SOURCE_SHADER_COMPILER
,
107 GL_DEBUG_SOURCE_THIRD_PARTY
,
108 GL_DEBUG_SOURCE_APPLICATION
,
109 GL_DEBUG_SOURCE_OTHER
,
112 static const GLenum debug_type_enums
[] = {
114 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
115 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
116 GL_DEBUG_TYPE_PORTABILITY
,
117 GL_DEBUG_TYPE_PERFORMANCE
,
119 GL_DEBUG_TYPE_MARKER
,
120 GL_DEBUG_TYPE_PUSH_GROUP
,
121 GL_DEBUG_TYPE_POP_GROUP
,
124 static const GLenum debug_severity_enums
[] = {
125 GL_DEBUG_SEVERITY_LOW
,
126 GL_DEBUG_SEVERITY_MEDIUM
,
127 GL_DEBUG_SEVERITY_HIGH
,
128 GL_DEBUG_SEVERITY_NOTIFICATION
,
132 static enum mesa_debug_source
133 gl_enum_to_debug_source(GLenum e
)
137 for (i
= 0; i
< Elements(debug_source_enums
); i
++) {
138 if (debug_source_enums
[i
] == e
)
144 static enum mesa_debug_type
145 gl_enum_to_debug_type(GLenum e
)
149 for (i
= 0; i
< Elements(debug_type_enums
); i
++) {
150 if (debug_type_enums
[i
] == e
)
156 static enum mesa_debug_severity
157 gl_enum_to_debug_severity(GLenum e
)
161 for (i
= 0; i
< Elements(debug_severity_enums
); i
++) {
162 if (debug_severity_enums
[i
] == e
)
170 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
173 * The GL API has this "ID" mechanism, where the intention is to allow a
174 * client to filter in/out messages based on source, type, and ID. Of course,
175 * building a giant enum list of all debug output messages that Mesa might
176 * generate is ridiculous, so instead we have our caller pass us a pointer to
177 * static storage where the ID should get stored. This ID will be shared
178 * across all contexts for that message (which seems like a desirable
179 * property, even if it's not expected by the spec), but note that it won't be
180 * the same between executions if messages aren't generated in the same order.
183 debug_get_id(GLuint
*id
)
186 mtx_lock(&DynamicIDMutex
);
188 *id
= NextDynamicID
++;
189 mtx_unlock(&DynamicIDMutex
);
195 * We store a bitfield in the hash table, with five possible values total.
197 * The ENABLED_BIT's purpose is self-explanatory.
199 * The FOUND_BIT is needed to differentiate the value of DISABLED from
200 * the value returned by HashTableLookup() when it can't find the given key.
202 * The KNOWN_SEVERITY bit is a bit complicated:
204 * A client may call Control() with an array of IDs, then call Control()
205 * on all message IDs of a certain severity, then Insert() one of the
206 * previously specified IDs, giving us a known severity level, then call
207 * Control() on all message IDs of a certain severity level again.
209 * After the first call, those IDs will have a FOUND_BIT, but will not
210 * exist in any severity-specific list, so the second call will not
211 * impact them. This is undesirable but unavoidable given the API:
212 * The only entrypoint that gives a severity for a client-defined ID
213 * is the Insert() call.
215 * For the sake of Control(), we want to maintain the invariant
216 * that an ID will either appear in none of the three severity lists,
217 * or appear once, to minimize pointless duplication and potential surprises.
219 * Because Insert() is the only place that will learn an ID's severity,
220 * it should insert an ID into the appropriate list, but only if the ID
221 * doesn't exist in it or any other list yet. Because searching all three
222 * lists at O(n) is needlessly expensive, we store KNOWN_SEVERITY.
226 ENABLED_BIT
= 1 << 1,
227 KNOWN_SEVERITY
= 1 << 2,
229 /* HashTable reserves zero as a return value meaning 'not found' */
231 DISABLED
= FOUND_BIT
,
232 ENABLED
= ENABLED_BIT
| FOUND_BIT
236 debug_message_clear(struct gl_debug_message
*msg
)
238 if (msg
->message
!= (char*)out_of_memory
)
245 debug_message_store(struct gl_debug_message
*msg
,
246 enum mesa_debug_source source
,
247 enum mesa_debug_type type
, GLuint id
,
248 enum mesa_debug_severity severity
,
249 GLsizei len
, const char *buf
)
251 assert(!msg
->message
&& !msg
->length
);
253 msg
->message
= malloc(len
+1);
255 (void) strncpy(msg
->message
, buf
, (size_t)len
);
256 msg
->message
[len
] = '\0';
259 msg
->source
= source
;
262 msg
->severity
= severity
;
264 static GLuint oom_msg_id
= 0;
265 debug_get_id(&oom_msg_id
);
268 msg
->message
= out_of_memory
;
269 msg
->length
= strlen(out_of_memory
)+1;
270 msg
->source
= MESA_DEBUG_SOURCE_OTHER
;
271 msg
->type
= MESA_DEBUG_TYPE_ERROR
;
272 msg
->id
= oom_msg_id
;
273 msg
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
278 * Allocate and initialize context debug state.
280 static struct gl_debug_state
*
283 struct gl_debug_state
*debug
;
286 debug
= CALLOC_STRUCT(gl_debug_state
);
290 /* Initialize state for filtering known debug messages. */
291 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
292 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
293 struct gl_debug_namespace
*nspace
=
294 &debug
->Groups
[0].Namespaces
[s
][t
];
296 nspace
->IDs
= _mesa_NewHashTable();
299 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++)
300 make_empty_list(&nspace
->Severity
[sev
]);
302 /* Enable all the messages with severity HIGH or MEDIUM by default */
303 nspace
->Defaults
[MESA_DEBUG_SEVERITY_HIGH
] = GL_TRUE
;
304 nspace
->Defaults
[MESA_DEBUG_SEVERITY_MEDIUM
] = GL_TRUE
;
312 debug_clear_group_cb(GLuint key
, void *data
, void *userData
)
317 * Free debug state for the given stack depth.
320 debug_clear_group(struct gl_debug_state
*debug
, GLint gstack
)
322 struct gl_debug_group
*grp
= &debug
->Groups
[gstack
];
323 enum mesa_debug_type t
;
324 enum mesa_debug_source s
;
325 enum mesa_debug_severity sev
;
327 /* Tear down state for filtering debug messages. */
328 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
329 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
330 struct gl_debug_namespace
*nspace
= &grp
->Namespaces
[s
][t
];
332 _mesa_HashDeleteAll(nspace
->IDs
, debug_clear_group_cb
, NULL
);
333 _mesa_DeleteHashTable(nspace
->IDs
);
334 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
335 struct simple_node
*node
, *tmp
;
336 struct gl_debug_severity
*entry
;
338 foreach_s(node
, tmp
, &nspace
->Severity
[sev
]) {
339 entry
= (struct gl_debug_severity
*)node
;
348 * Loop through debug group stack tearing down states for
349 * filtering debug messages. Then free debug output state.
352 debug_destroy(struct gl_debug_state
*debug
)
356 for (i
= 0; i
<= debug
->GroupStackDepth
; i
++)
357 debug_clear_group(debug
, i
);
363 * Sets the state of the given message source/type/ID tuple.
366 debug_set_message_enable(struct gl_debug_state
*debug
,
367 enum mesa_debug_source source
,
368 enum mesa_debug_type type
,
369 GLuint id
, GLboolean enabled
)
371 const GLint gstack
= debug
->GroupStackDepth
;
372 struct gl_debug_namespace
*nspace
=
373 &debug
->Groups
[gstack
].Namespaces
[source
][type
];
376 /* In addition to not being able to store zero as a value, HashTable also
377 * can't use zero as a key.
380 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
382 state
= nspace
->ZeroID
;
384 if (state
== NOT_FOUND
)
385 state
= enabled
? ENABLED
: DISABLED
;
388 state
|= ENABLED_BIT
;
390 state
&= ~ENABLED_BIT
;
394 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
396 nspace
->ZeroID
= state
;
400 * Set the state of all message IDs found in the given intersection of
401 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
402 * GL_DONT_CARE (include all messages in the class).
404 * This requires both setting the state of all previously seen message
405 * IDs in the hash table, and setting the default state for all
406 * applicable combinations of source/type/severity, so that all the
407 * yet-unknown message IDs that may be used in the future will be
408 * impacted as if they were already known.
411 debug_set_message_enable_all(struct gl_debug_state
*debug
,
412 enum mesa_debug_source source
,
413 enum mesa_debug_type type
,
414 enum mesa_debug_severity severity
,
417 const GLint gstack
= debug
->GroupStackDepth
;
418 int s
, t
, sev
, smax
, tmax
, sevmax
;
420 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
422 smax
= MESA_DEBUG_SOURCE_COUNT
;
427 if (type
== MESA_DEBUG_TYPE_COUNT
) {
429 tmax
= MESA_DEBUG_TYPE_COUNT
;
434 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
436 sevmax
= MESA_DEBUG_SEVERITY_COUNT
;
441 for (s
= source
; s
< smax
; s
++) {
442 for (t
= type
; t
< tmax
; t
++) {
443 struct gl_debug_namespace
*nspace
=
444 &debug
->Groups
[gstack
].Namespaces
[s
][t
];
446 for (sev
= severity
; sev
< sevmax
; sev
++) {
447 struct simple_node
*node
;
448 struct gl_debug_severity
*entry
;
450 /* change the default for IDs we've never seen before. */
451 nspace
->Defaults
[sev
] = enabled
;
453 /* Now change the state of IDs we *have* seen... */
454 foreach(node
, &nspace
->Severity
[sev
]) {
455 entry
= (struct gl_debug_severity
*)node
;
456 debug_set_message_enable(debug
, s
, t
, entry
->ID
, enabled
);
464 * Returns if the given message source/type/ID tuple is enabled.
467 debug_is_message_enabled(struct gl_debug_state
*debug
,
468 enum mesa_debug_source source
,
469 enum mesa_debug_type type
,
471 enum mesa_debug_severity severity
)
473 const GLint gstack
= debug
->GroupStackDepth
;
474 struct gl_debug_group
*grp
= &debug
->Groups
[gstack
];
475 struct gl_debug_namespace
*nspace
= &grp
->Namespaces
[source
][type
];
478 if (!debug
->DebugOutput
)
481 /* In addition to not being able to store zero as a value, HashTable also
482 * can't use zero as a key.
485 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
487 state
= nspace
->ZeroID
;
489 /* Only do this once for each ID. This makes sure the ID exists in,
490 * at most, one list, and does not pointlessly appear multiple times.
492 if (!(state
& KNOWN_SEVERITY
)) {
493 struct gl_debug_severity
*entry
;
495 if (state
== NOT_FOUND
) {
496 if (nspace
->Defaults
[severity
])
502 entry
= malloc(sizeof *entry
);
506 state
|= KNOWN_SEVERITY
;
509 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
511 nspace
->ZeroID
= state
;
514 insert_at_tail(&nspace
->Severity
[severity
], &entry
->link
);
517 return (state
& ENABLED_BIT
);
521 * 'buf' is not necessarily a null-terminated string. When logging, copy
522 * 'len' characters from it, store them in a new, null-terminated string,
523 * and remember the number of bytes used by that string, *including*
524 * the null terminator this time.
527 debug_log_message(struct gl_debug_state
*debug
,
528 enum mesa_debug_source source
,
529 enum mesa_debug_type type
, GLuint id
,
530 enum mesa_debug_severity severity
,
531 GLsizei len
, const char *buf
)
533 struct gl_debug_log
*log
= &debug
->Log
;
535 struct gl_debug_message
*emptySlot
;
537 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
539 if (log
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
542 nextEmpty
= (log
->NextMessage
+ log
->NumMessages
)
543 % MAX_DEBUG_LOGGED_MESSAGES
;
544 emptySlot
= &log
->Messages
[nextEmpty
];
546 debug_message_store(emptySlot
, source
, type
,
547 id
, severity
, len
, buf
);
553 * Return the oldest debug message out of the log.
555 static const struct gl_debug_message
*
556 debug_fetch_message(const struct gl_debug_state
*debug
)
558 const struct gl_debug_log
*log
= &debug
->Log
;
560 return (log
->NumMessages
) ? &log
->Messages
[log
->NextMessage
] : NULL
;
564 * Delete the oldest debug messages out of the log.
567 debug_delete_messages(struct gl_debug_state
*debug
, unsigned count
)
569 struct gl_debug_log
*log
= &debug
->Log
;
571 if (count
> log
->NumMessages
)
572 count
= log
->NumMessages
;
575 struct gl_debug_message
*msg
= &log
->Messages
[log
->NextMessage
];
577 debug_message_clear(msg
);
581 log
->NextMessage
%= MAX_DEBUG_LOGGED_MESSAGES
;
585 static struct gl_debug_message
*
586 debug_get_group_message(struct gl_debug_state
*debug
)
588 return &debug
->GroupMessages
[debug
->GroupStackDepth
];
592 debug_push_group(struct gl_debug_state
*debug
)
594 const GLint gstack
= debug
->GroupStackDepth
;
597 /* inherit the control volume of the debug group previously residing on
598 * the top of the debug group stack
600 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
601 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
602 const struct gl_debug_namespace
*nspace
=
603 &debug
->Groups
[gstack
].Namespaces
[s
][t
];
604 struct gl_debug_namespace
*next
=
605 &debug
->Groups
[gstack
+ 1].Namespaces
[s
][t
];
607 /* copy id settings */
608 next
->IDs
= _mesa_HashClone(nspace
->IDs
);
610 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
611 struct simple_node
*node
;
613 /* copy default settings for unknown ids */
614 next
->Defaults
[sev
] = nspace
->Defaults
[sev
];
616 /* copy known id severity settings */
617 make_empty_list(&next
->Severity
[sev
]);
618 foreach(node
, &nspace
->Severity
[sev
]) {
619 const struct gl_debug_severity
*entry
=
620 (const struct gl_debug_severity
*) node
;
621 struct gl_debug_severity
*copy
;
623 copy
= malloc(sizeof *entry
);
627 copy
->ID
= entry
->ID
;
628 insert_at_tail(&next
->Severity
[sev
], ©
->link
);
635 debug
->GroupStackDepth
++;
639 debug_pop_group(struct gl_debug_state
*debug
)
641 const GLint gstack
= debug
->GroupStackDepth
;
643 debug
->GroupStackDepth
--;
644 debug_clear_group(debug
, gstack
);
649 * Return debug state for the context. The debug state will be allocated
650 * and initialized upon the first call.
652 static struct gl_debug_state
*
653 _mesa_get_debug_state(struct gl_context
*ctx
)
656 ctx
->Debug
= debug_create();
658 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
666 * Set the integer debug state specified by \p pname. This can be called from
667 * _mesa_set_enable for example.
670 _mesa_set_debug_state_int(struct gl_context
*ctx
, GLenum pname
, GLint val
)
672 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
678 case GL_DEBUG_OUTPUT
:
679 debug
->DebugOutput
= (val
!= 0);
681 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
682 debug
->SyncOutput
= (val
!= 0);
685 assert(!"unknown debug output param");
693 * Query the integer debug state specified by \p pname. This can be called
694 * _mesa_GetIntegerv for example.
697 _mesa_get_debug_state_int(struct gl_context
*ctx
, GLenum pname
)
699 struct gl_debug_state
*debug
;
707 case GL_DEBUG_OUTPUT
:
708 val
= debug
->DebugOutput
;
710 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
711 val
= debug
->SyncOutput
;
713 case GL_DEBUG_LOGGED_MESSAGES
:
714 val
= debug
->Log
.NumMessages
;
716 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH
:
717 val
= (debug
->Log
.NumMessages
) ?
718 debug
->Log
.Messages
[debug
->Log
.NextMessage
].length
: 0;
720 case GL_DEBUG_GROUP_STACK_DEPTH
:
721 val
= debug
->GroupStackDepth
;
724 assert(!"unknown debug output param");
733 * Query the pointer debug state specified by \p pname. This can be called
734 * _mesa_GetPointerv for example.
737 _mesa_get_debug_state_ptr(struct gl_context
*ctx
, GLenum pname
)
739 struct gl_debug_state
*debug
;
747 case GL_DEBUG_CALLBACK_FUNCTION_ARB
:
748 val
= (void *) debug
->Callback
;
750 case GL_DEBUG_CALLBACK_USER_PARAM_ARB
:
751 val
= (void *) debug
->CallbackData
;
754 assert(!"unknown debug output param");
764 * Log a client or driver debug message.
767 log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
768 enum mesa_debug_type type
, GLuint id
,
769 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
771 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
776 if (!debug_is_message_enabled(debug
, source
, type
, id
, severity
))
779 if (debug
->Callback
) {
780 GLenum gl_type
= debug_type_enums
[type
];
781 GLenum gl_severity
= debug_severity_enums
[severity
];
783 debug
->Callback(debug_source_enums
[source
], gl_type
, id
, gl_severity
,
784 len
, buf
, debug
->CallbackData
);
788 debug_log_message(debug
, source
, type
, id
, severity
, len
, buf
);
793 * Verify that source, type, and severity are valid enums.
795 * The 'caller' param is used for handling values available
796 * only in glDebugMessageInsert or glDebugMessageControl
799 validate_params(struct gl_context
*ctx
, unsigned caller
,
800 const char *callerstr
, GLenum source
, GLenum type
,
806 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
807 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
809 case GL_DEBUG_SOURCE_API_ARB
:
810 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
811 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
812 case GL_DEBUG_SOURCE_OTHER_ARB
:
813 if (caller
!= INSERT
)
818 if (caller
== CONTROL
)
827 case GL_DEBUG_TYPE_ERROR_ARB
:
828 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
829 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
830 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
831 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
832 case GL_DEBUG_TYPE_OTHER_ARB
:
833 case GL_DEBUG_TYPE_MARKER
:
835 case GL_DEBUG_TYPE_PUSH_GROUP
:
836 case GL_DEBUG_TYPE_POP_GROUP
:
838 if (caller
== CONTROL
)
847 case GL_DEBUG_SEVERITY_HIGH_ARB
:
848 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
849 case GL_DEBUG_SEVERITY_LOW_ARB
:
850 case GL_DEBUG_SEVERITY_NOTIFICATION
:
853 if (caller
== CONTROL
)
863 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
864 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
865 source
, type
, severity
);
872 validate_length(struct gl_context
*ctx
, const char *callerstr
, GLsizei length
)
874 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
875 _mesa_error(ctx
, GL_INVALID_VALUE
,
876 "%s(length=%d, which is not less than "
877 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
878 MAX_DEBUG_MESSAGE_LENGTH
);
887 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
888 GLenum severity
, GLint length
,
891 const char *callerstr
= "glDebugMessageInsert";
893 GET_CURRENT_CONTEXT(ctx
);
895 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
896 return; /* GL_INVALID_ENUM */
899 length
= strlen(buf
);
900 if (!validate_length(ctx
, callerstr
, length
))
901 return; /* GL_INVALID_VALUE */
903 log_msg(ctx
, gl_enum_to_debug_source(source
),
904 gl_enum_to_debug_type(type
), id
,
905 gl_enum_to_debug_severity(severity
),
911 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
912 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
913 GLsizei
*lengths
, GLchar
*messageLog
)
915 GET_CURRENT_CONTEXT(ctx
);
916 struct gl_debug_state
*debug
;
923 _mesa_error(ctx
, GL_INVALID_VALUE
,
924 "glGetDebugMessageLog(logSize=%d : logSize must not be"
925 " negative)", logSize
);
929 debug
= _mesa_get_debug_state(ctx
);
933 for (ret
= 0; ret
< count
; ret
++) {
934 const struct gl_debug_message
*msg
= debug_fetch_message(debug
);
939 if (logSize
< msg
->length
&& messageLog
!= NULL
)
943 assert(msg
->message
[msg
->length
-1] == '\0');
944 (void) strncpy(messageLog
, msg
->message
, (size_t)msg
->length
);
946 messageLog
+= msg
->length
;
947 logSize
-= msg
->length
;
951 *lengths
++ = msg
->length
;
953 *severities
++ = debug_severity_enums
[msg
->severity
];
955 *sources
++ = debug_source_enums
[msg
->source
];
957 *types
++ = debug_type_enums
[msg
->type
];
961 debug_delete_messages(debug
, 1);
969 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
970 GLenum gl_severity
, GLsizei count
,
971 const GLuint
*ids
, GLboolean enabled
)
973 GET_CURRENT_CONTEXT(ctx
);
974 enum mesa_debug_source source
= gl_enum_to_debug_source(gl_source
);
975 enum mesa_debug_type type
= gl_enum_to_debug_type(gl_type
);
976 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(gl_severity
);
977 const char *callerstr
= "glDebugMessageControl";
978 struct gl_debug_state
*debug
;
981 _mesa_error(ctx
, GL_INVALID_VALUE
,
982 "%s(count=%d : count must not be negative)", callerstr
,
987 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
989 return; /* GL_INVALID_ENUM */
991 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
992 || gl_source
== GL_DONT_CARE
)) {
993 _mesa_error(ctx
, GL_INVALID_OPERATION
,
994 "%s(When passing an array of ids, severity must be"
995 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
1000 debug
= _mesa_get_debug_state(ctx
);
1006 for (i
= 0; i
< count
; i
++)
1007 debug_set_message_enable(debug
, source
, type
, ids
[i
], enabled
);
1010 debug_set_message_enable_all(debug
, source
, type
, severity
, enabled
);
1016 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
1018 GET_CURRENT_CONTEXT(ctx
);
1019 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
1021 debug
->Callback
= callback
;
1022 debug
->CallbackData
= userParam
;
1028 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
1029 const GLchar
*message
)
1031 GET_CURRENT_CONTEXT(ctx
);
1032 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
1033 const char *callerstr
= "glPushDebugGroup";
1034 struct gl_debug_message
*emptySlot
;
1039 if (debug
->GroupStackDepth
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
1040 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
1045 case GL_DEBUG_SOURCE_APPLICATION
:
1046 case GL_DEBUG_SOURCE_THIRD_PARTY
:
1049 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
1050 "(source=0x%x)", callerstr
, source
);
1055 length
= strlen(message
);
1056 if (!validate_length(ctx
, callerstr
, length
))
1057 return; /* GL_INVALID_VALUE */
1059 log_msg(ctx
, gl_enum_to_debug_source(source
),
1060 MESA_DEBUG_TYPE_PUSH_GROUP
, id
,
1061 MESA_DEBUG_SEVERITY_NOTIFICATION
, length
,
1064 /* pop reuses the message details from push so we store this */
1065 emptySlot
= debug_get_group_message(debug
);
1066 debug_message_store(emptySlot
,
1067 gl_enum_to_debug_source(source
),
1068 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
1070 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1073 debug_push_group(debug
);
1078 _mesa_PopDebugGroup(void)
1080 GET_CURRENT_CONTEXT(ctx
);
1081 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
1082 const char *callerstr
= "glPopDebugGroup";
1083 struct gl_debug_message
*gdmessage
;
1088 if (debug
->GroupStackDepth
<= 0) {
1089 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
1093 debug_pop_group(debug
);
1095 gdmessage
= debug_get_group_message(debug
);
1096 log_msg(ctx
, gdmessage
->source
,
1097 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
1099 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1100 gdmessage
->length
, gdmessage
->message
);
1102 debug_message_clear(gdmessage
);
1107 _mesa_init_errors(struct gl_context
*ctx
)
1114 _mesa_free_errors_data(struct gl_context
*ctx
)
1117 debug_destroy(ctx
->Debug
);
1118 /* set to NULL just in case it is used before context is completely gone. */
1124 /**********************************************************************/
1125 /** \name Diagnostics */
1129 output_if_debug(const char *prefixString
, const char *outputString
,
1132 static int debug
= -1;
1133 static FILE *fout
= NULL
;
1135 /* Init the local 'debug' var once.
1136 * Note: the _mesa_init_debug() function should have been called
1137 * by now so MESA_DEBUG_FLAGS will be initialized.
1140 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1141 * etc to the named file. Otherwise, output to stderr.
1143 const char *logFile
= _mesa_getenv("MESA_LOG_FILE");
1145 fout
= fopen(logFile
, "w");
1149 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1150 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1155 /* in release builds, be silent unless MESA_DEBUG is set */
1156 debug
= _mesa_getenv("MESA_DEBUG") != NULL
;
1160 /* Now only print the string if we're required to do so. */
1162 fprintf(fout
, "%s: %s", prefixString
, outputString
);
1164 fprintf(fout
, "\n");
1167 #if defined(_WIN32) && !defined(_WIN32_WCE)
1168 /* stderr from windows applications without console is not usually
1169 * visible, so communicate with the debugger instead */
1172 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1173 OutputDebugStringA(buf
);
1181 * When a new type of error is recorded, print a message describing
1182 * previous errors which were accumulated.
1185 flush_delayed_errors( struct gl_context
*ctx
)
1187 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1189 if (ctx
->ErrorDebugCount
) {
1190 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1191 ctx
->ErrorDebugCount
,
1192 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
1194 output_if_debug("Mesa", s
, GL_TRUE
);
1196 ctx
->ErrorDebugCount
= 0;
1202 * Report a warning (a recoverable error condition) to stderr if
1203 * either DEBUG is defined or the MESA_DEBUG env var is set.
1205 * \param ctx GL context.
1206 * \param fmtString printf()-like format string.
1209 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1211 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1213 va_start( args
, fmtString
);
1214 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1218 flush_delayed_errors( ctx
);
1220 output_if_debug("Mesa warning", str
, GL_TRUE
);
1225 * Report an internal implementation problem.
1226 * Prints the message to stderr via fprintf().
1228 * \param ctx GL context.
1229 * \param fmtString problem description string.
1232 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1235 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1236 static int numCalls
= 0;
1240 if (numCalls
< 50) {
1243 va_start( args
, fmtString
);
1244 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1246 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1247 PACKAGE_VERSION
, str
);
1248 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1254 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1256 static GLint debug
= -1;
1258 /* Check debug environment variable only once:
1261 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
1264 if (debugEnv
&& strstr(debugEnv
, "silent"))
1277 if (ctx
->ErrorValue
!= error
||
1278 ctx
->ErrorDebugFmtString
!= fmtString
) {
1279 flush_delayed_errors( ctx
);
1280 ctx
->ErrorDebugFmtString
= fmtString
;
1281 ctx
->ErrorDebugCount
= 0;
1284 ctx
->ErrorDebugCount
++;
1291 _mesa_gl_debug(struct gl_context
*ctx
,
1293 enum mesa_debug_type type
,
1294 enum mesa_debug_severity severity
,
1295 const char *fmtString
, ...)
1297 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1303 va_start(args
, fmtString
);
1304 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1307 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, type
, *id
, severity
, len
, s
);
1312 * Record an OpenGL state error. These usually occur when the user
1313 * passes invalid parameters to a GL function.
1315 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1316 * run-time via the MESA_DEBUG environment variable), report the error with
1319 * \param ctx the GL context.
1320 * \param error the error value.
1321 * \param fmtString printf() style format string, followed by optional args
1324 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1326 GLboolean do_output
, do_log
;
1327 /* Ideally this would be set up by the caller, so that we had proper IDs
1328 * per different message.
1330 static GLuint error_msg_id
= 0;
1332 debug_get_id(&error_msg_id
);
1334 do_output
= should_output(ctx
, error
, fmtString
);
1336 do_log
= debug_is_message_enabled(ctx
->Debug
,
1337 MESA_DEBUG_SOURCE_API
,
1338 MESA_DEBUG_TYPE_ERROR
,
1340 MESA_DEBUG_SEVERITY_HIGH
);
1346 if (do_output
|| do_log
) {
1347 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1351 va_start(args
, fmtString
);
1352 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1355 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1356 /* Too long error message. Whoever calls _mesa_error should use
1363 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1364 _mesa_lookup_enum_by_nr(error
), s
);
1365 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1366 /* Same as above. */
1371 /* Print the error to stderr if needed. */
1373 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1376 /* Log the error via ARB_debug_output if needed.*/
1378 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_ERROR
,
1379 error_msg_id
, MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1383 /* Set the GL context error state for glGetError. */
1384 _mesa_record_error(ctx
, error
);
1389 * Report debug information. Print error message to stderr via fprintf().
1390 * No-op if DEBUG mode not enabled.
1392 * \param ctx GL context.
1393 * \param fmtString printf()-style format string, followed by optional args.
1396 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1399 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1401 va_start(args
, fmtString
);
1402 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1404 output_if_debug("Mesa", s
, GL_FALSE
);
1412 * Report debug information from the shader compiler via GL_ARB_debug_output.
1414 * \param ctx GL context.
1415 * \param type The namespace to which this message belongs.
1416 * \param id The message ID within the given namespace.
1417 * \param msg The message to output. Need not be null-terminated.
1418 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1421 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1422 const char *msg
, int len
)
1424 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1425 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1432 /* Truncate the message if necessary. */
1433 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1434 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1436 log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);