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
;
84 * Debug message log. It works like a ring buffer.
87 struct gl_debug_message Messages
[MAX_DEBUG_LOGGED_MESSAGES
];
95 const void *CallbackData
;
97 GLboolean DebugOutput
;
99 struct gl_debug_group
*Groups
[MAX_DEBUG_GROUP_STACK_DEPTH
];
100 struct gl_debug_message GroupMessages
[MAX_DEBUG_GROUP_STACK_DEPTH
];
101 GLint GroupStackDepth
;
103 struct gl_debug_log Log
;
106 static char out_of_memory
[] = "Debugging error: out of memory";
108 static const GLenum debug_source_enums
[] = {
110 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
111 GL_DEBUG_SOURCE_SHADER_COMPILER
,
112 GL_DEBUG_SOURCE_THIRD_PARTY
,
113 GL_DEBUG_SOURCE_APPLICATION
,
114 GL_DEBUG_SOURCE_OTHER
,
117 static const GLenum debug_type_enums
[] = {
119 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
120 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
121 GL_DEBUG_TYPE_PORTABILITY
,
122 GL_DEBUG_TYPE_PERFORMANCE
,
124 GL_DEBUG_TYPE_MARKER
,
125 GL_DEBUG_TYPE_PUSH_GROUP
,
126 GL_DEBUG_TYPE_POP_GROUP
,
129 static const GLenum debug_severity_enums
[] = {
130 GL_DEBUG_SEVERITY_LOW
,
131 GL_DEBUG_SEVERITY_MEDIUM
,
132 GL_DEBUG_SEVERITY_HIGH
,
133 GL_DEBUG_SEVERITY_NOTIFICATION
,
137 static enum mesa_debug_source
138 gl_enum_to_debug_source(GLenum e
)
142 for (i
= 0; i
< ARRAY_SIZE(debug_source_enums
); i
++) {
143 if (debug_source_enums
[i
] == e
)
149 static enum mesa_debug_type
150 gl_enum_to_debug_type(GLenum e
)
154 for (i
= 0; i
< ARRAY_SIZE(debug_type_enums
); i
++) {
155 if (debug_type_enums
[i
] == e
)
161 static enum mesa_debug_severity
162 gl_enum_to_debug_severity(GLenum e
)
166 for (i
= 0; i
< ARRAY_SIZE(debug_severity_enums
); i
++) {
167 if (debug_severity_enums
[i
] == e
)
175 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
178 * The GL API has this "ID" mechanism, where the intention is to allow a
179 * client to filter in/out messages based on source, type, and ID. Of course,
180 * building a giant enum list of all debug output messages that Mesa might
181 * generate is ridiculous, so instead we have our caller pass us a pointer to
182 * static storage where the ID should get stored. This ID will be shared
183 * across all contexts for that message (which seems like a desirable
184 * property, even if it's not expected by the spec), but note that it won't be
185 * the same between executions if messages aren't generated in the same order.
188 debug_get_id(GLuint
*id
)
191 mtx_lock(&DynamicIDMutex
);
193 *id
= NextDynamicID
++;
194 mtx_unlock(&DynamicIDMutex
);
199 debug_message_clear(struct gl_debug_message
*msg
)
201 if (msg
->message
!= (char*)out_of_memory
)
208 debug_message_store(struct gl_debug_message
*msg
,
209 enum mesa_debug_source source
,
210 enum mesa_debug_type type
, GLuint id
,
211 enum mesa_debug_severity severity
,
212 GLsizei len
, const char *buf
)
214 assert(!msg
->message
&& !msg
->length
);
216 msg
->message
= malloc(len
+1);
218 (void) strncpy(msg
->message
, buf
, (size_t)len
);
219 msg
->message
[len
] = '\0';
222 msg
->source
= source
;
225 msg
->severity
= severity
;
227 static GLuint oom_msg_id
= 0;
228 debug_get_id(&oom_msg_id
);
231 msg
->message
= out_of_memory
;
232 msg
->length
= strlen(out_of_memory
)+1;
233 msg
->source
= MESA_DEBUG_SOURCE_OTHER
;
234 msg
->type
= MESA_DEBUG_TYPE_ERROR
;
235 msg
->id
= oom_msg_id
;
236 msg
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
241 debug_namespace_init(struct gl_debug_namespace
*ns
)
243 make_empty_list(&ns
->Elements
);
245 /* Enable all the messages with severity HIGH or MEDIUM by default */
246 ns
->DefaultState
= (1 << MESA_DEBUG_SEVERITY_HIGH
) |
247 (1 << MESA_DEBUG_SEVERITY_MEDIUM
);
251 debug_namespace_clear(struct gl_debug_namespace
*ns
)
253 struct simple_node
*node
, *tmp
;
255 foreach_s(node
, tmp
, &ns
->Elements
)
260 debug_namespace_copy(struct gl_debug_namespace
*dst
,
261 const struct gl_debug_namespace
*src
)
263 struct simple_node
*node
;
265 dst
->DefaultState
= src
->DefaultState
;
267 make_empty_list(&dst
->Elements
);
268 foreach(node
, &src
->Elements
) {
269 const struct gl_debug_element
*elem
=
270 (const struct gl_debug_element
*) node
;
271 struct gl_debug_element
*copy
;
273 copy
= malloc(sizeof(*copy
));
275 debug_namespace_clear(dst
);
280 copy
->State
= elem
->State
;
281 insert_at_tail(&dst
->Elements
, ©
->link
);
288 * Set the state of \p id in the namespace.
291 debug_namespace_set(struct gl_debug_namespace
*ns
,
292 GLuint id
, bool enabled
)
294 const uint32_t state
= (enabled
) ?
295 ((1 << MESA_DEBUG_SEVERITY_COUNT
) - 1) : 0;
296 struct gl_debug_element
*elem
= NULL
;
297 struct simple_node
*node
;
299 /* find the element */
300 foreach(node
, &ns
->Elements
) {
301 struct gl_debug_element
*tmp
= (struct gl_debug_element
*) node
;
308 /* we do not need the element if it has the default state */
309 if (ns
->DefaultState
== state
) {
311 remove_from_list(&elem
->link
);
318 elem
= malloc(sizeof(*elem
));
323 insert_at_tail(&ns
->Elements
, &elem
->link
);
332 * Set the default state of the namespace for \p severity. When \p severity
333 * is MESA_DEBUG_SEVERITY_COUNT, the default values for all severities are
337 debug_namespace_set_all(struct gl_debug_namespace
*ns
,
338 enum mesa_debug_severity severity
,
341 struct simple_node
*node
, *tmp
;
344 /* set all elements to the same state */
345 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
346 ns
->DefaultState
= (enabled
) ? ((1 << severity
) - 1) : 0;
347 debug_namespace_clear(ns
);
348 make_empty_list(&ns
->Elements
);
352 mask
= 1 << severity
;
353 val
= (enabled
) ? mask
: 0;
355 ns
->DefaultState
= (ns
->DefaultState
& ~mask
) | val
;
357 foreach_s(node
, tmp
, &ns
->Elements
) {
358 struct gl_debug_element
*elem
= (struct gl_debug_element
*) node
;
360 elem
->State
= (elem
->State
& ~mask
) | val
;
361 if (elem
->State
== ns
->DefaultState
) {
362 remove_from_list(node
);
369 * Get the state of \p id in the namespace.
372 debug_namespace_get(const struct gl_debug_namespace
*ns
, GLuint id
,
373 enum mesa_debug_severity severity
)
375 struct simple_node
*node
;
378 state
= ns
->DefaultState
;
379 foreach(node
, &ns
->Elements
) {
380 struct gl_debug_element
*elem
= (struct gl_debug_element
*) node
;
382 if (elem
->ID
== id
) {
388 return (state
& (1 << severity
));
392 * Allocate and initialize context debug state.
394 static struct gl_debug_state
*
397 struct gl_debug_state
*debug
;
400 debug
= CALLOC_STRUCT(gl_debug_state
);
404 debug
->Groups
[0] = malloc(sizeof(*debug
->Groups
[0]));
405 if (!debug
->Groups
[0]) {
410 /* Initialize state for filtering known debug messages. */
411 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
412 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
413 debug_namespace_init(&debug
->Groups
[0]->Namespaces
[s
][t
]);
420 * Return true if the top debug group points to the group below it.
423 debug_is_group_read_only(const struct gl_debug_state
*debug
)
425 const GLint gstack
= debug
->GroupStackDepth
;
426 return (gstack
> 0 && debug
->Groups
[gstack
] == debug
->Groups
[gstack
- 1]);
430 * Make the top debug group writable.
433 debug_make_group_writable(struct gl_debug_state
*debug
)
435 const GLint gstack
= debug
->GroupStackDepth
;
436 const struct gl_debug_group
*src
= debug
->Groups
[gstack
];
437 struct gl_debug_group
*dst
;
440 if (!debug_is_group_read_only(debug
))
443 dst
= malloc(sizeof(*dst
));
447 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
448 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
449 if (!debug_namespace_copy(&dst
->Namespaces
[s
][t
],
450 &src
->Namespaces
[s
][t
])) {
452 for (t
= t
- 1; t
>= 0; t
--)
453 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
454 for (s
= s
- 1; s
>= 0; s
--) {
455 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
456 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
464 debug
->Groups
[gstack
] = dst
;
470 * Free the top debug group.
473 debug_clear_group(struct gl_debug_state
*debug
)
475 const GLint gstack
= debug
->GroupStackDepth
;
477 if (!debug_is_group_read_only(debug
)) {
478 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
481 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
482 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
483 debug_namespace_clear(&grp
->Namespaces
[s
][t
]);
489 debug
->Groups
[gstack
] = NULL
;
493 * Loop through debug group stack tearing down states for
494 * filtering debug messages. Then free debug output state.
497 debug_destroy(struct gl_debug_state
*debug
)
499 while (debug
->GroupStackDepth
> 0) {
500 debug_clear_group(debug
);
501 debug
->GroupStackDepth
--;
504 debug_clear_group(debug
);
509 * Sets the state of the given message source/type/ID tuple.
512 debug_set_message_enable(struct gl_debug_state
*debug
,
513 enum mesa_debug_source source
,
514 enum mesa_debug_type type
,
515 GLuint id
, GLboolean enabled
)
517 const GLint gstack
= debug
->GroupStackDepth
;
518 struct gl_debug_namespace
*ns
;
520 debug_make_group_writable(debug
);
521 ns
= &debug
->Groups
[gstack
]->Namespaces
[source
][type
];
523 debug_namespace_set(ns
, id
, enabled
);
527 * Set the state of all message IDs found in the given intersection of
528 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
529 * GL_DONT_CARE (include all messages in the class).
531 * This requires both setting the state of all previously seen message
532 * IDs in the hash table, and setting the default state for all
533 * applicable combinations of source/type/severity, so that all the
534 * yet-unknown message IDs that may be used in the future will be
535 * impacted as if they were already known.
538 debug_set_message_enable_all(struct gl_debug_state
*debug
,
539 enum mesa_debug_source source
,
540 enum mesa_debug_type type
,
541 enum mesa_debug_severity severity
,
544 const GLint gstack
= debug
->GroupStackDepth
;
545 int s
, t
, smax
, tmax
;
547 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
549 smax
= MESA_DEBUG_SOURCE_COUNT
;
554 if (type
== MESA_DEBUG_TYPE_COUNT
) {
556 tmax
= MESA_DEBUG_TYPE_COUNT
;
561 debug_make_group_writable(debug
);
563 for (s
= source
; s
< smax
; s
++) {
564 for (t
= type
; t
< tmax
; t
++) {
565 struct gl_debug_namespace
*nspace
=
566 &debug
->Groups
[gstack
]->Namespaces
[s
][t
];
567 debug_namespace_set_all(nspace
, severity
, enabled
);
573 * Returns if the given message source/type/ID tuple is enabled.
576 debug_is_message_enabled(const struct gl_debug_state
*debug
,
577 enum mesa_debug_source source
,
578 enum mesa_debug_type type
,
580 enum mesa_debug_severity severity
)
582 const GLint gstack
= debug
->GroupStackDepth
;
583 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
584 struct gl_debug_namespace
*nspace
= &grp
->Namespaces
[source
][type
];
586 if (!debug
->DebugOutput
)
589 return debug_namespace_get(nspace
, id
, severity
);
593 * 'buf' is not necessarily a null-terminated string. When logging, copy
594 * 'len' characters from it, store them in a new, null-terminated string,
595 * and remember the number of bytes used by that string, *including*
596 * the null terminator this time.
599 debug_log_message(struct gl_debug_state
*debug
,
600 enum mesa_debug_source source
,
601 enum mesa_debug_type type
, GLuint id
,
602 enum mesa_debug_severity severity
,
603 GLsizei len
, const char *buf
)
605 struct gl_debug_log
*log
= &debug
->Log
;
607 struct gl_debug_message
*emptySlot
;
609 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
611 if (log
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
614 nextEmpty
= (log
->NextMessage
+ log
->NumMessages
)
615 % MAX_DEBUG_LOGGED_MESSAGES
;
616 emptySlot
= &log
->Messages
[nextEmpty
];
618 debug_message_store(emptySlot
, source
, type
,
619 id
, severity
, len
, buf
);
625 * Return the oldest debug message out of the log.
627 static const struct gl_debug_message
*
628 debug_fetch_message(const struct gl_debug_state
*debug
)
630 const struct gl_debug_log
*log
= &debug
->Log
;
632 return (log
->NumMessages
) ? &log
->Messages
[log
->NextMessage
] : NULL
;
636 * Delete the oldest debug messages out of the log.
639 debug_delete_messages(struct gl_debug_state
*debug
, int count
)
641 struct gl_debug_log
*log
= &debug
->Log
;
643 if (count
> log
->NumMessages
)
644 count
= log
->NumMessages
;
647 struct gl_debug_message
*msg
= &log
->Messages
[log
->NextMessage
];
649 debug_message_clear(msg
);
653 log
->NextMessage
%= MAX_DEBUG_LOGGED_MESSAGES
;
657 static struct gl_debug_message
*
658 debug_get_group_message(struct gl_debug_state
*debug
)
660 return &debug
->GroupMessages
[debug
->GroupStackDepth
];
664 debug_push_group(struct gl_debug_state
*debug
)
666 const GLint gstack
= debug
->GroupStackDepth
;
668 /* just point to the previous stack */
669 debug
->Groups
[gstack
+ 1] = debug
->Groups
[gstack
];
670 debug
->GroupStackDepth
++;
674 debug_pop_group(struct gl_debug_state
*debug
)
676 debug_clear_group(debug
);
677 debug
->GroupStackDepth
--;
682 * Lock and return debug state for the context. The debug state will be
683 * allocated and initialized upon the first call. When NULL is returned, the
684 * debug state is not locked.
686 static struct gl_debug_state
*
687 _mesa_lock_debug_state(struct gl_context
*ctx
)
689 mtx_lock(&ctx
->DebugMutex
);
692 ctx
->Debug
= debug_create();
694 GET_CURRENT_CONTEXT(cur
);
695 mtx_unlock(&ctx
->DebugMutex
);
698 * This function may be called from other threads. When that is the
699 * case, we cannot record this OOM error.
702 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
712 _mesa_unlock_debug_state(struct gl_context
*ctx
)
714 mtx_unlock(&ctx
->DebugMutex
);
718 * Set the integer debug state specified by \p pname. This can be called from
719 * _mesa_set_enable for example.
722 _mesa_set_debug_state_int(struct gl_context
*ctx
, GLenum pname
, GLint val
)
724 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
730 case GL_DEBUG_OUTPUT
:
731 debug
->DebugOutput
= (val
!= 0);
733 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
734 debug
->SyncOutput
= (val
!= 0);
737 assert(!"unknown debug output param");
741 _mesa_unlock_debug_state(ctx
);
747 * Query the integer debug state specified by \p pname. This can be called
748 * _mesa_GetIntegerv for example.
751 _mesa_get_debug_state_int(struct gl_context
*ctx
, GLenum pname
)
753 struct gl_debug_state
*debug
;
756 mtx_lock(&ctx
->DebugMutex
);
759 mtx_unlock(&ctx
->DebugMutex
);
764 case GL_DEBUG_OUTPUT
:
765 val
= debug
->DebugOutput
;
767 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
768 val
= debug
->SyncOutput
;
770 case GL_DEBUG_LOGGED_MESSAGES
:
771 val
= debug
->Log
.NumMessages
;
773 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH
:
774 val
= (debug
->Log
.NumMessages
) ?
775 debug
->Log
.Messages
[debug
->Log
.NextMessage
].length
: 0;
777 case GL_DEBUG_GROUP_STACK_DEPTH
:
778 val
= debug
->GroupStackDepth
;
781 assert(!"unknown debug output param");
786 mtx_unlock(&ctx
->DebugMutex
);
792 * Query the pointer debug state specified by \p pname. This can be called
793 * _mesa_GetPointerv for example.
796 _mesa_get_debug_state_ptr(struct gl_context
*ctx
, GLenum pname
)
798 struct gl_debug_state
*debug
;
801 mtx_lock(&ctx
->DebugMutex
);
804 mtx_unlock(&ctx
->DebugMutex
);
809 case GL_DEBUG_CALLBACK_FUNCTION_ARB
:
810 val
= (void *) debug
->Callback
;
812 case GL_DEBUG_CALLBACK_USER_PARAM_ARB
:
813 val
= (void *) debug
->CallbackData
;
816 assert(!"unknown debug output param");
821 mtx_unlock(&ctx
->DebugMutex
);
827 * Insert a debug message. The mutex is assumed to be locked, and will be
828 * unlocked by this call.
831 log_msg_locked_and_unlock(struct gl_context
*ctx
,
832 enum mesa_debug_source source
,
833 enum mesa_debug_type type
, GLuint id
,
834 enum mesa_debug_severity severity
,
835 GLint len
, const char *buf
)
837 struct gl_debug_state
*debug
= ctx
->Debug
;
839 if (!debug_is_message_enabled(debug
, source
, type
, id
, severity
)) {
840 _mesa_unlock_debug_state(ctx
);
844 if (ctx
->Debug
->Callback
) {
845 GLenum gl_source
= debug_source_enums
[source
];
846 GLenum gl_type
= debug_type_enums
[type
];
847 GLenum gl_severity
= debug_severity_enums
[severity
];
848 GLDEBUGPROC callback
= ctx
->Debug
->Callback
;
849 const void *data
= ctx
->Debug
->CallbackData
;
852 * When ctx->Debug->SyncOutput is GL_FALSE, the client is prepared for
853 * unsynchronous calls. When it is GL_TRUE, we will not spawn threads.
854 * In either case, we can call the callback unlocked.
856 _mesa_unlock_debug_state(ctx
);
857 callback(gl_source
, gl_type
, id
, gl_severity
, len
, buf
, data
);
860 debug_log_message(ctx
->Debug
, source
, type
, id
, severity
, len
, buf
);
861 _mesa_unlock_debug_state(ctx
);
866 * Log a client or driver debug message.
869 log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
870 enum mesa_debug_type type
, GLuint id
,
871 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
873 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
878 log_msg_locked_and_unlock(ctx
, source
, type
, id
, severity
, len
, buf
);
883 * Verify that source, type, and severity are valid enums.
885 * The 'caller' param is used for handling values available
886 * only in glDebugMessageInsert or glDebugMessageControl
889 validate_params(struct gl_context
*ctx
, unsigned caller
,
890 const char *callerstr
, GLenum source
, GLenum type
,
896 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
897 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
899 case GL_DEBUG_SOURCE_API_ARB
:
900 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
901 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
902 case GL_DEBUG_SOURCE_OTHER_ARB
:
903 if (caller
!= INSERT
)
908 if (caller
== CONTROL
)
917 case GL_DEBUG_TYPE_ERROR_ARB
:
918 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
919 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
920 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
921 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
922 case GL_DEBUG_TYPE_OTHER_ARB
:
923 case GL_DEBUG_TYPE_MARKER
:
925 case GL_DEBUG_TYPE_PUSH_GROUP
:
926 case GL_DEBUG_TYPE_POP_GROUP
:
928 if (caller
== CONTROL
)
937 case GL_DEBUG_SEVERITY_HIGH_ARB
:
938 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
939 case GL_DEBUG_SEVERITY_LOW_ARB
:
940 case GL_DEBUG_SEVERITY_NOTIFICATION
:
943 if (caller
== CONTROL
)
953 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
954 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
955 source
, type
, severity
);
962 validate_length(struct gl_context
*ctx
, const char *callerstr
, GLsizei length
)
964 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
965 _mesa_error(ctx
, GL_INVALID_VALUE
,
966 "%s(length=%d, which is not less than "
967 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
968 MAX_DEBUG_MESSAGE_LENGTH
);
977 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
978 GLenum severity
, GLint length
,
981 const char *callerstr
= "glDebugMessageInsert";
983 GET_CURRENT_CONTEXT(ctx
);
985 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
986 return; /* GL_INVALID_ENUM */
989 length
= strlen(buf
);
990 if (!validate_length(ctx
, callerstr
, length
))
991 return; /* GL_INVALID_VALUE */
993 log_msg(ctx
, gl_enum_to_debug_source(source
),
994 gl_enum_to_debug_type(type
), id
,
995 gl_enum_to_debug_severity(severity
),
1001 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
1002 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
1003 GLsizei
*lengths
, GLchar
*messageLog
)
1005 GET_CURRENT_CONTEXT(ctx
);
1006 struct gl_debug_state
*debug
;
1013 _mesa_error(ctx
, GL_INVALID_VALUE
,
1014 "glGetDebugMessageLog(logSize=%d : logSize must not be"
1015 " negative)", logSize
);
1019 debug
= _mesa_lock_debug_state(ctx
);
1023 for (ret
= 0; ret
< count
; ret
++) {
1024 const struct gl_debug_message
*msg
= debug_fetch_message(debug
);
1029 if (logSize
< msg
->length
&& messageLog
!= NULL
)
1033 assert(msg
->message
[msg
->length
-1] == '\0');
1034 (void) strncpy(messageLog
, msg
->message
, (size_t)msg
->length
);
1036 messageLog
+= msg
->length
;
1037 logSize
-= msg
->length
;
1041 *lengths
++ = msg
->length
;
1043 *severities
++ = debug_severity_enums
[msg
->severity
];
1045 *sources
++ = debug_source_enums
[msg
->source
];
1047 *types
++ = debug_type_enums
[msg
->type
];
1051 debug_delete_messages(debug
, 1);
1054 _mesa_unlock_debug_state(ctx
);
1061 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
1062 GLenum gl_severity
, GLsizei count
,
1063 const GLuint
*ids
, GLboolean enabled
)
1065 GET_CURRENT_CONTEXT(ctx
);
1066 enum mesa_debug_source source
= gl_enum_to_debug_source(gl_source
);
1067 enum mesa_debug_type type
= gl_enum_to_debug_type(gl_type
);
1068 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(gl_severity
);
1069 const char *callerstr
= "glDebugMessageControl";
1070 struct gl_debug_state
*debug
;
1073 _mesa_error(ctx
, GL_INVALID_VALUE
,
1074 "%s(count=%d : count must not be negative)", callerstr
,
1079 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
1081 return; /* GL_INVALID_ENUM */
1083 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
1084 || gl_source
== GL_DONT_CARE
)) {
1085 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1086 "%s(When passing an array of ids, severity must be"
1087 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
1092 debug
= _mesa_lock_debug_state(ctx
);
1098 for (i
= 0; i
< count
; i
++)
1099 debug_set_message_enable(debug
, source
, type
, ids
[i
], enabled
);
1102 debug_set_message_enable_all(debug
, source
, type
, severity
, enabled
);
1105 _mesa_unlock_debug_state(ctx
);
1110 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
1112 GET_CURRENT_CONTEXT(ctx
);
1113 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
1115 debug
->Callback
= callback
;
1116 debug
->CallbackData
= userParam
;
1117 _mesa_unlock_debug_state(ctx
);
1123 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
1124 const GLchar
*message
)
1126 GET_CURRENT_CONTEXT(ctx
);
1127 const char *callerstr
= "glPushDebugGroup";
1128 struct gl_debug_state
*debug
;
1129 struct gl_debug_message
*emptySlot
;
1132 case GL_DEBUG_SOURCE_APPLICATION
:
1133 case GL_DEBUG_SOURCE_THIRD_PARTY
:
1136 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
1137 "(source=0x%x)", callerstr
, source
);
1142 length
= strlen(message
);
1143 if (!validate_length(ctx
, callerstr
, length
))
1144 return; /* GL_INVALID_VALUE */
1146 debug
= _mesa_lock_debug_state(ctx
);
1150 if (debug
->GroupStackDepth
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
1151 _mesa_unlock_debug_state(ctx
);
1152 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
1156 /* pop reuses the message details from push so we store this */
1157 emptySlot
= debug_get_group_message(debug
);
1158 debug_message_store(emptySlot
,
1159 gl_enum_to_debug_source(source
),
1160 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
1162 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1165 debug_push_group(debug
);
1167 log_msg_locked_and_unlock(ctx
,
1168 gl_enum_to_debug_source(source
),
1169 MESA_DEBUG_TYPE_PUSH_GROUP
, id
,
1170 MESA_DEBUG_SEVERITY_NOTIFICATION
, length
,
1176 _mesa_PopDebugGroup(void)
1178 GET_CURRENT_CONTEXT(ctx
);
1179 const char *callerstr
= "glPopDebugGroup";
1180 struct gl_debug_state
*debug
;
1181 struct gl_debug_message
*gdmessage
, msg
;
1183 debug
= _mesa_lock_debug_state(ctx
);
1187 if (debug
->GroupStackDepth
<= 0) {
1188 _mesa_unlock_debug_state(ctx
);
1189 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
1193 debug_pop_group(debug
);
1195 /* make a shallow copy */
1196 gdmessage
= debug_get_group_message(debug
);
1198 gdmessage
->message
= NULL
;
1199 gdmessage
->length
= 0;
1201 log_msg_locked_and_unlock(ctx
,
1203 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
1205 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1206 msg
.length
, msg
.message
);
1208 debug_message_clear(&msg
);
1213 _mesa_init_errors(struct gl_context
*ctx
)
1215 mtx_init(&ctx
->DebugMutex
, mtx_plain
);
1220 _mesa_free_errors_data(struct gl_context
*ctx
)
1223 debug_destroy(ctx
->Debug
);
1224 /* set to NULL just in case it is used before context is completely gone. */
1228 mtx_destroy(&ctx
->DebugMutex
);
1232 /**********************************************************************/
1233 /** \name Diagnostics */
1236 static FILE *LogFile
= NULL
;
1240 output_if_debug(const char *prefixString
, const char *outputString
,
1243 static int debug
= -1;
1245 /* Init the local 'debug' var once.
1246 * Note: the _mesa_init_debug() function should have been called
1247 * by now so MESA_DEBUG_FLAGS will be initialized.
1250 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1251 * etc to the named file. Otherwise, output to stderr.
1253 const char *logFile
= getenv("MESA_LOG_FILE");
1255 LogFile
= fopen(logFile
, "w");
1259 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1260 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1265 /* in release builds, be silent unless MESA_DEBUG is set */
1266 debug
= getenv("MESA_DEBUG") != NULL
;
1270 /* Now only print the string if we're required to do so. */
1273 fprintf(LogFile
, "%s: %s", prefixString
, outputString
);
1275 fprintf(LogFile
, "%s", outputString
);
1277 fprintf(LogFile
, "\n");
1281 /* stderr from windows applications without console is not usually
1282 * visible, so communicate with the debugger instead */
1285 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1286 OutputDebugStringA(buf
);
1294 * Return the file handle to use for debug/logging. Defaults to stderr
1295 * unless MESA_LOG_FILE is defined.
1298 _mesa_get_log_file(void)
1306 * When a new type of error is recorded, print a message describing
1307 * previous errors which were accumulated.
1310 flush_delayed_errors( struct gl_context
*ctx
)
1312 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1314 if (ctx
->ErrorDebugCount
) {
1315 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1316 ctx
->ErrorDebugCount
,
1317 _mesa_enum_to_string(ctx
->ErrorValue
));
1319 output_if_debug("Mesa", s
, GL_TRUE
);
1321 ctx
->ErrorDebugCount
= 0;
1327 * Report a warning (a recoverable error condition) to stderr if
1328 * either DEBUG is defined or the MESA_DEBUG env var is set.
1330 * \param ctx GL context.
1331 * \param fmtString printf()-like format string.
1334 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1336 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1338 va_start( args
, fmtString
);
1339 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1343 flush_delayed_errors( ctx
);
1345 output_if_debug("Mesa warning", str
, GL_TRUE
);
1350 * Report an internal implementation problem.
1351 * Prints the message to stderr via fprintf().
1353 * \param ctx GL context.
1354 * \param fmtString problem description string.
1357 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1360 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1361 static int numCalls
= 0;
1365 if (numCalls
< 50) {
1368 va_start( args
, fmtString
);
1369 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1371 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1372 PACKAGE_VERSION
, str
);
1373 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1379 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1381 static GLint debug
= -1;
1383 /* Check debug environment variable only once:
1386 const char *debugEnv
= getenv("MESA_DEBUG");
1389 if (debugEnv
&& strstr(debugEnv
, "silent"))
1402 if (ctx
->ErrorValue
!= error
||
1403 ctx
->ErrorDebugFmtString
!= fmtString
) {
1404 flush_delayed_errors( ctx
);
1405 ctx
->ErrorDebugFmtString
= fmtString
;
1406 ctx
->ErrorDebugCount
= 0;
1409 ctx
->ErrorDebugCount
++;
1416 _mesa_gl_vdebug(struct gl_context
*ctx
,
1418 enum mesa_debug_source source
,
1419 enum mesa_debug_type type
,
1420 enum mesa_debug_severity severity
,
1421 const char *fmtString
,
1424 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1429 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1431 log_msg(ctx
, source
, type
, *id
, severity
, len
, s
);
1436 _mesa_gl_debug(struct gl_context
*ctx
,
1438 enum mesa_debug_source source
,
1439 enum mesa_debug_type type
,
1440 enum mesa_debug_severity severity
,
1441 const char *fmtString
, ...)
1444 va_start(args
, fmtString
);
1445 _mesa_gl_vdebug(ctx
, id
, source
, type
, severity
, fmtString
, args
);
1451 * Record an OpenGL state error. These usually occur when the user
1452 * passes invalid parameters to a GL function.
1454 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1455 * run-time via the MESA_DEBUG environment variable), report the error with
1458 * \param ctx the GL context.
1459 * \param error the error value.
1460 * \param fmtString printf() style format string, followed by optional args
1463 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1465 GLboolean do_output
, do_log
;
1466 /* Ideally this would be set up by the caller, so that we had proper IDs
1467 * per different message.
1469 static GLuint error_msg_id
= 0;
1471 debug_get_id(&error_msg_id
);
1473 do_output
= should_output(ctx
, error
, fmtString
);
1475 mtx_lock(&ctx
->DebugMutex
);
1477 do_log
= debug_is_message_enabled(ctx
->Debug
,
1478 MESA_DEBUG_SOURCE_API
,
1479 MESA_DEBUG_TYPE_ERROR
,
1481 MESA_DEBUG_SEVERITY_HIGH
);
1486 mtx_unlock(&ctx
->DebugMutex
);
1488 if (do_output
|| do_log
) {
1489 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1493 va_start(args
, fmtString
);
1494 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1497 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1498 /* Too long error message. Whoever calls _mesa_error should use
1505 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1506 _mesa_enum_to_string(error
), s
);
1507 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1508 /* Same as above. */
1513 /* Print the error to stderr if needed. */
1515 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1518 /* Log the error via ARB_debug_output if needed.*/
1520 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_ERROR
,
1521 error_msg_id
, MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1525 /* Set the GL context error state for glGetError. */
1526 _mesa_record_error(ctx
, error
);
1530 _mesa_error_no_memory(const char *caller
)
1532 GET_CURRENT_CONTEXT(ctx
);
1533 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "out of memory in %s", caller
);
1537 * Report debug information. Print error message to stderr via fprintf().
1538 * No-op if DEBUG mode not enabled.
1540 * \param ctx GL context.
1541 * \param fmtString printf()-style format string, followed by optional args.
1544 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1547 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1549 va_start(args
, fmtString
);
1550 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1552 output_if_debug("Mesa", s
, GL_FALSE
);
1560 _mesa_log(const char *fmtString
, ...)
1562 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1564 va_start(args
, fmtString
);
1565 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1567 output_if_debug("", s
, GL_FALSE
);
1572 * Report debug information from the shader compiler via GL_ARB_debug_output.
1574 * \param ctx GL context.
1575 * \param type The namespace to which this message belongs.
1576 * \param id The message ID within the given namespace.
1577 * \param msg The message to output. Need not be null-terminated.
1578 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1581 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1582 const char *msg
, int len
)
1584 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1585 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1592 /* Truncate the message if necessary. */
1593 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1594 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1596 log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);