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