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"
40 #include "glapi/glthread.h"
42 _glthread_DECLARE_STATIC_MUTEX(DynamicIDMutex
);
43 static GLuint NextDynamicID
= 1;
45 struct gl_debug_severity
47 struct simple_node link
;
51 static char out_of_memory
[] = "Debugging error: out of memory";
53 static const GLenum debug_source_enums
[] = {
55 GL_DEBUG_SOURCE_WINDOW_SYSTEM
,
56 GL_DEBUG_SOURCE_SHADER_COMPILER
,
57 GL_DEBUG_SOURCE_THIRD_PARTY
,
58 GL_DEBUG_SOURCE_APPLICATION
,
59 GL_DEBUG_SOURCE_OTHER
,
62 static const GLenum debug_type_enums
[] = {
64 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
65 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
66 GL_DEBUG_TYPE_PORTABILITY
,
67 GL_DEBUG_TYPE_PERFORMANCE
,
71 static const GLenum debug_severity_enums
[] = {
72 GL_DEBUG_SEVERITY_LOW
,
73 GL_DEBUG_SEVERITY_MEDIUM
,
74 GL_DEBUG_SEVERITY_HIGH
,
77 static enum mesa_debug_source
78 gl_enum_to_debug_source(GLenum e
)
82 for (i
= 0; i
< Elements(debug_source_enums
); i
++) {
83 if (debug_source_enums
[i
] == e
)
89 static enum mesa_debug_type
90 gl_enum_to_debug_type(GLenum e
)
94 for (i
= 0; i
< Elements(debug_type_enums
); i
++) {
95 if (debug_type_enums
[i
] == e
)
101 static enum mesa_debug_severity
102 gl_enum_to_debug_severity(GLenum e
)
106 for (i
= 0; i
< Elements(debug_severity_enums
); i
++) {
107 if (debug_severity_enums
[i
] == e
)
114 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
117 * The GL API has this "ID" mechanism, where the intention is to allow a
118 * client to filter in/out messages based on source, type, and ID. Of course,
119 * building a giant enum list of all debug output messages that Mesa might
120 * generate is ridiculous, so instead we have our caller pass us a pointer to
121 * static storage where the ID should get stored. This ID will be shared
122 * across all contexts for that message (which seems like a desirable
123 * property, even if it's not expected by the spec), but note that it won't be
124 * the same between executions if messages aren't generated in the same order.
127 debug_get_id(GLuint
*id
)
130 _glthread_LOCK_MUTEX(DynamicIDMutex
);
132 *id
= NextDynamicID
++;
133 _glthread_UNLOCK_MUTEX(DynamicIDMutex
);
138 * We store a bitfield in the hash table, with five possible values total.
140 * The ENABLED_BIT's purpose is self-explanatory.
142 * The FOUND_BIT is needed to differentiate the value of DISABLED from
143 * the value returned by HashTableLookup() when it can't find the given key.
145 * The KNOWN_SEVERITY bit is a bit complicated:
147 * A client may call Control() with an array of IDs, then call Control()
148 * on all message IDs of a certain severity, then Insert() one of the
149 * previously specified IDs, giving us a known severity level, then call
150 * Control() on all message IDs of a certain severity level again.
152 * After the first call, those IDs will have a FOUND_BIT, but will not
153 * exist in any severity-specific list, so the second call will not
154 * impact them. This is undesirable but unavoidable given the API:
155 * The only entrypoint that gives a severity for a client-defined ID
156 * is the Insert() call.
158 * For the sake of Control(), we want to maintain the invariant
159 * that an ID will either appear in none of the three severity lists,
160 * or appear once, to minimize pointless duplication and potential surprises.
162 * Because Insert() is the only place that will learn an ID's severity,
163 * it should insert an ID into the appropriate list, but only if the ID
164 * doesn't exist in it or any other list yet. Because searching all three
165 * lists at O(n) is needlessly expensive, we store KNOWN_SEVERITY.
169 ENABLED_BIT
= 1 << 1,
170 KNOWN_SEVERITY
= 1 << 2,
172 /* HashTable reserves zero as a return value meaning 'not found' */
174 DISABLED
= FOUND_BIT
,
175 ENABLED
= ENABLED_BIT
| FOUND_BIT
179 * Returns the state of the given message source/type/ID tuple.
182 should_log(struct gl_context
*ctx
,
183 enum mesa_debug_source source
,
184 enum mesa_debug_type type
,
186 enum mesa_debug_severity severity
)
188 struct gl_debug_namespace
*nspace
=
189 &ctx
->Debug
.Namespaces
[source
][type
];
192 /* In addition to not being able to store zero as a value, HashTable also
193 can't use zero as a key. */
195 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
197 state
= nspace
->ZeroID
;
199 /* Only do this once for each ID. This makes sure the ID exists in,
200 at most, one list, and does not pointlessly appear multiple times. */
201 if (!(state
& KNOWN_SEVERITY
)) {
202 struct gl_debug_severity
*entry
;
204 if (state
== NOT_FOUND
) {
205 if (ctx
->Debug
.Defaults
[severity
][source
][type
])
211 entry
= malloc(sizeof *entry
);
215 state
|= KNOWN_SEVERITY
;
218 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
220 nspace
->ZeroID
= state
;
223 insert_at_tail(&nspace
->Severity
[severity
], &entry
->link
);
227 return !!(state
& ENABLED_BIT
);
231 * Sets the state of the given message source/type/ID tuple.
234 set_message_state(struct gl_context
*ctx
,
235 enum mesa_debug_source source
,
236 enum mesa_debug_type type
,
237 GLuint id
, GLboolean enabled
)
239 struct gl_debug_namespace
*nspace
=
240 &ctx
->Debug
.Namespaces
[source
][type
];
243 /* In addition to not being able to store zero as a value, HashTable also
244 can't use zero as a key. */
246 state
= (uintptr_t)_mesa_HashLookup(nspace
->IDs
, id
);
248 state
= nspace
->ZeroID
;
250 if (state
== NOT_FOUND
)
251 state
= enabled
? ENABLED
: DISABLED
;
254 state
|= ENABLED_BIT
;
256 state
&= ~ENABLED_BIT
;
260 _mesa_HashInsert(nspace
->IDs
, id
, (void*)state
);
262 nspace
->ZeroID
= state
;
266 * 'buf' is not necessarily a null-terminated string. When logging, copy
267 * 'len' characters from it, store them in a new, null-terminated string,
268 * and remember the number of bytes used by that string, *including*
269 * the null terminator this time.
272 _mesa_log_msg(struct gl_context
*ctx
, enum mesa_debug_source source
,
273 enum mesa_debug_type type
, GLuint id
,
274 enum mesa_debug_severity severity
, GLint len
, const char *buf
)
277 struct gl_debug_msg
*emptySlot
;
279 assert(len
>= 0 && len
< MAX_DEBUG_MESSAGE_LENGTH
);
281 if (!should_log(ctx
, source
, type
, id
, severity
))
284 if (ctx
->Debug
.Callback
) {
285 ctx
->Debug
.Callback(debug_source_enums
[source
],
286 debug_type_enums
[type
],
288 debug_severity_enums
[severity
],
289 len
, buf
, ctx
->Debug
.CallbackData
);
293 if (ctx
->Debug
.NumMessages
== MAX_DEBUG_LOGGED_MESSAGES
)
296 nextEmpty
= (ctx
->Debug
.NextMsg
+ ctx
->Debug
.NumMessages
)
297 % MAX_DEBUG_LOGGED_MESSAGES
;
298 emptySlot
= &ctx
->Debug
.Log
[nextEmpty
];
300 assert(!emptySlot
->message
&& !emptySlot
->length
);
302 emptySlot
->message
= malloc(len
+1);
303 if (emptySlot
->message
) {
304 (void) strncpy(emptySlot
->message
, buf
, (size_t)len
);
305 emptySlot
->message
[len
] = '\0';
307 emptySlot
->length
= len
+1;
308 emptySlot
->source
= source
;
309 emptySlot
->type
= type
;
311 emptySlot
->severity
= severity
;
313 static GLuint oom_msg_id
= 0;
314 debug_get_id(&oom_msg_id
);
317 emptySlot
->message
= out_of_memory
;
318 emptySlot
->length
= strlen(out_of_memory
)+1;
319 emptySlot
->source
= MESA_DEBUG_SOURCE_OTHER
;
320 emptySlot
->type
= MESA_DEBUG_TYPE_ERROR
;
321 emptySlot
->id
= oom_msg_id
;
322 emptySlot
->severity
= MESA_DEBUG_SEVERITY_HIGH
;
325 if (ctx
->Debug
.NumMessages
== 0)
326 ctx
->Debug
.NextMsgLength
= ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
].length
;
328 ctx
->Debug
.NumMessages
++;
332 * Pop the oldest debug message out of the log.
333 * Writes the message string, including the null terminator, into 'buf',
334 * using up to 'bufSize' bytes. If 'bufSize' is too small, or
335 * if 'buf' is NULL, nothing is written.
337 * Returns the number of bytes written on success, or when 'buf' is NULL,
338 * the number that would have been written. A return value of 0
342 _mesa_get_msg(struct gl_context
*ctx
, GLenum
*source
, GLenum
*type
,
343 GLuint
*id
, GLenum
*severity
, GLsizei bufSize
, char *buf
)
345 struct gl_debug_msg
*msg
;
348 if (ctx
->Debug
.NumMessages
== 0)
351 msg
= &ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
];
352 length
= msg
->length
;
354 assert(length
> 0 && length
== ctx
->Debug
.NextMsgLength
);
356 if (bufSize
< length
&& buf
!= NULL
)
360 *severity
= debug_severity_enums
[msg
->severity
];
362 *source
= debug_source_enums
[msg
->source
];
364 *type
= debug_type_enums
[msg
->type
];
369 assert(msg
->message
[length
-1] == '\0');
370 (void) strncpy(buf
, msg
->message
, (size_t)length
);
373 if (msg
->message
!= (char*)out_of_memory
)
378 ctx
->Debug
.NumMessages
--;
379 ctx
->Debug
.NextMsg
++;
380 ctx
->Debug
.NextMsg
%= MAX_DEBUG_LOGGED_MESSAGES
;
381 ctx
->Debug
.NextMsgLength
= ctx
->Debug
.Log
[ctx
->Debug
.NextMsg
].length
;
387 * Verify that source, type, and severity are valid enums.
388 * glDebugMessageInsertARB only accepts two values for 'source',
389 * and glDebugMessageControlARB will additionally accept GL_DONT_CARE
390 * in any parameter, so handle those cases specially.
393 validate_params(struct gl_context
*ctx
, unsigned caller
,
394 GLenum source
, GLenum type
, GLenum severity
)
399 case GL_DEBUG_SOURCE_APPLICATION_ARB
:
400 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB
:
402 case GL_DEBUG_SOURCE_API_ARB
:
403 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
:
404 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
:
405 case GL_DEBUG_SOURCE_OTHER_ARB
:
406 if (caller
!= INSERT
)
409 if (caller
== CONTROL
)
416 case GL_DEBUG_TYPE_ERROR_ARB
:
417 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
418 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
419 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
420 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
421 case GL_DEBUG_TYPE_OTHER_ARB
:
424 if (caller
== CONTROL
)
431 case GL_DEBUG_SEVERITY_HIGH_ARB
:
432 case GL_DEBUG_SEVERITY_MEDIUM_ARB
:
433 case GL_DEBUG_SEVERITY_LOW_ARB
:
436 if (caller
== CONTROL
)
445 const char *callerstr
;
446 if (caller
== INSERT
)
447 callerstr
= "glDebugMessageInsertARB";
448 else if (caller
== CONTROL
)
449 callerstr
= "glDebugMessageControlARB";
453 _mesa_error( ctx
, GL_INVALID_ENUM
, "bad values passed to %s"
454 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr
,
455 source
, type
, severity
);
461 _mesa_DebugMessageInsertARB(GLenum source
, GLenum type
, GLuint id
,
462 GLenum severity
, GLint length
,
463 const GLcharARB
* buf
)
465 GET_CURRENT_CONTEXT(ctx
);
467 if (!validate_params(ctx
, INSERT
, source
, type
, severity
))
468 return; /* GL_INVALID_ENUM */
471 length
= strlen(buf
);
473 if (length
>= MAX_DEBUG_MESSAGE_LENGTH
) {
474 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDebugMessageInsertARB"
475 "(length=%d, which is not less than "
476 "GL_MAX_DEBUG_MESSAGE_LENGTH_ARB=%d)", length
,
477 MAX_DEBUG_MESSAGE_LENGTH
);
482 gl_enum_to_debug_source(source
),
483 gl_enum_to_debug_type(type
), id
,
484 gl_enum_to_debug_severity(severity
), length
, buf
);
488 _mesa_GetDebugMessageLogARB(GLuint count
, GLsizei logSize
, GLenum
* sources
,
489 GLenum
* types
, GLenum
* ids
, GLenum
* severities
,
490 GLsizei
* lengths
, GLcharARB
* messageLog
)
492 GET_CURRENT_CONTEXT(ctx
);
499 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetDebugMessageLogARB"
500 "(logSize=%d : logSize must not be negative)", logSize
);
504 for (ret
= 0; ret
< count
; ret
++) {
505 GLsizei written
= _mesa_get_msg(ctx
, sources
, types
, ids
, severities
,
506 logSize
, messageLog
);
511 messageLog
+= written
;
533 * Set the state of all message IDs found in the given intersection of
534 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
535 * GL_DONT_CARE (include all messages in the class).
537 * This requires both setting the state of all previously seen message
538 * IDs in the hash table, and setting the default state for all
539 * applicable combinations of source/type/severity, so that all the
540 * yet-unknown message IDs that may be used in the future will be
541 * impacted as if they were already known.
544 control_messages(struct gl_context
*ctx
,
545 enum mesa_debug_source source
,
546 enum mesa_debug_type type
,
547 enum mesa_debug_severity severity
,
550 int s
, t
, sev
, smax
, tmax
, sevmax
;
552 if (source
== MESA_DEBUG_SOURCE_COUNT
) {
554 smax
= MESA_DEBUG_SOURCE_COUNT
;
559 if (type
== MESA_DEBUG_TYPE_COUNT
) {
561 tmax
= MESA_DEBUG_TYPE_COUNT
;
566 if (severity
== MESA_DEBUG_SEVERITY_COUNT
) {
568 sevmax
= MESA_DEBUG_SEVERITY_COUNT
;
573 for (sev
= severity
; sev
< sevmax
; sev
++)
574 for (s
= source
; s
< smax
; s
++)
575 for (t
= type
; t
< tmax
; t
++) {
576 struct simple_node
*node
;
577 struct gl_debug_severity
*entry
;
579 /* change the default for IDs we've never seen before. */
580 ctx
->Debug
.Defaults
[sev
][s
][t
] = enabled
;
582 /* Now change the state of IDs we *have* seen... */
583 foreach(node
, &ctx
->Debug
.Namespaces
[s
][t
].Severity
[sev
]) {
584 entry
= (struct gl_debug_severity
*)node
;
585 set_message_state(ctx
, s
, t
, entry
->ID
, enabled
);
591 * Debugging-message namespaces with the source APPLICATION or THIRD_PARTY
592 * require special handling, since the IDs in them are controlled by clients,
593 * not the OpenGL implementation.
595 * 'count' is the length of the array 'ids'. If 'count' is nonzero, all
596 * the given IDs in the namespace defined by 'esource' and 'etype'
599 * If 'count' is zero, this sets the state of all IDs that match
600 * the combination of 'esource', 'etype', and 'eseverity'.
603 control_app_messages(struct gl_context
*ctx
, GLenum esource
, GLenum etype
,
604 GLenum eseverity
, GLsizei count
, const GLuint
*ids
,
608 enum mesa_debug_source source
= gl_enum_to_debug_source(esource
);
609 enum mesa_debug_type type
= gl_enum_to_debug_type(etype
);
610 enum mesa_debug_severity severity
= gl_enum_to_debug_severity(eseverity
);
613 assert(severity
== MESA_DEBUG_SEVERITY_COUNT
614 && type
!= MESA_DEBUG_TYPE_COUNT
615 && source
!= MESA_DEBUG_SOURCE_COUNT
);
617 for (i
= 0; i
< count
; i
++)
618 set_message_state(ctx
, source
, type
, ids
[i
], enabled
);
623 control_messages(ctx
, source
, type
, severity
, enabled
);
627 _mesa_DebugMessageControlARB(GLenum gl_source
, GLenum gl_type
,
629 GLsizei count
, const GLuint
*ids
,
632 enum mesa_debug_source source
;
633 enum mesa_debug_type type
;
634 enum mesa_debug_severity severity
;
635 GET_CURRENT_CONTEXT(ctx
);
638 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDebugMessageControlARB"
639 "(count=%d : count must not be negative)", count
);
643 if (!validate_params(ctx
, CONTROL
, gl_source
, gl_type
, gl_severity
))
644 return; /* GL_INVALID_ENUM */
646 if (count
&& (gl_severity
!= GL_DONT_CARE
|| gl_type
== GL_DONT_CARE
647 || gl_source
== GL_DONT_CARE
)) {
648 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDebugMessageControlARB"
649 "(When passing an array of ids, severity must be"
650 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.");
654 source
= gl_enum_to_debug_source(gl_source
);
655 type
= gl_enum_to_debug_type(gl_type
);
656 severity
= gl_enum_to_debug_severity(gl_severity
);
658 control_app_messages(ctx
, source
, type
, severity
, count
, ids
, enabled
);
662 _mesa_DebugMessageCallbackARB(GLDEBUGPROCARB callback
, const GLvoid
*userParam
)
664 GET_CURRENT_CONTEXT(ctx
);
665 ctx
->Debug
.Callback
= callback
;
666 ctx
->Debug
.CallbackData
= (void *) userParam
;
670 _mesa_init_errors(struct gl_context
*ctx
)
674 ctx
->Debug
.Callback
= NULL
;
675 ctx
->Debug
.SyncOutput
= GL_FALSE
;
676 ctx
->Debug
.Log
[0].length
= 0;
677 ctx
->Debug
.NumMessages
= 0;
678 ctx
->Debug
.NextMsg
= 0;
679 ctx
->Debug
.NextMsgLength
= 0;
681 /* Enable all the messages with severity HIGH or MEDIUM by default. */
682 memset(ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_HIGH
], GL_TRUE
,
683 sizeof ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_HIGH
]);
684 memset(ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_MEDIUM
], GL_TRUE
,
685 sizeof ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_MEDIUM
]);
686 memset(ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_LOW
], GL_FALSE
,
687 sizeof ctx
->Debug
.Defaults
[MESA_DEBUG_SEVERITY_LOW
]);
689 /* Initialize state for filtering known debug messages. */
690 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++)
691 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
692 ctx
->Debug
.Namespaces
[s
][t
].IDs
= _mesa_NewHashTable();
693 assert(ctx
->Debug
.Namespaces
[s
][t
].IDs
);
695 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++)
696 make_empty_list(&ctx
->Debug
.Namespaces
[s
][t
].Severity
[sev
]);
701 do_nothing(GLuint key
, void *data
, void *userData
)
706 _mesa_free_errors_data(struct gl_context
*ctx
)
708 enum mesa_debug_type t
;
709 enum mesa_debug_source s
;
710 enum mesa_debug_severity sev
;
712 /* Tear down state for filtering debug messages. */
713 for (s
= 0; s
< MESA_DEBUG_SOURCE_COUNT
; s
++)
714 for (t
= 0; t
< MESA_DEBUG_TYPE_COUNT
; t
++) {
715 _mesa_HashDeleteAll(ctx
->Debug
.Namespaces
[s
][t
].IDs
, do_nothing
, NULL
);
716 _mesa_DeleteHashTable(ctx
->Debug
.Namespaces
[s
][t
].IDs
);
717 for (sev
= 0; sev
< MESA_DEBUG_SEVERITY_COUNT
; sev
++) {
718 struct simple_node
*node
, *tmp
;
719 struct gl_debug_severity
*entry
;
721 foreach_s(node
, tmp
, &ctx
->Debug
.Namespaces
[s
][t
].Severity
[sev
]) {
722 entry
= (struct gl_debug_severity
*)node
;
729 /**********************************************************************/
730 /** \name Diagnostics */
734 output_if_debug(const char *prefixString
, const char *outputString
,
737 static int debug
= -1;
738 static FILE *fout
= NULL
;
740 /* Init the local 'debug' var once.
741 * Note: the _mesa_init_debug() function should have been called
742 * by now so MESA_DEBUG_FLAGS will be initialized.
745 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
746 * etc to the named file. Otherwise, output to stderr.
748 const char *logFile
= _mesa_getenv("MESA_LOG_FILE");
750 fout
= fopen(logFile
, "w");
754 /* in debug builds, print messages unless MESA_DEBUG="silent" */
755 if (MESA_DEBUG_FLAGS
& DEBUG_SILENT
)
760 /* in release builds, be silent unless MESA_DEBUG is set */
761 debug
= _mesa_getenv("MESA_DEBUG") != NULL
;
765 /* Now only print the string if we're required to do so. */
767 fprintf(fout
, "%s: %s", prefixString
, outputString
);
772 #if defined(_WIN32) && !defined(_WIN32_WCE)
773 /* stderr from windows applications without console is not usually
774 * visible, so communicate with the debugger instead */
777 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
778 OutputDebugStringA(buf
);
785 * When a new type of error is recorded, print a message describing
786 * previous errors which were accumulated.
789 flush_delayed_errors( struct gl_context
*ctx
)
791 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
793 if (ctx
->ErrorDebugCount
) {
794 _mesa_snprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, "%d similar %s errors",
795 ctx
->ErrorDebugCount
,
796 _mesa_lookup_enum_by_nr(ctx
->ErrorValue
));
798 output_if_debug("Mesa", s
, GL_TRUE
);
800 ctx
->ErrorDebugCount
= 0;
806 * Report a warning (a recoverable error condition) to stderr if
807 * either DEBUG is defined or the MESA_DEBUG env var is set.
809 * \param ctx GL context.
810 * \param fmtString printf()-like format string.
813 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
815 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
817 va_start( args
, fmtString
);
818 (void) _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
822 flush_delayed_errors( ctx
);
824 output_if_debug("Mesa warning", str
, GL_TRUE
);
829 * Report an internal implementation problem.
830 * Prints the message to stderr via fprintf().
832 * \param ctx GL context.
833 * \param fmtString problem description string.
836 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
839 char str
[MAX_DEBUG_MESSAGE_LENGTH
];
840 static int numCalls
= 0;
847 va_start( args
, fmtString
);
848 _mesa_vsnprintf( str
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
850 fprintf(stderr
, "Mesa %s implementation error: %s\n",
851 PACKAGE_VERSION
, str
);
852 fprintf(stderr
, "Please report at " PACKAGE_BUGREPORT
"\n");
857 should_output(struct gl_context
*ctx
, GLenum error
, const char *fmtString
)
859 static GLint debug
= -1;
861 /* Check debug environment variable only once:
864 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
867 if (debugEnv
&& strstr(debugEnv
, "silent"))
880 if (ctx
->ErrorValue
!= error
||
881 ctx
->ErrorDebugFmtString
!= fmtString
) {
882 flush_delayed_errors( ctx
);
883 ctx
->ErrorDebugFmtString
= fmtString
;
884 ctx
->ErrorDebugCount
= 0;
887 ctx
->ErrorDebugCount
++;
893 _mesa_gl_debug(struct gl_context
*ctx
,
895 enum mesa_debug_type type
,
896 enum mesa_debug_severity severity
,
897 const char *fmtString
, ...)
899 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
905 va_start(args
, fmtString
);
906 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
909 _mesa_log_msg(ctx
, MESA_DEBUG_SOURCE_API
, type
,
910 *id
, severity
, len
, s
);
915 * Record an OpenGL state error. These usually occur when the user
916 * passes invalid parameters to a GL function.
918 * If debugging is enabled (either at compile-time via the DEBUG macro, or
919 * run-time via the MESA_DEBUG environment variable), report the error with
922 * \param ctx the GL context.
923 * \param error the error value.
924 * \param fmtString printf() style format string, followed by optional args
927 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
929 GLboolean do_output
, do_log
;
930 /* Ideally this would be set up by the caller, so that we had proper IDs
931 * per different message.
933 static GLuint error_msg_id
= 0;
935 debug_get_id(&error_msg_id
);
937 do_output
= should_output(ctx
, error
, fmtString
);
938 do_log
= should_log(ctx
,
939 MESA_DEBUG_SOURCE_API
,
940 MESA_DEBUG_TYPE_ERROR
,
942 MESA_DEBUG_SEVERITY_HIGH
);
944 if (do_output
|| do_log
) {
945 char s
[MAX_DEBUG_MESSAGE_LENGTH
], s2
[MAX_DEBUG_MESSAGE_LENGTH
];
949 va_start(args
, fmtString
);
950 len
= _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
953 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
954 /* Too long error message. Whoever calls _mesa_error should use
955 * shorter strings. */
960 len
= _mesa_snprintf(s2
, MAX_DEBUG_MESSAGE_LENGTH
, "%s in %s",
961 _mesa_lookup_enum_by_nr(error
), s
);
962 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
) {
968 /* Print the error to stderr if needed. */
970 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
973 /* Log the error via ARB_debug_output if needed.*/
976 MESA_DEBUG_SOURCE_API
,
977 MESA_DEBUG_TYPE_ERROR
,
979 MESA_DEBUG_SEVERITY_HIGH
, len
, s2
);
983 /* Set the GL context error state for glGetError. */
984 _mesa_record_error(ctx
, error
);
989 * Report debug information. Print error message to stderr via fprintf().
990 * No-op if DEBUG mode not enabled.
992 * \param ctx GL context.
993 * \param fmtString printf()-style format string, followed by optional args.
996 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
999 char s
[MAX_DEBUG_MESSAGE_LENGTH
];
1001 va_start(args
, fmtString
);
1002 _mesa_vsnprintf(s
, MAX_DEBUG_MESSAGE_LENGTH
, fmtString
, args
);
1004 output_if_debug("Mesa", s
, GL_FALSE
);
1012 * Report debug information from the shader compiler via GL_ARB_debug_output.
1014 * \param ctx GL context.
1015 * \param type The namespace to which this message belongs.
1016 * \param id The message ID within the given namespace.
1017 * \param msg The message to output. Need not be null-terminated.
1018 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1021 _mesa_shader_debug( struct gl_context
*ctx
, GLenum type
, GLuint
*id
,
1022 const char *msg
, int len
)
1024 enum mesa_debug_source source
= MESA_DEBUG_SOURCE_SHADER_COMPILER
;
1025 enum mesa_debug_severity severity
= MESA_DEBUG_SEVERITY_HIGH
;
1032 /* Truncate the message if necessary. */
1033 if (len
>= MAX_DEBUG_MESSAGE_LENGTH
)
1034 len
= MAX_DEBUG_MESSAGE_LENGTH
- 1;
1036 _mesa_log_msg(ctx
, source
, type
, *id
, severity
, len
, msg
);