mesa: refactor debug output should_log
[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 *
9 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
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.
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 #include "hash_table.h"
40
41 static mtx_t DynamicIDMutex = _MTX_INITIALIZER_NP;
42 static GLuint NextDynamicID = 1;
43
44 struct gl_debug_severity
45 {
46 struct simple_node link;
47 GLuint ID;
48 };
49
50 static char out_of_memory[] = "Debugging error: out of memory";
51
52 static const GLenum debug_source_enums[] = {
53 GL_DEBUG_SOURCE_API,
54 GL_DEBUG_SOURCE_WINDOW_SYSTEM,
55 GL_DEBUG_SOURCE_SHADER_COMPILER,
56 GL_DEBUG_SOURCE_THIRD_PARTY,
57 GL_DEBUG_SOURCE_APPLICATION,
58 GL_DEBUG_SOURCE_OTHER,
59 };
60
61 static const GLenum debug_type_enums[] = {
62 GL_DEBUG_TYPE_ERROR,
63 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
64 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
65 GL_DEBUG_TYPE_PORTABILITY,
66 GL_DEBUG_TYPE_PERFORMANCE,
67 GL_DEBUG_TYPE_OTHER,
68 GL_DEBUG_TYPE_MARKER,
69 GL_DEBUG_TYPE_PUSH_GROUP,
70 GL_DEBUG_TYPE_POP_GROUP,
71 };
72
73 static const GLenum debug_severity_enums[] = {
74 GL_DEBUG_SEVERITY_LOW,
75 GL_DEBUG_SEVERITY_MEDIUM,
76 GL_DEBUG_SEVERITY_HIGH,
77 GL_DEBUG_SEVERITY_NOTIFICATION,
78 };
79
80
81 static enum mesa_debug_source
82 gl_enum_to_debug_source(GLenum e)
83 {
84 int i;
85
86 for (i = 0; i < Elements(debug_source_enums); i++) {
87 if (debug_source_enums[i] == e)
88 break;
89 }
90 return i;
91 }
92
93 static enum mesa_debug_type
94 gl_enum_to_debug_type(GLenum e)
95 {
96 int i;
97
98 for (i = 0; i < Elements(debug_type_enums); i++) {
99 if (debug_type_enums[i] == e)
100 break;
101 }
102 return i;
103 }
104
105 static enum mesa_debug_severity
106 gl_enum_to_debug_severity(GLenum e)
107 {
108 int i;
109
110 for (i = 0; i < Elements(debug_severity_enums); i++) {
111 if (debug_severity_enums[i] == e)
112 break;
113 }
114 return i;
115 }
116
117
118 /**
119 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
120 * GLSL compiler.
121 *
122 * The GL API has this "ID" mechanism, where the intention is to allow a
123 * client to filter in/out messages based on source, type, and ID. Of course,
124 * building a giant enum list of all debug output messages that Mesa might
125 * generate is ridiculous, so instead we have our caller pass us a pointer to
126 * static storage where the ID should get stored. This ID will be shared
127 * across all contexts for that message (which seems like a desirable
128 * property, even if it's not expected by the spec), but note that it won't be
129 * the same between executions if messages aren't generated in the same order.
130 */
131 static void
132 debug_get_id(GLuint *id)
133 {
134 if (!(*id)) {
135 mtx_lock(&DynamicIDMutex);
136 if (!(*id))
137 *id = NextDynamicID++;
138 mtx_unlock(&DynamicIDMutex);
139 }
140 }
141
142
143 /*
144 * We store a bitfield in the hash table, with five possible values total.
145 *
146 * The ENABLED_BIT's purpose is self-explanatory.
147 *
148 * The FOUND_BIT is needed to differentiate the value of DISABLED from
149 * the value returned by HashTableLookup() when it can't find the given key.
150 *
151 * The KNOWN_SEVERITY bit is a bit complicated:
152 *
153 * A client may call Control() with an array of IDs, then call Control()
154 * on all message IDs of a certain severity, then Insert() one of the
155 * previously specified IDs, giving us a known severity level, then call
156 * Control() on all message IDs of a certain severity level again.
157 *
158 * After the first call, those IDs will have a FOUND_BIT, but will not
159 * exist in any severity-specific list, so the second call will not
160 * impact them. This is undesirable but unavoidable given the API:
161 * The only entrypoint that gives a severity for a client-defined ID
162 * is the Insert() call.
163 *
164 * For the sake of Control(), we want to maintain the invariant
165 * that an ID will either appear in none of the three severity lists,
166 * or appear once, to minimize pointless duplication and potential surprises.
167 *
168 * Because Insert() is the only place that will learn an ID's severity,
169 * it should insert an ID into the appropriate list, but only if the ID
170 * doesn't exist in it or any other list yet. Because searching all three
171 * lists at O(n) is needlessly expensive, we store KNOWN_SEVERITY.
172 */
173 enum {
174 FOUND_BIT = 1 << 0,
175 ENABLED_BIT = 1 << 1,
176 KNOWN_SEVERITY = 1 << 2,
177
178 /* HashTable reserves zero as a return value meaning 'not found' */
179 NOT_FOUND = 0,
180 DISABLED = FOUND_BIT,
181 ENABLED = ENABLED_BIT | FOUND_BIT
182 };
183
184 /**
185 * Allocate and initialize context debug state.
186 */
187 static struct gl_debug_state *
188 debug_create(void)
189 {
190 struct gl_debug_state *debug;
191 int s, t, sev;
192
193 debug = CALLOC_STRUCT(gl_debug_state);
194 if (!debug)
195 return NULL;
196
197 /* Enable all the messages with severity HIGH or MEDIUM by default. */
198 memset(debug->Defaults[0][MESA_DEBUG_SEVERITY_HIGH], GL_TRUE,
199 sizeof debug->Defaults[0][MESA_DEBUG_SEVERITY_HIGH]);
200 memset(debug->Defaults[0][MESA_DEBUG_SEVERITY_MEDIUM], GL_TRUE,
201 sizeof debug->Defaults[0][MESA_DEBUG_SEVERITY_MEDIUM]);
202 memset(debug->Defaults[0][MESA_DEBUG_SEVERITY_LOW], GL_FALSE,
203 sizeof debug->Defaults[0][MESA_DEBUG_SEVERITY_LOW]);
204
205 /* Initialize state for filtering known debug messages. */
206 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) {
207 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) {
208 debug->Namespaces[0][s][t].IDs = _mesa_NewHashTable();
209 assert(debug->Namespaces[0][s][t].IDs);
210
211 for (sev = 0; sev < MESA_DEBUG_SEVERITY_COUNT; sev++) {
212 make_empty_list(&debug->Namespaces[0][s][t].Severity[sev]);
213 }
214 }
215 }
216
217 return debug;
218 }
219
220 /**
221 * Returns if the given message source/type/ID tuple is enabled.
222 */
223 static bool
224 debug_is_message_enabled(struct gl_debug_state *debug,
225 enum mesa_debug_source source,
226 enum mesa_debug_type type,
227 GLuint id,
228 enum mesa_debug_severity severity)
229 {
230 const GLint gstack = debug->GroupStackDepth;
231 struct gl_debug_namespace *nspace =
232 &debug->Namespaces[gstack][source][type];
233 uintptr_t state = 0;
234
235 if (!debug->DebugOutput)
236 return false;
237
238 /* In addition to not being able to store zero as a value, HashTable also
239 * can't use zero as a key.
240 */
241 if (id)
242 state = (uintptr_t)_mesa_HashLookup(nspace->IDs, id);
243 else
244 state = nspace->ZeroID;
245
246 /* Only do this once for each ID. This makes sure the ID exists in,
247 * at most, one list, and does not pointlessly appear multiple times.
248 */
249 if (!(state & KNOWN_SEVERITY)) {
250 struct gl_debug_severity *entry;
251
252 if (state == NOT_FOUND) {
253 if (debug->Defaults[gstack][severity][source][type])
254 state = ENABLED;
255 else
256 state = DISABLED;
257 }
258
259 entry = malloc(sizeof *entry);
260 if (!entry)
261 goto out;
262
263 state |= KNOWN_SEVERITY;
264
265 if (id)
266 _mesa_HashInsert(nspace->IDs, id, (void*)state);
267 else
268 nspace->ZeroID = state;
269
270 entry->ID = id;
271 insert_at_tail(&nspace->Severity[severity], &entry->link);
272 }
273 out:
274 return (state & ENABLED_BIT);
275 }
276
277
278 /**
279 * Return debug state for the context. The debug state will be allocated
280 * and initialized upon the first call.
281 */
282 struct gl_debug_state *
283 _mesa_get_debug_state(struct gl_context *ctx)
284 {
285 if (!ctx->Debug) {
286 ctx->Debug = debug_create();
287 if (!ctx->Debug) {
288 _mesa_error(ctx, GL_OUT_OF_MEMORY, "allocating debug state");
289 }
290 }
291
292 return ctx->Debug;
293 }
294
295
296
297 static GLboolean
298 should_log(struct gl_context *ctx,
299 enum mesa_debug_source source,
300 enum mesa_debug_type type,
301 GLuint id,
302 enum mesa_debug_severity severity)
303 {
304 struct gl_debug_state *debug;
305
306 if (!ctx->Debug) {
307 /* no debug state set so far */
308 return GL_FALSE;
309 }
310
311 debug = _mesa_get_debug_state(ctx);
312 if (debug)
313 return debug_is_message_enabled(debug, source, type, id, severity);
314 else
315 return GL_FALSE;
316 }
317
318
319 /**
320 * Sets the state of the given message source/type/ID tuple.
321 */
322 static void
323 set_message_state(struct gl_context *ctx,
324 enum mesa_debug_source source,
325 enum mesa_debug_type type,
326 GLuint id, GLboolean enabled)
327 {
328 struct gl_debug_state *debug = _mesa_get_debug_state(ctx);
329
330 if (debug) {
331 GLint gstack = debug->GroupStackDepth;
332 struct gl_debug_namespace *nspace =
333 &debug->Namespaces[gstack][source][type];
334 uintptr_t state;
335
336 /* In addition to not being able to store zero as a value, HashTable also
337 * can't use zero as a key.
338 */
339 if (id)
340 state = (uintptr_t)_mesa_HashLookup(nspace->IDs, id);
341 else
342 state = nspace->ZeroID;
343
344 if (state == NOT_FOUND)
345 state = enabled ? ENABLED : DISABLED;
346 else {
347 if (enabled)
348 state |= ENABLED_BIT;
349 else
350 state &= ~ENABLED_BIT;
351 }
352
353 if (id)
354 _mesa_HashInsert(nspace->IDs, id, (void*)state);
355 else
356 nspace->ZeroID = state;
357 }
358 }
359
360
361 static void
362 store_message_details(struct gl_debug_msg *emptySlot,
363 enum mesa_debug_source source,
364 enum mesa_debug_type type, GLuint id,
365 enum mesa_debug_severity severity, GLint len,
366 const char *buf)
367 {
368 assert(!emptySlot->message && !emptySlot->length);
369
370 emptySlot->message = malloc(len+1);
371 if (emptySlot->message) {
372 (void) strncpy(emptySlot->message, buf, (size_t)len);
373 emptySlot->message[len] = '\0';
374
375 emptySlot->length = len+1;
376 emptySlot->source = source;
377 emptySlot->type = type;
378 emptySlot->id = id;
379 emptySlot->severity = severity;
380 } else {
381 static GLuint oom_msg_id = 0;
382 debug_get_id(&oom_msg_id);
383
384 /* malloc failed! */
385 emptySlot->message = out_of_memory;
386 emptySlot->length = strlen(out_of_memory)+1;
387 emptySlot->source = MESA_DEBUG_SOURCE_OTHER;
388 emptySlot->type = MESA_DEBUG_TYPE_ERROR;
389 emptySlot->id = oom_msg_id;
390 emptySlot->severity = MESA_DEBUG_SEVERITY_HIGH;
391 }
392 }
393
394
395 /**
396 * 'buf' is not necessarily a null-terminated string. When logging, copy
397 * 'len' characters from it, store them in a new, null-terminated string,
398 * and remember the number of bytes used by that string, *including*
399 * the null terminator this time.
400 */
401 static void
402 log_msg(struct gl_context *ctx, enum mesa_debug_source source,
403 enum mesa_debug_type type, GLuint id,
404 enum mesa_debug_severity severity, GLint len, const char *buf)
405 {
406 struct gl_debug_state *debug = _mesa_get_debug_state(ctx);
407 GLint nextEmpty;
408 struct gl_debug_msg *emptySlot;
409
410 if (!debug)
411 return;
412
413 assert(len >= 0 && len < MAX_DEBUG_MESSAGE_LENGTH);
414
415 if (!should_log(ctx, source, type, id, severity))
416 return;
417
418 if (debug->Callback) {
419 GLenum gl_type = debug_type_enums[type];
420 GLenum gl_severity = debug_severity_enums[severity];
421
422 debug->Callback(debug_source_enums[source], gl_type, id, gl_severity,
423 len, buf, debug->CallbackData);
424 return;
425 }
426
427 if (debug->NumMessages == MAX_DEBUG_LOGGED_MESSAGES)
428 return;
429
430 nextEmpty = (debug->NextMsg + debug->NumMessages)
431 % MAX_DEBUG_LOGGED_MESSAGES;
432 emptySlot = &debug->Log[nextEmpty];
433
434 store_message_details(emptySlot, source, type, id, severity, len, buf);
435
436 if (debug->NumMessages == 0)
437 debug->NextMsgLength = debug->Log[debug->NextMsg].length;
438
439 debug->NumMessages++;
440 }
441
442
443 /**
444 * Pop the oldest debug message out of the log.
445 * Writes the message string, including the null terminator, into 'buf',
446 * using up to 'bufSize' bytes. If 'bufSize' is too small, or
447 * if 'buf' is NULL, nothing is written.
448 *
449 * Returns the number of bytes written on success, or when 'buf' is NULL,
450 * the number that would have been written. A return value of 0
451 * indicates failure.
452 */
453 static GLsizei
454 get_msg(struct gl_context *ctx, GLenum *source, GLenum *type,
455 GLuint *id, GLenum *severity, GLsizei bufSize, char *buf)
456 {
457 struct gl_debug_state *debug = _mesa_get_debug_state(ctx);
458 struct gl_debug_msg *msg;
459 GLsizei length;
460
461 if (!debug || debug->NumMessages == 0)
462 return 0;
463
464 msg = &debug->Log[debug->NextMsg];
465 length = msg->length;
466
467 assert(length > 0 && length == debug->NextMsgLength);
468
469 if (bufSize < length && buf != NULL)
470 return 0;
471
472 if (severity) {
473 *severity = debug_severity_enums[msg->severity];
474 }
475
476 if (source) {
477 *source = debug_source_enums[msg->source];
478 }
479
480 if (type) {
481 *type = debug_type_enums[msg->type];
482 }
483
484 if (id) {
485 *id = msg->id;
486 }
487
488 if (buf) {
489 assert(msg->message[length-1] == '\0');
490 (void) strncpy(buf, msg->message, (size_t)length);
491 }
492
493 if (msg->message != (char*)out_of_memory)
494 free(msg->message);
495 msg->message = NULL;
496 msg->length = 0;
497
498 debug->NumMessages--;
499 debug->NextMsg++;
500 debug->NextMsg %= MAX_DEBUG_LOGGED_MESSAGES;
501 debug->NextMsgLength = debug->Log[debug->NextMsg].length;
502
503 return length;
504 }
505
506
507 /**
508 * Verify that source, type, and severity are valid enums.
509 *
510 * The 'caller' param is used for handling values available
511 * only in glDebugMessageInsert or glDebugMessageControl
512 */
513 static GLboolean
514 validate_params(struct gl_context *ctx, unsigned caller,
515 const char *callerstr, GLenum source, GLenum type,
516 GLenum severity)
517 {
518 #define INSERT 1
519 #define CONTROL 2
520 switch(source) {
521 case GL_DEBUG_SOURCE_APPLICATION_ARB:
522 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB:
523 break;
524 case GL_DEBUG_SOURCE_API_ARB:
525 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB:
526 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB:
527 case GL_DEBUG_SOURCE_OTHER_ARB:
528 if (caller != INSERT)
529 break;
530 else
531 goto error;
532 case GL_DONT_CARE:
533 if (caller == CONTROL)
534 break;
535 else
536 goto error;
537 default:
538 goto error;
539 }
540
541 switch(type) {
542 case GL_DEBUG_TYPE_ERROR_ARB:
543 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
544 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
545 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
546 case GL_DEBUG_TYPE_PORTABILITY_ARB:
547 case GL_DEBUG_TYPE_OTHER_ARB:
548 case GL_DEBUG_TYPE_MARKER:
549 break;
550 case GL_DEBUG_TYPE_PUSH_GROUP:
551 case GL_DEBUG_TYPE_POP_GROUP:
552 case GL_DONT_CARE:
553 if (caller == CONTROL)
554 break;
555 else
556 goto error;
557 default:
558 goto error;
559 }
560
561 switch(severity) {
562 case GL_DEBUG_SEVERITY_HIGH_ARB:
563 case GL_DEBUG_SEVERITY_MEDIUM_ARB:
564 case GL_DEBUG_SEVERITY_LOW_ARB:
565 case GL_DEBUG_SEVERITY_NOTIFICATION:
566 break;
567 case GL_DONT_CARE:
568 if (caller == CONTROL)
569 break;
570 else
571 goto error;
572 default:
573 goto error;
574 }
575 return GL_TRUE;
576
577 error:
578 _mesa_error(ctx, GL_INVALID_ENUM, "bad values passed to %s"
579 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr,
580 source, type, severity);
581
582 return GL_FALSE;
583 }
584
585
586 /**
587 * Set the state of all message IDs found in the given intersection of
588 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
589 * GL_DONT_CARE (include all messages in the class).
590 *
591 * This requires both setting the state of all previously seen message
592 * IDs in the hash table, and setting the default state for all
593 * applicable combinations of source/type/severity, so that all the
594 * yet-unknown message IDs that may be used in the future will be
595 * impacted as if they were already known.
596 */
597 static void
598 control_messages(struct gl_context *ctx,
599 enum mesa_debug_source source,
600 enum mesa_debug_type type,
601 enum mesa_debug_severity severity,
602 GLboolean enabled)
603 {
604 struct gl_debug_state *debug = _mesa_get_debug_state(ctx);
605 int s, t, sev, smax, tmax, sevmax;
606 const GLint gstack = debug ? debug->GroupStackDepth : 0;
607
608 if (!debug)
609 return;
610
611 if (source == MESA_DEBUG_SOURCE_COUNT) {
612 source = 0;
613 smax = MESA_DEBUG_SOURCE_COUNT;
614 } else {
615 smax = source+1;
616 }
617
618 if (type == MESA_DEBUG_TYPE_COUNT) {
619 type = 0;
620 tmax = MESA_DEBUG_TYPE_COUNT;
621 } else {
622 tmax = type+1;
623 }
624
625 if (severity == MESA_DEBUG_SEVERITY_COUNT) {
626 severity = 0;
627 sevmax = MESA_DEBUG_SEVERITY_COUNT;
628 } else {
629 sevmax = severity+1;
630 }
631
632 for (sev = severity; sev < sevmax; sev++) {
633 for (s = source; s < smax; s++) {
634 for (t = type; t < tmax; t++) {
635 struct simple_node *node;
636 struct gl_debug_severity *entry;
637
638 /* change the default for IDs we've never seen before. */
639 debug->Defaults[gstack][sev][s][t] = enabled;
640
641 /* Now change the state of IDs we *have* seen... */
642 foreach(node, &debug->Namespaces[gstack][s][t].Severity[sev]) {
643 entry = (struct gl_debug_severity *)node;
644 set_message_state(ctx, s, t, entry->ID, enabled);
645 }
646 }
647 }
648 }
649 }
650
651
652 /**
653 * Debugging-message namespaces with the source APPLICATION or THIRD_PARTY
654 * require special handling, since the IDs in them are controlled by clients,
655 * not the OpenGL implementation.
656 *
657 * 'count' is the length of the array 'ids'. If 'count' is nonzero, all
658 * the given IDs in the namespace defined by 'esource' and 'etype'
659 * will be affected.
660 *
661 * If 'count' is zero, this sets the state of all IDs that match
662 * the combination of 'esource', 'etype', and 'eseverity'.
663 */
664 static void
665 control_app_messages(struct gl_context *ctx, GLenum esource, GLenum etype,
666 GLenum eseverity, GLsizei count, const GLuint *ids,
667 GLboolean enabled)
668 {
669 GLsizei i;
670 enum mesa_debug_source source = gl_enum_to_debug_source(esource);
671 enum mesa_debug_type type = gl_enum_to_debug_type(etype);
672 enum mesa_debug_severity severity = gl_enum_to_debug_severity(eseverity);
673
674 for (i = 0; i < count; i++)
675 set_message_state(ctx, source, type, ids[i], enabled);
676
677 if (count)
678 return;
679
680 control_messages(ctx, source, type, severity, enabled);
681 }
682
683
684 /**
685 * This is a generic message insert function.
686 * Validation of source, type and severity parameters should be done
687 * before calling this funtion.
688 */
689 static void
690 message_insert(GLenum source, GLenum type, GLuint id,
691 GLenum severity, GLint length, const GLchar *buf,
692 const char *callerstr)
693 {
694 GET_CURRENT_CONTEXT(ctx);
695
696 if (length < 0)
697 length = strlen(buf);
698
699 if (length >= MAX_DEBUG_MESSAGE_LENGTH) {
700 _mesa_error(ctx, GL_INVALID_VALUE,
701 "%s(length=%d, which is not less than "
702 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr, length,
703 MAX_DEBUG_MESSAGE_LENGTH);
704 return;
705 }
706
707 log_msg(ctx,
708 gl_enum_to_debug_source(source),
709 gl_enum_to_debug_type(type), id,
710 gl_enum_to_debug_severity(severity), length, buf);
711 }
712
713
714 static void
715 do_nothing(GLuint key, void *data, void *userData)
716 {
717 }
718
719
720 /**
721 * Free context state pertaining to error/debug state for the given stack
722 * depth.
723 */
724 static void
725 free_errors_data(struct gl_context *ctx, GLint gstack)
726 {
727 struct gl_debug_state *debug = ctx->Debug;
728 enum mesa_debug_type t;
729 enum mesa_debug_source s;
730 enum mesa_debug_severity sev;
731
732 assert(debug);
733
734 /* Tear down state for filtering debug messages. */
735 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) {
736 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) {
737 _mesa_HashDeleteAll(debug->Namespaces[gstack][s][t].IDs,
738 do_nothing, NULL);
739 _mesa_DeleteHashTable(debug->Namespaces[gstack][s][t].IDs);
740 for (sev = 0; sev < MESA_DEBUG_SEVERITY_COUNT; sev++) {
741 struct simple_node *node, *tmp;
742 struct gl_debug_severity *entry;
743
744 foreach_s(node, tmp,
745 &debug->Namespaces[gstack][s][t].Severity[sev]) {
746 entry = (struct gl_debug_severity *)node;
747 free(entry);
748 }
749 }
750 }
751 }
752 }
753
754
755 void GLAPIENTRY
756 _mesa_DebugMessageInsert(GLenum source, GLenum type, GLuint id,
757 GLenum severity, GLint length,
758 const GLchar *buf)
759 {
760 const char *callerstr = "glDebugMessageInsert";
761
762 GET_CURRENT_CONTEXT(ctx);
763
764 if (!validate_params(ctx, INSERT, callerstr, source, type, severity))
765 return; /* GL_INVALID_ENUM */
766
767 message_insert(source, type, id, severity, length, buf, callerstr);
768 }
769
770
771 GLuint GLAPIENTRY
772 _mesa_GetDebugMessageLog(GLuint count, GLsizei logSize, GLenum *sources,
773 GLenum *types, GLenum *ids, GLenum *severities,
774 GLsizei *lengths, GLchar *messageLog)
775 {
776 GET_CURRENT_CONTEXT(ctx);
777 GLuint ret;
778
779 if (!messageLog)
780 logSize = 0;
781
782 if (logSize < 0) {
783 _mesa_error(ctx, GL_INVALID_VALUE,
784 "glGetDebugMessageLog(logSize=%d : logSize must not be"
785 " negative)", logSize);
786 return 0;
787 }
788
789 for (ret = 0; ret < count; ret++) {
790 GLsizei written = get_msg(ctx, sources, types, ids, severities,
791 logSize, messageLog);
792 if (!written)
793 break;
794
795 if (messageLog) {
796 messageLog += written;
797 logSize -= written;
798 }
799 if (lengths) {
800 *lengths = written;
801 lengths++;
802 }
803
804 if (severities)
805 severities++;
806 if (sources)
807 sources++;
808 if (types)
809 types++;
810 if (ids)
811 ids++;
812 }
813
814 return ret;
815 }
816
817
818 void GLAPIENTRY
819 _mesa_DebugMessageControl(GLenum gl_source, GLenum gl_type,
820 GLenum gl_severity, GLsizei count,
821 const GLuint *ids, GLboolean enabled)
822 {
823 const char *callerstr = "glDebugMessageControl";
824
825 GET_CURRENT_CONTEXT(ctx);
826
827 if (count < 0) {
828 _mesa_error(ctx, GL_INVALID_VALUE,
829 "%s(count=%d : count must not be negative)", callerstr,
830 count);
831 return;
832 }
833
834 if (!validate_params(ctx, CONTROL, callerstr, gl_source, gl_type,
835 gl_severity))
836 return; /* GL_INVALID_ENUM */
837
838 if (count && (gl_severity != GL_DONT_CARE || gl_type == GL_DONT_CARE
839 || gl_source == GL_DONT_CARE)) {
840 _mesa_error(ctx, GL_INVALID_OPERATION,
841 "%s(When passing an array of ids, severity must be"
842 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
843 callerstr);
844 return;
845 }
846
847 control_app_messages(ctx, gl_source, gl_type, gl_severity,
848 count, ids, enabled);
849 }
850
851
852 void GLAPIENTRY
853 _mesa_DebugMessageCallback(GLDEBUGPROC callback, const void *userParam)
854 {
855 GET_CURRENT_CONTEXT(ctx);
856 struct gl_debug_state *debug = _mesa_get_debug_state(ctx);
857 if (debug) {
858 debug->Callback = callback;
859 debug->CallbackData = userParam;
860 }
861 }
862
863
864 void GLAPIENTRY
865 _mesa_PushDebugGroup(GLenum source, GLuint id, GLsizei length,
866 const GLchar *message)
867 {
868 GET_CURRENT_CONTEXT(ctx);
869 struct gl_debug_state *debug = _mesa_get_debug_state(ctx);
870 const char *callerstr = "glPushDebugGroup";
871 int s, t, sev;
872 GLint prevStackDepth;
873 GLint currStackDepth;
874 struct gl_debug_msg *emptySlot;
875
876 if (!debug)
877 return;
878
879 if (debug->GroupStackDepth >= MAX_DEBUG_GROUP_STACK_DEPTH-1) {
880 _mesa_error(ctx, GL_STACK_OVERFLOW, "%s", callerstr);
881 return;
882 }
883
884 switch(source) {
885 case GL_DEBUG_SOURCE_APPLICATION:
886 case GL_DEBUG_SOURCE_THIRD_PARTY:
887 break;
888 default:
889 _mesa_error(ctx, GL_INVALID_ENUM, "bad value passed to %s"
890 "(source=0x%x)", callerstr, source);
891 return;
892 }
893
894 message_insert(source, GL_DEBUG_TYPE_PUSH_GROUP, id,
895 GL_DEBUG_SEVERITY_NOTIFICATION, length,
896 message, callerstr);
897
898 prevStackDepth = debug->GroupStackDepth;
899 debug->GroupStackDepth++;
900 currStackDepth = debug->GroupStackDepth;
901
902 /* pop reuses the message details from push so we store this */
903 if (length < 0)
904 length = strlen(message);
905 emptySlot = &debug->DebugGroupMsgs[debug->GroupStackDepth];
906 store_message_details(emptySlot, gl_enum_to_debug_source(source),
907 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP),
908 id,
909 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION),
910 length, message);
911
912 /* inherit the control volume of the debug group previously residing on
913 * the top of the debug group stack
914 */
915 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) {
916 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) {
917 /* copy id settings */
918 debug->Namespaces[currStackDepth][s][t].IDs =
919 _mesa_HashClone(debug->Namespaces[prevStackDepth][s][t].IDs);
920
921 for (sev = 0; sev < MESA_DEBUG_SEVERITY_COUNT; sev++) {
922 struct gl_debug_severity *entry, *prevEntry;
923 struct simple_node *node;
924
925 /* copy default settings for unknown ids */
926 debug->Defaults[currStackDepth][sev][s][t] =
927 debug->Defaults[prevStackDepth][sev][s][t];
928
929 /* copy known id severity settings */
930 make_empty_list(&debug->Namespaces[currStackDepth][s][t].Severity[sev]);
931 foreach(node, &debug->Namespaces[prevStackDepth][s][t].Severity[sev]) {
932 prevEntry = (struct gl_debug_severity *)node;
933 entry = malloc(sizeof *entry);
934 if (!entry)
935 return;
936
937 entry->ID = prevEntry->ID;
938 insert_at_tail(&debug->Namespaces[currStackDepth][s][t].Severity[sev], &entry->link);
939 }
940 }
941 }
942 }
943 }
944
945
946 void GLAPIENTRY
947 _mesa_PopDebugGroup(void)
948 {
949 GET_CURRENT_CONTEXT(ctx);
950 struct gl_debug_state *debug = _mesa_get_debug_state(ctx);
951 const char *callerstr = "glPopDebugGroup";
952 struct gl_debug_msg *gdmessage;
953 GLint prevStackDepth;
954
955 if (!debug)
956 return;
957
958 if (debug->GroupStackDepth <= 0) {
959 _mesa_error(ctx, GL_STACK_UNDERFLOW, "%s", callerstr);
960 return;
961 }
962
963 prevStackDepth = debug->GroupStackDepth;
964 debug->GroupStackDepth--;
965
966 gdmessage = &debug->DebugGroupMsgs[prevStackDepth];
967 /* using log_msg() directly here as verification of parameters
968 * already done in push
969 */
970 log_msg(ctx, gdmessage->source,
971 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP),
972 gdmessage->id,
973 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION),
974 gdmessage->length, gdmessage->message);
975
976 if (gdmessage->message != (char*)out_of_memory)
977 free(gdmessage->message);
978 gdmessage->message = NULL;
979 gdmessage->length = 0;
980
981 /* free popped debug group data */
982 free_errors_data(ctx, prevStackDepth);
983 }
984
985
986 void
987 _mesa_init_errors(struct gl_context *ctx)
988 {
989 /* no-op */
990 }
991
992
993 /**
994 * Loop through debug group stack tearing down states for
995 * filtering debug messages. Then free debug output state.
996 */
997 void
998 _mesa_free_errors_data(struct gl_context *ctx)
999 {
1000 if (ctx->Debug) {
1001 GLint i;
1002
1003 for (i = 0; i <= ctx->Debug->GroupStackDepth; i++) {
1004 free_errors_data(ctx, i);
1005 }
1006 free(ctx->Debug);
1007 /* set to NULL just in case it is used before context is completely gone. */
1008 ctx->Debug = NULL;
1009 }
1010 }
1011
1012
1013 /**********************************************************************/
1014 /** \name Diagnostics */
1015 /*@{*/
1016
1017 static void
1018 output_if_debug(const char *prefixString, const char *outputString,
1019 GLboolean newline)
1020 {
1021 static int debug = -1;
1022 static FILE *fout = NULL;
1023
1024 /* Init the local 'debug' var once.
1025 * Note: the _mesa_init_debug() function should have been called
1026 * by now so MESA_DEBUG_FLAGS will be initialized.
1027 */
1028 if (debug == -1) {
1029 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1030 * etc to the named file. Otherwise, output to stderr.
1031 */
1032 const char *logFile = _mesa_getenv("MESA_LOG_FILE");
1033 if (logFile)
1034 fout = fopen(logFile, "w");
1035 if (!fout)
1036 fout = stderr;
1037 #ifdef DEBUG
1038 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1039 if (MESA_DEBUG_FLAGS & DEBUG_SILENT)
1040 debug = 0;
1041 else
1042 debug = 1;
1043 #else
1044 /* in release builds, be silent unless MESA_DEBUG is set */
1045 debug = _mesa_getenv("MESA_DEBUG") != NULL;
1046 #endif
1047 }
1048
1049 /* Now only print the string if we're required to do so. */
1050 if (debug) {
1051 fprintf(fout, "%s: %s", prefixString, outputString);
1052 if (newline)
1053 fprintf(fout, "\n");
1054 fflush(fout);
1055
1056 #if defined(_WIN32) && !defined(_WIN32_WCE)
1057 /* stderr from windows applications without console is not usually
1058 * visible, so communicate with the debugger instead */
1059 {
1060 char buf[4096];
1061 _mesa_snprintf(buf, sizeof(buf), "%s: %s%s", prefixString, outputString, newline ? "\n" : "");
1062 OutputDebugStringA(buf);
1063 }
1064 #endif
1065 }
1066 }
1067
1068
1069 /**
1070 * When a new type of error is recorded, print a message describing
1071 * previous errors which were accumulated.
1072 */
1073 static void
1074 flush_delayed_errors( struct gl_context *ctx )
1075 {
1076 char s[MAX_DEBUG_MESSAGE_LENGTH];
1077
1078 if (ctx->ErrorDebugCount) {
1079 _mesa_snprintf(s, MAX_DEBUG_MESSAGE_LENGTH, "%d similar %s errors",
1080 ctx->ErrorDebugCount,
1081 _mesa_lookup_enum_by_nr(ctx->ErrorValue));
1082
1083 output_if_debug("Mesa", s, GL_TRUE);
1084
1085 ctx->ErrorDebugCount = 0;
1086 }
1087 }
1088
1089
1090 /**
1091 * Report a warning (a recoverable error condition) to stderr if
1092 * either DEBUG is defined or the MESA_DEBUG env var is set.
1093 *
1094 * \param ctx GL context.
1095 * \param fmtString printf()-like format string.
1096 */
1097 void
1098 _mesa_warning( struct gl_context *ctx, const char *fmtString, ... )
1099 {
1100 char str[MAX_DEBUG_MESSAGE_LENGTH];
1101 va_list args;
1102 va_start( args, fmtString );
1103 (void) _mesa_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
1104 va_end( args );
1105
1106 if (ctx)
1107 flush_delayed_errors( ctx );
1108
1109 output_if_debug("Mesa warning", str, GL_TRUE);
1110 }
1111
1112
1113 /**
1114 * Report an internal implementation problem.
1115 * Prints the message to stderr via fprintf().
1116 *
1117 * \param ctx GL context.
1118 * \param fmtString problem description string.
1119 */
1120 void
1121 _mesa_problem( const struct gl_context *ctx, const char *fmtString, ... )
1122 {
1123 va_list args;
1124 char str[MAX_DEBUG_MESSAGE_LENGTH];
1125 static int numCalls = 0;
1126
1127 (void) ctx;
1128
1129 if (numCalls < 50) {
1130 numCalls++;
1131
1132 va_start( args, fmtString );
1133 _mesa_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
1134 va_end( args );
1135 fprintf(stderr, "Mesa %s implementation error: %s\n",
1136 PACKAGE_VERSION, str);
1137 fprintf(stderr, "Please report at " PACKAGE_BUGREPORT "\n");
1138 }
1139 }
1140
1141
1142 static GLboolean
1143 should_output(struct gl_context *ctx, GLenum error, const char *fmtString)
1144 {
1145 static GLint debug = -1;
1146
1147 /* Check debug environment variable only once:
1148 */
1149 if (debug == -1) {
1150 const char *debugEnv = _mesa_getenv("MESA_DEBUG");
1151
1152 #ifdef DEBUG
1153 if (debugEnv && strstr(debugEnv, "silent"))
1154 debug = GL_FALSE;
1155 else
1156 debug = GL_TRUE;
1157 #else
1158 if (debugEnv)
1159 debug = GL_TRUE;
1160 else
1161 debug = GL_FALSE;
1162 #endif
1163 }
1164
1165 if (debug) {
1166 if (ctx->ErrorValue != error ||
1167 ctx->ErrorDebugFmtString != fmtString) {
1168 flush_delayed_errors( ctx );
1169 ctx->ErrorDebugFmtString = fmtString;
1170 ctx->ErrorDebugCount = 0;
1171 return GL_TRUE;
1172 }
1173 ctx->ErrorDebugCount++;
1174 }
1175 return GL_FALSE;
1176 }
1177
1178
1179 void
1180 _mesa_gl_debug(struct gl_context *ctx,
1181 GLuint *id,
1182 enum mesa_debug_type type,
1183 enum mesa_debug_severity severity,
1184 const char *fmtString, ...)
1185 {
1186 char s[MAX_DEBUG_MESSAGE_LENGTH];
1187 int len;
1188 va_list args;
1189
1190 debug_get_id(id);
1191
1192 va_start(args, fmtString);
1193 len = _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
1194 va_end(args);
1195
1196 log_msg(ctx, MESA_DEBUG_SOURCE_API, type, *id, severity, len, s);
1197 }
1198
1199
1200 /**
1201 * Record an OpenGL state error. These usually occur when the user
1202 * passes invalid parameters to a GL function.
1203 *
1204 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1205 * run-time via the MESA_DEBUG environment variable), report the error with
1206 * _mesa_debug().
1207 *
1208 * \param ctx the GL context.
1209 * \param error the error value.
1210 * \param fmtString printf() style format string, followed by optional args
1211 */
1212 void
1213 _mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... )
1214 {
1215 GLboolean do_output, do_log;
1216 /* Ideally this would be set up by the caller, so that we had proper IDs
1217 * per different message.
1218 */
1219 static GLuint error_msg_id = 0;
1220
1221 debug_get_id(&error_msg_id);
1222
1223 do_output = should_output(ctx, error, fmtString);
1224 do_log = should_log(ctx,
1225 MESA_DEBUG_SOURCE_API,
1226 MESA_DEBUG_TYPE_ERROR,
1227 error_msg_id,
1228 MESA_DEBUG_SEVERITY_HIGH);
1229
1230 if (do_output || do_log) {
1231 char s[MAX_DEBUG_MESSAGE_LENGTH], s2[MAX_DEBUG_MESSAGE_LENGTH];
1232 int len;
1233 va_list args;
1234
1235 va_start(args, fmtString);
1236 len = _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
1237 va_end(args);
1238
1239 if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
1240 /* Too long error message. Whoever calls _mesa_error should use
1241 * shorter strings.
1242 */
1243 ASSERT(0);
1244 return;
1245 }
1246
1247 len = _mesa_snprintf(s2, MAX_DEBUG_MESSAGE_LENGTH, "%s in %s",
1248 _mesa_lookup_enum_by_nr(error), s);
1249 if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
1250 /* Same as above. */
1251 ASSERT(0);
1252 return;
1253 }
1254
1255 /* Print the error to stderr if needed. */
1256 if (do_output) {
1257 output_if_debug("Mesa: User error", s2, GL_TRUE);
1258 }
1259
1260 /* Log the error via ARB_debug_output if needed.*/
1261 if (do_log) {
1262 log_msg(ctx, MESA_DEBUG_SOURCE_API, MESA_DEBUG_TYPE_ERROR,
1263 error_msg_id, MESA_DEBUG_SEVERITY_HIGH, len, s2);
1264 }
1265 }
1266
1267 /* Set the GL context error state for glGetError. */
1268 _mesa_record_error(ctx, error);
1269 }
1270
1271
1272 /**
1273 * Report debug information. Print error message to stderr via fprintf().
1274 * No-op if DEBUG mode not enabled.
1275 *
1276 * \param ctx GL context.
1277 * \param fmtString printf()-style format string, followed by optional args.
1278 */
1279 void
1280 _mesa_debug( const struct gl_context *ctx, const char *fmtString, ... )
1281 {
1282 #ifdef DEBUG
1283 char s[MAX_DEBUG_MESSAGE_LENGTH];
1284 va_list args;
1285 va_start(args, fmtString);
1286 _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
1287 va_end(args);
1288 output_if_debug("Mesa", s, GL_FALSE);
1289 #endif /* DEBUG */
1290 (void) ctx;
1291 (void) fmtString;
1292 }
1293
1294
1295 /**
1296 * Report debug information from the shader compiler via GL_ARB_debug_output.
1297 *
1298 * \param ctx GL context.
1299 * \param type The namespace to which this message belongs.
1300 * \param id The message ID within the given namespace.
1301 * \param msg The message to output. Need not be null-terminated.
1302 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1303 */
1304 void
1305 _mesa_shader_debug( struct gl_context *ctx, GLenum type, GLuint *id,
1306 const char *msg, int len )
1307 {
1308 enum mesa_debug_source source = MESA_DEBUG_SOURCE_SHADER_COMPILER;
1309 enum mesa_debug_severity severity = MESA_DEBUG_SEVERITY_HIGH;
1310
1311 debug_get_id(id);
1312
1313 if (len < 0)
1314 len = strlen(msg);
1315
1316 /* Truncate the message if necessary. */
1317 if (len >= MAX_DEBUG_MESSAGE_LENGTH)
1318 len = MAX_DEBUG_MESSAGE_LENGTH - 1;
1319
1320 log_msg(ctx, source, type, *id, severity, len, msg);
1321 }
1322
1323 /*@}*/