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.
41 #include "util/hash_table.h"
42 #include "util/simple_list.h"
44 static mtx_t DynamicIDMutex
= _MTX_INITIALIZER_NP
;
45 static GLuint NextDynamicID
= 1;
48 * A namespace element.
50 struct gl_debug_element
52 struct simple_node link
;
55 /* at which severity levels (mesa_debug_severity) is the message enabled */
59 struct gl_debug_namespace
61 struct simple_node Elements
;
62 GLbitfield DefaultState
;
65 struct gl_debug_group
{
66 struct gl_debug_namespace Namespaces
[MESA_DEBUG_SOURCE_COUNT
][MESA_DEBUG_TYPE_COUNT
];
70 * An error, warning, or other piece of debug information for an application
71 * to consume via GL_ARB_debug_output/GL_KHR_debug.
73 struct gl_debug_message
75 enum mesa_debug_source source
;
76 enum mesa_debug_type type
;
78 enum mesa_debug_severity severity
;
79 /* length as given by the user - if message was explicitly null terminated,
80 * length can be negative */
86 * Debug message log. It works like a ring buffer.
89 struct gl_debug_message Messages
[MAX_DEBUG_LOGGED_MESSAGES
];
97 const void *CallbackData
;
99 GLboolean DebugOutput
;
101 struct gl_debug_group
*Groups
[MAX_DEBUG_GROUP_STACK_DEPTH
];
102 struct gl_debug_message GroupMessages
[MAX_DEBUG_GROUP_STACK_DEPTH
];
103 GLint CurrentGroup
; // GroupStackDepth - 1
105 struct gl_debug_log Log
;
108 static char out_of_memory
[] = "Debugging error: out of memory";
110 static const GLenum debug_source_enums
[] = {
112 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
113 GL_DEBUG_SOURCE_SHADER_COMPILER
,
114 GL_DEBUG_SOURCE_THIRD_PARTY
,
115 GL_DEBUG_SOURCE_APPLICATION
,
116 GL_DEBUG_SOURCE_OTHER
,
119 static const GLenum debug_type_enums
[] = {
121 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
122 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
123 GL_DEBUG_TYPE_PORTABILITY
,
124 GL_DEBUG_TYPE_PERFORMANCE
,
126 GL_DEBUG_TYPE_MARKER
,
127 GL_DEBUG_TYPE_PUSH_GROUP
,
128 GL_DEBUG_TYPE_POP_GROUP
,
131 static const GLenum debug_severity_enums
[] = {
132 GL_DEBUG_SEVERITY_LOW
,
133 GL_DEBUG_SEVERITY_MEDIUM
,
134 GL_DEBUG_SEVERITY_HIGH
,
135 GL_DEBUG_SEVERITY_NOTIFICATION
,
139 static enum mesa_debug_source
140 gl_enum_to_debug_source(GLenum e
)
144 for (i
= 0; i
< ARRAY_SIZE(debug_source_enums
); i
++) {
145 if (debug_source_enums
[i
] == e
)
151 static enum mesa_debug_type
152 gl_enum_to_debug_type(GLenum e
)
156 for (i
= 0; i
< ARRAY_SIZE(debug_type_enums
); i
++) {
157 if (debug_type_enums
[i
] == e
)
163 static enum mesa_debug_severity
164 gl_enum_to_debug_severity(GLenum e
)
168 for (i
= 0; i
< ARRAY_SIZE(debug_severity_enums
); i
++) {
169 if (debug_severity_enums
[i
] == e
)
177 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
180 * The GL API has this "ID" mechanism, where the intention is to allow a
181 * client to filter in/out messages based on source, type, and ID. Of course,
182 * building a giant enum list of all debug output messages that Mesa might
183 * generate is ridiculous, so instead we have our caller pass us a pointer to
184 * static storage where the ID should get stored. This ID will be shared
185 * across all contexts for that message (which seems like a desirable
186 * property, even if it's not expected by the spec), but note that it won't be
187 * the same between executions if messages aren't generated in the same order.
190 debug_get_id(GLuint
*id
)
193 mtx_lock(&DynamicIDMutex
);
195 *id
= NextDynamicID
++;
196 mtx_unlock(&DynamicIDMutex
);
201 debug_message_clear(struct gl_debug_message
*msg
)
203 if (msg
->message
!= (char*)out_of_memory
)
210 debug_message_store(struct gl_debug_message
*msg
,
211 enum mesa_debug_source source
,
212 enum mesa_debug_type type
, GLuint id
,
213 enum mesa_debug_severity severity
,
214 GLsizei len
, const char *buf
)
216 GLsizei length
= len
;
218 assert(!msg
->message
&& !msg
->length
);
221 length
= strlen(buf
);
223 msg
->message
= malloc(length
+1);
225 (void) strncpy(msg
->message
, buf
, (size_t)length
);
226 msg
->message
[length
] = '\0';
229 msg
->source
= source
;
232 msg
->severity
= severity
;
234 static GLuint oom_msg_id
= 0;
235 debug_get_id(&oom_msg_id
);
238 msg
->message
= out_of_memory
;
240 msg
->source
= MESA_DEBUG_SOURCE_OTHER
;
241 msg
->type
= MESA_DEBUG_TYPE_ERROR
;
242 msg
->id
= oom_msg_id
;
243 msg
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
248 debug_namespace_init(struct gl_debug_namespace
*ns
)
250 make_empty_list(&ns
->Elements
);
252 /* Enable all the messages with severity HIGH or MEDIUM by default */
253 ns
->DefaultState
= (1 << MESA_DEBUG_SEVERITY_MEDIUM
) |
254 (1 << MESA_DEBUG_SEVERITY_HIGH
) |
255 (1 << MESA_DEBUG_SEVERITY_NOTIFICATION
);
259 debug_namespace_clear(struct gl_debug_namespace
*ns
)
261 struct simple_node
*node
, *tmp
;
263 foreach_s(node
, tmp
, &ns
->Elements
)
268 debug_namespace_copy(struct gl_debug_namespace
*dst
,
269 const struct gl_debug_namespace
*src
)
271 struct simple_node
*node
;
273 dst
->DefaultState
= src
->DefaultState
;
275 make_empty_list(&dst
->Elements
);
276 foreach(node
, &src
->Elements
) {
277 const struct gl_debug_element
*elem
=
278 (const struct gl_debug_element
*) node
;
279 struct gl_debug_element
*copy
;
281 copy
= malloc(sizeof(*copy
));
283 debug_namespace_clear(dst
);
288 copy
->State
= elem
->State
;
289 insert_at_tail(&dst
->Elements
, ©
->link
);
296 * Set the state of \p id in the namespace.
299 debug_namespace_set(struct gl_debug_namespace
*ns
,
300 GLuint id
, bool enabled
)
302 const uint32_t state
= (enabled
) ?
303 ((1 << MESA_DEBUG_SEVERITY_COUNT
) - 1) : 0;
304 struct gl_debug_element
*elem
= NULL
;
305 struct simple_node
*node
;
307 /* find the element */
308 foreach(node
, &ns
->Elements
) {
309 struct gl_debug_element
*tmp
= (struct gl_debug_element
*) node
;
316 /* we do not need the element if it has the default state */
317 if (ns
->DefaultState
== state
) {
319 remove_from_list(&elem
->link
);
326 elem
= malloc(sizeof(*elem
));
331 insert_at_tail(&ns
->Elements
, &elem
->link
);
340 * Set the default state of the namespace for \p severity. When \p severity
341 * is MESA_DEBUG_SEVERITY_COUNT, the default values for all severities are
345 debug_namespace_set_all(struct gl_debug_namespace
*ns
,
346 enum mesa_debug_severity severity
,
349 struct simple_node
*node
, *tmp
;
352 /* set all elements to the same state */
353 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
354 ns
->DefaultState
= (enabled
) ? ((1 << severity
) - 1) : 0;
355 debug_namespace_clear(ns
);
356 make_empty_list(&ns
->Elements
);
360 mask
= 1 << severity
;
361 val
= (enabled
) ? mask
: 0;
363 ns
->DefaultState
= (ns
->DefaultState
& ~mask
) | val
;
365 foreach_s(node
, tmp
, &ns
->Elements
) {
366 struct gl_debug_element
*elem
= (struct gl_debug_element
*) node
;
368 elem
->State
= (elem
->State
& ~mask
) | val
;
369 if (elem
->State
== ns
->DefaultState
) {
370 remove_from_list(node
);
377 * Get the state of \p id in the namespace.
380 debug_namespace_get(const struct gl_debug_namespace
*ns
, GLuint id
,
381 enum mesa_debug_severity severity
)
383 struct simple_node
*node
;
386 state
= ns
->DefaultState
;
387 foreach(node
, &ns
->Elements
) {
388 struct gl_debug_element
*elem
= (struct gl_debug_element
*) node
;
390 if (elem
->ID
== id
) {
396 return (state
& (1 << severity
));
400 * Allocate and initialize context debug state.
402 static struct gl_debug_state
*
405 struct gl_debug_state
*debug
;
408 debug
= CALLOC_STRUCT(gl_debug_state
);
412 debug
->Groups
[0] = malloc(sizeof(*debug
->Groups
[0]));
413 if (!debug
->Groups
[0]) {
418 /* Initialize state for filtering known debug messages. */
419 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
420 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
421 debug_namespace_init(&debug
->Groups
[0]->Namespaces
[s
][t
]);
428 * Return true if the top debug group points to the group below it.
431 debug_is_group_read_only(const struct gl_debug_state
*debug
)
433 const GLint gstack
= debug
->CurrentGroup
;
434 return (gstack
> 0 && debug
->Groups
[gstack
] == debug
->Groups
[gstack
- 1]);
438 * Make the top debug group writable.
441 debug_make_group_writable(struct gl_debug_state
*debug
)
443 const GLint gstack
= debug
->CurrentGroup
;
444 const struct gl_debug_group
*src
= debug
->Groups
[gstack
];
445 struct gl_debug_group
*dst
;
448 if (!debug_is_group_read_only(debug
))
451 dst
= malloc(sizeof(*dst
));
455 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
456 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
457 if (!debug_namespace_copy(&dst
->Namespaces
[s
][t
],
458 &src
->Namespaces
[s
][t
])) {
460 for (t
= t
- 1; t
>= 0; t
--)
461 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
462 for (s
= s
- 1; s
>= 0; s
--) {
463 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
464 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
472 debug
->Groups
[gstack
] = dst
;
478 * Free the top debug group.
481 debug_clear_group(struct gl_debug_state
*debug
)
483 const GLint gstack
= debug
->CurrentGroup
;
485 if (!debug_is_group_read_only(debug
)) {
486 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
489 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
490 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
491 debug_namespace_clear(&grp
->Namespaces
[s
][t
]);
497 debug
->Groups
[gstack
] = NULL
;
501 * Loop through debug group stack tearing down states for
502 * filtering debug messages. Then free debug output state.
505 debug_destroy(struct gl_debug_state
*debug
)
507 while (debug
->CurrentGroup
> 0) {
508 debug_clear_group(debug
);
509 debug
->CurrentGroup
--;
512 debug_clear_group(debug
);
517 * Sets the state of the given message source/type/ID tuple.
520 debug_set_message_enable(struct gl_debug_state
*debug
,
521 enum mesa_debug_source source
,
522 enum mesa_debug_type type
,
523 GLuint id
, GLboolean enabled
)
525 const GLint gstack
= debug
->CurrentGroup
;
526 struct gl_debug_namespace
*ns
;
528 debug_make_group_writable(debug
);
529 ns
= &debug
->Groups
[gstack
]->Namespaces
[source
][type
];
531 debug_namespace_set(ns
, id
, enabled
);
535 * Set the state of all message IDs found in the given intersection of
536 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
537 * GL_DONT_CARE (include all messages in the class).
539 * This requires both setting the state of all previously seen message
540 * IDs in the hash table, and setting the default state for all
541 * applicable combinations of source/type/severity, so that all the
542 * yet-unknown message IDs that may be used in the future will be
543 * impacted as if they were already known.
546 debug_set_message_enable_all(struct gl_debug_state
*debug
,
547 enum mesa_debug_source source
,
548 enum mesa_debug_type type
,
549 enum mesa_debug_severity severity
,
552 const GLint gstack
= debug
->CurrentGroup
;
553 int s
, t
, smax
, tmax
;
555 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
557 smax
= MESA_DEBUG_SOURCE_COUNT
;
562 if (type
== MESA_DEBUG_TYPE_COUNT
) {
564 tmax
= MESA_DEBUG_TYPE_COUNT
;
569 debug_make_group_writable(debug
);
571 for (s
= source
; s
< smax
; s
++) {
572 for (t
= type
; t
< tmax
; t
++) {
573 struct gl_debug_namespace
*nspace
=
574 &debug
->Groups
[gstack
]->Namespaces
[s
][t
];
575 debug_namespace_set_all(nspace
, severity
, enabled
);
581 * Returns if the given message source/type/ID tuple is enabled.
584 debug_is_message_enabled(const struct gl_debug_state
*debug
,
585 enum mesa_debug_source source
,
586 enum mesa_debug_type type
,
588 enum mesa_debug_severity severity
)
590 const GLint gstack
= debug
->CurrentGroup
;
591 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
592 struct gl_debug_namespace
*nspace
= &grp
->Namespaces
[source
][type
];
594 if (!debug
->DebugOutput
)
597 return debug_namespace_get(nspace
, id
, severity
);
601 * 'buf' is not necessarily a null-terminated string. When logging, copy
602 * 'len' characters from it, store them in a new, null-terminated string,
603 * and remember the number of bytes used by that string, *including*
604 * the null terminator this time.
607 debug_log_message(struct gl_debug_state
*debug
,
608 enum mesa_debug_source source
,
609 enum mesa_debug_type type
, GLuint id
,
610 enum mesa_debug_severity severity
,
611 GLsizei len
, const char *buf
)
613 struct gl_debug_log
*log
= &debug
->Log
;
615 struct gl_debug_message
*emptySlot
;
617 assert(len
< MAX_DEBUG_MESSAGE_LENGTH
);
619 if (log
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
622 nextEmpty
= (log
->NextMessage
+ log
->NumMessages
)
623 % MAX_DEBUG_LOGGED_MESSAGES
;
624 emptySlot
= &log
->Messages
[nextEmpty
];
626 debug_message_store(emptySlot
, source
, type
,
627 id
, severity
, len
, buf
);
633 * Return the oldest debug message out of the log.
635 static const struct gl_debug_message
*
636 debug_fetch_message(const struct gl_debug_state
*debug
)
638 const struct gl_debug_log
*log
= &debug
->Log
;
640 return (log
->NumMessages
) ? &log
->Messages
[log
->NextMessage
] : NULL
;
644 * Delete the oldest debug messages out of the log.
647 debug_delete_messages(struct gl_debug_state
*debug
, int count
)
649 struct gl_debug_log
*log
= &debug
->Log
;
651 if (count
> log
->NumMessages
)
652 count
= log
->NumMessages
;
655 struct gl_debug_message
*msg
= &log
->Messages
[log
->NextMessage
];
657 debug_message_clear(msg
);
661 log
->NextMessage
%= MAX_DEBUG_LOGGED_MESSAGES
;
665 static struct gl_debug_message
*
666 debug_get_group_message(struct gl_debug_state
*debug
)
668 return &debug
->GroupMessages
[debug
->CurrentGroup
];
672 debug_push_group(struct gl_debug_state
*debug
)
674 const GLint gstack
= debug
->CurrentGroup
;
676 /* just point to the previous stack */
677 debug
->Groups
[gstack
+ 1] = debug
->Groups
[gstack
];
678 debug
->CurrentGroup
++;
682 debug_pop_group(struct gl_debug_state
*debug
)
684 debug_clear_group(debug
);
685 debug
->CurrentGroup
--;
690 * Lock and return debug state for the context. The debug state will be
691 * allocated and initialized upon the first call. When NULL is returned, the
692 * debug state is not locked.
694 static struct gl_debug_state
*
695 _mesa_lock_debug_state(struct gl_context
*ctx
)
697 mtx_lock(&ctx
->DebugMutex
);
700 ctx
->Debug
= debug_create();
702 GET_CURRENT_CONTEXT(cur
);
703 mtx_unlock(&ctx
->DebugMutex
);
706 * This function may be called from other threads. When that is the
707 * case, we cannot record this OOM error.
710 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
720 _mesa_unlock_debug_state(struct gl_context
*ctx
)
722 mtx_unlock(&ctx
->DebugMutex
);
726 * Set the integer debug state specified by \p pname. This can be called from
727 * _mesa_set_enable for example.
730 _mesa_set_debug_state_int(struct gl_context
*ctx
, GLenum pname
, GLint val
)
732 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
738 case GL_DEBUG_OUTPUT
:
739 debug
->DebugOutput
= (val
!= 0);
741 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
742 debug
->SyncOutput
= (val
!= 0);
745 assert(!"unknown debug output param");
749 _mesa_unlock_debug_state(ctx
);
755 * Query the integer debug state specified by \p pname. This can be called
756 * _mesa_GetIntegerv for example.
759 _mesa_get_debug_state_int(struct gl_context
*ctx
, GLenum pname
)
761 struct gl_debug_state
*debug
;
764 mtx_lock(&ctx
->DebugMutex
);
767 mtx_unlock(&ctx
->DebugMutex
);
772 case GL_DEBUG_OUTPUT
:
773 val
= debug
->DebugOutput
;
775 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
776 val
= debug
->SyncOutput
;
778 case GL_DEBUG_LOGGED_MESSAGES
:
779 val
= debug
->Log
.NumMessages
;
781 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH
:
782 val
= (debug
->Log
.NumMessages
) ?
783 debug
->Log
.Messages
[debug
->Log
.NextMessage
].length
: 0;
785 case GL_DEBUG_GROUP_STACK_DEPTH
:
786 val
= debug
->CurrentGroup
+ 1;
789 assert(!"unknown debug output param");
794 mtx_unlock(&ctx
->DebugMutex
);
800 * Query the pointer debug state specified by \p pname. This can be called
801 * _mesa_GetPointerv for example.
804 _mesa_get_debug_state_ptr(struct gl_context
*ctx
, GLenum pname
)
806 struct gl_debug_state
*debug
;
809 mtx_lock(&ctx
->DebugMutex
);
812 mtx_unlock(&ctx
->DebugMutex
);
817 case GL_DEBUG_CALLBACK_FUNCTION_ARB
:
818 val
= (void *) debug
->Callback
;
820 case GL_DEBUG_CALLBACK_USER_PARAM_ARB
:
821 val
= (void *) debug
->CallbackData
;
824 assert(!"unknown debug output param");
829 mtx_unlock(&ctx
->DebugMutex
);
835 * Insert a debug message. The mutex is assumed to be locked, and will be
836 * unlocked by this call.
839 log_msg_locked_and_unlock(struct gl_context
*ctx
,
840 enum mesa_debug_source source
,
841 enum mesa_debug_type type
, GLuint id
,
842 enum mesa_debug_severity severity
,
843 GLint len
, const char *buf
)
845 struct gl_debug_state
*debug
= ctx
->Debug
;
847 if (!debug_is_message_enabled(debug
, source
, type
, id
, severity
)) {
848 _mesa_unlock_debug_state(ctx
);
852 if (ctx
->Debug
->Callback
) {
853 GLenum gl_source
= debug_source_enums
[source
];
854 GLenum gl_type
= debug_type_enums
[type
];
855 GLenum gl_severity
= debug_severity_enums
[severity
];
856 GLDEBUGPROC callback
= ctx
->Debug
->Callback
;
857 const void *data
= ctx
->Debug
->CallbackData
;
860 * When ctx->Debug->SyncOutput is GL_FALSE, the client is prepared for
861 * unsynchronous calls. When it is GL_TRUE, we will not spawn threads.
862 * In either case, we can call the callback unlocked.
864 _mesa_unlock_debug_state(ctx
);
865 callback(gl_source
, gl_type
, id
, gl_severity
, len
, buf
, data
);
868 debug_log_message(ctx
->Debug
, source
, type
, id
, severity
, len
, buf
);
869 _mesa_unlock_debug_state(ctx
);
874 * Log a client or driver debug message.
877 log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
878 enum mesa_debug_type type
, GLuint id
,
879 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
881 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
886 log_msg_locked_and_unlock(ctx
, source
, type
, id
, severity
, len
, buf
);
891 * Verify that source, type, and severity are valid enums.
893 * The 'caller' param is used for handling values available
894 * only in glDebugMessageInsert or glDebugMessageControl
897 validate_params(struct gl_context
*ctx
, unsigned caller
,
898 const char *callerstr
, GLenum source
, GLenum type
,
904 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
905 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
907 case GL_DEBUG_SOURCE_API_ARB
:
908 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
909 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
910 case GL_DEBUG_SOURCE_OTHER_ARB
:
911 if (caller
!= INSERT
)
916 if (caller
== CONTROL
)
925 case GL_DEBUG_TYPE_ERROR_ARB
:
926 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
927 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
928 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
929 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
930 case GL_DEBUG_TYPE_OTHER_ARB
:
931 case GL_DEBUG_TYPE_MARKER
:
932 case GL_DEBUG_TYPE_PUSH_GROUP
:
933 case GL_DEBUG_TYPE_POP_GROUP
:
936 if (caller
== CONTROL
)
945 case GL_DEBUG_SEVERITY_HIGH_ARB
:
946 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
947 case GL_DEBUG_SEVERITY_LOW_ARB
:
948 case GL_DEBUG_SEVERITY_NOTIFICATION
:
951 if (caller
== CONTROL
)
961 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
962 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
963 source
, type
, severity
);
970 validate_length(struct gl_context
*ctx
, const char *callerstr
, GLsizei length
,
975 GLsizei len
= strlen(buf
);
977 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
978 _mesa_error(ctx
, GL_INVALID_VALUE
,
979 "%s(null terminated string length=%d, is not less than "
980 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, len
,
981 MAX_DEBUG_MESSAGE_LENGTH
);
986 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
987 _mesa_error(ctx
, GL_INVALID_VALUE
,
988 "%s(length=%d, which is not less than "
989 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
990 MAX_DEBUG_MESSAGE_LENGTH
);
999 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
1000 GLenum severity
, GLint length
,
1003 GET_CURRENT_CONTEXT(ctx
);
1004 const char *callerstr
;
1006 if (_mesa_is_desktop_gl(ctx
))
1007 callerstr
= "glDebugMessageInsert";
1009 callerstr
= "glDebugMessageInsertKHR";
1011 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
1012 return; /* GL_INVALID_ENUM */
1014 if (!validate_length(ctx
, callerstr
, length
, buf
))
1015 return; /* GL_INVALID_VALUE */
1017 log_msg(ctx
, gl_enum_to_debug_source(source
),
1018 gl_enum_to_debug_type(type
), id
,
1019 gl_enum_to_debug_severity(severity
),
1025 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
1026 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
1027 GLsizei
*lengths
, GLchar
*messageLog
)
1029 GET_CURRENT_CONTEXT(ctx
);
1030 struct gl_debug_state
*debug
;
1031 const char *callerstr
;
1034 if (_mesa_is_desktop_gl(ctx
))
1035 callerstr
= "glGetDebugMessageLog";
1037 callerstr
= "glGetDebugMessageLogKHR";
1043 _mesa_error(ctx
, GL_INVALID_VALUE
,
1044 "%s(logSize=%d : logSize must not be negative)",
1045 callerstr
, logSize
);
1049 debug
= _mesa_lock_debug_state(ctx
);
1053 for (ret
= 0; ret
< count
; ret
++) {
1054 const struct gl_debug_message
*msg
= debug_fetch_message(debug
);
1062 len
= strlen(msg
->message
);
1064 if (logSize
< len
+1 && messageLog
!= NULL
)
1068 assert(msg
->message
[len
] == '\0');
1069 (void) strncpy(messageLog
, msg
->message
, (size_t)len
+1);
1071 messageLog
+= len
+1;
1078 *severities
++ = debug_severity_enums
[msg
->severity
];
1080 *sources
++ = debug_source_enums
[msg
->source
];
1082 *types
++ = debug_type_enums
[msg
->type
];
1086 debug_delete_messages(debug
, 1);
1089 _mesa_unlock_debug_state(ctx
);
1096 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
1097 GLenum gl_severity
, GLsizei count
,
1098 const GLuint
*ids
, GLboolean enabled
)
1100 GET_CURRENT_CONTEXT(ctx
);
1101 enum mesa_debug_source source
= gl_enum_to_debug_source(gl_source
);
1102 enum mesa_debug_type type
= gl_enum_to_debug_type(gl_type
);
1103 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(gl_severity
);
1104 const char *callerstr
;
1105 struct gl_debug_state
*debug
;
1107 if (_mesa_is_desktop_gl(ctx
))
1108 callerstr
= "glDebugMessageControl";
1110 callerstr
= "glDebugMessageControlKHR";
1113 _mesa_error(ctx
, GL_INVALID_VALUE
,
1114 "%s(count=%d : count must not be negative)", callerstr
,
1119 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
1121 return; /* GL_INVALID_ENUM */
1123 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
1124 || gl_source
== GL_DONT_CARE
)) {
1125 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1126 "%s(When passing an array of ids, severity must be"
1127 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
1132 debug
= _mesa_lock_debug_state(ctx
);
1138 for (i
= 0; i
< count
; i
++)
1139 debug_set_message_enable(debug
, source
, type
, ids
[i
], enabled
);
1142 debug_set_message_enable_all(debug
, source
, type
, severity
, enabled
);
1145 _mesa_unlock_debug_state(ctx
);
1150 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
1152 GET_CURRENT_CONTEXT(ctx
);
1153 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
1155 debug
->Callback
= callback
;
1156 debug
->CallbackData
= userParam
;
1157 _mesa_unlock_debug_state(ctx
);
1163 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
1164 const GLchar
*message
)
1166 GET_CURRENT_CONTEXT(ctx
);
1167 const char *callerstr
;
1168 struct gl_debug_state
*debug
;
1169 struct gl_debug_message
*emptySlot
;
1171 if (_mesa_is_desktop_gl(ctx
))
1172 callerstr
= "glPushDebugGroup";
1174 callerstr
= "glPushDebugGroupKHR";
1177 case GL_DEBUG_SOURCE_APPLICATION
:
1178 case GL_DEBUG_SOURCE_THIRD_PARTY
:
1181 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
1182 "(source=0x%x)", callerstr
, source
);
1186 if (!validate_length(ctx
, callerstr
, length
, message
))
1187 return; /* GL_INVALID_VALUE */
1189 debug
= _mesa_lock_debug_state(ctx
);
1193 if (debug
->CurrentGroup
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
1194 _mesa_unlock_debug_state(ctx
);
1195 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
1199 /* pop reuses the message details from push so we store this */
1200 emptySlot
= debug_get_group_message(debug
);
1201 debug_message_store(emptySlot
,
1202 gl_enum_to_debug_source(source
),
1203 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
1205 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1208 debug_push_group(debug
);
1210 log_msg_locked_and_unlock(ctx
,
1211 gl_enum_to_debug_source(source
),
1212 MESA_DEBUG_TYPE_PUSH_GROUP
, id
,
1213 MESA_DEBUG_SEVERITY_NOTIFICATION
, length
,
1219 _mesa_PopDebugGroup(void)
1221 GET_CURRENT_CONTEXT(ctx
);
1222 const char *callerstr
;
1223 struct gl_debug_state
*debug
;
1224 struct gl_debug_message
*gdmessage
, msg
;
1226 if (_mesa_is_desktop_gl(ctx
))
1227 callerstr
= "glPopDebugGroup";
1229 callerstr
= "glPopDebugGroupKHR";
1231 debug
= _mesa_lock_debug_state(ctx
);
1235 if (debug
->CurrentGroup
<= 0) {
1236 _mesa_unlock_debug_state(ctx
);
1237 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
1241 debug_pop_group(debug
);
1243 /* make a shallow copy */
1244 gdmessage
= debug_get_group_message(debug
);
1246 gdmessage
->message
= NULL
;
1247 gdmessage
->length
= 0;
1249 log_msg_locked_and_unlock(ctx
,
1251 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
1253 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1254 msg
.length
, msg
.message
);
1256 debug_message_clear(&msg
);
1261 _mesa_init_errors(struct gl_context
*ctx
)
1263 mtx_init(&ctx
->DebugMutex
, mtx_plain
);
1268 _mesa_free_errors_data(struct gl_context
*ctx
)
1271 debug_destroy(ctx
->Debug
);
1272 /* set to NULL just in case it is used before context is completely gone. */
1276 mtx_destroy(&ctx
->DebugMutex
);
1280 _mesa_StringMarkerGREMEDY(GLsizei len
, const GLvoid
*string
)
1282 GET_CURRENT_CONTEXT(ctx
);
1283 if (ctx
->Extensions
.GREMEDY_string_marker
) {
1284 /* if length not specified, string will be null terminated: */
1286 len
= strlen(string
);
1287 ctx
->Driver
.EmitStringMarker(ctx
, string
, len
);
1289 _mesa_error(ctx
, GL_INVALID_OPERATION
, "StringMarkerGREMEDY");
1293 /**********************************************************************/
1294 /** \name Diagnostics */
1297 static FILE *LogFile
= NULL
;
1301 output_if_debug(const char *prefixString
, const char *outputString
,
1304 static int debug
= -1;
1306 /* Init the local 'debug' var once.
1307 * Note: the _mesa_init_debug() function should have been called
1308 * by now so MESA_DEBUG_FLAGS will be initialized.
1311 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1312 * etc to the named file. Otherwise, output to stderr.
1314 const char *logFile
= getenv("MESA_LOG_FILE");
1316 LogFile
= fopen(logFile
, "w");
1320 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1321 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1326 /* in release builds, be silent unless MESA_DEBUG is set */
1327 debug
= getenv("MESA_DEBUG") != NULL
;
1331 /* Now only print the string if we're required to do so. */
1334 fprintf(LogFile
, "%s: %s", prefixString
, outputString
);
1336 fprintf(LogFile
, "%s", outputString
);
1338 fprintf(LogFile
, "\n");
1342 /* stderr from windows applications without console is not usually
1343 * visible, so communicate with the debugger instead */
1346 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1347 OutputDebugStringA(buf
);
1355 * Return the file handle to use for debug/logging. Defaults to stderr
1356 * unless MESA_LOG_FILE is defined.
1359 _mesa_get_log_file(void)
1367 * When a new type of error is recorded, print a message describing
1368 * previous errors which were accumulated.
1371 flush_delayed_errors( struct gl_context
*ctx
)
1373 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1375 if (ctx
->ErrorDebugCount
) {
1376 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1377 ctx
->ErrorDebugCount
,
1378 _mesa_enum_to_string(ctx
->ErrorValue
));
1380 output_if_debug("Mesa", s
, GL_TRUE
);
1382 ctx
->ErrorDebugCount
= 0;
1388 * Report a warning (a recoverable error condition) to stderr if
1389 * either DEBUG is defined or the MESA_DEBUG env var is set.
1391 * \param ctx GL context.
1392 * \param fmtString printf()-like format string.
1395 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1397 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1399 va_start( args
, fmtString
);
1400 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1404 flush_delayed_errors( ctx
);
1406 output_if_debug("Mesa warning", str
, GL_TRUE
);
1411 * Report an internal implementation problem.
1412 * Prints the message to stderr via fprintf().
1414 * \param ctx GL context.
1415 * \param fmtString problem description string.
1418 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1421 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1422 static int numCalls
= 0;
1426 if (numCalls
< 50) {
1429 va_start( args
, fmtString
);
1430 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1432 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1433 PACKAGE_VERSION
, str
);
1434 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1440 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1442 static GLint debug
= -1;
1444 /* Check debug environment variable only once:
1447 const char *debugEnv
= getenv("MESA_DEBUG");
1450 if (debugEnv
&& strstr(debugEnv
, "silent"))
1463 if (ctx
->ErrorValue
!= error
||
1464 ctx
->ErrorDebugFmtString
!= fmtString
) {
1465 flush_delayed_errors( ctx
);
1466 ctx
->ErrorDebugFmtString
= fmtString
;
1467 ctx
->ErrorDebugCount
= 0;
1470 ctx
->ErrorDebugCount
++;
1477 _mesa_gl_vdebug(struct gl_context
*ctx
,
1479 enum mesa_debug_source source
,
1480 enum mesa_debug_type type
,
1481 enum mesa_debug_severity severity
,
1482 const char *fmtString
,
1485 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1490 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1492 log_msg(ctx
, source
, type
, *id
, severity
, len
, s
);
1497 _mesa_gl_debug(struct gl_context
*ctx
,
1499 enum mesa_debug_source source
,
1500 enum mesa_debug_type type
,
1501 enum mesa_debug_severity severity
,
1502 const char *fmtString
, ...)
1505 va_start(args
, fmtString
);
1506 _mesa_gl_vdebug(ctx
, id
, source
, type
, severity
, fmtString
, args
);
1512 * Record an OpenGL state error. These usually occur when the user
1513 * passes invalid parameters to a GL function.
1515 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1516 * run-time via the MESA_DEBUG environment variable), report the error with
1519 * \param ctx the GL context.
1520 * \param error the error value.
1521 * \param fmtString printf() style format string, followed by optional args
1524 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1526 GLboolean do_output
, do_log
;
1527 /* Ideally this would be set up by the caller, so that we had proper IDs
1528 * per different message.
1530 static GLuint error_msg_id
= 0;
1532 debug_get_id(&error_msg_id
);
1534 do_output
= should_output(ctx
, error
, fmtString
);
1536 mtx_lock(&ctx
->DebugMutex
);
1538 do_log
= debug_is_message_enabled(ctx
->Debug
,
1539 MESA_DEBUG_SOURCE_API
,
1540 MESA_DEBUG_TYPE_ERROR
,
1542 MESA_DEBUG_SEVERITY_HIGH
);
1547 mtx_unlock(&ctx
->DebugMutex
);
1549 if (do_output
|| do_log
) {
1550 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1554 va_start(args
, fmtString
);
1555 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1558 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1559 /* Too long error message. Whoever calls _mesa_error should use
1566 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1567 _mesa_enum_to_string(error
), s
);
1568 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1569 /* Same as above. */
1574 /* Print the error to stderr if needed. */
1576 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1579 /* Log the error via ARB_debug_output if needed.*/
1581 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_ERROR
,
1582 error_msg_id
, MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1586 /* Set the GL context error state for glGetError. */
1587 _mesa_record_error(ctx
, error
);
1591 _mesa_error_no_memory(const char *caller
)
1593 GET_CURRENT_CONTEXT(ctx
);
1594 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "out of memory in %s", caller
);
1598 * Report debug information. Print error message to stderr via fprintf().
1599 * No-op if DEBUG mode not enabled.
1601 * \param ctx GL context.
1602 * \param fmtString printf()-style format string, followed by optional args.
1605 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1608 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1610 va_start(args
, fmtString
);
1611 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1613 output_if_debug("Mesa", s
, GL_FALSE
);
1621 _mesa_log(const char *fmtString
, ...)
1623 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1625 va_start(args
, fmtString
);
1626 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1628 output_if_debug("", s
, GL_FALSE
);
1633 * Report debug information from the shader compiler via GL_ARB_debug_output.
1635 * \param ctx GL context.
1636 * \param type The namespace to which this message belongs.
1637 * \param id The message ID within the given namespace.
1638 * \param msg The message to output. Must be null-terminated.
1641 _mesa_shader_debug(struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1644 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1645 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1652 /* Truncate the message if necessary. */
1653 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1654 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1656 log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);