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