3 * Mesa debugging and error handling functions.
7 * Mesa 3-D graphics library
9 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
39 #include "hash_table.h"
41 static mtx_t DynamicIDMutex
= _MTX_INITIALIZER_NP
;
42 static GLuint NextDynamicID
= 1;
44 struct gl_debug_severity
46 struct simple_node link
;
51 * An error, warning, or other piece of debug information for an application
52 * to consume via GL_ARB_debug_output/GL_KHR_debug.
54 struct gl_debug_message
56 enum mesa_debug_source source
;
57 enum mesa_debug_type type
;
59 enum mesa_debug_severity severity
;
65 * Debug message log. It works like a ring buffer.
68 struct gl_debug_message Messages
[MAX_DEBUG_LOGGED_MESSAGES
];
73 struct gl_debug_namespace
75 struct _mesa_HashTable
*IDs
;
76 unsigned ZeroID
; /* a HashTable won't take zero, so store its state here */
77 /** lists of IDs in the hash table at each severity */
78 struct simple_node Severity
[MESA_DEBUG_SEVERITY_COUNT
];
84 const void *CallbackData
;
86 GLboolean DebugOutput
;
88 struct gl_debug_log Log
;
90 GLboolean Defaults
[MAX_DEBUG_GROUP_STACK_DEPTH
][MESA_DEBUG_SEVERITY_COUNT
][MESA_DEBUG_SOURCE_COUNT
][MESA_DEBUG_TYPE_COUNT
];
91 struct gl_debug_namespace Namespaces
[MAX_DEBUG_GROUP_STACK_DEPTH
][MESA_DEBUG_SOURCE_COUNT
][MESA_DEBUG_TYPE_COUNT
];
92 struct gl_debug_message DebugGroupMsgs
[MAX_DEBUG_GROUP_STACK_DEPTH
];
93 GLint GroupStackDepth
;
96 static char out_of_memory
[] = "Debugging error: out of memory";
98 static const GLenum debug_source_enums
[] = {
100 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
101 GL_DEBUG_SOURCE_SHADER_COMPILER
,
102 GL_DEBUG_SOURCE_THIRD_PARTY
,
103 GL_DEBUG_SOURCE_APPLICATION
,
104 GL_DEBUG_SOURCE_OTHER
,
107 static const GLenum debug_type_enums
[] = {
109 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
110 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
111 GL_DEBUG_TYPE_PORTABILITY
,
112 GL_DEBUG_TYPE_PERFORMANCE
,
114 GL_DEBUG_TYPE_MARKER
,
115 GL_DEBUG_TYPE_PUSH_GROUP
,
116 GL_DEBUG_TYPE_POP_GROUP
,
119 static const GLenum debug_severity_enums
[] = {
120 GL_DEBUG_SEVERITY_LOW
,
121 GL_DEBUG_SEVERITY_MEDIUM
,
122 GL_DEBUG_SEVERITY_HIGH
,
123 GL_DEBUG_SEVERITY_NOTIFICATION
,
127 static enum mesa_debug_source
128 gl_enum_to_debug_source(GLenum e
)
132 for (i
= 0; i
< Elements(debug_source_enums
); i
++) {
133 if (debug_source_enums
[i
] == e
)
139 static enum mesa_debug_type
140 gl_enum_to_debug_type(GLenum e
)
144 for (i
= 0; i
< Elements(debug_type_enums
); i
++) {
145 if (debug_type_enums
[i
] == e
)
151 static enum mesa_debug_severity
152 gl_enum_to_debug_severity(GLenum e
)
156 for (i
= 0; i
< Elements(debug_severity_enums
); i
++) {
157 if (debug_severity_enums
[i
] == e
)
165 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
168 * The GL API has this "ID" mechanism, where the intention is to allow a
169 * client to filter in/out messages based on source, type, and ID. Of course,
170 * building a giant enum list of all debug output messages that Mesa might
171 * generate is ridiculous, so instead we have our caller pass us a pointer to
172 * static storage where the ID should get stored. This ID will be shared
173 * across all contexts for that message (which seems like a desirable
174 * property, even if it's not expected by the spec), but note that it won't be
175 * the same between executions if messages aren't generated in the same order.
178 debug_get_id(GLuint
*id
)
181 mtx_lock(&DynamicIDMutex
);
183 *id
= NextDynamicID
++;
184 mtx_unlock(&DynamicIDMutex
);
190 * We store a bitfield in the hash table, with five possible values total.
192 * The ENABLED_BIT's purpose is self-explanatory.
194 * The FOUND_BIT is needed to differentiate the value of DISABLED from
195 * the value returned by HashTableLookup() when it can't find the given key.
197 * The KNOWN_SEVERITY bit is a bit complicated:
199 * A client may call Control() with an array of IDs, then call Control()
200 * on all message IDs of a certain severity, then Insert() one of the
201 * previously specified IDs, giving us a known severity level, then call
202 * Control() on all message IDs of a certain severity level again.
204 * After the first call, those IDs will have a FOUND_BIT, but will not
205 * exist in any severity-specific list, so the second call will not
206 * impact them. This is undesirable but unavoidable given the API:
207 * The only entrypoint that gives a severity for a client-defined ID
208 * is the Insert() call.
210 * For the sake of Control(), we want to maintain the invariant
211 * that an ID will either appear in none of the three severity lists,
212 * or appear once, to minimize pointless duplication and potential surprises.
214 * Because Insert() is the only place that will learn an ID's severity,
215 * it should insert an ID into the appropriate list, but only if the ID
216 * doesn't exist in it or any other list yet. Because searching all three
217 * lists at O(n) is needlessly expensive, we store KNOWN_SEVERITY.
221 ENABLED_BIT
= 1 << 1,
222 KNOWN_SEVERITY
= 1 << 2,
224 /* HashTable reserves zero as a return value meaning 'not found' */
226 DISABLED
= FOUND_BIT
,
227 ENABLED
= ENABLED_BIT
| FOUND_BIT
231 debug_message_clear(struct gl_debug_message
*msg
)
233 if (msg
->message
!= (char*)out_of_memory
)
240 debug_message_store(struct gl_debug_message
*msg
,
241 enum mesa_debug_source source
,
242 enum mesa_debug_type type
, GLuint id
,
243 enum mesa_debug_severity severity
,
244 GLsizei len
, const char *buf
)
246 assert(!msg
->message
&& !msg
->length
);
248 msg
->message
= malloc(len
+1);
250 (void) strncpy(msg
->message
, buf
, (size_t)len
);
251 msg
->message
[len
] = '\0';
254 msg
->source
= source
;
257 msg
->severity
= severity
;
259 static GLuint oom_msg_id
= 0;
260 debug_get_id(&oom_msg_id
);
263 msg
->message
= out_of_memory
;
264 msg
->length
= strlen(out_of_memory
)+1;
265 msg
->source
= MESA_DEBUG_SOURCE_OTHER
;
266 msg
->type
= MESA_DEBUG_TYPE_ERROR
;
267 msg
->id
= oom_msg_id
;
268 msg
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
273 * Allocate and initialize context debug state.
275 static struct gl_debug_state
*
278 struct gl_debug_state
*debug
;
281 debug
= CALLOC_STRUCT(gl_debug_state
);
285 /* Enable all the messages with severity HIGH or MEDIUM by default. */
286 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
], GL_TRUE
,
287 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_HIGH
]);
288 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
], GL_TRUE
,
289 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_MEDIUM
]);
290 memset(debug
->Defaults
[0][MESA_DEBUG_SEVERITY_LOW
], GL_FALSE
,
291 sizeof debug
->Defaults
[0][MESA_DEBUG_SEVERITY_LOW
]);
293 /* Initialize state for filtering known debug messages. */
294 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
295 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
296 debug
->Namespaces
[0][s
][t
].IDs
= _mesa_NewHashTable();
297 assert(debug
->Namespaces
[0][s
][t
].IDs
);
299 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
300 make_empty_list(&debug
->Namespaces
[0][s
][t
].Severity
[sev
]);
309 debug_clear_group_cb(GLuint key
, void *data
, void *userData
)
314 * Free debug state for the given stack depth.
317 debug_clear_group(struct gl_debug_state
*debug
, GLint gstack
)
319 enum mesa_debug_type t
;
320 enum mesa_debug_source s
;
321 enum mesa_debug_severity sev
;
323 /* Tear down state for filtering debug messages. */
324 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
325 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
326 struct gl_debug_namespace
*nspace
= &debug
->Namespaces
[gstack
][s
][t
];
328 _mesa_HashDeleteAll(nspace
->IDs
, debug_clear_group_cb
, NULL
);
329 _mesa_DeleteHashTable(nspace
->IDs
);
330 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
331 struct simple_node
*node
, *tmp
;
332 struct gl_debug_severity
*entry
;
334 foreach_s(node
, tmp
, &nspace
->Severity
[sev
]) {
335 entry
= (struct gl_debug_severity
*)node
;
344 * Loop through debug group stack tearing down states for
345 * filtering debug messages. Then free debug output state.
348 debug_destroy(struct gl_debug_state
*debug
)
352 for (i
= 0; i
<= debug
->GroupStackDepth
; i
++)
353 debug_clear_group(debug
, i
);
359 * Sets the state of the given message source/type/ID tuple.
362 debug_set_message_enable(struct gl_debug_state
*debug
,
363 enum mesa_debug_source source
,
364 enum mesa_debug_type type
,
365 GLuint id
, GLboolean enabled
)
367 GLint gstack
= debug
->GroupStackDepth
;
368 struct gl_debug_namespace
*nspace
=
369 &debug
->Namespaces
[gstack
][source
][type
];
372 /* In addition to not being able to store zero as a value, HashTable also
373 * can't use zero as a key.
376 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
378 state
= nspace
->ZeroID
;
380 if (state
== NOT_FOUND
)
381 state
= enabled
? ENABLED
: DISABLED
;
384 state
|= ENABLED_BIT
;
386 state
&= ~ENABLED_BIT
;
390 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
392 nspace
->ZeroID
= state
;
396 * Set the state of all message IDs found in the given intersection of
397 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
398 * GL_DONT_CARE (include all messages in the class).
400 * This requires both setting the state of all previously seen message
401 * IDs in the hash table, and setting the default state for all
402 * applicable combinations of source/type/severity, so that all the
403 * yet-unknown message IDs that may be used in the future will be
404 * impacted as if they were already known.
407 debug_set_message_enable_all(struct gl_debug_state
*debug
,
408 enum mesa_debug_source source
,
409 enum mesa_debug_type type
,
410 enum mesa_debug_severity severity
,
413 const GLint gstack
= debug
->GroupStackDepth
;
414 int s
, t
, sev
, smax
, tmax
, sevmax
;
416 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
418 smax
= MESA_DEBUG_SOURCE_COUNT
;
423 if (type
== MESA_DEBUG_TYPE_COUNT
) {
425 tmax
= MESA_DEBUG_TYPE_COUNT
;
430 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
432 sevmax
= MESA_DEBUG_SEVERITY_COUNT
;
437 for (sev
= severity
; sev
< sevmax
; sev
++) {
438 for (s
= source
; s
< smax
; s
++) {
439 for (t
= type
; t
< tmax
; t
++) {
440 struct simple_node
*node
;
441 struct gl_debug_severity
*entry
;
443 /* change the default for IDs we've never seen before. */
444 debug
->Defaults
[gstack
][sev
][s
][t
] = enabled
;
446 /* Now change the state of IDs we *have* seen... */
447 foreach(node
, &debug
->Namespaces
[gstack
][s
][t
].Severity
[sev
]) {
448 entry
= (struct gl_debug_severity
*)node
;
449 debug_set_message_enable(debug
, s
, t
, entry
->ID
, enabled
);
457 * Returns if the given message source/type/ID tuple is enabled.
460 debug_is_message_enabled(struct gl_debug_state
*debug
,
461 enum mesa_debug_source source
,
462 enum mesa_debug_type type
,
464 enum mesa_debug_severity severity
)
466 const GLint gstack
= debug
->GroupStackDepth
;
467 struct gl_debug_namespace
*nspace
=
468 &debug
->Namespaces
[gstack
][source
][type
];
471 if (!debug
->DebugOutput
)
474 /* In addition to not being able to store zero as a value, HashTable also
475 * can't use zero as a key.
478 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
480 state
= nspace
->ZeroID
;
482 /* Only do this once for each ID. This makes sure the ID exists in,
483 * at most, one list, and does not pointlessly appear multiple times.
485 if (!(state
& KNOWN_SEVERITY
)) {
486 struct gl_debug_severity
*entry
;
488 if (state
== NOT_FOUND
) {
489 if (debug
->Defaults
[gstack
][severity
][source
][type
])
495 entry
= malloc(sizeof *entry
);
499 state
|= KNOWN_SEVERITY
;
502 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
504 nspace
->ZeroID
= state
;
507 insert_at_tail(&nspace
->Severity
[severity
], &entry
->link
);
510 return (state
& ENABLED_BIT
);
514 * 'buf' is not necessarily a null-terminated string. When logging, copy
515 * 'len' characters from it, store them in a new, null-terminated string,
516 * and remember the number of bytes used by that string, *including*
517 * the null terminator this time.
520 debug_log_message(struct gl_debug_state
*debug
,
521 enum mesa_debug_source source
,
522 enum mesa_debug_type type
, GLuint id
,
523 enum mesa_debug_severity severity
,
524 GLsizei len
, const char *buf
)
526 struct gl_debug_log
*log
= &debug
->Log
;
528 struct gl_debug_message
*emptySlot
;
530 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
532 if (log
->NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
535 nextEmpty
= (log
->NextMessage
+ log
->NumMessages
)
536 % MAX_DEBUG_LOGGED_MESSAGES
;
537 emptySlot
= &log
->Messages
[nextEmpty
];
539 debug_message_store(emptySlot
, source
, type
,
540 id
, severity
, len
, buf
);
546 * Return the oldest debug message out of the log.
548 static const struct gl_debug_message
*
549 debug_fetch_message(const struct gl_debug_state
*debug
)
551 const struct gl_debug_log
*log
= &debug
->Log
;
553 return (log
->NumMessages
) ? &log
->Messages
[log
->NextMessage
] : NULL
;
557 * Delete the oldest debug messages out of the log.
560 debug_delete_messages(struct gl_debug_state
*debug
, unsigned count
)
562 struct gl_debug_log
*log
= &debug
->Log
;
564 if (count
> log
->NumMessages
)
565 count
= log
->NumMessages
;
568 struct gl_debug_message
*msg
= &log
->Messages
[log
->NextMessage
];
570 debug_message_clear(msg
);
574 log
->NextMessage
%= MAX_DEBUG_LOGGED_MESSAGES
;
578 static struct gl_debug_message
*
579 debug_get_group_message(struct gl_debug_state
*debug
)
581 return &debug
->DebugGroupMsgs
[debug
->GroupStackDepth
];
585 debug_push_group(struct gl_debug_state
*debug
)
587 const GLint gstack
= debug
->GroupStackDepth
;
590 /* inherit the control volume of the debug group previously residing on
591 * the top of the debug group stack
593 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++) {
594 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
595 const struct gl_debug_namespace
*nspace
=
596 &debug
->Namespaces
[gstack
][s
][t
];
597 struct gl_debug_namespace
*next
=
598 &debug
->Namespaces
[gstack
+ 1][s
][t
];
600 /* copy id settings */
601 next
->IDs
= _mesa_HashClone(nspace
->IDs
);
603 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
604 struct simple_node
*node
;
606 /* copy default settings for unknown ids */
607 debug
->Defaults
[gstack
+ 1][sev
][s
][t
] =
608 debug
->Defaults
[gstack
][sev
][s
][t
];
610 /* copy known id severity settings */
611 make_empty_list(&next
->Severity
[sev
]);
612 foreach(node
, &nspace
->Severity
[sev
]) {
613 const struct gl_debug_severity
*entry
=
614 (const struct gl_debug_severity
*) node
;
615 struct gl_debug_severity
*copy
;
617 copy
= malloc(sizeof *entry
);
621 copy
->ID
= entry
->ID
;
622 insert_at_tail(&next
->Severity
[sev
], ©
->link
);
629 debug
->GroupStackDepth
++;
633 debug_pop_group(struct gl_debug_state
*debug
)
635 const GLint gstack
= debug
->GroupStackDepth
;
637 debug
->GroupStackDepth
--;
638 debug_clear_group(debug
, gstack
);
643 * Return debug state for the context. The debug state will be allocated
644 * and initialized upon the first call.
646 static struct gl_debug_state
*
647 _mesa_get_debug_state(struct gl_context
*ctx
)
650 ctx
->Debug
= debug_create();
652 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "allocating debug state");
660 * Set the integer debug state specified by \p pname. This can be called from
661 * _mesa_set_enable for example.
664 _mesa_set_debug_state_int(struct gl_context
*ctx
, GLenum pname
, GLint val
)
666 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
672 case GL_DEBUG_OUTPUT
:
673 debug
->DebugOutput
= (val
!= 0);
675 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
676 debug
->SyncOutput
= (val
!= 0);
679 assert(!"unknown debug output param");
687 * Query the integer debug state specified by \p pname. This can be called
688 * _mesa_GetIntegerv for example.
691 _mesa_get_debug_state_int(struct gl_context
*ctx
, GLenum pname
)
693 struct gl_debug_state
*debug
;
701 case GL_DEBUG_OUTPUT
:
702 val
= debug
->DebugOutput
;
704 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
:
705 val
= debug
->SyncOutput
;
707 case GL_DEBUG_LOGGED_MESSAGES
:
708 val
= debug
->Log
.NumMessages
;
710 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH
:
711 val
= (debug
->Log
.NumMessages
) ?
712 debug
->Log
.Messages
[debug
->Log
.NextMessage
].length
: 0;
714 case GL_DEBUG_GROUP_STACK_DEPTH
:
715 val
= debug
->GroupStackDepth
;
718 assert(!"unknown debug output param");
727 * Query the pointer debug state specified by \p pname. This can be called
728 * _mesa_GetPointerv for example.
731 _mesa_get_debug_state_ptr(struct gl_context
*ctx
, GLenum pname
)
733 struct gl_debug_state
*debug
;
741 case GL_DEBUG_CALLBACK_FUNCTION_ARB
:
742 val
= (void *) debug
->Callback
;
744 case GL_DEBUG_CALLBACK_USER_PARAM_ARB
:
745 val
= (void *) debug
->CallbackData
;
748 assert(!"unknown debug output param");
758 * Log a client or driver debug message.
761 log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
762 enum mesa_debug_type type
, GLuint id
,
763 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
765 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
770 if (!debug_is_message_enabled(debug
, source
, type
, id
, severity
))
773 if (debug
->Callback
) {
774 GLenum gl_type
= debug_type_enums
[type
];
775 GLenum gl_severity
= debug_severity_enums
[severity
];
777 debug
->Callback(debug_source_enums
[source
], gl_type
, id
, gl_severity
,
778 len
, buf
, debug
->CallbackData
);
782 debug_log_message(debug
, source
, type
, id
, severity
, len
, buf
);
787 * Verify that source, type, and severity are valid enums.
789 * The 'caller' param is used for handling values available
790 * only in glDebugMessageInsert or glDebugMessageControl
793 validate_params(struct gl_context
*ctx
, unsigned caller
,
794 const char *callerstr
, GLenum source
, GLenum type
,
800 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
801 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
803 case GL_DEBUG_SOURCE_API_ARB
:
804 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
805 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
806 case GL_DEBUG_SOURCE_OTHER_ARB
:
807 if (caller
!= INSERT
)
812 if (caller
== CONTROL
)
821 case GL_DEBUG_TYPE_ERROR_ARB
:
822 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
823 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
824 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
825 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
826 case GL_DEBUG_TYPE_OTHER_ARB
:
827 case GL_DEBUG_TYPE_MARKER
:
829 case GL_DEBUG_TYPE_PUSH_GROUP
:
830 case GL_DEBUG_TYPE_POP_GROUP
:
832 if (caller
== CONTROL
)
841 case GL_DEBUG_SEVERITY_HIGH_ARB
:
842 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
843 case GL_DEBUG_SEVERITY_LOW_ARB
:
844 case GL_DEBUG_SEVERITY_NOTIFICATION
:
847 if (caller
== CONTROL
)
857 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
858 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
859 source
, type
, severity
);
866 validate_length(struct gl_context
*ctx
, const char *callerstr
, GLsizei length
)
868 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
869 _mesa_error(ctx
, GL_INVALID_VALUE
,
870 "%s(length=%d, which is not less than "
871 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr
, length
,
872 MAX_DEBUG_MESSAGE_LENGTH
);
881 _mesa_DebugMessageInsert(GLenum source
, GLenum type
, GLuint id
,
882 GLenum severity
, GLint length
,
885 const char *callerstr
= "glDebugMessageInsert";
887 GET_CURRENT_CONTEXT(ctx
);
889 if (!validate_params(ctx
, INSERT
, callerstr
, source
, type
, severity
))
890 return; /* GL_INVALID_ENUM */
893 length
= strlen(buf
);
894 if (!validate_length(ctx
, callerstr
, length
))
895 return; /* GL_INVALID_VALUE */
897 log_msg(ctx
, gl_enum_to_debug_source(source
),
898 gl_enum_to_debug_type(type
), id
,
899 gl_enum_to_debug_severity(severity
),
905 _mesa_GetDebugMessageLog(GLuint count
, GLsizei logSize
, GLenum
*sources
,
906 GLenum
*types
, GLenum
*ids
, GLenum
*severities
,
907 GLsizei
*lengths
, GLchar
*messageLog
)
909 GET_CURRENT_CONTEXT(ctx
);
910 struct gl_debug_state
*debug
;
917 _mesa_error(ctx
, GL_INVALID_VALUE
,
918 "glGetDebugMessageLog(logSize=%d : logSize must not be"
919 " negative)", logSize
);
923 debug
= _mesa_get_debug_state(ctx
);
927 for (ret
= 0; ret
< count
; ret
++) {
928 const struct gl_debug_message
*msg
= debug_fetch_message(debug
);
933 if (logSize
< msg
->length
&& messageLog
!= NULL
)
937 assert(msg
->message
[msg
->length
-1] == '\0');
938 (void) strncpy(messageLog
, msg
->message
, (size_t)msg
->length
);
940 messageLog
+= msg
->length
;
941 logSize
-= msg
->length
;
945 *lengths
++ = msg
->length
;
947 *severities
++ = debug_severity_enums
[msg
->severity
];
949 *sources
++ = debug_source_enums
[msg
->source
];
951 *types
++ = debug_type_enums
[msg
->type
];
955 debug_delete_messages(debug
, 1);
963 _mesa_DebugMessageControl(GLenum gl_source
, GLenum gl_type
,
964 GLenum gl_severity
, GLsizei count
,
965 const GLuint
*ids
, GLboolean enabled
)
967 GET_CURRENT_CONTEXT(ctx
);
968 enum mesa_debug_source source
= gl_enum_to_debug_source(gl_source
);
969 enum mesa_debug_type type
= gl_enum_to_debug_type(gl_type
);
970 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(gl_severity
);
971 const char *callerstr
= "glDebugMessageControl";
972 struct gl_debug_state
*debug
;
975 _mesa_error(ctx
, GL_INVALID_VALUE
,
976 "%s(count=%d : count must not be negative)", callerstr
,
981 if (!validate_params(ctx
, CONTROL
, callerstr
, gl_source
, gl_type
,
983 return; /* GL_INVALID_ENUM */
985 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
986 || gl_source
== GL_DONT_CARE
)) {
987 _mesa_error(ctx
, GL_INVALID_OPERATION
,
988 "%s(When passing an array of ids, severity must be"
989 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
994 debug
= _mesa_get_debug_state(ctx
);
1000 for (i
= 0; i
< count
; i
++)
1001 debug_set_message_enable(debug
, source
, type
, ids
[i
], enabled
);
1004 debug_set_message_enable_all(debug
, source
, type
, severity
, enabled
);
1010 _mesa_DebugMessageCallback(GLDEBUGPROC callback
, const void *userParam
)
1012 GET_CURRENT_CONTEXT(ctx
);
1013 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
1015 debug
->Callback
= callback
;
1016 debug
->CallbackData
= userParam
;
1022 _mesa_PushDebugGroup(GLenum source
, GLuint id
, GLsizei length
,
1023 const GLchar
*message
)
1025 GET_CURRENT_CONTEXT(ctx
);
1026 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
1027 const char *callerstr
= "glPushDebugGroup";
1028 struct gl_debug_message
*emptySlot
;
1033 if (debug
->GroupStackDepth
>= MAX_DEBUG_GROUP_STACK_DEPTH
-1) {
1034 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s", callerstr
);
1039 case GL_DEBUG_SOURCE_APPLICATION
:
1040 case GL_DEBUG_SOURCE_THIRD_PARTY
:
1043 _mesa_error(ctx
, GL_INVALID_ENUM
, "bad value passed to %s"
1044 "(source=0x%x)", callerstr
, source
);
1049 length
= strlen(message
);
1050 if (!validate_length(ctx
, callerstr
, length
))
1051 return; /* GL_INVALID_VALUE */
1053 log_msg(ctx
, gl_enum_to_debug_source(source
),
1054 MESA_DEBUG_TYPE_PUSH_GROUP
, id
,
1055 MESA_DEBUG_SEVERITY_NOTIFICATION
, length
,
1058 /* pop reuses the message details from push so we store this */
1059 emptySlot
= debug_get_group_message(debug
);
1060 debug_message_store(emptySlot
,
1061 gl_enum_to_debug_source(source
),
1062 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP
),
1064 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1067 debug_push_group(debug
);
1072 _mesa_PopDebugGroup(void)
1074 GET_CURRENT_CONTEXT(ctx
);
1075 struct gl_debug_state
*debug
= _mesa_get_debug_state(ctx
);
1076 const char *callerstr
= "glPopDebugGroup";
1077 struct gl_debug_message
*gdmessage
;
1082 if (debug
->GroupStackDepth
<= 0) {
1083 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "%s", callerstr
);
1087 debug_pop_group(debug
);
1089 gdmessage
= debug_get_group_message(debug
);
1090 log_msg(ctx
, gdmessage
->source
,
1091 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP
),
1093 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION
),
1094 gdmessage
->length
, gdmessage
->message
);
1096 debug_message_clear(gdmessage
);
1101 _mesa_init_errors(struct gl_context
*ctx
)
1108 _mesa_free_errors_data(struct gl_context
*ctx
)
1111 debug_destroy(ctx
->Debug
);
1112 /* set to NULL just in case it is used before context is completely gone. */
1118 /**********************************************************************/
1119 /** \name Diagnostics */
1123 output_if_debug(const char *prefixString
, const char *outputString
,
1126 static int debug
= -1;
1127 static FILE *fout
= NULL
;
1129 /* Init the local 'debug' var once.
1130 * Note: the _mesa_init_debug() function should have been called
1131 * by now so MESA_DEBUG_FLAGS will be initialized.
1134 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1135 * etc to the named file. Otherwise, output to stderr.
1137 const char *logFile
= _mesa_getenv("MESA_LOG_FILE");
1139 fout
= fopen(logFile
, "w");
1143 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1144 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
1149 /* in release builds, be silent unless MESA_DEBUG is set */
1150 debug
= _mesa_getenv("MESA_DEBUG") != NULL
;
1154 /* Now only print the string if we're required to do so. */
1156 fprintf(fout
, "%s: %s", prefixString
, outputString
);
1158 fprintf(fout
, "\n");
1161 #if defined(_WIN32) && !defined(_WIN32_WCE)
1162 /* stderr from windows applications without console is not usually
1163 * visible, so communicate with the debugger instead */
1166 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
1167 OutputDebugStringA(buf
);
1175 * When a new type of error is recorded, print a message describing
1176 * previous errors which were accumulated.
1179 flush_delayed_errors( struct gl_context
*ctx
)
1181 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1183 if (ctx
->ErrorDebugCount
) {
1184 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
1185 ctx
->ErrorDebugCount
,
1186 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
1188 output_if_debug("Mesa", s
, GL_TRUE
);
1190 ctx
->ErrorDebugCount
= 0;
1196 * Report a warning (a recoverable error condition) to stderr if
1197 * either DEBUG is defined or the MESA_DEBUG env var is set.
1199 * \param ctx GL context.
1200 * \param fmtString printf()-like format string.
1203 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
1205 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1207 va_start( args
, fmtString
);
1208 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1212 flush_delayed_errors( ctx
);
1214 output_if_debug("Mesa warning", str
, GL_TRUE
);
1219 * Report an internal implementation problem.
1220 * Prints the message to stderr via fprintf().
1222 * \param ctx GL context.
1223 * \param fmtString problem description string.
1226 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
1229 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
1230 static int numCalls
= 0;
1234 if (numCalls
< 50) {
1237 va_start( args
, fmtString
);
1238 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1240 fprintf(stderr
, "Mesa %s implementation error: %s\n",
1241 PACKAGE_VERSION
, str
);
1242 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
1248 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
1250 static GLint debug
= -1;
1252 /* Check debug environment variable only once:
1255 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
1258 if (debugEnv
&& strstr(debugEnv
, "silent"))
1271 if (ctx
->ErrorValue
!= error
||
1272 ctx
->ErrorDebugFmtString
!= fmtString
) {
1273 flush_delayed_errors( ctx
);
1274 ctx
->ErrorDebugFmtString
= fmtString
;
1275 ctx
->ErrorDebugCount
= 0;
1278 ctx
->ErrorDebugCount
++;
1285 _mesa_gl_debug(struct gl_context
*ctx
,
1287 enum mesa_debug_type type
,
1288 enum mesa_debug_severity severity
,
1289 const char *fmtString
, ...)
1291 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1297 va_start(args
, fmtString
);
1298 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1301 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, type
, *id
, severity
, len
, s
);
1306 * Record an OpenGL state error. These usually occur when the user
1307 * passes invalid parameters to a GL function.
1309 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1310 * run-time via the MESA_DEBUG environment variable), report the error with
1313 * \param ctx the GL context.
1314 * \param error the error value.
1315 * \param fmtString printf() style format string, followed by optional args
1318 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
1320 GLboolean do_output
, do_log
;
1321 /* Ideally this would be set up by the caller, so that we had proper IDs
1322 * per different message.
1324 static GLuint error_msg_id
= 0;
1326 debug_get_id(&error_msg_id
);
1328 do_output
= should_output(ctx
, error
, fmtString
);
1330 do_log
= debug_is_message_enabled(ctx
->Debug
,
1331 MESA_DEBUG_SOURCE_API
,
1332 MESA_DEBUG_TYPE_ERROR
,
1334 MESA_DEBUG_SEVERITY_HIGH
);
1340 if (do_output
|| do_log
) {
1341 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
1345 va_start(args
, fmtString
);
1346 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1349 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1350 /* Too long error message. Whoever calls _mesa_error should use
1357 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
1358 _mesa_lookup_enum_by_nr(error
), s
);
1359 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
1360 /* Same as above. */
1365 /* Print the error to stderr if needed. */
1367 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1370 /* Log the error via ARB_debug_output if needed.*/
1372 log_msg(ctx
, MESA_DEBUG_SOURCE_API
, MESA_DEBUG_TYPE_ERROR
,
1373 error_msg_id
, MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
1377 /* Set the GL context error state for glGetError. */
1378 _mesa_record_error(ctx
, error
);
1383 * Report debug information. Print error message to stderr via fprintf().
1384 * No-op if DEBUG mode not enabled.
1386 * \param ctx GL context.
1387 * \param fmtString printf()-style format string, followed by optional args.
1390 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1393 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1395 va_start(args
, fmtString
);
1396 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1398 output_if_debug("Mesa", s
, GL_FALSE
);
1406 * Report debug information from the shader compiler via GL_ARB_debug_output.
1408 * \param ctx GL context.
1409 * \param type The namespace to which this message belongs.
1410 * \param id The message ID within the given namespace.
1411 * \param msg The message to output. Need not be null-terminated.
1412 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1415 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1416 const char *msg
, int len
)
1418 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1419 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1426 /* Truncate the message if necessary. */
1427 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1428 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1430 log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);