mesa: Make glGet queries initialize ctx->Debug when necessary.
[mesa.git] / src / mesa / main / debug_output.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2016 Brian Paul, et al All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include "context.h"
29 #include "debug_output.h"
30 #include "dispatch.h"
31 #include "enums.h"
32 #include "imports.h"
33 #include "hash.h"
34 #include "mtypes.h"
35 #include "version.h"
36 #include "util/hash_table.h"
37 #include "util/simple_list.h"
38
39
40 static mtx_t DynamicIDMutex = _MTX_INITIALIZER_NP;
41 static GLuint NextDynamicID = 1;
42
43
44 /**
45 * A namespace element.
46 */
47 struct gl_debug_element
48 {
49 struct simple_node link;
50
51 GLuint ID;
52 /* at which severity levels (mesa_debug_severity) is the message enabled */
53 GLbitfield State;
54 };
55
56
57 struct gl_debug_namespace
58 {
59 struct simple_node Elements;
60 GLbitfield DefaultState;
61 };
62
63
64 struct gl_debug_group {
65 struct gl_debug_namespace Namespaces[MESA_DEBUG_SOURCE_COUNT][MESA_DEBUG_TYPE_COUNT];
66 };
67
68
69 /**
70 * An error, warning, or other piece of debug information for an application
71 * to consume via GL_ARB_debug_output/GL_KHR_debug.
72 */
73 struct gl_debug_message
74 {
75 enum mesa_debug_source source;
76 enum mesa_debug_type type;
77 GLuint id;
78 enum mesa_debug_severity severity;
79 /* length as given by the user - if message was explicitly null terminated,
80 * length can be negative */
81 GLsizei length;
82 GLcharARB *message;
83 };
84
85
86 /**
87 * Debug message log. It works like a ring buffer.
88 */
89 struct gl_debug_log {
90 struct gl_debug_message Messages[MAX_DEBUG_LOGGED_MESSAGES];
91 GLint NextMessage;
92 GLint NumMessages;
93 };
94
95
96 struct gl_debug_state
97 {
98 GLDEBUGPROC Callback;
99 const void *CallbackData;
100 GLboolean SyncOutput;
101 GLboolean DebugOutput;
102
103 struct gl_debug_group *Groups[MAX_DEBUG_GROUP_STACK_DEPTH];
104 struct gl_debug_message GroupMessages[MAX_DEBUG_GROUP_STACK_DEPTH];
105 GLint CurrentGroup; // GroupStackDepth - 1
106
107 struct gl_debug_log Log;
108 };
109
110
111 static char out_of_memory[] = "Debugging error: out of memory";
112
113 static const GLenum debug_source_enums[] = {
114 GL_DEBUG_SOURCE_API,
115 GL_DEBUG_SOURCE_WINDOW_SYSTEM,
116 GL_DEBUG_SOURCE_SHADER_COMPILER,
117 GL_DEBUG_SOURCE_THIRD_PARTY,
118 GL_DEBUG_SOURCE_APPLICATION,
119 GL_DEBUG_SOURCE_OTHER,
120 };
121
122 static const GLenum debug_type_enums[] = {
123 GL_DEBUG_TYPE_ERROR,
124 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
125 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
126 GL_DEBUG_TYPE_PORTABILITY,
127 GL_DEBUG_TYPE_PERFORMANCE,
128 GL_DEBUG_TYPE_OTHER,
129 GL_DEBUG_TYPE_MARKER,
130 GL_DEBUG_TYPE_PUSH_GROUP,
131 GL_DEBUG_TYPE_POP_GROUP,
132 };
133
134 static const GLenum debug_severity_enums[] = {
135 GL_DEBUG_SEVERITY_LOW,
136 GL_DEBUG_SEVERITY_MEDIUM,
137 GL_DEBUG_SEVERITY_HIGH,
138 GL_DEBUG_SEVERITY_NOTIFICATION,
139 };
140
141
142 static enum mesa_debug_source
143 gl_enum_to_debug_source(GLenum e)
144 {
145 unsigned i;
146
147 for (i = 0; i < ARRAY_SIZE(debug_source_enums); i++) {
148 if (debug_source_enums[i] == e)
149 break;
150 }
151 return i;
152 }
153
154 static enum mesa_debug_type
155 gl_enum_to_debug_type(GLenum e)
156 {
157 unsigned i;
158
159 for (i = 0; i < ARRAY_SIZE(debug_type_enums); i++) {
160 if (debug_type_enums[i] == e)
161 break;
162 }
163 return i;
164 }
165
166 static enum mesa_debug_severity
167 gl_enum_to_debug_severity(GLenum e)
168 {
169 unsigned i;
170
171 for (i = 0; i < ARRAY_SIZE(debug_severity_enums); i++) {
172 if (debug_severity_enums[i] == e)
173 break;
174 }
175 return i;
176 }
177
178
179 /**
180 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
181 * GLSL compiler.
182 *
183 * The GL API has this "ID" mechanism, where the intention is to allow a
184 * client to filter in/out messages based on source, type, and ID. Of course,
185 * building a giant enum list of all debug output messages that Mesa might
186 * generate is ridiculous, so instead we have our caller pass us a pointer to
187 * static storage where the ID should get stored. This ID will be shared
188 * across all contexts for that message (which seems like a desirable
189 * property, even if it's not expected by the spec), but note that it won't be
190 * the same between executions if messages aren't generated in the same order.
191 */
192 void
193 _mesa_debug_get_id(GLuint *id)
194 {
195 if (!(*id)) {
196 mtx_lock(&DynamicIDMutex);
197 if (!(*id))
198 *id = NextDynamicID++;
199 mtx_unlock(&DynamicIDMutex);
200 }
201 }
202
203 static void
204 debug_message_clear(struct gl_debug_message *msg)
205 {
206 if (msg->message != (char*)out_of_memory)
207 free(msg->message);
208 msg->message = NULL;
209 msg->length = 0;
210 }
211
212 static void
213 debug_message_store(struct gl_debug_message *msg,
214 enum mesa_debug_source source,
215 enum mesa_debug_type type, GLuint id,
216 enum mesa_debug_severity severity,
217 GLsizei len, const char *buf)
218 {
219 GLsizei length = len;
220
221 assert(!msg->message && !msg->length);
222
223 if (length < 0)
224 length = strlen(buf);
225
226 msg->message = malloc(length+1);
227 if (msg->message) {
228 (void) strncpy(msg->message, buf, (size_t)length);
229 msg->message[length] = '\0';
230
231 msg->length = len;
232 msg->source = source;
233 msg->type = type;
234 msg->id = id;
235 msg->severity = severity;
236 } else {
237 static GLuint oom_msg_id = 0;
238 _mesa_debug_get_id(&oom_msg_id);
239
240 /* malloc failed! */
241 msg->message = out_of_memory;
242 msg->length = -1;
243 msg->source = MESA_DEBUG_SOURCE_OTHER;
244 msg->type = MESA_DEBUG_TYPE_ERROR;
245 msg->id = oom_msg_id;
246 msg->severity = MESA_DEBUG_SEVERITY_HIGH;
247 }
248 }
249
250 static void
251 debug_namespace_init(struct gl_debug_namespace *ns)
252 {
253 make_empty_list(&ns->Elements);
254
255 /* Enable all the messages with severity HIGH or MEDIUM by default */
256 ns->DefaultState = (1 << MESA_DEBUG_SEVERITY_MEDIUM ) |
257 (1 << MESA_DEBUG_SEVERITY_HIGH) |
258 (1 << MESA_DEBUG_SEVERITY_NOTIFICATION);
259 }
260
261 static void
262 debug_namespace_clear(struct gl_debug_namespace *ns)
263 {
264 struct simple_node *node, *tmp;
265
266 foreach_s(node, tmp, &ns->Elements)
267 free(node);
268 }
269
270 static bool
271 debug_namespace_copy(struct gl_debug_namespace *dst,
272 const struct gl_debug_namespace *src)
273 {
274 struct simple_node *node;
275
276 dst->DefaultState = src->DefaultState;
277
278 make_empty_list(&dst->Elements);
279 foreach(node, &src->Elements) {
280 const struct gl_debug_element *elem =
281 (const struct gl_debug_element *) node;
282 struct gl_debug_element *copy;
283
284 copy = malloc(sizeof(*copy));
285 if (!copy) {
286 debug_namespace_clear(dst);
287 return false;
288 }
289
290 copy->ID = elem->ID;
291 copy->State = elem->State;
292 insert_at_tail(&dst->Elements, &copy->link);
293 }
294
295 return true;
296 }
297
298 /**
299 * Set the state of \p id in the namespace.
300 */
301 static bool
302 debug_namespace_set(struct gl_debug_namespace *ns,
303 GLuint id, bool enabled)
304 {
305 const uint32_t state = (enabled) ?
306 ((1 << MESA_DEBUG_SEVERITY_COUNT) - 1) : 0;
307 struct gl_debug_element *elem = NULL;
308 struct simple_node *node;
309
310 /* find the element */
311 foreach(node, &ns->Elements) {
312 struct gl_debug_element *tmp = (struct gl_debug_element *) node;
313 if (tmp->ID == id) {
314 elem = tmp;
315 break;
316 }
317 }
318
319 /* we do not need the element if it has the default state */
320 if (ns->DefaultState == state) {
321 if (elem) {
322 remove_from_list(&elem->link);
323 free(elem);
324 }
325 return true;
326 }
327
328 if (!elem) {
329 elem = malloc(sizeof(*elem));
330 if (!elem)
331 return false;
332
333 elem->ID = id;
334 insert_at_tail(&ns->Elements, &elem->link);
335 }
336
337 elem->State = state;
338
339 return true;
340 }
341
342 /**
343 * Set the default state of the namespace for \p severity. When \p severity
344 * is MESA_DEBUG_SEVERITY_COUNT, the default values for all severities are
345 * updated.
346 */
347 static void
348 debug_namespace_set_all(struct gl_debug_namespace *ns,
349 enum mesa_debug_severity severity,
350 bool enabled)
351 {
352 struct simple_node *node, *tmp;
353 uint32_t mask, val;
354
355 /* set all elements to the same state */
356 if (severity == MESA_DEBUG_SEVERITY_COUNT) {
357 ns->DefaultState = (enabled) ? ((1 << severity) - 1) : 0;
358 debug_namespace_clear(ns);
359 make_empty_list(&ns->Elements);
360 return;
361 }
362
363 mask = 1 << severity;
364 val = (enabled) ? mask : 0;
365
366 ns->DefaultState = (ns->DefaultState & ~mask) | val;
367
368 foreach_s(node, tmp, &ns->Elements) {
369 struct gl_debug_element *elem = (struct gl_debug_element *) node;
370
371 elem->State = (elem->State & ~mask) | val;
372 if (elem->State == ns->DefaultState) {
373 remove_from_list(node);
374 free(node);
375 }
376 }
377 }
378
379 /**
380 * Get the state of \p id in the namespace.
381 */
382 static bool
383 debug_namespace_get(const struct gl_debug_namespace *ns, GLuint id,
384 enum mesa_debug_severity severity)
385 {
386 struct simple_node *node;
387 uint32_t state;
388
389 state = ns->DefaultState;
390 foreach(node, &ns->Elements) {
391 struct gl_debug_element *elem = (struct gl_debug_element *) node;
392
393 if (elem->ID == id) {
394 state = elem->State;
395 break;
396 }
397 }
398
399 return (state & (1 << severity));
400 }
401
402 /**
403 * Allocate and initialize context debug state.
404 */
405 static struct gl_debug_state *
406 debug_create(void)
407 {
408 struct gl_debug_state *debug;
409 int s, t;
410
411 debug = CALLOC_STRUCT(gl_debug_state);
412 if (!debug)
413 return NULL;
414
415 debug->Groups[0] = malloc(sizeof(*debug->Groups[0]));
416 if (!debug->Groups[0]) {
417 free(debug);
418 return NULL;
419 }
420
421 /* Initialize state for filtering known debug messages. */
422 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) {
423 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++)
424 debug_namespace_init(&debug->Groups[0]->Namespaces[s][t]);
425 }
426
427 return debug;
428 }
429
430 /**
431 * Return true if the top debug group points to the group below it.
432 */
433 static bool
434 debug_is_group_read_only(const struct gl_debug_state *debug)
435 {
436 const GLint gstack = debug->CurrentGroup;
437 return (gstack > 0 && debug->Groups[gstack] == debug->Groups[gstack - 1]);
438 }
439
440 /**
441 * Make the top debug group writable.
442 */
443 static bool
444 debug_make_group_writable(struct gl_debug_state *debug)
445 {
446 const GLint gstack = debug->CurrentGroup;
447 const struct gl_debug_group *src = debug->Groups[gstack];
448 struct gl_debug_group *dst;
449 int s, t;
450
451 if (!debug_is_group_read_only(debug))
452 return true;
453
454 dst = malloc(sizeof(*dst));
455 if (!dst)
456 return false;
457
458 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) {
459 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) {
460 if (!debug_namespace_copy(&dst->Namespaces[s][t],
461 &src->Namespaces[s][t])) {
462 /* error path! */
463 for (t = t - 1; t >= 0; t--)
464 debug_namespace_clear(&dst->Namespaces[s][t]);
465 for (s = s - 1; s >= 0; s--) {
466 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++)
467 debug_namespace_clear(&dst->Namespaces[s][t]);
468 }
469 free(dst);
470 return false;
471 }
472 }
473 }
474
475 debug->Groups[gstack] = dst;
476
477 return true;
478 }
479
480 /**
481 * Free the top debug group.
482 */
483 static void
484 debug_clear_group(struct gl_debug_state *debug)
485 {
486 const GLint gstack = debug->CurrentGroup;
487
488 if (!debug_is_group_read_only(debug)) {
489 struct gl_debug_group *grp = debug->Groups[gstack];
490 int s, t;
491
492 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) {
493 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++)
494 debug_namespace_clear(&grp->Namespaces[s][t]);
495 }
496
497 free(grp);
498 }
499
500 debug->Groups[gstack] = NULL;
501 }
502
503 /**
504 * Loop through debug group stack tearing down states for
505 * filtering debug messages. Then free debug output state.
506 */
507 static void
508 debug_destroy(struct gl_debug_state *debug)
509 {
510 while (debug->CurrentGroup > 0) {
511 debug_clear_group(debug);
512 debug->CurrentGroup--;
513 }
514
515 debug_clear_group(debug);
516 free(debug);
517 }
518
519 /**
520 * Sets the state of the given message source/type/ID tuple.
521 */
522 static void
523 debug_set_message_enable(struct gl_debug_state *debug,
524 enum mesa_debug_source source,
525 enum mesa_debug_type type,
526 GLuint id, GLboolean enabled)
527 {
528 const GLint gstack = debug->CurrentGroup;
529 struct gl_debug_namespace *ns;
530
531 debug_make_group_writable(debug);
532 ns = &debug->Groups[gstack]->Namespaces[source][type];
533
534 debug_namespace_set(ns, id, enabled);
535 }
536
537 /*
538 * Set the state of all message IDs found in the given intersection of
539 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
540 * GL_DONT_CARE (include all messages in the class).
541 *
542 * This requires both setting the state of all previously seen message
543 * IDs in the hash table, and setting the default state for all
544 * applicable combinations of source/type/severity, so that all the
545 * yet-unknown message IDs that may be used in the future will be
546 * impacted as if they were already known.
547 */
548 static void
549 debug_set_message_enable_all(struct gl_debug_state *debug,
550 enum mesa_debug_source source,
551 enum mesa_debug_type type,
552 enum mesa_debug_severity severity,
553 GLboolean enabled)
554 {
555 const GLint gstack = debug->CurrentGroup;
556 int s, t, smax, tmax;
557
558 if (source == MESA_DEBUG_SOURCE_COUNT) {
559 source = 0;
560 smax = MESA_DEBUG_SOURCE_COUNT;
561 } else {
562 smax = source+1;
563 }
564
565 if (type == MESA_DEBUG_TYPE_COUNT) {
566 type = 0;
567 tmax = MESA_DEBUG_TYPE_COUNT;
568 } else {
569 tmax = type+1;
570 }
571
572 debug_make_group_writable(debug);
573
574 for (s = source; s < smax; s++) {
575 for (t = type; t < tmax; t++) {
576 struct gl_debug_namespace *nspace =
577 &debug->Groups[gstack]->Namespaces[s][t];
578 debug_namespace_set_all(nspace, severity, enabled);
579 }
580 }
581 }
582
583 /**
584 * Returns if the given message source/type/ID tuple is enabled.
585 */
586 bool
587 _mesa_debug_is_message_enabled(const struct gl_debug_state *debug,
588 enum mesa_debug_source source,
589 enum mesa_debug_type type,
590 GLuint id,
591 enum mesa_debug_severity severity)
592 {
593 const GLint gstack = debug->CurrentGroup;
594 struct gl_debug_group *grp = debug->Groups[gstack];
595 struct gl_debug_namespace *nspace = &grp->Namespaces[source][type];
596
597 if (!debug->DebugOutput)
598 return false;
599
600 return debug_namespace_get(nspace, id, severity);
601 }
602
603 /**
604 * 'buf' is not necessarily a null-terminated string. When logging, copy
605 * 'len' characters from it, store them in a new, null-terminated string,
606 * and remember the number of bytes used by that string, *including*
607 * the null terminator this time.
608 */
609 static void
610 debug_log_message(struct gl_debug_state *debug,
611 enum mesa_debug_source source,
612 enum mesa_debug_type type, GLuint id,
613 enum mesa_debug_severity severity,
614 GLsizei len, const char *buf)
615 {
616 struct gl_debug_log *log = &debug->Log;
617 GLint nextEmpty;
618 struct gl_debug_message *emptySlot;
619
620 assert(len < MAX_DEBUG_MESSAGE_LENGTH);
621
622 if (log->NumMessages == MAX_DEBUG_LOGGED_MESSAGES)
623 return;
624
625 nextEmpty = (log->NextMessage + log->NumMessages)
626 % MAX_DEBUG_LOGGED_MESSAGES;
627 emptySlot = &log->Messages[nextEmpty];
628
629 debug_message_store(emptySlot, source, type,
630 id, severity, len, buf);
631
632 log->NumMessages++;
633 }
634
635 /**
636 * Return the oldest debug message out of the log.
637 */
638 static const struct gl_debug_message *
639 debug_fetch_message(const struct gl_debug_state *debug)
640 {
641 const struct gl_debug_log *log = &debug->Log;
642
643 return (log->NumMessages) ? &log->Messages[log->NextMessage] : NULL;
644 }
645
646 /**
647 * Delete the oldest debug messages out of the log.
648 */
649 static void
650 debug_delete_messages(struct gl_debug_state *debug, int count)
651 {
652 struct gl_debug_log *log = &debug->Log;
653
654 if (count > log->NumMessages)
655 count = log->NumMessages;
656
657 while (count--) {
658 struct gl_debug_message *msg = &log->Messages[log->NextMessage];
659
660 debug_message_clear(msg);
661
662 log->NumMessages--;
663 log->NextMessage++;
664 log->NextMessage %= MAX_DEBUG_LOGGED_MESSAGES;
665 }
666 }
667
668 static struct gl_debug_message *
669 debug_get_group_message(struct gl_debug_state *debug)
670 {
671 return &debug->GroupMessages[debug->CurrentGroup];
672 }
673
674 static void
675 debug_push_group(struct gl_debug_state *debug)
676 {
677 const GLint gstack = debug->CurrentGroup;
678
679 /* just point to the previous stack */
680 debug->Groups[gstack + 1] = debug->Groups[gstack];
681 debug->CurrentGroup++;
682 }
683
684 static void
685 debug_pop_group(struct gl_debug_state *debug)
686 {
687 debug_clear_group(debug);
688 debug->CurrentGroup--;
689 }
690
691
692 /**
693 * Lock and return debug state for the context. The debug state will be
694 * allocated and initialized upon the first call. When NULL is returned, the
695 * debug state is not locked.
696 */
697 static struct gl_debug_state *
698 _mesa_lock_debug_state(struct gl_context *ctx)
699 {
700 mtx_lock(&ctx->DebugMutex);
701
702 if (!ctx->Debug) {
703 ctx->Debug = debug_create();
704 if (!ctx->Debug) {
705 GET_CURRENT_CONTEXT(cur);
706 mtx_unlock(&ctx->DebugMutex);
707
708 /*
709 * This function may be called from other threads. When that is the
710 * case, we cannot record this OOM error.
711 */
712 if (ctx == cur)
713 _mesa_error(ctx, GL_OUT_OF_MEMORY, "allocating debug state");
714
715 return NULL;
716 }
717 }
718
719 return ctx->Debug;
720 }
721
722 static void
723 _mesa_unlock_debug_state(struct gl_context *ctx)
724 {
725 mtx_unlock(&ctx->DebugMutex);
726 }
727
728 /**
729 * Set the integer debug state specified by \p pname. This can be called from
730 * _mesa_set_enable for example.
731 */
732 bool
733 _mesa_set_debug_state_int(struct gl_context *ctx, GLenum pname, GLint val)
734 {
735 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
736
737 if (!debug)
738 return false;
739
740 switch (pname) {
741 case GL_DEBUG_OUTPUT:
742 debug->DebugOutput = (val != 0);
743 break;
744 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB:
745 debug->SyncOutput = (val != 0);
746 break;
747 default:
748 assert(!"unknown debug output param");
749 break;
750 }
751
752 _mesa_unlock_debug_state(ctx);
753
754 return true;
755 }
756
757 /**
758 * Query the integer debug state specified by \p pname. This can be called
759 * _mesa_GetIntegerv for example.
760 */
761 GLint
762 _mesa_get_debug_state_int(struct gl_context *ctx, GLenum pname)
763 {
764 GLint val;
765
766 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
767 if (!debug)
768 return 0;
769
770 switch (pname) {
771 case GL_DEBUG_OUTPUT:
772 val = debug->DebugOutput;
773 break;
774 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB:
775 val = debug->SyncOutput;
776 break;
777 case GL_DEBUG_LOGGED_MESSAGES:
778 val = debug->Log.NumMessages;
779 break;
780 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH:
781 val = (debug->Log.NumMessages) ?
782 debug->Log.Messages[debug->Log.NextMessage].length : 0;
783 break;
784 case GL_DEBUG_GROUP_STACK_DEPTH:
785 val = debug->CurrentGroup + 1;
786 break;
787 default:
788 assert(!"unknown debug output param");
789 val = 0;
790 break;
791 }
792
793 _mesa_unlock_debug_state(ctx);
794
795 return val;
796 }
797
798 /**
799 * Query the pointer debug state specified by \p pname. This can be called
800 * _mesa_GetPointerv for example.
801 */
802 void *
803 _mesa_get_debug_state_ptr(struct gl_context *ctx, GLenum pname)
804 {
805 void *val;
806 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
807
808 if (!debug)
809 return NULL;
810
811 switch (pname) {
812 case GL_DEBUG_CALLBACK_FUNCTION_ARB:
813 val = (void *) debug->Callback;
814 break;
815 case GL_DEBUG_CALLBACK_USER_PARAM_ARB:
816 val = (void *) debug->CallbackData;
817 break;
818 default:
819 assert(!"unknown debug output param");
820 val = NULL;
821 break;
822 }
823
824 _mesa_unlock_debug_state(ctx);
825
826 return val;
827 }
828
829 /**
830 * Insert a debug message. The mutex is assumed to be locked, and will be
831 * unlocked by this call.
832 */
833 static void
834 log_msg_locked_and_unlock(struct gl_context *ctx,
835 enum mesa_debug_source source,
836 enum mesa_debug_type type, GLuint id,
837 enum mesa_debug_severity severity,
838 GLint len, const char *buf)
839 {
840 struct gl_debug_state *debug = ctx->Debug;
841
842 if (!_mesa_debug_is_message_enabled(debug, source, type, id, severity)) {
843 _mesa_unlock_debug_state(ctx);
844 return;
845 }
846
847 if (ctx->Debug->Callback) {
848 GLenum gl_source = debug_source_enums[source];
849 GLenum gl_type = debug_type_enums[type];
850 GLenum gl_severity = debug_severity_enums[severity];
851 GLDEBUGPROC callback = ctx->Debug->Callback;
852 const void *data = ctx->Debug->CallbackData;
853
854 /*
855 * When ctx->Debug->SyncOutput is GL_FALSE, the client is prepared for
856 * unsynchronous calls. When it is GL_TRUE, we will not spawn threads.
857 * In either case, we can call the callback unlocked.
858 */
859 _mesa_unlock_debug_state(ctx);
860 callback(gl_source, gl_type, id, gl_severity, len, buf, data);
861 }
862 else {
863 debug_log_message(ctx->Debug, source, type, id, severity, len, buf);
864 _mesa_unlock_debug_state(ctx);
865 }
866 }
867
868 /**
869 * Log a client or driver debug message.
870 */
871 void
872 _mesa_log_msg(struct gl_context *ctx, enum mesa_debug_source source,
873 enum mesa_debug_type type, GLuint id,
874 enum mesa_debug_severity severity, GLint len, const char *buf)
875 {
876 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
877
878 if (!debug)
879 return;
880
881 log_msg_locked_and_unlock(ctx, source, type, id, severity, len, buf);
882 }
883
884
885 /**
886 * Verify that source, type, and severity are valid enums.
887 *
888 * The 'caller' param is used for handling values available
889 * only in glDebugMessageInsert or glDebugMessageControl
890 */
891 static GLboolean
892 validate_params(struct gl_context *ctx, unsigned caller,
893 const char *callerstr, GLenum source, GLenum type,
894 GLenum severity)
895 {
896 #define INSERT 1
897 #define CONTROL 2
898 switch(source) {
899 case GL_DEBUG_SOURCE_APPLICATION_ARB:
900 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB:
901 break;
902 case GL_DEBUG_SOURCE_API_ARB:
903 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB:
904 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB:
905 case GL_DEBUG_SOURCE_OTHER_ARB:
906 if (caller != INSERT)
907 break;
908 else
909 goto error;
910 case GL_DONT_CARE:
911 if (caller == CONTROL)
912 break;
913 else
914 goto error;
915 default:
916 goto error;
917 }
918
919 switch(type) {
920 case GL_DEBUG_TYPE_ERROR_ARB:
921 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
922 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
923 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
924 case GL_DEBUG_TYPE_PORTABILITY_ARB:
925 case GL_DEBUG_TYPE_OTHER_ARB:
926 case GL_DEBUG_TYPE_MARKER:
927 case GL_DEBUG_TYPE_PUSH_GROUP:
928 case GL_DEBUG_TYPE_POP_GROUP:
929 break;
930 case GL_DONT_CARE:
931 if (caller == CONTROL)
932 break;
933 else
934 goto error;
935 default:
936 goto error;
937 }
938
939 switch(severity) {
940 case GL_DEBUG_SEVERITY_HIGH_ARB:
941 case GL_DEBUG_SEVERITY_MEDIUM_ARB:
942 case GL_DEBUG_SEVERITY_LOW_ARB:
943 case GL_DEBUG_SEVERITY_NOTIFICATION:
944 break;
945 case GL_DONT_CARE:
946 if (caller == CONTROL)
947 break;
948 else
949 goto error;
950 default:
951 goto error;
952 }
953 return GL_TRUE;
954
955 error:
956 _mesa_error(ctx, GL_INVALID_ENUM, "bad values passed to %s"
957 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr,
958 source, type, severity);
959
960 return GL_FALSE;
961 }
962
963
964 static GLboolean
965 validate_length(struct gl_context *ctx, const char *callerstr, GLsizei length,
966 const GLchar *buf)
967 {
968
969 if (length < 0) {
970 GLsizei len = strlen(buf);
971
972 if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
973 _mesa_error(ctx, GL_INVALID_VALUE,
974 "%s(null terminated string length=%d, is not less than "
975 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr, len,
976 MAX_DEBUG_MESSAGE_LENGTH);
977 return GL_FALSE;
978 }
979 }
980
981 if (length >= MAX_DEBUG_MESSAGE_LENGTH) {
982 _mesa_error(ctx, GL_INVALID_VALUE,
983 "%s(length=%d, which is not less than "
984 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr, length,
985 MAX_DEBUG_MESSAGE_LENGTH);
986 return GL_FALSE;
987 }
988
989 return GL_TRUE;
990 }
991
992
993 void GLAPIENTRY
994 _mesa_DebugMessageInsert(GLenum source, GLenum type, GLuint id,
995 GLenum severity, GLint length,
996 const GLchar *buf)
997 {
998 GET_CURRENT_CONTEXT(ctx);
999 const char *callerstr;
1000
1001 if (_mesa_is_desktop_gl(ctx))
1002 callerstr = "glDebugMessageInsert";
1003 else
1004 callerstr = "glDebugMessageInsertKHR";
1005
1006 if (!validate_params(ctx, INSERT, callerstr, source, type, severity))
1007 return; /* GL_INVALID_ENUM */
1008
1009 if (!validate_length(ctx, callerstr, length, buf))
1010 return; /* GL_INVALID_VALUE */
1011
1012 _mesa_log_msg(ctx, gl_enum_to_debug_source(source),
1013 gl_enum_to_debug_type(type), id,
1014 gl_enum_to_debug_severity(severity),
1015 length, buf);
1016
1017 if (type == GL_DEBUG_TYPE_MARKER && ctx->Driver.EmitStringMarker) {
1018 /* if length not specified, string will be null terminated: */
1019 if (length < 0)
1020 length = strlen(buf);
1021 ctx->Driver.EmitStringMarker(ctx, buf, length);
1022 }
1023 }
1024
1025
1026 GLuint GLAPIENTRY
1027 _mesa_GetDebugMessageLog(GLuint count, GLsizei logSize, GLenum *sources,
1028 GLenum *types, GLenum *ids, GLenum *severities,
1029 GLsizei *lengths, GLchar *messageLog)
1030 {
1031 GET_CURRENT_CONTEXT(ctx);
1032 struct gl_debug_state *debug;
1033 const char *callerstr;
1034 GLuint ret;
1035
1036 if (_mesa_is_desktop_gl(ctx))
1037 callerstr = "glGetDebugMessageLog";
1038 else
1039 callerstr = "glGetDebugMessageLogKHR";
1040
1041 if (!messageLog)
1042 logSize = 0;
1043
1044 if (logSize < 0) {
1045 _mesa_error(ctx, GL_INVALID_VALUE,
1046 "%s(logSize=%d : logSize must not be negative)",
1047 callerstr, logSize);
1048 return 0;
1049 }
1050
1051 debug = _mesa_lock_debug_state(ctx);
1052 if (!debug)
1053 return 0;
1054
1055 for (ret = 0; ret < count; ret++) {
1056 const struct gl_debug_message *msg = debug_fetch_message(debug);
1057 GLsizei len;
1058
1059 if (!msg)
1060 break;
1061
1062 len = msg->length;
1063 if (len < 0)
1064 len = strlen(msg->message);
1065
1066 if (logSize < len+1 && messageLog != NULL)
1067 break;
1068
1069 if (messageLog) {
1070 assert(msg->message[len] == '\0');
1071 (void) strncpy(messageLog, msg->message, (size_t)len+1);
1072
1073 messageLog += len+1;
1074 logSize -= len+1;
1075 }
1076
1077 if (lengths)
1078 *lengths++ = len+1;
1079 if (severities)
1080 *severities++ = debug_severity_enums[msg->severity];
1081 if (sources)
1082 *sources++ = debug_source_enums[msg->source];
1083 if (types)
1084 *types++ = debug_type_enums[msg->type];
1085 if (ids)
1086 *ids++ = msg->id;
1087
1088 debug_delete_messages(debug, 1);
1089 }
1090
1091 _mesa_unlock_debug_state(ctx);
1092
1093 return ret;
1094 }
1095
1096
1097 void GLAPIENTRY
1098 _mesa_DebugMessageControl(GLenum gl_source, GLenum gl_type,
1099 GLenum gl_severity, GLsizei count,
1100 const GLuint *ids, GLboolean enabled)
1101 {
1102 GET_CURRENT_CONTEXT(ctx);
1103 enum mesa_debug_source source = gl_enum_to_debug_source(gl_source);
1104 enum mesa_debug_type type = gl_enum_to_debug_type(gl_type);
1105 enum mesa_debug_severity severity = gl_enum_to_debug_severity(gl_severity);
1106 const char *callerstr;
1107 struct gl_debug_state *debug;
1108
1109 if (_mesa_is_desktop_gl(ctx))
1110 callerstr = "glDebugMessageControl";
1111 else
1112 callerstr = "glDebugMessageControlKHR";
1113
1114 if (count < 0) {
1115 _mesa_error(ctx, GL_INVALID_VALUE,
1116 "%s(count=%d : count must not be negative)", callerstr,
1117 count);
1118 return;
1119 }
1120
1121 if (!validate_params(ctx, CONTROL, callerstr, gl_source, gl_type,
1122 gl_severity))
1123 return; /* GL_INVALID_ENUM */
1124
1125 if (count && (gl_severity != GL_DONT_CARE || gl_type == GL_DONT_CARE
1126 || gl_source == GL_DONT_CARE)) {
1127 _mesa_error(ctx, GL_INVALID_OPERATION,
1128 "%s(When passing an array of ids, severity must be"
1129 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
1130 callerstr);
1131 return;
1132 }
1133
1134 debug = _mesa_lock_debug_state(ctx);
1135 if (!debug)
1136 return;
1137
1138 if (count) {
1139 GLsizei i;
1140 for (i = 0; i < count; i++)
1141 debug_set_message_enable(debug, source, type, ids[i], enabled);
1142 }
1143 else {
1144 debug_set_message_enable_all(debug, source, type, severity, enabled);
1145 }
1146
1147 _mesa_unlock_debug_state(ctx);
1148 }
1149
1150
1151 void GLAPIENTRY
1152 _mesa_DebugMessageCallback(GLDEBUGPROC callback, const void *userParam)
1153 {
1154 GET_CURRENT_CONTEXT(ctx);
1155 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
1156 if (debug) {
1157 debug->Callback = callback;
1158 debug->CallbackData = userParam;
1159 _mesa_unlock_debug_state(ctx);
1160 }
1161 }
1162
1163
1164 void GLAPIENTRY
1165 _mesa_PushDebugGroup(GLenum source, GLuint id, GLsizei length,
1166 const GLchar *message)
1167 {
1168 GET_CURRENT_CONTEXT(ctx);
1169 const char *callerstr;
1170 struct gl_debug_state *debug;
1171 struct gl_debug_message *emptySlot;
1172
1173 if (_mesa_is_desktop_gl(ctx))
1174 callerstr = "glPushDebugGroup";
1175 else
1176 callerstr = "glPushDebugGroupKHR";
1177
1178 switch(source) {
1179 case GL_DEBUG_SOURCE_APPLICATION:
1180 case GL_DEBUG_SOURCE_THIRD_PARTY:
1181 break;
1182 default:
1183 _mesa_error(ctx, GL_INVALID_ENUM, "bad value passed to %s"
1184 "(source=0x%x)", callerstr, source);
1185 return;
1186 }
1187
1188 if (!validate_length(ctx, callerstr, length, message))
1189 return; /* GL_INVALID_VALUE */
1190
1191 debug = _mesa_lock_debug_state(ctx);
1192 if (!debug)
1193 return;
1194
1195 if (debug->CurrentGroup >= MAX_DEBUG_GROUP_STACK_DEPTH-1) {
1196 _mesa_unlock_debug_state(ctx);
1197 _mesa_error(ctx, GL_STACK_OVERFLOW, "%s", callerstr);
1198 return;
1199 }
1200
1201 /* pop reuses the message details from push so we store this */
1202 emptySlot = debug_get_group_message(debug);
1203 debug_message_store(emptySlot,
1204 gl_enum_to_debug_source(source),
1205 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP),
1206 id,
1207 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION),
1208 length, message);
1209
1210 debug_push_group(debug);
1211
1212 log_msg_locked_and_unlock(ctx,
1213 gl_enum_to_debug_source(source),
1214 MESA_DEBUG_TYPE_PUSH_GROUP, id,
1215 MESA_DEBUG_SEVERITY_NOTIFICATION, length,
1216 message);
1217 }
1218
1219
1220 void GLAPIENTRY
1221 _mesa_PopDebugGroup(void)
1222 {
1223 GET_CURRENT_CONTEXT(ctx);
1224 const char *callerstr;
1225 struct gl_debug_state *debug;
1226 struct gl_debug_message *gdmessage, msg;
1227
1228 if (_mesa_is_desktop_gl(ctx))
1229 callerstr = "glPopDebugGroup";
1230 else
1231 callerstr = "glPopDebugGroupKHR";
1232
1233 debug = _mesa_lock_debug_state(ctx);
1234 if (!debug)
1235 return;
1236
1237 if (debug->CurrentGroup <= 0) {
1238 _mesa_unlock_debug_state(ctx);
1239 _mesa_error(ctx, GL_STACK_UNDERFLOW, "%s", callerstr);
1240 return;
1241 }
1242
1243 debug_pop_group(debug);
1244
1245 /* make a shallow copy */
1246 gdmessage = debug_get_group_message(debug);
1247 msg = *gdmessage;
1248 gdmessage->message = NULL;
1249 gdmessage->length = 0;
1250
1251 log_msg_locked_and_unlock(ctx,
1252 msg.source,
1253 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP),
1254 msg.id,
1255 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION),
1256 msg.length, msg.message);
1257
1258 debug_message_clear(&msg);
1259 }
1260
1261
1262 void
1263 _mesa_init_debug_output(struct gl_context *ctx)
1264 {
1265 mtx_init(&ctx->DebugMutex, mtx_plain);
1266 }
1267
1268
1269 void
1270 _mesa_free_errors_data(struct gl_context *ctx)
1271 {
1272 if (ctx->Debug) {
1273 debug_destroy(ctx->Debug);
1274 /* set to NULL just in case it is used before context is completely gone. */
1275 ctx->Debug = NULL;
1276 }
1277
1278 mtx_destroy(&ctx->DebugMutex);
1279 }
1280
1281 void GLAPIENTRY
1282 _mesa_StringMarkerGREMEDY(GLsizei len, const GLvoid *string)
1283 {
1284 GET_CURRENT_CONTEXT(ctx);
1285 if (ctx->Extensions.GREMEDY_string_marker) {
1286 /* if length not specified, string will be null terminated: */
1287 if (len <= 0)
1288 len = strlen(string);
1289 ctx->Driver.EmitStringMarker(ctx, string, len);
1290 } else {
1291 _mesa_error(ctx, GL_INVALID_OPERATION, "StringMarkerGREMEDY");
1292 }
1293 }