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 "util/hash_table.h"
41 static mtx_t DynamicIDMutex
= _MTX_INITIALIZER_NP
;
42 static GLuint NextDynamicID
= 1;
45 * A namespace element.
47 struct gl_debug_element
49 struct simple_node link
;
52 /* at which severity levels (mesa_debug_severity) is the message enabled */
56 struct gl_debug_namespace
58 struct simple_node Elements
;
59 GLbitfield DefaultState
;
62 struct gl_debug_group
{
63 struct gl_debug_namespace Namespaces
[MESA_DEBUG_SOURCE_COUNT
][MESA_DEBUG_TYPE_COUNT
];
67 * An error, warning, or other piece of debug information for an application
68 * to consume via GL_ARB_debug_output/GL_KHR_debug.
70 struct gl_debug_message
72 enum mesa_debug_source source
;
73 enum mesa_debug_type type
;
75 enum mesa_debug_severity severity
;
81 * Debug message log. It works like a ring buffer.
84 struct gl_debug_message Messages
[MAX_DEBUG_LOGGED_MESSAGES
];
92 const void *CallbackData
;
94 GLboolean DebugOutput
;
96 struct gl_debug_group
*Groups
[MAX_DEBUG_GROUP_STACK_DEPTH
];
97 struct gl_debug_message GroupMessages
[MAX_DEBUG_GROUP_STACK_DEPTH
];
98 GLint GroupStackDepth
;
100 struct gl_debug_log Log
;
103 static char out_of_memory
[] = "Debugging error: out of memory";
105 static const GLenum debug_source_enums
[] = {
107 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
108 GL_DEBUG_SOURCE_SHADER_COMPILER
,
109 GL_DEBUG_SOURCE_THIRD_PARTY
,
110 GL_DEBUG_SOURCE_APPLICATION
,
111 GL_DEBUG_SOURCE_OTHER
,
114 static const GLenum debug_type_enums
[] = {
116 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
117 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
118 GL_DEBUG_TYPE_PORTABILITY
,
119 GL_DEBUG_TYPE_PERFORMANCE
,
121 GL_DEBUG_TYPE_MARKER
,
122 GL_DEBUG_TYPE_PUSH_GROUP
,
123 GL_DEBUG_TYPE_POP_GROUP
,
126 static const GLenum debug_severity_enums
[] = {
127 GL_DEBUG_SEVERITY_LOW
,
128 GL_DEBUG_SEVERITY_MEDIUM
,
129 GL_DEBUG_SEVERITY_HIGH
,
130 GL_DEBUG_SEVERITY_NOTIFICATION
,
134 static enum mesa_debug_source
135 gl_enum_to_debug_source(GLenum e
)
139 for (i
= 0; i
< Elements(debug_source_enums
); i
++) {
140 if (debug_source_enums
[i
] == e
)
146 static enum mesa_debug_type
147 gl_enum_to_debug_type(GLenum e
)
151 for (i
= 0; i
< Elements(debug_type_enums
); i
++) {
152 if (debug_type_enums
[i
] == e
)
158 static enum mesa_debug_severity
159 gl_enum_to_debug_severity(GLenum e
)
163 for (i
= 0; i
< Elements(debug_severity_enums
); i
++) {
164 if (debug_severity_enums
[i
] == e
)
172 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
175 * The GL API has this "ID" mechanism, where the intention is to allow a
176 * client to filter in/out messages based on source, type, and ID. Of course,
177 * building a giant enum list of all debug output messages that Mesa might
178 * generate is ridiculous, so instead we have our caller pass us a pointer to
179 * static storage where the ID should get stored. This ID will be shared
180 * across all contexts for that message (which seems like a desirable
181 * property, even if it's not expected by the spec), but note that it won't be
182 * the same between executions if messages aren't generated in the same order.
185 debug_get_id(GLuint
*id
)
188 mtx_lock(&DynamicIDMutex
);
190 *id
= NextDynamicID
++;
191 mtx_unlock(&DynamicIDMutex
);
196 debug_message_clear(struct gl_debug_message
*msg
)
198 if (msg
->message
!= (char*)out_of_memory
)
205 debug_message_store(struct gl_debug_message
*msg
,
206 enum mesa_debug_source source
,
207 enum mesa_debug_type type
, GLuint id
,
208 enum mesa_debug_severity severity
,
209 GLsizei len
, const char *buf
)
211 assert(!msg
->message
&& !msg
->length
);
213 msg
->message
= malloc(len
+1);
215 (void) strncpy(msg
->message
, buf
, (size_t)len
);
216 msg
->message
[len
] = '\0';
219 msg
->source
= source
;
222 msg
->severity
= severity
;
224 static GLuint oom_msg_id
= 0;
225 debug_get_id(&oom_msg_id
);
228 msg
->message
= out_of_memory
;
229 msg
->length
= strlen(out_of_memory
)+1;
230 msg
->source
= MESA_DEBUG_SOURCE_OTHER
;
231 msg
->type
= MESA_DEBUG_TYPE_ERROR
;
232 msg
->id
= oom_msg_id
;
233 msg
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
238 debug_namespace_init(struct gl_debug_namespace
*ns
)
240 make_empty_list(&ns
->Elements
);
242 /* Enable all the messages with severity HIGH or MEDIUM by default */
243 ns
->DefaultState
= (1 << MESA_DEBUG_SEVERITY_HIGH
) |
244 (1 << MESA_DEBUG_SEVERITY_MEDIUM
);
248 debug_namespace_clear(struct gl_debug_namespace
*ns
)
250 struct simple_node
*node
, *tmp
;
252 foreach_s(node
, tmp
, &ns
->Elements
)
257 debug_namespace_copy(struct gl_debug_namespace
*dst
,
258 const struct gl_debug_namespace
*src
)
260 struct simple_node
*node
;
262 dst
->DefaultState
= src
->DefaultState
;
264 make_empty_list(&dst
->Elements
);
265 foreach(node
, &src
->Elements
) {
266 const struct gl_debug_element
*elem
=
267 (const struct gl_debug_element
*) node
;
268 struct gl_debug_element
*copy
;
270 copy
= malloc(sizeof(*copy
));
272 debug_namespace_clear(dst
);
277 copy
->State
= elem
->State
;
278 insert_at_tail(&dst
->Elements
, ©
->link
);
285 * Set the state of \p id in the namespace.
288 debug_namespace_set(struct gl_debug_namespace
*ns
,
289 GLuint id
, bool enabled
)
291 const uint32_t state
= (enabled
) ?
292 ((1 << MESA_DEBUG_SEVERITY_COUNT
) - 1) : 0;
293 struct gl_debug_element
*elem
= NULL
;
294 struct simple_node
*node
;
296 /* find the element */
297 foreach(node
, &ns
->Elements
) {
298 struct gl_debug_element
*tmp
= (struct gl_debug_element
*) node
;
305 /* we do not need the element if it has the default state */
306 if (ns
->DefaultState
== state
) {
308 remove_from_list(&elem
->link
);
315 elem
= malloc(sizeof(*elem
));
320 insert_at_tail(&ns
->Elements
, &elem
->link
);
329 * Set the default state of the namespace for \p severity. When \p severity
330 * is MESA_DEBUG_SEVERITY_COUNT, the default values for all severities are
334 debug_namespace_set_all(struct gl_debug_namespace
*ns
,
335 enum mesa_debug_severity severity
,
338 struct simple_node
*node
, *tmp
;
341 /* set all elements to the same state */
342 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
343 ns
->DefaultState
= (enabled
) ? ((1 << severity
) - 1) : 0;
344 debug_namespace_clear(ns
);
345 make_empty_list(&ns
->Elements
);
349 mask
= 1 << severity
;
350 val
= (enabled
) ? mask
: 0;
352 ns
->DefaultState
= (ns
->DefaultState
& ~mask
) | val
;
354 foreach_s(node
, tmp
, &ns
->Elements
) {
355 struct gl_debug_element
*elem
= (struct gl_debug_element
*) node
;
357 elem
->State
= (elem
->State
& ~mask
) | val
;
358 if (elem
->State
== ns
->DefaultState
) {
359 remove_from_list(node
);
366 * Get the state of \p id in the namespace.
369 debug_namespace_get(const struct gl_debug_namespace
*ns
, GLuint id
,
370 enum mesa_debug_severity severity
)
372 struct simple_node
*node
;
375 state
= ns
->DefaultState
;
376 foreach(node
, &ns
->Elements
) {
377 struct gl_debug_element
*elem
= (struct gl_debug_element
*) node
;
379 if (elem
->ID
== id
) {
385 return (state
& (1 << severity
));
389 * Allocate and initialize context debug state.
391 static struct gl_debug_state
*
394 struct gl_debug_state
*debug
;
397 debug
= CALLOC_STRUCT(gl_debug_state
);
401 debug
->Groups
[0] = malloc(sizeof(*debug
->Groups
[0]));
402 if (!debug
->Groups
[0]) {
407 /* Initialize state for filtering known debug messages. */
408 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
409 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
410 debug_namespace_init(&debug
->Groups
[0]->Namespaces
[s
][t
]);
417 * Return true if the top debug group points to the group below it.
420 debug_is_group_read_only(const struct gl_debug_state
*debug
)
422 const GLint gstack
= debug
->GroupStackDepth
;
423 return (gstack
> 0 && debug
->Groups
[gstack
] == debug
->Groups
[gstack
- 1]);
427 * Make the top debug group writable.
430 debug_make_group_writable(struct gl_debug_state
*debug
)
432 const GLint gstack
= debug
->GroupStackDepth
;
433 const struct gl_debug_group
*src
= debug
->Groups
[gstack
];
434 struct gl_debug_group
*dst
;
437 if (!debug_is_group_read_only(debug
))
440 dst
= malloc(sizeof(*dst
));
444 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
445 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
446 if (!debug_namespace_copy(&dst
->Namespaces
[s
][t
],
447 &src
->Namespaces
[s
][t
])) {
449 for (t
= t
- 1; t
>= 0; t
--)
450 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
451 for (s
= s
- 1; s
>= 0; s
--) {
452 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
453 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
461 debug
->Groups
[gstack
] = dst
;
467 * Free the top debug group.
470 debug_clear_group(struct gl_debug_state
*debug
)
472 const GLint gstack
= debug
->GroupStackDepth
;
474 if (!debug_is_group_read_only(debug
)) {
475 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
478 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
479 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
480 debug_namespace_clear(&grp
->Namespaces
[s
][t
]);
486 debug
->Groups
[gstack
] = NULL
;
490 * Loop through debug group stack tearing down states for
491 * filtering debug messages. Then free debug output state.
494 debug_destroy(struct gl_debug_state
*debug
)
496 while (debug
->GroupStackDepth
> 0) {
497 debug_clear_group(debug
);
498 debug
->GroupStackDepth
--;
501 debug_clear_group(debug
);
506 * Sets the state of the given message source/type/ID tuple.
509 debug_set_message_enable(struct gl_debug_state
*debug
,
510 enum mesa_debug_source source
,
511 enum mesa_debug_type type
,
512 GLuint id
, GLboolean enabled
)
514 const GLint gstack
= debug
->GroupStackDepth
;
515 struct gl_debug_namespace
*ns
;
517 debug_make_group_writable(debug
);
518 ns
= &debug
->Groups
[gstack
]->Namespaces
[source
][type
];
520 debug_namespace_set(ns
, id
, enabled
);
524 * Set the state of all message IDs found in the given intersection of
525 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
526 * GL_DONT_CARE (include all messages in the class).
528 * This requires both setting the state of all previously seen message
529 * IDs in the hash table, and setting the default state for all
530 * applicable combinations of source/type/severity, so that all the
531 * yet-unknown message IDs that may be used in the future will be
532 * impacted as if they were already known.
535 debug_set_message_enable_all(struct gl_debug_state
*debug
,
536 enum mesa_debug_source source
,
537 enum mesa_debug_type type
,
538 enum mesa_debug_severity severity
,
541 const GLint gstack
= debug
->GroupStackDepth
;
542 int s
, t
, smax
, tmax
;
544 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
546 smax
= MESA_DEBUG_SOURCE_COUNT
;
551 if (type
== MESA_DEBUG_TYPE_COUNT
) {
553 tmax
= MESA_DEBUG_TYPE_COUNT
;
558 debug_make_group_writable(debug
);
560 for (s
= source
; s
< smax
; s
++) {
561 for (t
= type
; t
< tmax
; t
++) {
562 struct gl_debug_namespace
*nspace
=
563 &debug
->Groups
[gstack
]->Namespaces
[s
][t
];
564 debug_namespace_set_all(nspace
, severity
, enabled
);
570 * Returns if the given message source/type/ID tuple is enabled.
573 debug_is_message_enabled(const struct gl_debug_state
*debug
,
574 enum mesa_debug_source source
,
575 enum mesa_debug_type type
,
577 enum mesa_debug_severity severity
)
579 const GLint gstack
= debug
->GroupStackDepth
;
580 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
581 struct gl_debug_namespace
*nspace
= &grp
->Namespaces
[source
][type
];
583 if (!debug
->DebugOutput
)
586 return debug_namespace_get(nspace
, id
, severity
);
590 * 'buf' is not necessarily a null-terminated string. When logging, copy
591 * 'len' characters from it, store them in a new, null-terminated string,
592 * and remember the number of bytes used by that string, *including*
593 * the null terminator this time.
596 debug_log_message(struct gl_debug_state
*debug
,
597 enum mesa_debug_source source
,
598 enum mesa_debug_type type
, GLuint id
,
599 enum mesa_debug_severity severity
,
600 GLsizei len
, const char *buf
)
602 struct gl_debug_log
*log
= &debug
->Log
;
604 struct gl_debug_message
*emptySlot
;
606 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
608 if (log
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
611 nextEmpty
= (log
->NextMessage
+ log
->NumMessages
)
612 % MAX_DEBUG_LOGGED_MESSAGES
;
613 emptySlot
= &log
->Messages
[nextEmpty
];
615 debug_message_store(emptySlot
, source
, type
,
616 id
, severity
, len
, buf
);
622 * Return the oldest debug message out of the log.
624 static const struct gl_debug_message
*
625 debug_fetch_message(const struct gl_debug_state
*debug
)
627 const struct gl_debug_log
*log
= &debug
->Log
;
629 return (log
->NumMessages
) ? &log
->Messages
[log
->NextMessage
] : NULL
;
633 * Delete the oldest debug messages out of the log.
636 debug_delete_messages(struct gl_debug_state
*debug
, int count
)
638 struct gl_debug_log
*log
= &debug
->Log
;
640 if (count
> log
->NumMessages
)
641 count
= log
->NumMessages
;
644 struct gl_debug_message
*msg
= &log
->Messages
[log
->NextMessage
];
646 debug_message_clear(msg
);
650 log
->NextMessage
%= MAX_DEBUG_LOGGED_MESSAGES
;
654 static struct gl_debug_message
*
655 debug_get_group_message(struct gl_debug_state
*debug
)
657 return &debug
->GroupMessages
[debug
->GroupStackDepth
];
661 debug_push_group(struct gl_debug_state
*debug
)
663 const GLint gstack
= debug
->GroupStackDepth
;
665 /* just point to the previous stack */
666 debug
->Groups
[gstack
+ 1] = debug
->Groups
[gstack
];
667 debug
->GroupStackDepth
++;
671 debug_pop_group(struct gl_debug_state
*debug
)
673 debug_clear_group(debug
);
674 debug
->GroupStackDepth
--;
679 * Lock and return debug state for the context. The debug state will be
680 * allocated and initialized upon the first call. When NULL is returned, the
681 * debug state is not locked.
683 static struct gl_debug_state
*
684 _mesa_lock_debug_state(struct gl_context
*ctx
)
686 mtx_lock(&ctx
->DebugMutex
);
689 ctx
->Debug
= debug_create();
691 GET_CURRENT_CONTEXT(cur
);
692 mtx_unlock(&ctx
->DebugMutex
);
695 * This function may be called from other threads. When that is the
696 * case, we cannot record this OOM error.
699 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
709 _mesa_unlock_debug_state(struct gl_context
*ctx
)
711 mtx_unlock(&ctx
->DebugMutex
);
715 * Set the integer debug state specified by \p pname. This can be called from
716 * _mesa_set_enable for example.
719 _mesa_set_debug_state_int(struct gl_context
*ctx
, GLenum pname
, GLint val
)
721 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
727 case GL_DEBUG_OUTPUT
:
728 debug
->DebugOutput
= (val
!= 0);
730 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
731 debug
->SyncOutput
= (val
!= 0);
734 assert(!"unknown debug output param");
738 _mesa_unlock_debug_state(ctx
);
744 * Query the integer debug state specified by \p pname. This can be called
745 * _mesa_GetIntegerv for example.
748 _mesa_get_debug_state_int(struct gl_context
*ctx
, GLenum pname
)
750 struct gl_debug_state
*debug
;
753 mtx_lock(&ctx
->DebugMutex
);
756 mtx_unlock(&ctx
->DebugMutex
);
761 case GL_DEBUG_OUTPUT
:
762 val
= debug
->DebugOutput
;
764 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
765 val
= debug
->SyncOutput
;
767 case GL_DEBUG_LOGGED_MESSAGES
:
768 val
= debug
->Log
.NumMessages
;
770 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH
:
771 val
= (debug
->Log
.NumMessages
) ?
772 debug
->Log
.Messages
[debug
->Log
.NextMessage
].length
: 0;
774 case GL_DEBUG_GROUP_STACK_DEPTH
:
775 val
= debug
->GroupStackDepth
;
778 assert(!"unknown debug output param");
783 mtx_unlock(&ctx
->DebugMutex
);
789 * Query the pointer debug state specified by \p pname. This can be called
790 * _mesa_GetPointerv for example.
793 _mesa_get_debug_state_ptr(struct gl_context
*ctx
, GLenum pname
)
795 struct gl_debug_state
*debug
;
798 mtx_lock(&ctx
->DebugMutex
);
801 mtx_unlock(&ctx
->DebugMutex
);
806 case GL_DEBUG_CALLBACK_FUNCTION_ARB
:
807 val
= (void *) debug
->Callback
;
809 case GL_DEBUG_CALLBACK_USER_PARAM_ARB
:
810 val
= (void *) debug
->CallbackData
;
813 assert(!"unknown debug output param");
818 mtx_unlock(&ctx
->DebugMutex
);
824 * Insert a debug message. The mutex is assumed to be locked, and will be
825 * unlocked by this call.
828 log_msg_locked_and_unlock(struct gl_context
*ctx
,
829 enum mesa_debug_source source
,
830 enum mesa_debug_type type
, GLuint id
,
831 enum mesa_debug_severity severity
,
832 GLint len
, const char *buf
)
834 struct gl_debug_state
*debug
= ctx
->Debug
;
836 if (!debug_is_message_enabled(debug
, source
, type
, id
, severity
)) {
837 _mesa_unlock_debug_state(ctx
);
841 if (ctx
->Debug
->Callback
) {
842 GLenum gl_source
= debug_source_enums
[source
];
843 GLenum gl_type
= debug_type_enums
[type
];
844 GLenum gl_severity
= debug_severity_enums
[severity
];
845 GLDEBUGPROC callback
= ctx
->Debug
->Callback
;
846 const void *data
= ctx
->Debug
->CallbackData
;
849 * When ctx->Debug->SyncOutput is GL_FALSE, the client is prepared for
850 * unsynchronous calls. When it is GL_TRUE, we will not spawn threads.
851 * In either case, we can call the callback unlocked.
853 _mesa_unlock_debug_state(ctx
);
854 callback(gl_source
, gl_type
, id
, gl_severity
, len
, buf
, data
);
857 debug_log_message(ctx
->Debug
, source
, type
, id
, severity
, len
, buf
);
858 _mesa_unlock_debug_state(ctx
);
863 * Log a client or driver debug message.
866 log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
867 enum mesa_debug_type type
, GLuint id
,
868 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
870 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
875 log_msg_locked_and_unlock(ctx
, source
, type
, id
, severity
, len
, buf
);
880 * Verify that source, type, and severity are valid enums.
882 * The 'caller' param is used for handling values available
883 * only in glDebugMessageInsert or glDebugMessageControl
886 validate_params(struct gl_context
*ctx
, unsigned caller
,
887 const char *callerstr
, GLenum source
, GLenum type
,
893 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
894 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
896 case GL_DEBUG_SOURCE_API_ARB
:
897 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
898 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
899 case GL_DEBUG_SOURCE_OTHER_ARB
:
900 if (caller
!= INSERT
)
905 if (caller
== CONTROL
)
914 case GL_DEBUG_TYPE_ERROR_ARB
:
915 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
916 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
917 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
918 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
919 case GL_DEBUG_TYPE_OTHER_ARB
:
920 case GL_DEBUG_TYPE_MARKER
:
922 case GL_DEBUG_TYPE_PUSH_GROUP
:
923 case GL_DEBUG_TYPE_POP_GROUP
:
925 if (caller
== CONTROL
)
934 case GL_DEBUG_SEVERITY_HIGH_ARB
:
935 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
936 case GL_DEBUG_SEVERITY_LOW_ARB
:
937 case GL_DEBUG_SEVERITY_NOTIFICATION
:
940 if (caller
== CONTROL
)
950 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
951 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
952 source
, type
, severity
);
959 validate_length(struct gl_context
*ctx
, const char *callerstr
, GLsizei length
)
961 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
962 _mesa_error(ctx
, GL_INVALID_VALUE
,
963 "%s(length=%d, which is not less than "
964 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
965 MAX_DEBUG_MESSAGE_LENGTH
);
974 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
975 GLenum severity
, GLint length
,
978 const char *callerstr
= "glDebugMessageInsert";
980 GET_CURRENT_CONTEXT(ctx
);
982 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
983 return; /* GL_INVALID_ENUM */
986 length
= strlen(buf
);
987 if (!validate_length(ctx
, callerstr
, length
))
988 return; /* GL_INVALID_VALUE */
990 log_msg(ctx
, gl_enum_to_debug_source(source
),
991 gl_enum_to_debug_type(type
), id
,
992 gl_enum_to_debug_severity(severity
),
998 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
999 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
1000 GLsizei
*lengths
, GLchar
*messageLog
)
1002 GET_CURRENT_CONTEXT(ctx
);
1003 struct gl_debug_state
*debug
;
1010 _mesa_error(ctx
, GL_INVALID_VALUE
,
1011 "glGetDebugMessageLog(logSize=%d : logSize must not be"
1012 " negative)", logSize
);
1016 debug
= _mesa_lock_debug_state(ctx
);
1020 for (ret
= 0; ret
< count
; ret
++) {
1021 const struct gl_debug_message
*msg
= debug_fetch_message(debug
);
1026 if (logSize
< msg
->length
&& messageLog
!= NULL
)
1030 assert(msg
->message
[msg
->length
-1] == '\0');
1031 (void) strncpy(messageLog
, msg
->message
, (size_t)msg
->length
);
1033 messageLog
+= msg
->length
;
1034 logSize
-= msg
->length
;
1038 *lengths
++ = msg
->length
;
1040 *severities
++ = debug_severity_enums
[msg
->severity
];
1042 *sources
++ = debug_source_enums
[msg
->source
];
1044 *types
++ = debug_type_enums
[msg
->type
];
1048 debug_delete_messages(debug
, 1);
1051 _mesa_unlock_debug_state(ctx
);
1058 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
1059 GLenum gl_severity
, GLsizei count
,
1060 const GLuint
*ids
, GLboolean enabled
)
1062 GET_CURRENT_CONTEXT(ctx
);
1063 enum mesa_debug_source source
= gl_enum_to_debug_source(gl_source
);
1064 enum mesa_debug_type type
= gl_enum_to_debug_type(gl_type
);
1065 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(gl_severity
);
1066 const char *callerstr
= "glDebugMessageControl";
1067 struct gl_debug_state
*debug
;
1070 _mesa_error(ctx
, GL_INVALID_VALUE
,
1071 "%s(count=%d : count must not be negative)", callerstr
,
1076 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
1078 return; /* GL_INVALID_ENUM */
1080 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
1081 || gl_source
== GL_DONT_CARE
)) {
1082 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1083 "%s(When passing an array of ids, severity must be"
1084 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
1089 debug
= _mesa_lock_debug_state(ctx
);
1095 for (i
= 0; i
< count
; i
++)
1096 debug_set_message_enable(debug
, source
, type
, ids
[i
], enabled
);
1099 debug_set_message_enable_all(debug
, source
, type
, severity
, enabled
);
1102 _mesa_unlock_debug_state(ctx
);
1107 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
1109 GET_CURRENT_CONTEXT(ctx
);
1110 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
1112 debug
->Callback
= callback
;
1113 debug
->CallbackData
= userParam
;
1114 _mesa_unlock_debug_state(ctx
);
1120 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
1121 const GLchar
*message
)
1123 GET_CURRENT_CONTEXT(ctx
);
1124 const char *callerstr
= "glPushDebugGroup";
1125 struct gl_debug_state
*debug
;
1126 struct gl_debug_message
*emptySlot
;
1129 case GL_DEBUG_SOURCE_APPLICATION
:
1130 case GL_DEBUG_SOURCE_THIRD_PARTY
:
1133 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
1134 "(source=0x%x)", callerstr
, source
);
1139 length
= strlen(message
);
1140 if (!validate_length(ctx
, callerstr
, length
))
1141 return; /* GL_INVALID_VALUE */
1143 debug
= _mesa_lock_debug_state(ctx
);
1147 if (debug
->GroupStackDepth
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
1148 _mesa_unlock_debug_state(ctx
);
1149 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
1153 /* pop reuses the message details from push so we store this */
1154 emptySlot
= debug_get_group_message(debug
);
1155 debug_message_store(emptySlot
,
1156 gl_enum_to_debug_source(source
),
1157 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
1159 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1162 debug_push_group(debug
);
1164 log_msg_locked_and_unlock(ctx
,
1165 gl_enum_to_debug_source(source
),
1166 MESA_DEBUG_TYPE_PUSH_GROUP
, id
,
1167 MESA_DEBUG_SEVERITY_NOTIFICATION
, length
,
1173 _mesa_PopDebugGroup(void)
1175 GET_CURRENT_CONTEXT(ctx
);
1176 const char *callerstr
= "glPopDebugGroup";
1177 struct gl_debug_state
*debug
;
1178 struct gl_debug_message
*gdmessage
, msg
;
1180 debug
= _mesa_lock_debug_state(ctx
);
1184 if (debug
->GroupStackDepth
<= 0) {
1185 _mesa_unlock_debug_state(ctx
);
1186 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
1190 debug_pop_group(debug
);
1192 /* make a shallow copy */
1193 gdmessage
= debug_get_group_message(debug
);
1195 gdmessage
->message
= NULL
;
1196 gdmessage
->length
= 0;
1198 log_msg_locked_and_unlock(ctx
,
1200 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
1202 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1203 msg
.length
, msg
.message
);
1205 debug_message_clear(&msg
);
1210 _mesa_init_errors(struct gl_context
*ctx
)
1212 mtx_init(&ctx
->DebugMutex
, mtx_plain
);
1217 _mesa_free_errors_data(struct gl_context
*ctx
)
1220 debug_destroy(ctx
->Debug
);
1221 /* set to NULL just in case it is used before context is completely gone. */
1225 mtx_destroy(&ctx
->DebugMutex
);
1229 /**********************************************************************/
1230 /** \name Diagnostics */
1234 output_if_debug(const char *prefixString
, const char *outputString
,
1237 static int debug
= -1;
1238 static FILE *fout
= NULL
;
1240 /* Init the local 'debug' var once.
1241 * Note: the _mesa_init_debug() function should have been called
1242 * by now so MESA_DEBUG_FLAGS will be initialized.
1245 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1246 * etc to the named file. Otherwise, output to stderr.
1248 const char *logFile
= getenv("MESA_LOG_FILE");
1250 fout
= fopen(logFile
, "w");
1254 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1255 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1260 /* in release builds, be silent unless MESA_DEBUG is set */
1261 debug
= getenv("MESA_DEBUG") != NULL
;
1265 /* Now only print the string if we're required to do so. */
1267 fprintf(fout
, "%s: %s", prefixString
, outputString
);
1269 fprintf(fout
, "\n");
1273 /* stderr from windows applications without console is not usually
1274 * visible, so communicate with the debugger instead */
1277 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1278 OutputDebugStringA(buf
);
1286 * When a new type of error is recorded, print a message describing
1287 * previous errors which were accumulated.
1290 flush_delayed_errors( struct gl_context
*ctx
)
1292 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1294 if (ctx
->ErrorDebugCount
) {
1295 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1296 ctx
->ErrorDebugCount
,
1297 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
1299 output_if_debug("Mesa", s
, GL_TRUE
);
1301 ctx
->ErrorDebugCount
= 0;
1307 * Report a warning (a recoverable error condition) to stderr if
1308 * either DEBUG is defined or the MESA_DEBUG env var is set.
1310 * \param ctx GL context.
1311 * \param fmtString printf()-like format string.
1314 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1316 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1318 va_start( args
, fmtString
);
1319 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1323 flush_delayed_errors( ctx
);
1325 output_if_debug("Mesa warning", str
, GL_TRUE
);
1330 * Report an internal implementation problem.
1331 * Prints the message to stderr via fprintf().
1333 * \param ctx GL context.
1334 * \param fmtString problem description string.
1337 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1340 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1341 static int numCalls
= 0;
1345 if (numCalls
< 50) {
1348 va_start( args
, fmtString
);
1349 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1351 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1352 PACKAGE_VERSION
, str
);
1353 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1359 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1361 static GLint debug
= -1;
1363 /* Check debug environment variable only once:
1366 const char *debugEnv
= getenv("MESA_DEBUG");
1369 if (debugEnv
&& strstr(debugEnv
, "silent"))
1382 if (ctx
->ErrorValue
!= error
||
1383 ctx
->ErrorDebugFmtString
!= fmtString
) {
1384 flush_delayed_errors( ctx
);
1385 ctx
->ErrorDebugFmtString
= fmtString
;
1386 ctx
->ErrorDebugCount
= 0;
1389 ctx
->ErrorDebugCount
++;
1396 _mesa_gl_debug(struct gl_context
*ctx
,
1398 enum mesa_debug_source source
,
1399 enum mesa_debug_type type
,
1400 enum mesa_debug_severity severity
,
1401 const char *fmtString
, ...)
1403 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1409 va_start(args
, fmtString
);
1410 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1413 log_msg(ctx
, source
, type
, *id
, severity
, len
, s
);
1418 * Record an OpenGL state error. These usually occur when the user
1419 * passes invalid parameters to a GL function.
1421 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1422 * run-time via the MESA_DEBUG environment variable), report the error with
1425 * \param ctx the GL context.
1426 * \param error the error value.
1427 * \param fmtString printf() style format string, followed by optional args
1430 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1432 GLboolean do_output
, do_log
;
1433 /* Ideally this would be set up by the caller, so that we had proper IDs
1434 * per different message.
1436 static GLuint error_msg_id
= 0;
1438 debug_get_id(&error_msg_id
);
1440 do_output
= should_output(ctx
, error
, fmtString
);
1442 mtx_lock(&ctx
->DebugMutex
);
1444 do_log
= debug_is_message_enabled(ctx
->Debug
,
1445 MESA_DEBUG_SOURCE_API
,
1446 MESA_DEBUG_TYPE_ERROR
,
1448 MESA_DEBUG_SEVERITY_HIGH
);
1453 mtx_unlock(&ctx
->DebugMutex
);
1455 if (do_output
|| do_log
) {
1456 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1460 va_start(args
, fmtString
);
1461 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1464 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1465 /* Too long error message. Whoever calls _mesa_error should use
1472 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1473 _mesa_lookup_enum_by_nr(error
), s
);
1474 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1475 /* Same as above. */
1480 /* Print the error to stderr if needed. */
1482 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1485 /* Log the error via ARB_debug_output if needed.*/
1487 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_ERROR
,
1488 error_msg_id
, MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1492 /* Set the GL context error state for glGetError. */
1493 _mesa_record_error(ctx
, error
);
1497 _mesa_error_no_memory(const char *caller
)
1499 GET_CURRENT_CONTEXT(ctx
);
1500 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "out of memory in %s", caller
);
1504 * Report debug information. Print error message to stderr via fprintf().
1505 * No-op if DEBUG mode not enabled.
1507 * \param ctx GL context.
1508 * \param fmtString printf()-style format string, followed by optional args.
1511 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1514 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1516 va_start(args
, fmtString
);
1517 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1519 output_if_debug("Mesa", s
, GL_FALSE
);
1527 * Report debug information from the shader compiler via GL_ARB_debug_output.
1529 * \param ctx GL context.
1530 * \param type The namespace to which this message belongs.
1531 * \param id The message ID within the given namespace.
1532 * \param msg The message to output. Need not be null-terminated.
1533 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1536 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1537 const char *msg
, int len
)
1539 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1540 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1547 /* Truncate the message if necessary. */
1548 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1549 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1551 log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);