mesa: Merge handling of application-provided and built-in error sources.
[mesa.git] / src / mesa / main / errors.c
1 /**
2 * \file errors.c
3 * Mesa debugging and error handling functions.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 7.1
9 *
10 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include "errors.h"
32 #include "enums.h"
33 #include "imports.h"
34 #include "context.h"
35 #include "dispatch.h"
36 #include "hash.h"
37 #include "mtypes.h"
38 #include "version.h"
39
40
41
42 struct gl_debug_severity
43 {
44 struct simple_node link;
45 GLuint ID;
46 };
47
48 static char out_of_memory[] = "Debugging error: out of memory";
49
50 static const GLenum debug_source_enums[] = {
51 GL_DEBUG_SOURCE_API,
52 GL_DEBUG_SOURCE_WINDOW_SYSTEM,
53 GL_DEBUG_SOURCE_SHADER_COMPILER,
54 GL_DEBUG_SOURCE_THIRD_PARTY,
55 GL_DEBUG_SOURCE_APPLICATION,
56 GL_DEBUG_SOURCE_OTHER,
57 };
58
59 static const GLenum debug_type_enums[] = {
60 GL_DEBUG_TYPE_ERROR,
61 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
62 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
63 GL_DEBUG_TYPE_PORTABILITY,
64 GL_DEBUG_TYPE_PERFORMANCE,
65 GL_DEBUG_TYPE_OTHER,
66 };
67
68 static const GLenum debug_severity_enums[] = {
69 GL_DEBUG_SEVERITY_LOW,
70 GL_DEBUG_SEVERITY_MEDIUM,
71 GL_DEBUG_SEVERITY_HIGH,
72 };
73
74 static enum mesa_debug_source
75 gl_enum_to_debug_source(GLenum e)
76 {
77 int i;
78
79 for (i = 0; i < Elements(debug_source_enums); i++) {
80 if (debug_source_enums[i] == e)
81 break;
82 }
83 return i;
84 }
85
86 static enum mesa_debug_type
87 gl_enum_to_debug_type(GLenum e)
88 {
89 int i;
90
91 for (i = 0; i < Elements(debug_type_enums); i++) {
92 if (debug_type_enums[i] == e)
93 break;
94 }
95 return i;
96 }
97
98 static enum mesa_debug_severity
99 gl_enum_to_debug_severity(GLenum e)
100 {
101 int i;
102
103 for (i = 0; i < Elements(debug_severity_enums); i++) {
104 if (debug_severity_enums[i] == e)
105 break;
106 }
107 return i;
108 }
109
110 /*
111 * We store a bitfield in the hash table, with five possible values total.
112 *
113 * The ENABLED_BIT's purpose is self-explanatory.
114 *
115 * The FOUND_BIT is needed to differentiate the value of DISABLED from
116 * the value returned by HashTableLookup() when it can't find the given key.
117 *
118 * The KNOWN_SEVERITY bit is a bit complicated:
119 *
120 * A client may call Control() with an array of IDs, then call Control()
121 * on all message IDs of a certain severity, then Insert() one of the
122 * previously specified IDs, giving us a known severity level, then call
123 * Control() on all message IDs of a certain severity level again.
124 *
125 * After the first call, those IDs will have a FOUND_BIT, but will not
126 * exist in any severity-specific list, so the second call will not
127 * impact them. This is undesirable but unavoidable given the API:
128 * The only entrypoint that gives a severity for a client-defined ID
129 * is the Insert() call.
130 *
131 * For the sake of Control(), we want to maintain the invariant
132 * that an ID will either appear in none of the three severity lists,
133 * or appear once, to minimize pointless duplication and potential surprises.
134 *
135 * Because Insert() is the only place that will learn an ID's severity,
136 * it should insert an ID into the appropriate list, but only if the ID
137 * doesn't exist in it or any other list yet. Because searching all three
138 * lists at O(n) is needlessly expensive, we store KNOWN_SEVERITY.
139 */
140 enum {
141 FOUND_BIT = 1 << 0,
142 ENABLED_BIT = 1 << 1,
143 KNOWN_SEVERITY = 1 << 2,
144
145 /* HashTable reserves zero as a return value meaning 'not found' */
146 NOT_FOUND = 0,
147 DISABLED = FOUND_BIT,
148 ENABLED = ENABLED_BIT | FOUND_BIT
149 };
150
151 /**
152 * Returns the state of the given message source/type/ID tuple.
153 */
154 static GLboolean
155 should_log(struct gl_context *ctx,
156 enum mesa_debug_source source,
157 enum mesa_debug_type type,
158 GLuint id,
159 enum mesa_debug_severity severity)
160 {
161 struct gl_debug_namespace *nspace =
162 &ctx->Debug.Namespaces[source][type];
163 uintptr_t state;
164
165 /* In addition to not being able to store zero as a value, HashTable also
166 can't use zero as a key. */
167 if (id)
168 state = (uintptr_t)_mesa_HashLookup(nspace->IDs, id);
169 else
170 state = nspace->ZeroID;
171
172 /* Only do this once for each ID. This makes sure the ID exists in,
173 at most, one list, and does not pointlessly appear multiple times. */
174 if (!(state & KNOWN_SEVERITY)) {
175 struct gl_debug_severity *entry;
176
177 if (state == NOT_FOUND) {
178 if (ctx->Debug.Defaults[severity][source][type])
179 state = ENABLED;
180 else
181 state = DISABLED;
182 }
183
184 entry = malloc(sizeof *entry);
185 if (!entry)
186 goto out;
187
188 state |= KNOWN_SEVERITY;
189
190 if (id)
191 _mesa_HashInsert(nspace->IDs, id, (void*)state);
192 else
193 nspace->ZeroID = state;
194
195 entry->ID = id;
196 insert_at_tail(&nspace->Severity[severity], &entry->link);
197 }
198
199 out:
200 return !!(state & ENABLED_BIT);
201 }
202
203 /**
204 * Sets the state of the given message source/type/ID tuple.
205 */
206 static void
207 set_message_state(struct gl_context *ctx,
208 enum mesa_debug_source source,
209 enum mesa_debug_type type,
210 GLuint id, GLboolean enabled)
211 {
212 struct gl_debug_namespace *nspace =
213 &ctx->Debug.Namespaces[source][type];
214 uintptr_t state;
215
216 /* In addition to not being able to store zero as a value, HashTable also
217 can't use zero as a key. */
218 if (id)
219 state = (uintptr_t)_mesa_HashLookup(nspace->IDs, id);
220 else
221 state = nspace->ZeroID;
222
223 if (state == NOT_FOUND)
224 state = enabled ? ENABLED : DISABLED;
225 else {
226 if (enabled)
227 state |= ENABLED_BIT;
228 else
229 state &= ~ENABLED_BIT;
230 }
231
232 if (id)
233 _mesa_HashInsert(nspace->IDs, id, (void*)state);
234 else
235 nspace->ZeroID = state;
236 }
237
238 /**
239 * 'buf' is not necessarily a null-terminated string. When logging, copy
240 * 'len' characters from it, store them in a new, null-terminated string,
241 * and remember the number of bytes used by that string, *including*
242 * the null terminator this time.
243 */
244 static void
245 _mesa_log_msg(struct gl_context *ctx, enum mesa_debug_source source,
246 enum mesa_debug_type type, GLuint id,
247 enum mesa_debug_severity severity, GLint len, const char *buf)
248 {
249 GLint nextEmpty;
250 struct gl_debug_msg *emptySlot;
251
252 assert(len >= 0 && len < MAX_DEBUG_MESSAGE_LENGTH);
253
254 if (!should_log(ctx, source, type, id, severity))
255 return;
256
257 if (ctx->Debug.Callback) {
258 ctx->Debug.Callback(debug_source_enums[source],
259 debug_type_enums[type],
260 id,
261 debug_severity_enums[severity],
262 len, buf, ctx->Debug.CallbackData);
263 return;
264 }
265
266 if (ctx->Debug.NumMessages == MAX_DEBUG_LOGGED_MESSAGES)
267 return;
268
269 nextEmpty = (ctx->Debug.NextMsg + ctx->Debug.NumMessages)
270 % MAX_DEBUG_LOGGED_MESSAGES;
271 emptySlot = &ctx->Debug.Log[nextEmpty];
272
273 assert(!emptySlot->message && !emptySlot->length);
274
275 emptySlot->message = malloc(len+1);
276 if (emptySlot->message) {
277 (void) strncpy(emptySlot->message, buf, (size_t)len);
278 emptySlot->message[len] = '\0';
279
280 emptySlot->length = len+1;
281 emptySlot->source = source;
282 emptySlot->type = type;
283 emptySlot->id = id;
284 emptySlot->severity = severity;
285 } else {
286 /* malloc failed! */
287 emptySlot->message = out_of_memory;
288 emptySlot->length = strlen(out_of_memory)+1;
289 emptySlot->source = MESA_DEBUG_SOURCE_OTHER;
290 emptySlot->type = MESA_DEBUG_TYPE_ERROR;
291 emptySlot->id = OTHER_ERROR_OUT_OF_MEMORY;
292 emptySlot->severity = MESA_DEBUG_SEVERITY_HIGH;
293 }
294
295 if (ctx->Debug.NumMessages == 0)
296 ctx->Debug.NextMsgLength = ctx->Debug.Log[ctx->Debug.NextMsg].length;
297
298 ctx->Debug.NumMessages++;
299 }
300
301 /**
302 * Pop the oldest debug message out of the log.
303 * Writes the message string, including the null terminator, into 'buf',
304 * using up to 'bufSize' bytes. If 'bufSize' is too small, or
305 * if 'buf' is NULL, nothing is written.
306 *
307 * Returns the number of bytes written on success, or when 'buf' is NULL,
308 * the number that would have been written. A return value of 0
309 * indicates failure.
310 */
311 static GLsizei
312 _mesa_get_msg(struct gl_context *ctx, GLenum *source, GLenum *type,
313 GLuint *id, GLenum *severity, GLsizei bufSize, char *buf)
314 {
315 struct gl_debug_msg *msg;
316 GLsizei length;
317
318 if (ctx->Debug.NumMessages == 0)
319 return 0;
320
321 msg = &ctx->Debug.Log[ctx->Debug.NextMsg];
322 length = msg->length;
323
324 assert(length > 0 && length == ctx->Debug.NextMsgLength);
325
326 if (bufSize < length && buf != NULL)
327 return 0;
328
329 if (severity)
330 *severity = debug_severity_enums[msg->severity];
331 if (source)
332 *source = debug_source_enums[msg->source];
333 if (type)
334 *type = debug_type_enums[msg->type];
335 if (id)
336 *id = msg->id;
337
338 if (buf) {
339 assert(msg->message[length-1] == '\0');
340 (void) strncpy(buf, msg->message, (size_t)length);
341 }
342
343 if (msg->message != (char*)out_of_memory)
344 free(msg->message);
345 msg->message = NULL;
346 msg->length = 0;
347
348 ctx->Debug.NumMessages--;
349 ctx->Debug.NextMsg++;
350 ctx->Debug.NextMsg %= MAX_DEBUG_LOGGED_MESSAGES;
351 ctx->Debug.NextMsgLength = ctx->Debug.Log[ctx->Debug.NextMsg].length;
352
353 return length;
354 }
355
356 /**
357 * Verify that source, type, and severity are valid enums.
358 * glDebugMessageInsertARB only accepts two values for 'source',
359 * and glDebugMessageControlARB will additionally accept GL_DONT_CARE
360 * in any parameter, so handle those cases specially.
361 */
362 static GLboolean
363 validate_params(struct gl_context *ctx, unsigned caller,
364 GLenum source, GLenum type, GLenum severity)
365 {
366 #define INSERT 1
367 #define CONTROL 2
368 switch(source) {
369 case GL_DEBUG_SOURCE_APPLICATION_ARB:
370 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB:
371 break;
372 case GL_DEBUG_SOURCE_API_ARB:
373 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB:
374 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB:
375 case GL_DEBUG_SOURCE_OTHER_ARB:
376 if (caller != INSERT)
377 break;
378 case GL_DONT_CARE:
379 if (caller == CONTROL)
380 break;
381 default:
382 goto error;
383 }
384
385 switch(type) {
386 case GL_DEBUG_TYPE_ERROR_ARB:
387 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
388 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
389 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
390 case GL_DEBUG_TYPE_PORTABILITY_ARB:
391 case GL_DEBUG_TYPE_OTHER_ARB:
392 break;
393 case GL_DONT_CARE:
394 if (caller == CONTROL)
395 break;
396 default:
397 goto error;
398 }
399
400 switch(severity) {
401 case GL_DEBUG_SEVERITY_HIGH_ARB:
402 case GL_DEBUG_SEVERITY_MEDIUM_ARB:
403 case GL_DEBUG_SEVERITY_LOW_ARB:
404 break;
405 case GL_DONT_CARE:
406 if (caller == CONTROL)
407 break;
408 default:
409 goto error;
410 }
411 return GL_TRUE;
412
413 error:
414 {
415 const char *callerstr;
416 if (caller == INSERT)
417 callerstr = "glDebugMessageInsertARB";
418 else if (caller == CONTROL)
419 callerstr = "glDebugMessageControlARB";
420 else
421 return GL_FALSE;
422
423 _mesa_error( ctx, GL_INVALID_ENUM, "bad values passed to %s"
424 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr,
425 source, type, severity);
426 }
427 return GL_FALSE;
428 }
429
430 void GLAPIENTRY
431 _mesa_DebugMessageInsertARB(GLenum source, GLenum type, GLuint id,
432 GLenum severity, GLint length,
433 const GLcharARB* buf)
434 {
435 GET_CURRENT_CONTEXT(ctx);
436
437 if (!validate_params(ctx, INSERT, source, type, severity))
438 return; /* GL_INVALID_ENUM */
439
440 if (length < 0)
441 length = strlen(buf);
442
443 if (length >= MAX_DEBUG_MESSAGE_LENGTH) {
444 _mesa_error(ctx, GL_INVALID_VALUE, "glDebugMessageInsertARB"
445 "(length=%d, which is not less than "
446 "GL_MAX_DEBUG_MESSAGE_LENGTH_ARB=%d)", length,
447 MAX_DEBUG_MESSAGE_LENGTH);
448 return;
449 }
450
451 _mesa_log_msg(ctx,
452 gl_enum_to_debug_source(source),
453 gl_enum_to_debug_type(type), id,
454 gl_enum_to_debug_severity(severity), length, buf);
455 }
456
457 GLuint GLAPIENTRY
458 _mesa_GetDebugMessageLogARB(GLuint count, GLsizei logSize, GLenum* sources,
459 GLenum* types, GLenum* ids, GLenum* severities,
460 GLsizei* lengths, GLcharARB* messageLog)
461 {
462 GET_CURRENT_CONTEXT(ctx);
463 GLuint ret;
464
465 if (!messageLog)
466 logSize = 0;
467
468 if (logSize < 0) {
469 _mesa_error(ctx, GL_INVALID_VALUE, "glGetDebugMessageLogARB"
470 "(logSize=%d : logSize must not be negative)", logSize);
471 return 0;
472 }
473
474 for (ret = 0; ret < count; ret++) {
475 GLsizei written = _mesa_get_msg(ctx, sources, types, ids, severities,
476 logSize, messageLog);
477 if (!written)
478 break;
479
480 if (messageLog) {
481 messageLog += written;
482 logSize -= written;
483 }
484 if (lengths) {
485 *lengths = written;
486 lengths++;
487 }
488
489 if (severities)
490 severities++;
491 if (sources)
492 sources++;
493 if (types)
494 types++;
495 if (ids)
496 ids++;
497 }
498
499 return ret;
500 }
501
502 /**
503 * Set the state of all message IDs found in the given intersection of
504 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
505 * GL_DONT_CARE (include all messages in the class).
506 *
507 * This requires both setting the state of all previously seen message
508 * IDs in the hash table, and setting the default state for all
509 * applicable combinations of source/type/severity, so that all the
510 * yet-unknown message IDs that may be used in the future will be
511 * impacted as if they were already known.
512 */
513 static void
514 control_messages(struct gl_context *ctx,
515 enum mesa_debug_source source,
516 enum mesa_debug_type type,
517 enum mesa_debug_severity severity,
518 GLboolean enabled)
519 {
520 int s, t, sev, smax, tmax, sevmax;
521
522 if (source == MESA_DEBUG_SOURCE_COUNT) {
523 source = 0;
524 smax = MESA_DEBUG_SOURCE_COUNT;
525 } else {
526 smax = source+1;
527 }
528
529 if (type == MESA_DEBUG_TYPE_COUNT) {
530 type = 0;
531 tmax = MESA_DEBUG_TYPE_COUNT;
532 } else {
533 tmax = type+1;
534 }
535
536 if (severity == MESA_DEBUG_SEVERITY_COUNT) {
537 severity = 0;
538 sevmax = MESA_DEBUG_SEVERITY_COUNT;
539 } else {
540 sevmax = severity+1;
541 }
542
543 for (sev = severity; sev < sevmax; sev++)
544 for (s = source; s < smax; s++)
545 for (t = type; t < tmax; t++) {
546 struct simple_node *node;
547 struct gl_debug_severity *entry;
548
549 /* change the default for IDs we've never seen before. */
550 ctx->Debug.Defaults[sev][s][t] = enabled;
551
552 /* Now change the state of IDs we *have* seen... */
553 foreach(node, &ctx->Debug.Namespaces[s][t].Severity[sev]) {
554 entry = (struct gl_debug_severity *)node;
555 set_message_state(ctx, s, t, entry->ID, enabled);
556 }
557 }
558 }
559
560 /**
561 * Debugging-message namespaces with the source APPLICATION or THIRD_PARTY
562 * require special handling, since the IDs in them are controlled by clients,
563 * not the OpenGL implementation.
564 *
565 * 'count' is the length of the array 'ids'. If 'count' is nonzero, all
566 * the given IDs in the namespace defined by 'esource' and 'etype'
567 * will be affected.
568 *
569 * If 'count' is zero, this sets the state of all IDs that match
570 * the combination of 'esource', 'etype', and 'eseverity'.
571 */
572 static void
573 control_app_messages(struct gl_context *ctx, GLenum esource, GLenum etype,
574 GLenum eseverity, GLsizei count, const GLuint *ids,
575 GLboolean enabled)
576 {
577 GLsizei i;
578 enum mesa_debug_source source = gl_enum_to_debug_source(esource);
579 enum mesa_debug_type type = gl_enum_to_debug_type(etype);
580 enum mesa_debug_severity severity = gl_enum_to_debug_severity(eseverity);
581
582 if (count)
583 assert(severity == MESA_DEBUG_SEVERITY_COUNT
584 && type != MESA_DEBUG_TYPE_COUNT
585 && source != MESA_DEBUG_SOURCE_COUNT);
586
587 for (i = 0; i < count; i++)
588 set_message_state(ctx, source, type, ids[i], enabled);
589
590 if (count)
591 return;
592
593 control_messages(ctx, source, type, severity, enabled);
594 }
595
596 void GLAPIENTRY
597 _mesa_DebugMessageControlARB(GLenum gl_source, GLenum gl_type,
598 GLenum gl_severity,
599 GLsizei count, const GLuint *ids,
600 GLboolean enabled)
601 {
602 enum mesa_debug_source source;
603 enum mesa_debug_type type;
604 enum mesa_debug_severity severity;
605 GET_CURRENT_CONTEXT(ctx);
606
607 if (count < 0) {
608 _mesa_error(ctx, GL_INVALID_VALUE, "glDebugMessageControlARB"
609 "(count=%d : count must not be negative)", count);
610 return;
611 }
612
613 if (!validate_params(ctx, CONTROL, gl_source, gl_type, gl_severity))
614 return; /* GL_INVALID_ENUM */
615
616 if (count && (gl_severity != GL_DONT_CARE || gl_type == GL_DONT_CARE
617 || gl_source == GL_DONT_CARE)) {
618 _mesa_error(ctx, GL_INVALID_OPERATION, "glDebugMessageControlARB"
619 "(When passing an array of ids, severity must be"
620 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.");
621 return;
622 }
623
624 source = gl_enum_to_debug_severity(gl_source);
625 type = gl_enum_to_debug_severity(gl_type);
626 severity = gl_enum_to_debug_severity(gl_severity);
627
628 control_app_messages(ctx, source, type, severity, count, ids, enabled);
629 }
630
631 void GLAPIENTRY
632 _mesa_DebugMessageCallbackARB(GLDEBUGPROCARB callback, const GLvoid *userParam)
633 {
634 GET_CURRENT_CONTEXT(ctx);
635 ctx->Debug.Callback = callback;
636 ctx->Debug.CallbackData = (void *) userParam;
637 }
638
639 void
640 _mesa_init_errors(struct gl_context *ctx)
641 {
642 int s, t, sev;
643
644 ctx->Debug.Callback = NULL;
645 ctx->Debug.SyncOutput = GL_FALSE;
646 ctx->Debug.Log[0].length = 0;
647 ctx->Debug.NumMessages = 0;
648 ctx->Debug.NextMsg = 0;
649 ctx->Debug.NextMsgLength = 0;
650
651 /* Enable all the messages with severity HIGH or MEDIUM by default. */
652 memset(ctx->Debug.Defaults[MESA_DEBUG_SEVERITY_HIGH], GL_TRUE,
653 sizeof ctx->Debug.Defaults[MESA_DEBUG_SEVERITY_HIGH]);
654 memset(ctx->Debug.Defaults[MESA_DEBUG_SEVERITY_MEDIUM], GL_TRUE,
655 sizeof ctx->Debug.Defaults[MESA_DEBUG_SEVERITY_MEDIUM]);
656 memset(ctx->Debug.Defaults[MESA_DEBUG_SEVERITY_LOW], GL_FALSE,
657 sizeof ctx->Debug.Defaults[MESA_DEBUG_SEVERITY_LOW]);
658
659 /* Initialize state for filtering known debug messages. */
660 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++)
661 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) {
662 ctx->Debug.Namespaces[s][t].IDs = _mesa_NewHashTable();
663 assert(ctx->Debug.Namespaces[s][t].IDs);
664
665 for (sev = 0; sev < MESA_DEBUG_SEVERITY_COUNT; sev++)
666 make_empty_list(&ctx->Debug.Namespaces[s][t].Severity[sev]);
667 }
668 }
669
670 static void
671 do_nothing(GLuint key, void *data, void *userData)
672 {
673 }
674
675 void
676 _mesa_free_errors_data(struct gl_context *ctx)
677 {
678 enum mesa_debug_type t;
679 enum mesa_debug_source s;
680 enum mesa_debug_severity sev;
681
682 /* Tear down state for filtering debug messages. */
683 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++)
684 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) {
685 _mesa_HashDeleteAll(ClientIDs->Namespaces[s][t].IDs, do_nothing, NULL);
686 _mesa_DeleteHashTable(ClientIDs->Namespaces[s][t].IDs);
687 for (sev = 0; sev < MESA_DEBUG_SEVERITY_COUNT; sev++) {
688 struct simple_node *node, *tmp;
689 struct gl_debug_severity *entry;
690
691 foreach_s(node, tmp, &ctx->Debug.Namespaces[s][t].Severity[sev]) {
692 entry = (struct gl_debug_severity *)node;
693 free(entry);
694 }
695 }
696 }
697 }
698
699 /**********************************************************************/
700 /** \name Diagnostics */
701 /*@{*/
702
703 static void
704 output_if_debug(const char *prefixString, const char *outputString,
705 GLboolean newline)
706 {
707 static int debug = -1;
708 static FILE *fout = NULL;
709
710 /* Init the local 'debug' var once.
711 * Note: the _mesa_init_debug() function should have been called
712 * by now so MESA_DEBUG_FLAGS will be initialized.
713 */
714 if (debug == -1) {
715 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
716 * etc to the named file. Otherwise, output to stderr.
717 */
718 const char *logFile = _mesa_getenv("MESA_LOG_FILE");
719 if (logFile)
720 fout = fopen(logFile, "w");
721 if (!fout)
722 fout = stderr;
723 #ifdef DEBUG
724 /* in debug builds, print messages unless MESA_DEBUG="silent" */
725 if (MESA_DEBUG_FLAGS & DEBUG_SILENT)
726 debug = 0;
727 else
728 debug = 1;
729 #else
730 /* in release builds, be silent unless MESA_DEBUG is set */
731 debug = _mesa_getenv("MESA_DEBUG") != NULL;
732 #endif
733 }
734
735 /* Now only print the string if we're required to do so. */
736 if (debug) {
737 fprintf(fout, "%s: %s", prefixString, outputString);
738 if (newline)
739 fprintf(fout, "\n");
740 fflush(fout);
741
742 #if defined(_WIN32) && !defined(_WIN32_WCE)
743 /* stderr from windows applications without console is not usually
744 * visible, so communicate with the debugger instead */
745 {
746 char buf[4096];
747 _mesa_snprintf(buf, sizeof(buf), "%s: %s%s", prefixString, outputString, newline ? "\n" : "");
748 OutputDebugStringA(buf);
749 }
750 #endif
751 }
752 }
753
754 /**
755 * When a new type of error is recorded, print a message describing
756 * previous errors which were accumulated.
757 */
758 static void
759 flush_delayed_errors( struct gl_context *ctx )
760 {
761 char s[MAX_DEBUG_MESSAGE_LENGTH];
762
763 if (ctx->ErrorDebugCount) {
764 _mesa_snprintf(s, MAX_DEBUG_MESSAGE_LENGTH, "%d similar %s errors",
765 ctx->ErrorDebugCount,
766 _mesa_lookup_enum_by_nr(ctx->ErrorValue));
767
768 output_if_debug("Mesa", s, GL_TRUE);
769
770 ctx->ErrorDebugCount = 0;
771 }
772 }
773
774
775 /**
776 * Report a warning (a recoverable error condition) to stderr if
777 * either DEBUG is defined or the MESA_DEBUG env var is set.
778 *
779 * \param ctx GL context.
780 * \param fmtString printf()-like format string.
781 */
782 void
783 _mesa_warning( struct gl_context *ctx, const char *fmtString, ... )
784 {
785 char str[MAX_DEBUG_MESSAGE_LENGTH];
786 va_list args;
787 va_start( args, fmtString );
788 (void) _mesa_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
789 va_end( args );
790
791 if (ctx)
792 flush_delayed_errors( ctx );
793
794 output_if_debug("Mesa warning", str, GL_TRUE);
795 }
796
797
798 /**
799 * Report an internal implementation problem.
800 * Prints the message to stderr via fprintf().
801 *
802 * \param ctx GL context.
803 * \param fmtString problem description string.
804 */
805 void
806 _mesa_problem( const struct gl_context *ctx, const char *fmtString, ... )
807 {
808 va_list args;
809 char str[MAX_DEBUG_MESSAGE_LENGTH];
810 static int numCalls = 0;
811
812 (void) ctx;
813
814 if (numCalls < 50) {
815 numCalls++;
816
817 va_start( args, fmtString );
818 _mesa_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
819 va_end( args );
820 fprintf(stderr, "Mesa %s implementation error: %s\n",
821 MESA_VERSION_STRING, str);
822 fprintf(stderr, "Please report at bugs.freedesktop.org\n");
823 }
824 }
825
826 static GLboolean
827 should_output(struct gl_context *ctx, GLenum error, const char *fmtString)
828 {
829 static GLint debug = -1;
830
831 /* Check debug environment variable only once:
832 */
833 if (debug == -1) {
834 const char *debugEnv = _mesa_getenv("MESA_DEBUG");
835
836 #ifdef DEBUG
837 if (debugEnv && strstr(debugEnv, "silent"))
838 debug = GL_FALSE;
839 else
840 debug = GL_TRUE;
841 #else
842 if (debugEnv)
843 debug = GL_TRUE;
844 else
845 debug = GL_FALSE;
846 #endif
847 }
848
849 if (debug) {
850 if (ctx->ErrorValue != error ||
851 ctx->ErrorDebugFmtString != fmtString) {
852 flush_delayed_errors( ctx );
853 ctx->ErrorDebugFmtString = fmtString;
854 ctx->ErrorDebugCount = 0;
855 return GL_TRUE;
856 }
857 ctx->ErrorDebugCount++;
858 }
859 return GL_FALSE;
860 }
861
862
863 /**
864 * Record an OpenGL state error. These usually occur when the user
865 * passes invalid parameters to a GL function.
866 *
867 * If debugging is enabled (either at compile-time via the DEBUG macro, or
868 * run-time via the MESA_DEBUG environment variable), report the error with
869 * _mesa_debug().
870 *
871 * \param ctx the GL context.
872 * \param error the error value.
873 * \param fmtString printf() style format string, followed by optional args
874 */
875 void
876 _mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... )
877 {
878 GLboolean do_output, do_log;
879
880 do_output = should_output(ctx, error, fmtString);
881 do_log = should_log(ctx,
882 MESA_DEBUG_SOURCE_API,
883 MESA_DEBUG_TYPE_ERROR,
884 API_ERROR_UNKNOWN,
885 MESA_DEBUG_SEVERITY_HIGH);
886
887 if (do_output || do_log) {
888 char s[MAX_DEBUG_MESSAGE_LENGTH], s2[MAX_DEBUG_MESSAGE_LENGTH];
889 int len;
890 va_list args;
891
892 va_start(args, fmtString);
893 len = _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
894 va_end(args);
895
896 if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
897 /* Too long error message. Whoever calls _mesa_error should use
898 * shorter strings. */
899 ASSERT(0);
900 return;
901 }
902
903 len = _mesa_snprintf(s2, MAX_DEBUG_MESSAGE_LENGTH, "%s in %s",
904 _mesa_lookup_enum_by_nr(error), s);
905 if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
906 /* Same as above. */
907 ASSERT(0);
908 return;
909 }
910
911 /* Print the error to stderr if needed. */
912 if (do_output) {
913 output_if_debug("Mesa: User error", s2, GL_TRUE);
914 }
915
916 /* Log the error via ARB_debug_output if needed.*/
917 if (do_log) {
918 _mesa_log_msg(ctx,
919 MESA_DEBUG_SOURCE_API,
920 MESA_DEBUG_TYPE_ERROR,
921 API_ERROR_UNKNOWN,
922 MESA_DEBUG_SEVERITY_HIGH, len, s2);
923 }
924 }
925
926 /* Set the GL context error state for glGetError. */
927 _mesa_record_error(ctx, error);
928 }
929
930
931 /**
932 * Report debug information. Print error message to stderr via fprintf().
933 * No-op if DEBUG mode not enabled.
934 *
935 * \param ctx GL context.
936 * \param fmtString printf()-style format string, followed by optional args.
937 */
938 void
939 _mesa_debug( const struct gl_context *ctx, const char *fmtString, ... )
940 {
941 #ifdef DEBUG
942 char s[MAX_DEBUG_MESSAGE_LENGTH];
943 va_list args;
944 va_start(args, fmtString);
945 _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
946 va_end(args);
947 output_if_debug("Mesa", s, GL_FALSE);
948 #endif /* DEBUG */
949 (void) ctx;
950 (void) fmtString;
951 }
952
953
954 /**
955 * Report debug information from the shader compiler via GL_ARB_debug_output.
956 *
957 * \param ctx GL context.
958 * \param type The namespace to which this message belongs.
959 * \param id The message ID within the given namespace.
960 * \param msg The message to output. Need not be null-terminated.
961 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
962 */
963 void
964 _mesa_shader_debug( struct gl_context *ctx, GLenum type, GLuint id,
965 const char *msg, int len )
966 {
967 enum mesa_debug_source source = MESA_DEBUG_SOURCE_SHADER_COMPILER;
968 enum mesa_debug_severity severity = MESA_DEBUG_SEVERITY_HIGH;
969
970 if (len < 0)
971 len = strlen(msg);
972
973 /* Truncate the message if necessary. */
974 if (len >= MAX_DEBUG_MESSAGE_LENGTH)
975 len = MAX_DEBUG_MESSAGE_LENGTH - 1;
976
977 _mesa_log_msg(ctx, source, type, id, severity, len, msg);
978 }
979
980 /*@}*/