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"
43 static mtx_t DynamicIDMutex
= _MTX_INITIALIZER_NP
;
44 static GLuint NextDynamicID
= 1;
47 * A namespace element.
49 struct gl_debug_element
51 struct simple_node link
;
54 /* at which severity levels (mesa_debug_severity) is the message enabled */
58 struct gl_debug_namespace
60 struct simple_node Elements
;
61 GLbitfield DefaultState
;
64 struct gl_debug_group
{
65 struct gl_debug_namespace Namespaces
[MESA_DEBUG_SOURCE_COUNT
][MESA_DEBUG_TYPE_COUNT
];
69 * An error, warning, or other piece of debug information for an application
70 * to consume via GL_ARB_debug_output/GL_KHR_debug.
72 struct gl_debug_message
74 enum mesa_debug_source source
;
75 enum mesa_debug_type type
;
77 enum mesa_debug_severity severity
;
83 * Debug message log. It works like a ring buffer.
86 struct gl_debug_message Messages
[MAX_DEBUG_LOGGED_MESSAGES
];
94 const void *CallbackData
;
96 GLboolean DebugOutput
;
98 struct gl_debug_group
*Groups
[MAX_DEBUG_GROUP_STACK_DEPTH
];
99 struct gl_debug_message GroupMessages
[MAX_DEBUG_GROUP_STACK_DEPTH
];
100 GLint GroupStackDepth
;
102 struct gl_debug_log Log
;
105 static char out_of_memory
[] = "Debugging error: out of memory";
107 static const GLenum debug_source_enums
[] = {
109 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
110 GL_DEBUG_SOURCE_SHADER_COMPILER
,
111 GL_DEBUG_SOURCE_THIRD_PARTY
,
112 GL_DEBUG_SOURCE_APPLICATION
,
113 GL_DEBUG_SOURCE_OTHER
,
116 static const GLenum debug_type_enums
[] = {
118 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
119 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
120 GL_DEBUG_TYPE_PORTABILITY
,
121 GL_DEBUG_TYPE_PERFORMANCE
,
123 GL_DEBUG_TYPE_MARKER
,
124 GL_DEBUG_TYPE_PUSH_GROUP
,
125 GL_DEBUG_TYPE_POP_GROUP
,
128 static const GLenum debug_severity_enums
[] = {
129 GL_DEBUG_SEVERITY_LOW
,
130 GL_DEBUG_SEVERITY_MEDIUM
,
131 GL_DEBUG_SEVERITY_HIGH
,
132 GL_DEBUG_SEVERITY_NOTIFICATION
,
136 static enum mesa_debug_source
137 gl_enum_to_debug_source(GLenum e
)
141 for (i
= 0; i
< ARRAY_SIZE(debug_source_enums
); i
++) {
142 if (debug_source_enums
[i
] == e
)
148 static enum mesa_debug_type
149 gl_enum_to_debug_type(GLenum e
)
153 for (i
= 0; i
< ARRAY_SIZE(debug_type_enums
); i
++) {
154 if (debug_type_enums
[i
] == e
)
160 static enum mesa_debug_severity
161 gl_enum_to_debug_severity(GLenum e
)
165 for (i
= 0; i
< ARRAY_SIZE(debug_severity_enums
); i
++) {
166 if (debug_severity_enums
[i
] == e
)
174 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
177 * The GL API has this "ID" mechanism, where the intention is to allow a
178 * client to filter in/out messages based on source, type, and ID. Of course,
179 * building a giant enum list of all debug output messages that Mesa might
180 * generate is ridiculous, so instead we have our caller pass us a pointer to
181 * static storage where the ID should get stored. This ID will be shared
182 * across all contexts for that message (which seems like a desirable
183 * property, even if it's not expected by the spec), but note that it won't be
184 * the same between executions if messages aren't generated in the same order.
187 debug_get_id(GLuint
*id
)
190 mtx_lock(&DynamicIDMutex
);
192 *id
= NextDynamicID
++;
193 mtx_unlock(&DynamicIDMutex
);
198 debug_message_clear(struct gl_debug_message
*msg
)
200 if (msg
->message
!= (char*)out_of_memory
)
207 debug_message_store(struct gl_debug_message
*msg
,
208 enum mesa_debug_source source
,
209 enum mesa_debug_type type
, GLuint id
,
210 enum mesa_debug_severity severity
,
211 GLsizei len
, const char *buf
)
213 assert(!msg
->message
&& !msg
->length
);
215 msg
->message
= malloc(len
+1);
217 (void) strncpy(msg
->message
, buf
, (size_t)len
);
218 msg
->message
[len
] = '\0';
221 msg
->source
= source
;
224 msg
->severity
= severity
;
226 static GLuint oom_msg_id
= 0;
227 debug_get_id(&oom_msg_id
);
230 msg
->message
= out_of_memory
;
231 msg
->length
= strlen(out_of_memory
)+1;
232 msg
->source
= MESA_DEBUG_SOURCE_OTHER
;
233 msg
->type
= MESA_DEBUG_TYPE_ERROR
;
234 msg
->id
= oom_msg_id
;
235 msg
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
240 debug_namespace_init(struct gl_debug_namespace
*ns
)
242 make_empty_list(&ns
->Elements
);
244 /* Enable all the messages with severity HIGH or MEDIUM by default */
245 ns
->DefaultState
= (1 << MESA_DEBUG_SEVERITY_HIGH
) |
246 (1 << MESA_DEBUG_SEVERITY_MEDIUM
);
250 debug_namespace_clear(struct gl_debug_namespace
*ns
)
252 struct simple_node
*node
, *tmp
;
254 foreach_s(node
, tmp
, &ns
->Elements
)
259 debug_namespace_copy(struct gl_debug_namespace
*dst
,
260 const struct gl_debug_namespace
*src
)
262 struct simple_node
*node
;
264 dst
->DefaultState
= src
->DefaultState
;
266 make_empty_list(&dst
->Elements
);
267 foreach(node
, &src
->Elements
) {
268 const struct gl_debug_element
*elem
=
269 (const struct gl_debug_element
*) node
;
270 struct gl_debug_element
*copy
;
272 copy
= malloc(sizeof(*copy
));
274 debug_namespace_clear(dst
);
279 copy
->State
= elem
->State
;
280 insert_at_tail(&dst
->Elements
, ©
->link
);
287 * Set the state of \p id in the namespace.
290 debug_namespace_set(struct gl_debug_namespace
*ns
,
291 GLuint id
, bool enabled
)
293 const uint32_t state
= (enabled
) ?
294 ((1 << MESA_DEBUG_SEVERITY_COUNT
) - 1) : 0;
295 struct gl_debug_element
*elem
= NULL
;
296 struct simple_node
*node
;
298 /* find the element */
299 foreach(node
, &ns
->Elements
) {
300 struct gl_debug_element
*tmp
= (struct gl_debug_element
*) node
;
307 /* we do not need the element if it has the default state */
308 if (ns
->DefaultState
== state
) {
310 remove_from_list(&elem
->link
);
317 elem
= malloc(sizeof(*elem
));
322 insert_at_tail(&ns
->Elements
, &elem
->link
);
331 * Set the default state of the namespace for \p severity. When \p severity
332 * is MESA_DEBUG_SEVERITY_COUNT, the default values for all severities are
336 debug_namespace_set_all(struct gl_debug_namespace
*ns
,
337 enum mesa_debug_severity severity
,
340 struct simple_node
*node
, *tmp
;
343 /* set all elements to the same state */
344 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
345 ns
->DefaultState
= (enabled
) ? ((1 << severity
) - 1) : 0;
346 debug_namespace_clear(ns
);
347 make_empty_list(&ns
->Elements
);
351 mask
= 1 << severity
;
352 val
= (enabled
) ? mask
: 0;
354 ns
->DefaultState
= (ns
->DefaultState
& ~mask
) | val
;
356 foreach_s(node
, tmp
, &ns
->Elements
) {
357 struct gl_debug_element
*elem
= (struct gl_debug_element
*) node
;
359 elem
->State
= (elem
->State
& ~mask
) | val
;
360 if (elem
->State
== ns
->DefaultState
) {
361 remove_from_list(node
);
368 * Get the state of \p id in the namespace.
371 debug_namespace_get(const struct gl_debug_namespace
*ns
, GLuint id
,
372 enum mesa_debug_severity severity
)
374 struct simple_node
*node
;
377 state
= ns
->DefaultState
;
378 foreach(node
, &ns
->Elements
) {
379 struct gl_debug_element
*elem
= (struct gl_debug_element
*) node
;
381 if (elem
->ID
== id
) {
387 return (state
& (1 << severity
));
391 * Allocate and initialize context debug state.
393 static struct gl_debug_state
*
396 struct gl_debug_state
*debug
;
399 debug
= CALLOC_STRUCT(gl_debug_state
);
403 debug
->Groups
[0] = malloc(sizeof(*debug
->Groups
[0]));
404 if (!debug
->Groups
[0]) {
409 /* Initialize state for filtering known debug messages. */
410 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
411 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
412 debug_namespace_init(&debug
->Groups
[0]->Namespaces
[s
][t
]);
419 * Return true if the top debug group points to the group below it.
422 debug_is_group_read_only(const struct gl_debug_state
*debug
)
424 const GLint gstack
= debug
->GroupStackDepth
;
425 return (gstack
> 0 && debug
->Groups
[gstack
] == debug
->Groups
[gstack
- 1]);
429 * Make the top debug group writable.
432 debug_make_group_writable(struct gl_debug_state
*debug
)
434 const GLint gstack
= debug
->GroupStackDepth
;
435 const struct gl_debug_group
*src
= debug
->Groups
[gstack
];
436 struct gl_debug_group
*dst
;
439 if (!debug_is_group_read_only(debug
))
442 dst
= malloc(sizeof(*dst
));
446 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
447 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
448 if (!debug_namespace_copy(&dst
->Namespaces
[s
][t
],
449 &src
->Namespaces
[s
][t
])) {
451 for (t
= t
- 1; t
>= 0; t
--)
452 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
453 for (s
= s
- 1; s
>= 0; s
--) {
454 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
455 debug_namespace_clear(&dst
->Namespaces
[s
][t
]);
463 debug
->Groups
[gstack
] = dst
;
469 * Free the top debug group.
472 debug_clear_group(struct gl_debug_state
*debug
)
474 const GLint gstack
= debug
->GroupStackDepth
;
476 if (!debug_is_group_read_only(debug
)) {
477 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
480 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
481 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++)
482 debug_namespace_clear(&grp
->Namespaces
[s
][t
]);
488 debug
->Groups
[gstack
] = NULL
;
492 * Loop through debug group stack tearing down states for
493 * filtering debug messages. Then free debug output state.
496 debug_destroy(struct gl_debug_state
*debug
)
498 while (debug
->GroupStackDepth
> 0) {
499 debug_clear_group(debug
);
500 debug
->GroupStackDepth
--;
503 debug_clear_group(debug
);
508 * Sets the state of the given message source/type/ID tuple.
511 debug_set_message_enable(struct gl_debug_state
*debug
,
512 enum mesa_debug_source source
,
513 enum mesa_debug_type type
,
514 GLuint id
, GLboolean enabled
)
516 const GLint gstack
= debug
->GroupStackDepth
;
517 struct gl_debug_namespace
*ns
;
519 debug_make_group_writable(debug
);
520 ns
= &debug
->Groups
[gstack
]->Namespaces
[source
][type
];
522 debug_namespace_set(ns
, id
, enabled
);
526 * Set the state of all message IDs found in the given intersection of
527 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
528 * GL_DONT_CARE (include all messages in the class).
530 * This requires both setting the state of all previously seen message
531 * IDs in the hash table, and setting the default state for all
532 * applicable combinations of source/type/severity, so that all the
533 * yet-unknown message IDs that may be used in the future will be
534 * impacted as if they were already known.
537 debug_set_message_enable_all(struct gl_debug_state
*debug
,
538 enum mesa_debug_source source
,
539 enum mesa_debug_type type
,
540 enum mesa_debug_severity severity
,
543 const GLint gstack
= debug
->GroupStackDepth
;
544 int s
, t
, smax
, tmax
;
546 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
548 smax
= MESA_DEBUG_SOURCE_COUNT
;
553 if (type
== MESA_DEBUG_TYPE_COUNT
) {
555 tmax
= MESA_DEBUG_TYPE_COUNT
;
560 debug_make_group_writable(debug
);
562 for (s
= source
; s
< smax
; s
++) {
563 for (t
= type
; t
< tmax
; t
++) {
564 struct gl_debug_namespace
*nspace
=
565 &debug
->Groups
[gstack
]->Namespaces
[s
][t
];
566 debug_namespace_set_all(nspace
, severity
, enabled
);
572 * Returns if the given message source/type/ID tuple is enabled.
575 debug_is_message_enabled(const struct gl_debug_state
*debug
,
576 enum mesa_debug_source source
,
577 enum mesa_debug_type type
,
579 enum mesa_debug_severity severity
)
581 const GLint gstack
= debug
->GroupStackDepth
;
582 struct gl_debug_group
*grp
= debug
->Groups
[gstack
];
583 struct gl_debug_namespace
*nspace
= &grp
->Namespaces
[source
][type
];
585 if (!debug
->DebugOutput
)
588 return debug_namespace_get(nspace
, id
, severity
);
592 * 'buf' is not necessarily a null-terminated string. When logging, copy
593 * 'len' characters from it, store them in a new, null-terminated string,
594 * and remember the number of bytes used by that string, *including*
595 * the null terminator this time.
598 debug_log_message(struct gl_debug_state
*debug
,
599 enum mesa_debug_source source
,
600 enum mesa_debug_type type
, GLuint id
,
601 enum mesa_debug_severity severity
,
602 GLsizei len
, const char *buf
)
604 struct gl_debug_log
*log
= &debug
->Log
;
606 struct gl_debug_message
*emptySlot
;
608 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
610 if (log
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
613 nextEmpty
= (log
->NextMessage
+ log
->NumMessages
)
614 % MAX_DEBUG_LOGGED_MESSAGES
;
615 emptySlot
= &log
->Messages
[nextEmpty
];
617 debug_message_store(emptySlot
, source
, type
,
618 id
, severity
, len
, buf
);
624 * Return the oldest debug message out of the log.
626 static const struct gl_debug_message
*
627 debug_fetch_message(const struct gl_debug_state
*debug
)
629 const struct gl_debug_log
*log
= &debug
->Log
;
631 return (log
->NumMessages
) ? &log
->Messages
[log
->NextMessage
] : NULL
;
635 * Delete the oldest debug messages out of the log.
638 debug_delete_messages(struct gl_debug_state
*debug
, int count
)
640 struct gl_debug_log
*log
= &debug
->Log
;
642 if (count
> log
->NumMessages
)
643 count
= log
->NumMessages
;
646 struct gl_debug_message
*msg
= &log
->Messages
[log
->NextMessage
];
648 debug_message_clear(msg
);
652 log
->NextMessage
%= MAX_DEBUG_LOGGED_MESSAGES
;
656 static struct gl_debug_message
*
657 debug_get_group_message(struct gl_debug_state
*debug
)
659 return &debug
->GroupMessages
[debug
->GroupStackDepth
];
663 debug_push_group(struct gl_debug_state
*debug
)
665 const GLint gstack
= debug
->GroupStackDepth
;
667 /* just point to the previous stack */
668 debug
->Groups
[gstack
+ 1] = debug
->Groups
[gstack
];
669 debug
->GroupStackDepth
++;
673 debug_pop_group(struct gl_debug_state
*debug
)
675 debug_clear_group(debug
);
676 debug
->GroupStackDepth
--;
681 * Lock and return debug state for the context. The debug state will be
682 * allocated and initialized upon the first call. When NULL is returned, the
683 * debug state is not locked.
685 static struct gl_debug_state
*
686 _mesa_lock_debug_state(struct gl_context
*ctx
)
688 mtx_lock(&ctx
->DebugMutex
);
691 ctx
->Debug
= debug_create();
693 GET_CURRENT_CONTEXT(cur
);
694 mtx_unlock(&ctx
->DebugMutex
);
697 * This function may be called from other threads. When that is the
698 * case, we cannot record this OOM error.
701 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
711 _mesa_unlock_debug_state(struct gl_context
*ctx
)
713 mtx_unlock(&ctx
->DebugMutex
);
717 * Set the integer debug state specified by \p pname. This can be called from
718 * _mesa_set_enable for example.
721 _mesa_set_debug_state_int(struct gl_context
*ctx
, GLenum pname
, GLint val
)
723 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
729 case GL_DEBUG_OUTPUT
:
730 debug
->DebugOutput
= (val
!= 0);
732 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
733 debug
->SyncOutput
= (val
!= 0);
736 assert(!"unknown debug output param");
740 _mesa_unlock_debug_state(ctx
);
746 * Query the integer debug state specified by \p pname. This can be called
747 * _mesa_GetIntegerv for example.
750 _mesa_get_debug_state_int(struct gl_context
*ctx
, GLenum pname
)
752 struct gl_debug_state
*debug
;
755 mtx_lock(&ctx
->DebugMutex
);
758 mtx_unlock(&ctx
->DebugMutex
);
763 case GL_DEBUG_OUTPUT
:
764 val
= debug
->DebugOutput
;
766 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
767 val
= debug
->SyncOutput
;
769 case GL_DEBUG_LOGGED_MESSAGES
:
770 val
= debug
->Log
.NumMessages
;
772 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH
:
773 val
= (debug
->Log
.NumMessages
) ?
774 debug
->Log
.Messages
[debug
->Log
.NextMessage
].length
: 0;
776 case GL_DEBUG_GROUP_STACK_DEPTH
:
777 val
= debug
->GroupStackDepth
;
780 assert(!"unknown debug output param");
785 mtx_unlock(&ctx
->DebugMutex
);
791 * Query the pointer debug state specified by \p pname. This can be called
792 * _mesa_GetPointerv for example.
795 _mesa_get_debug_state_ptr(struct gl_context
*ctx
, GLenum pname
)
797 struct gl_debug_state
*debug
;
800 mtx_lock(&ctx
->DebugMutex
);
803 mtx_unlock(&ctx
->DebugMutex
);
808 case GL_DEBUG_CALLBACK_FUNCTION_ARB
:
809 val
= (void *) debug
->Callback
;
811 case GL_DEBUG_CALLBACK_USER_PARAM_ARB
:
812 val
= (void *) debug
->CallbackData
;
815 assert(!"unknown debug output param");
820 mtx_unlock(&ctx
->DebugMutex
);
826 * Insert a debug message. The mutex is assumed to be locked, and will be
827 * unlocked by this call.
830 log_msg_locked_and_unlock(struct gl_context
*ctx
,
831 enum mesa_debug_source source
,
832 enum mesa_debug_type type
, GLuint id
,
833 enum mesa_debug_severity severity
,
834 GLint len
, const char *buf
)
836 struct gl_debug_state
*debug
= ctx
->Debug
;
838 if (!debug_is_message_enabled(debug
, source
, type
, id
, severity
)) {
839 _mesa_unlock_debug_state(ctx
);
843 if (ctx
->Debug
->Callback
) {
844 GLenum gl_source
= debug_source_enums
[source
];
845 GLenum gl_type
= debug_type_enums
[type
];
846 GLenum gl_severity
= debug_severity_enums
[severity
];
847 GLDEBUGPROC callback
= ctx
->Debug
->Callback
;
848 const void *data
= ctx
->Debug
->CallbackData
;
851 * When ctx->Debug->SyncOutput is GL_FALSE, the client is prepared for
852 * unsynchronous calls. When it is GL_TRUE, we will not spawn threads.
853 * In either case, we can call the callback unlocked.
855 _mesa_unlock_debug_state(ctx
);
856 callback(gl_source
, gl_type
, id
, gl_severity
, len
, buf
, data
);
859 debug_log_message(ctx
->Debug
, source
, type
, id
, severity
, len
, buf
);
860 _mesa_unlock_debug_state(ctx
);
865 * Log a client or driver debug message.
868 log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
869 enum mesa_debug_type type
, GLuint id
,
870 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
872 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
877 log_msg_locked_and_unlock(ctx
, source
, type
, id
, severity
, len
, buf
);
882 * Verify that source, type, and severity are valid enums.
884 * The 'caller' param is used for handling values available
885 * only in glDebugMessageInsert or glDebugMessageControl
888 validate_params(struct gl_context
*ctx
, unsigned caller
,
889 const char *callerstr
, GLenum source
, GLenum type
,
895 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
896 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
898 case GL_DEBUG_SOURCE_API_ARB
:
899 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
900 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
901 case GL_DEBUG_SOURCE_OTHER_ARB
:
902 if (caller
!= INSERT
)
907 if (caller
== CONTROL
)
916 case GL_DEBUG_TYPE_ERROR_ARB
:
917 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
918 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
919 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
920 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
921 case GL_DEBUG_TYPE_OTHER_ARB
:
922 case GL_DEBUG_TYPE_MARKER
:
924 case GL_DEBUG_TYPE_PUSH_GROUP
:
925 case GL_DEBUG_TYPE_POP_GROUP
:
927 if (caller
== CONTROL
)
936 case GL_DEBUG_SEVERITY_HIGH_ARB
:
937 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
938 case GL_DEBUG_SEVERITY_LOW_ARB
:
939 case GL_DEBUG_SEVERITY_NOTIFICATION
:
942 if (caller
== CONTROL
)
952 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
953 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
954 source
, type
, severity
);
961 validate_length(struct gl_context
*ctx
, const char *callerstr
, GLsizei length
)
963 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
964 _mesa_error(ctx
, GL_INVALID_VALUE
,
965 "%s(length=%d, which is not less than "
966 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
967 MAX_DEBUG_MESSAGE_LENGTH
);
976 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
977 GLenum severity
, GLint length
,
980 const char *callerstr
= "glDebugMessageInsert";
982 GET_CURRENT_CONTEXT(ctx
);
984 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
985 return; /* GL_INVALID_ENUM */
988 length
= strlen(buf
);
989 if (!validate_length(ctx
, callerstr
, length
))
990 return; /* GL_INVALID_VALUE */
992 log_msg(ctx
, gl_enum_to_debug_source(source
),
993 gl_enum_to_debug_type(type
), id
,
994 gl_enum_to_debug_severity(severity
),
1000 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
1001 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
1002 GLsizei
*lengths
, GLchar
*messageLog
)
1004 GET_CURRENT_CONTEXT(ctx
);
1005 struct gl_debug_state
*debug
;
1012 _mesa_error(ctx
, GL_INVALID_VALUE
,
1013 "glGetDebugMessageLog(logSize=%d : logSize must not be"
1014 " negative)", logSize
);
1018 debug
= _mesa_lock_debug_state(ctx
);
1022 for (ret
= 0; ret
< count
; ret
++) {
1023 const struct gl_debug_message
*msg
= debug_fetch_message(debug
);
1028 if (logSize
< msg
->length
&& messageLog
!= NULL
)
1032 assert(msg
->message
[msg
->length
-1] == '\0');
1033 (void) strncpy(messageLog
, msg
->message
, (size_t)msg
->length
);
1035 messageLog
+= msg
->length
;
1036 logSize
-= msg
->length
;
1040 *lengths
++ = msg
->length
;
1042 *severities
++ = debug_severity_enums
[msg
->severity
];
1044 *sources
++ = debug_source_enums
[msg
->source
];
1046 *types
++ = debug_type_enums
[msg
->type
];
1050 debug_delete_messages(debug
, 1);
1053 _mesa_unlock_debug_state(ctx
);
1060 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
1061 GLenum gl_severity
, GLsizei count
,
1062 const GLuint
*ids
, GLboolean enabled
)
1064 GET_CURRENT_CONTEXT(ctx
);
1065 enum mesa_debug_source source
= gl_enum_to_debug_source(gl_source
);
1066 enum mesa_debug_type type
= gl_enum_to_debug_type(gl_type
);
1067 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(gl_severity
);
1068 const char *callerstr
= "glDebugMessageControl";
1069 struct gl_debug_state
*debug
;
1072 _mesa_error(ctx
, GL_INVALID_VALUE
,
1073 "%s(count=%d : count must not be negative)", callerstr
,
1078 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
1080 return; /* GL_INVALID_ENUM */
1082 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
1083 || gl_source
== GL_DONT_CARE
)) {
1084 _mesa_error(ctx
, GL_INVALID_OPERATION
,
1085 "%s(When passing an array of ids, severity must be"
1086 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
1091 debug
= _mesa_lock_debug_state(ctx
);
1097 for (i
= 0; i
< count
; i
++)
1098 debug_set_message_enable(debug
, source
, type
, ids
[i
], enabled
);
1101 debug_set_message_enable_all(debug
, source
, type
, severity
, enabled
);
1104 _mesa_unlock_debug_state(ctx
);
1109 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
1111 GET_CURRENT_CONTEXT(ctx
);
1112 struct gl_debug_state
*debug
= _mesa_lock_debug_state(ctx
);
1114 debug
->Callback
= callback
;
1115 debug
->CallbackData
= userParam
;
1116 _mesa_unlock_debug_state(ctx
);
1122 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
1123 const GLchar
*message
)
1125 GET_CURRENT_CONTEXT(ctx
);
1126 const char *callerstr
= "glPushDebugGroup";
1127 struct gl_debug_state
*debug
;
1128 struct gl_debug_message
*emptySlot
;
1131 case GL_DEBUG_SOURCE_APPLICATION
:
1132 case GL_DEBUG_SOURCE_THIRD_PARTY
:
1135 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
1136 "(source=0x%x)", callerstr
, source
);
1141 length
= strlen(message
);
1142 if (!validate_length(ctx
, callerstr
, length
))
1143 return; /* GL_INVALID_VALUE */
1145 debug
= _mesa_lock_debug_state(ctx
);
1149 if (debug
->GroupStackDepth
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
1150 _mesa_unlock_debug_state(ctx
);
1151 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
1155 /* pop reuses the message details from push so we store this */
1156 emptySlot
= debug_get_group_message(debug
);
1157 debug_message_store(emptySlot
,
1158 gl_enum_to_debug_source(source
),
1159 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
1161 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1164 debug_push_group(debug
);
1166 log_msg_locked_and_unlock(ctx
,
1167 gl_enum_to_debug_source(source
),
1168 MESA_DEBUG_TYPE_PUSH_GROUP
, id
,
1169 MESA_DEBUG_SEVERITY_NOTIFICATION
, length
,
1175 _mesa_PopDebugGroup(void)
1177 GET_CURRENT_CONTEXT(ctx
);
1178 const char *callerstr
= "glPopDebugGroup";
1179 struct gl_debug_state
*debug
;
1180 struct gl_debug_message
*gdmessage
, msg
;
1182 debug
= _mesa_lock_debug_state(ctx
);
1186 if (debug
->GroupStackDepth
<= 0) {
1187 _mesa_unlock_debug_state(ctx
);
1188 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
1192 debug_pop_group(debug
);
1194 /* make a shallow copy */
1195 gdmessage
= debug_get_group_message(debug
);
1197 gdmessage
->message
= NULL
;
1198 gdmessage
->length
= 0;
1200 log_msg_locked_and_unlock(ctx
,
1202 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
1204 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1205 msg
.length
, msg
.message
);
1207 debug_message_clear(&msg
);
1212 _mesa_init_errors(struct gl_context
*ctx
)
1214 mtx_init(&ctx
->DebugMutex
, mtx_plain
);
1219 _mesa_free_errors_data(struct gl_context
*ctx
)
1222 debug_destroy(ctx
->Debug
);
1223 /* set to NULL just in case it is used before context is completely gone. */
1227 mtx_destroy(&ctx
->DebugMutex
);
1231 /**********************************************************************/
1232 /** \name Diagnostics */
1236 output_if_debug(const char *prefixString
, const char *outputString
,
1239 static int debug
= -1;
1240 static FILE *fout
= NULL
;
1242 /* Init the local 'debug' var once.
1243 * Note: the _mesa_init_debug() function should have been called
1244 * by now so MESA_DEBUG_FLAGS will be initialized.
1247 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1248 * etc to the named file. Otherwise, output to stderr.
1250 const char *logFile
= getenv("MESA_LOG_FILE");
1252 fout
= fopen(logFile
, "w");
1256 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1257 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1262 /* in release builds, be silent unless MESA_DEBUG is set */
1263 debug
= getenv("MESA_DEBUG") != NULL
;
1267 /* Now only print the string if we're required to do so. */
1269 fprintf(fout
, "%s: %s", prefixString
, outputString
);
1271 fprintf(fout
, "\n");
1275 /* stderr from windows applications without console is not usually
1276 * visible, so communicate with the debugger instead */
1279 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1280 OutputDebugStringA(buf
);
1288 * When a new type of error is recorded, print a message describing
1289 * previous errors which were accumulated.
1292 flush_delayed_errors( struct gl_context
*ctx
)
1294 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1296 if (ctx
->ErrorDebugCount
) {
1297 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1298 ctx
->ErrorDebugCount
,
1299 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
1301 output_if_debug("Mesa", s
, GL_TRUE
);
1303 ctx
->ErrorDebugCount
= 0;
1309 * Report a warning (a recoverable error condition) to stderr if
1310 * either DEBUG is defined or the MESA_DEBUG env var is set.
1312 * \param ctx GL context.
1313 * \param fmtString printf()-like format string.
1316 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1318 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1320 va_start( args
, fmtString
);
1321 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1325 flush_delayed_errors( ctx
);
1327 output_if_debug("Mesa warning", str
, GL_TRUE
);
1332 * Report an internal implementation problem.
1333 * Prints the message to stderr via fprintf().
1335 * \param ctx GL context.
1336 * \param fmtString problem description string.
1339 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1342 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1343 static int numCalls
= 0;
1347 if (numCalls
< 50) {
1350 va_start( args
, fmtString
);
1351 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1353 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1354 PACKAGE_VERSION
, str
);
1355 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1361 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1363 static GLint debug
= -1;
1365 /* Check debug environment variable only once:
1368 const char *debugEnv
= getenv("MESA_DEBUG");
1371 if (debugEnv
&& strstr(debugEnv
, "silent"))
1384 if (ctx
->ErrorValue
!= error
||
1385 ctx
->ErrorDebugFmtString
!= fmtString
) {
1386 flush_delayed_errors( ctx
);
1387 ctx
->ErrorDebugFmtString
= fmtString
;
1388 ctx
->ErrorDebugCount
= 0;
1391 ctx
->ErrorDebugCount
++;
1398 _mesa_gl_debug(struct gl_context
*ctx
,
1400 enum mesa_debug_source source
,
1401 enum mesa_debug_type type
,
1402 enum mesa_debug_severity severity
,
1403 const char *fmtString
, ...)
1405 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1411 va_start(args
, fmtString
);
1412 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1415 log_msg(ctx
, source
, type
, *id
, severity
, len
, s
);
1420 * Record an OpenGL state error. These usually occur when the user
1421 * passes invalid parameters to a GL function.
1423 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1424 * run-time via the MESA_DEBUG environment variable), report the error with
1427 * \param ctx the GL context.
1428 * \param error the error value.
1429 * \param fmtString printf() style format string, followed by optional args
1432 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1434 GLboolean do_output
, do_log
;
1435 /* Ideally this would be set up by the caller, so that we had proper IDs
1436 * per different message.
1438 static GLuint error_msg_id
= 0;
1440 debug_get_id(&error_msg_id
);
1442 do_output
= should_output(ctx
, error
, fmtString
);
1444 mtx_lock(&ctx
->DebugMutex
);
1446 do_log
= debug_is_message_enabled(ctx
->Debug
,
1447 MESA_DEBUG_SOURCE_API
,
1448 MESA_DEBUG_TYPE_ERROR
,
1450 MESA_DEBUG_SEVERITY_HIGH
);
1455 mtx_unlock(&ctx
->DebugMutex
);
1457 if (do_output
|| do_log
) {
1458 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1462 va_start(args
, fmtString
);
1463 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1466 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1467 /* Too long error message. Whoever calls _mesa_error should use
1474 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1475 _mesa_lookup_enum_by_nr(error
), s
);
1476 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1477 /* Same as above. */
1482 /* Print the error to stderr if needed. */
1484 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1487 /* Log the error via ARB_debug_output if needed.*/
1489 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_ERROR
,
1490 error_msg_id
, MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1494 /* Set the GL context error state for glGetError. */
1495 _mesa_record_error(ctx
, error
);
1499 _mesa_error_no_memory(const char *caller
)
1501 GET_CURRENT_CONTEXT(ctx
);
1502 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "out of memory in %s", caller
);
1506 * Report debug information. Print error message to stderr via fprintf().
1507 * No-op if DEBUG mode not enabled.
1509 * \param ctx GL context.
1510 * \param fmtString printf()-style format string, followed by optional args.
1513 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1516 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1518 va_start(args
, fmtString
);
1519 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1521 output_if_debug("Mesa", s
, GL_FALSE
);
1529 * Report debug information from the shader compiler via GL_ARB_debug_output.
1531 * \param ctx GL context.
1532 * \param type The namespace to which this message belongs.
1533 * \param id The message ID within the given namespace.
1534 * \param msg The message to output. Need not be null-terminated.
1535 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1538 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1539 const char *msg
, int len
)
1541 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1542 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1549 /* Truncate the message if necessary. */
1550 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1551 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1553 log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);