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