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