mesa: Fix some signed-unsigned comparison warnings
[mesa.git] / src / mesa / main / errors.c
1 /**
2 * \file errors.c
3 * Mesa debugging and error handling functions.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 *
9 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include "errors.h"
32 #include "enums.h"
33 #include "imports.h"
34 #include "context.h"
35 #include "dispatch.h"
36 #include "hash.h"
37 #include "mtypes.h"
38 #include "version.h"
39 #include "util/hash_table.h"
40
41 static mtx_t DynamicIDMutex = _MTX_INITIALIZER_NP;
42 static GLuint NextDynamicID = 1;
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 struct gl_debug_namespace
57 {
58 struct simple_node Elements;
59 GLbitfield DefaultState;
60 };
61
62 struct gl_debug_group {
63 struct gl_debug_namespace Namespaces[MESA_DEBUG_SOURCE_COUNT][MESA_DEBUG_TYPE_COUNT];
64 };
65
66 /**
67 * An error, warning, or other piece of debug information for an application
68 * to consume via GL_ARB_debug_output/GL_KHR_debug.
69 */
70 struct gl_debug_message
71 {
72 enum mesa_debug_source source;
73 enum mesa_debug_type type;
74 GLuint id;
75 enum mesa_debug_severity severity;
76 GLsizei length;
77 GLcharARB *message;
78 };
79
80 /**
81 * Debug message log. It works like a ring buffer.
82 */
83 struct gl_debug_log {
84 struct gl_debug_message Messages[MAX_DEBUG_LOGGED_MESSAGES];
85 GLint NextMessage;
86 GLint NumMessages;
87 };
88
89 struct gl_debug_state
90 {
91 GLDEBUGPROC Callback;
92 const void *CallbackData;
93 GLboolean SyncOutput;
94 GLboolean DebugOutput;
95
96 struct gl_debug_group *Groups[MAX_DEBUG_GROUP_STACK_DEPTH];
97 struct gl_debug_message GroupMessages[MAX_DEBUG_GROUP_STACK_DEPTH];
98 GLint GroupStackDepth;
99
100 struct gl_debug_log Log;
101 };
102
103 static char out_of_memory[] = "Debugging error: out of memory";
104
105 static const GLenum debug_source_enums[] = {
106 GL_DEBUG_SOURCE_API,
107 GL_DEBUG_SOURCE_WINDOW_SYSTEM,
108 GL_DEBUG_SOURCE_SHADER_COMPILER,
109 GL_DEBUG_SOURCE_THIRD_PARTY,
110 GL_DEBUG_SOURCE_APPLICATION,
111 GL_DEBUG_SOURCE_OTHER,
112 };
113
114 static const GLenum debug_type_enums[] = {
115 GL_DEBUG_TYPE_ERROR,
116 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
117 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
118 GL_DEBUG_TYPE_PORTABILITY,
119 GL_DEBUG_TYPE_PERFORMANCE,
120 GL_DEBUG_TYPE_OTHER,
121 GL_DEBUG_TYPE_MARKER,
122 GL_DEBUG_TYPE_PUSH_GROUP,
123 GL_DEBUG_TYPE_POP_GROUP,
124 };
125
126 static const GLenum debug_severity_enums[] = {
127 GL_DEBUG_SEVERITY_LOW,
128 GL_DEBUG_SEVERITY_MEDIUM,
129 GL_DEBUG_SEVERITY_HIGH,
130 GL_DEBUG_SEVERITY_NOTIFICATION,
131 };
132
133
134 static enum mesa_debug_source
135 gl_enum_to_debug_source(GLenum e)
136 {
137 unsigned i;
138
139 for (i = 0; i < Elements(debug_source_enums); i++) {
140 if (debug_source_enums[i] == e)
141 break;
142 }
143 return i;
144 }
145
146 static enum mesa_debug_type
147 gl_enum_to_debug_type(GLenum e)
148 {
149 unsigned i;
150
151 for (i = 0; i < Elements(debug_type_enums); i++) {
152 if (debug_type_enums[i] == e)
153 break;
154 }
155 return i;
156 }
157
158 static enum mesa_debug_severity
159 gl_enum_to_debug_severity(GLenum e)
160 {
161 unsigned i;
162
163 for (i = 0; i < Elements(debug_severity_enums); i++) {
164 if (debug_severity_enums[i] == e)
165 break;
166 }
167 return i;
168 }
169
170
171 /**
172 * Handles generating a GL_ARB_debug_output message ID generated by the GL or
173 * GLSL compiler.
174 *
175 * The GL API has this "ID" mechanism, where the intention is to allow a
176 * client to filter in/out messages based on source, type, and ID. Of course,
177 * building a giant enum list of all debug output messages that Mesa might
178 * generate is ridiculous, so instead we have our caller pass us a pointer to
179 * static storage where the ID should get stored. This ID will be shared
180 * across all contexts for that message (which seems like a desirable
181 * property, even if it's not expected by the spec), but note that it won't be
182 * the same between executions if messages aren't generated in the same order.
183 */
184 static void
185 debug_get_id(GLuint *id)
186 {
187 if (!(*id)) {
188 mtx_lock(&DynamicIDMutex);
189 if (!(*id))
190 *id = NextDynamicID++;
191 mtx_unlock(&DynamicIDMutex);
192 }
193 }
194
195 static void
196 debug_message_clear(struct gl_debug_message *msg)
197 {
198 if (msg->message != (char*)out_of_memory)
199 free(msg->message);
200 msg->message = NULL;
201 msg->length = 0;
202 }
203
204 static void
205 debug_message_store(struct gl_debug_message *msg,
206 enum mesa_debug_source source,
207 enum mesa_debug_type type, GLuint id,
208 enum mesa_debug_severity severity,
209 GLsizei len, const char *buf)
210 {
211 assert(!msg->message && !msg->length);
212
213 msg->message = malloc(len+1);
214 if (msg->message) {
215 (void) strncpy(msg->message, buf, (size_t)len);
216 msg->message[len] = '\0';
217
218 msg->length = len+1;
219 msg->source = source;
220 msg->type = type;
221 msg->id = id;
222 msg->severity = severity;
223 } else {
224 static GLuint oom_msg_id = 0;
225 debug_get_id(&oom_msg_id);
226
227 /* malloc failed! */
228 msg->message = out_of_memory;
229 msg->length = strlen(out_of_memory)+1;
230 msg->source = MESA_DEBUG_SOURCE_OTHER;
231 msg->type = MESA_DEBUG_TYPE_ERROR;
232 msg->id = oom_msg_id;
233 msg->severity = MESA_DEBUG_SEVERITY_HIGH;
234 }
235 }
236
237 static void
238 debug_namespace_init(struct gl_debug_namespace *ns)
239 {
240 make_empty_list(&ns->Elements);
241
242 /* Enable all the messages with severity HIGH or MEDIUM by default */
243 ns->DefaultState = (1 << MESA_DEBUG_SEVERITY_HIGH) |
244 (1 << MESA_DEBUG_SEVERITY_MEDIUM);
245 }
246
247 static void
248 debug_namespace_clear(struct gl_debug_namespace *ns)
249 {
250 struct simple_node *node, *tmp;
251
252 foreach_s(node, tmp, &ns->Elements)
253 free(node);
254 }
255
256 static bool
257 debug_namespace_copy(struct gl_debug_namespace *dst,
258 const struct gl_debug_namespace *src)
259 {
260 struct simple_node *node;
261
262 dst->DefaultState = src->DefaultState;
263
264 make_empty_list(&dst->Elements);
265 foreach(node, &src->Elements) {
266 const struct gl_debug_element *elem =
267 (const struct gl_debug_element *) node;
268 struct gl_debug_element *copy;
269
270 copy = malloc(sizeof(*copy));
271 if (!copy) {
272 debug_namespace_clear(dst);
273 return false;
274 }
275
276 copy->ID = elem->ID;
277 copy->State = elem->State;
278 insert_at_tail(&dst->Elements, &copy->link);
279 }
280
281 return true;
282 }
283
284 /**
285 * Set the state of \p id in the namespace.
286 */
287 static bool
288 debug_namespace_set(struct gl_debug_namespace *ns,
289 GLuint id, bool enabled)
290 {
291 const uint32_t state = (enabled) ?
292 ((1 << MESA_DEBUG_SEVERITY_COUNT) - 1) : 0;
293 struct gl_debug_element *elem = NULL;
294 struct simple_node *node;
295
296 /* find the element */
297 foreach(node, &ns->Elements) {
298 struct gl_debug_element *tmp = (struct gl_debug_element *) node;
299 if (tmp->ID == id) {
300 elem = tmp;
301 break;
302 }
303 }
304
305 /* we do not need the element if it has the default state */
306 if (ns->DefaultState == state) {
307 if (elem) {
308 remove_from_list(&elem->link);
309 free(elem);
310 }
311 return true;
312 }
313
314 if (!elem) {
315 elem = malloc(sizeof(*elem));
316 if (!elem)
317 return false;
318
319 elem->ID = id;
320 insert_at_tail(&ns->Elements, &elem->link);
321 }
322
323 elem->State = state;
324
325 return true;
326 }
327
328 /**
329 * Set the default state of the namespace for \p severity. When \p severity
330 * is MESA_DEBUG_SEVERITY_COUNT, the default values for all severities are
331 * updated.
332 */
333 static void
334 debug_namespace_set_all(struct gl_debug_namespace *ns,
335 enum mesa_debug_severity severity,
336 bool enabled)
337 {
338 struct simple_node *node, *tmp;
339 uint32_t mask, val;
340
341 /* set all elements to the same state */
342 if (severity == MESA_DEBUG_SEVERITY_COUNT) {
343 ns->DefaultState = (enabled) ? ((1 << severity) - 1) : 0;
344 debug_namespace_clear(ns);
345 make_empty_list(&ns->Elements);
346 return;
347 }
348
349 mask = 1 << severity;
350 val = (enabled) ? mask : 0;
351
352 ns->DefaultState = (ns->DefaultState & ~mask) | val;
353
354 foreach_s(node, tmp, &ns->Elements) {
355 struct gl_debug_element *elem = (struct gl_debug_element *) node;
356
357 elem->State = (elem->State & ~mask) | val;
358 if (elem->State == ns->DefaultState) {
359 remove_from_list(node);
360 free(node);
361 }
362 }
363 }
364
365 /**
366 * Get the state of \p id in the namespace.
367 */
368 static bool
369 debug_namespace_get(const struct gl_debug_namespace *ns, GLuint id,
370 enum mesa_debug_severity severity)
371 {
372 struct simple_node *node;
373 uint32_t state;
374
375 state = ns->DefaultState;
376 foreach(node, &ns->Elements) {
377 struct gl_debug_element *elem = (struct gl_debug_element *) node;
378
379 if (elem->ID == id) {
380 state = elem->State;
381 break;
382 }
383 }
384
385 return (state & (1 << severity));
386 }
387
388 /**
389 * Allocate and initialize context debug state.
390 */
391 static struct gl_debug_state *
392 debug_create(void)
393 {
394 struct gl_debug_state *debug;
395 int s, t;
396
397 debug = CALLOC_STRUCT(gl_debug_state);
398 if (!debug)
399 return NULL;
400
401 debug->Groups[0] = malloc(sizeof(*debug->Groups[0]));
402 if (!debug->Groups[0]) {
403 free(debug);
404 return NULL;
405 }
406
407 /* Initialize state for filtering known debug messages. */
408 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) {
409 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++)
410 debug_namespace_init(&debug->Groups[0]->Namespaces[s][t]);
411 }
412
413 return debug;
414 }
415
416 /**
417 * Return true if the top debug group points to the group below it.
418 */
419 static bool
420 debug_is_group_read_only(const struct gl_debug_state *debug)
421 {
422 const GLint gstack = debug->GroupStackDepth;
423 return (gstack > 0 && debug->Groups[gstack] == debug->Groups[gstack - 1]);
424 }
425
426 /**
427 * Make the top debug group writable.
428 */
429 static bool
430 debug_make_group_writable(struct gl_debug_state *debug)
431 {
432 const GLint gstack = debug->GroupStackDepth;
433 const struct gl_debug_group *src = debug->Groups[gstack];
434 struct gl_debug_group *dst;
435 int s, t;
436
437 if (!debug_is_group_read_only(debug))
438 return true;
439
440 dst = malloc(sizeof(*dst));
441 if (!dst)
442 return false;
443
444 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) {
445 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) {
446 if (!debug_namespace_copy(&dst->Namespaces[s][t],
447 &src->Namespaces[s][t])) {
448 /* error path! */
449 for (t = t - 1; t >= 0; t--)
450 debug_namespace_clear(&dst->Namespaces[s][t]);
451 for (s = s - 1; s >= 0; s--) {
452 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++)
453 debug_namespace_clear(&dst->Namespaces[s][t]);
454 }
455 free(dst);
456 return false;
457 }
458 }
459 }
460
461 debug->Groups[gstack] = dst;
462
463 return true;
464 }
465
466 /**
467 * Free the top debug group.
468 */
469 static void
470 debug_clear_group(struct gl_debug_state *debug)
471 {
472 const GLint gstack = debug->GroupStackDepth;
473
474 if (!debug_is_group_read_only(debug)) {
475 struct gl_debug_group *grp = debug->Groups[gstack];
476 int s, t;
477
478 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) {
479 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++)
480 debug_namespace_clear(&grp->Namespaces[s][t]);
481 }
482
483 free(grp);
484 }
485
486 debug->Groups[gstack] = NULL;
487 }
488
489 /**
490 * Loop through debug group stack tearing down states for
491 * filtering debug messages. Then free debug output state.
492 */
493 static void
494 debug_destroy(struct gl_debug_state *debug)
495 {
496 while (debug->GroupStackDepth > 0) {
497 debug_clear_group(debug);
498 debug->GroupStackDepth--;
499 }
500
501 debug_clear_group(debug);
502 free(debug);
503 }
504
505 /**
506 * Sets the state of the given message source/type/ID tuple.
507 */
508 static void
509 debug_set_message_enable(struct gl_debug_state *debug,
510 enum mesa_debug_source source,
511 enum mesa_debug_type type,
512 GLuint id, GLboolean enabled)
513 {
514 const GLint gstack = debug->GroupStackDepth;
515 struct gl_debug_namespace *ns;
516
517 debug_make_group_writable(debug);
518 ns = &debug->Groups[gstack]->Namespaces[source][type];
519
520 debug_namespace_set(ns, id, enabled);
521 }
522
523 /*
524 * Set the state of all message IDs found in the given intersection of
525 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
526 * GL_DONT_CARE (include all messages in the class).
527 *
528 * This requires both setting the state of all previously seen message
529 * IDs in the hash table, and setting the default state for all
530 * applicable combinations of source/type/severity, so that all the
531 * yet-unknown message IDs that may be used in the future will be
532 * impacted as if they were already known.
533 */
534 static void
535 debug_set_message_enable_all(struct gl_debug_state *debug,
536 enum mesa_debug_source source,
537 enum mesa_debug_type type,
538 enum mesa_debug_severity severity,
539 GLboolean enabled)
540 {
541 const GLint gstack = debug->GroupStackDepth;
542 int s, t, smax, tmax;
543
544 if (source == MESA_DEBUG_SOURCE_COUNT) {
545 source = 0;
546 smax = MESA_DEBUG_SOURCE_COUNT;
547 } else {
548 smax = source+1;
549 }
550
551 if (type == MESA_DEBUG_TYPE_COUNT) {
552 type = 0;
553 tmax = MESA_DEBUG_TYPE_COUNT;
554 } else {
555 tmax = type+1;
556 }
557
558 debug_make_group_writable(debug);
559
560 for (s = source; s < smax; s++) {
561 for (t = type; t < tmax; t++) {
562 struct gl_debug_namespace *nspace =
563 &debug->Groups[gstack]->Namespaces[s][t];
564 debug_namespace_set_all(nspace, severity, enabled);
565 }
566 }
567 }
568
569 /**
570 * Returns if the given message source/type/ID tuple is enabled.
571 */
572 static bool
573 debug_is_message_enabled(const struct gl_debug_state *debug,
574 enum mesa_debug_source source,
575 enum mesa_debug_type type,
576 GLuint id,
577 enum mesa_debug_severity severity)
578 {
579 const GLint gstack = debug->GroupStackDepth;
580 struct gl_debug_group *grp = debug->Groups[gstack];
581 struct gl_debug_namespace *nspace = &grp->Namespaces[source][type];
582
583 if (!debug->DebugOutput)
584 return false;
585
586 return debug_namespace_get(nspace, id, severity);
587 }
588
589 /**
590 * 'buf' is not necessarily a null-terminated string. When logging, copy
591 * 'len' characters from it, store them in a new, null-terminated string,
592 * and remember the number of bytes used by that string, *including*
593 * the null terminator this time.
594 */
595 static void
596 debug_log_message(struct gl_debug_state *debug,
597 enum mesa_debug_source source,
598 enum mesa_debug_type type, GLuint id,
599 enum mesa_debug_severity severity,
600 GLsizei len, const char *buf)
601 {
602 struct gl_debug_log *log = &debug->Log;
603 GLint nextEmpty;
604 struct gl_debug_message *emptySlot;
605
606 assert(len >= 0 && len < MAX_DEBUG_MESSAGE_LENGTH);
607
608 if (log->NumMessages == MAX_DEBUG_LOGGED_MESSAGES)
609 return;
610
611 nextEmpty = (log->NextMessage + log->NumMessages)
612 % MAX_DEBUG_LOGGED_MESSAGES;
613 emptySlot = &log->Messages[nextEmpty];
614
615 debug_message_store(emptySlot, source, type,
616 id, severity, len, buf);
617
618 log->NumMessages++;
619 }
620
621 /**
622 * Return the oldest debug message out of the log.
623 */
624 static const struct gl_debug_message *
625 debug_fetch_message(const struct gl_debug_state *debug)
626 {
627 const struct gl_debug_log *log = &debug->Log;
628
629 return (log->NumMessages) ? &log->Messages[log->NextMessage] : NULL;
630 }
631
632 /**
633 * Delete the oldest debug messages out of the log.
634 */
635 static void
636 debug_delete_messages(struct gl_debug_state *debug, int count)
637 {
638 struct gl_debug_log *log = &debug->Log;
639
640 if (count > log->NumMessages)
641 count = log->NumMessages;
642
643 while (count--) {
644 struct gl_debug_message *msg = &log->Messages[log->NextMessage];
645
646 debug_message_clear(msg);
647
648 log->NumMessages--;
649 log->NextMessage++;
650 log->NextMessage %= MAX_DEBUG_LOGGED_MESSAGES;
651 }
652 }
653
654 static struct gl_debug_message *
655 debug_get_group_message(struct gl_debug_state *debug)
656 {
657 return &debug->GroupMessages[debug->GroupStackDepth];
658 }
659
660 static void
661 debug_push_group(struct gl_debug_state *debug)
662 {
663 const GLint gstack = debug->GroupStackDepth;
664
665 /* just point to the previous stack */
666 debug->Groups[gstack + 1] = debug->Groups[gstack];
667 debug->GroupStackDepth++;
668 }
669
670 static void
671 debug_pop_group(struct gl_debug_state *debug)
672 {
673 debug_clear_group(debug);
674 debug->GroupStackDepth--;
675 }
676
677
678 /**
679 * Lock and return debug state for the context. The debug state will be
680 * allocated and initialized upon the first call. When NULL is returned, the
681 * debug state is not locked.
682 */
683 static struct gl_debug_state *
684 _mesa_lock_debug_state(struct gl_context *ctx)
685 {
686 mtx_lock(&ctx->DebugMutex);
687
688 if (!ctx->Debug) {
689 ctx->Debug = debug_create();
690 if (!ctx->Debug) {
691 GET_CURRENT_CONTEXT(cur);
692 mtx_unlock(&ctx->DebugMutex);
693
694 /*
695 * This function may be called from other threads. When that is the
696 * case, we cannot record this OOM error.
697 */
698 if (ctx == cur)
699 _mesa_error(ctx, GL_OUT_OF_MEMORY, "allocating debug state");
700
701 return NULL;
702 }
703 }
704
705 return ctx->Debug;
706 }
707
708 static void
709 _mesa_unlock_debug_state(struct gl_context *ctx)
710 {
711 mtx_unlock(&ctx->DebugMutex);
712 }
713
714 /**
715 * Set the integer debug state specified by \p pname. This can be called from
716 * _mesa_set_enable for example.
717 */
718 bool
719 _mesa_set_debug_state_int(struct gl_context *ctx, GLenum pname, GLint val)
720 {
721 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
722
723 if (!debug)
724 return false;
725
726 switch (pname) {
727 case GL_DEBUG_OUTPUT:
728 debug->DebugOutput = (val != 0);
729 break;
730 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB:
731 debug->SyncOutput = (val != 0);
732 break;
733 default:
734 assert(!"unknown debug output param");
735 break;
736 }
737
738 _mesa_unlock_debug_state(ctx);
739
740 return true;
741 }
742
743 /**
744 * Query the integer debug state specified by \p pname. This can be called
745 * _mesa_GetIntegerv for example.
746 */
747 GLint
748 _mesa_get_debug_state_int(struct gl_context *ctx, GLenum pname)
749 {
750 struct gl_debug_state *debug;
751 GLint val;
752
753 mtx_lock(&ctx->DebugMutex);
754 debug = ctx->Debug;
755 if (!debug) {
756 mtx_unlock(&ctx->DebugMutex);
757 return 0;
758 }
759
760 switch (pname) {
761 case GL_DEBUG_OUTPUT:
762 val = debug->DebugOutput;
763 break;
764 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB:
765 val = debug->SyncOutput;
766 break;
767 case GL_DEBUG_LOGGED_MESSAGES:
768 val = debug->Log.NumMessages;
769 break;
770 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH:
771 val = (debug->Log.NumMessages) ?
772 debug->Log.Messages[debug->Log.NextMessage].length : 0;
773 break;
774 case GL_DEBUG_GROUP_STACK_DEPTH:
775 val = debug->GroupStackDepth;
776 break;
777 default:
778 assert(!"unknown debug output param");
779 val = 0;
780 break;
781 }
782
783 mtx_unlock(&ctx->DebugMutex);
784
785 return val;
786 }
787
788 /**
789 * Query the pointer debug state specified by \p pname. This can be called
790 * _mesa_GetPointerv for example.
791 */
792 void *
793 _mesa_get_debug_state_ptr(struct gl_context *ctx, GLenum pname)
794 {
795 struct gl_debug_state *debug;
796 void *val;
797
798 mtx_lock(&ctx->DebugMutex);
799 debug = ctx->Debug;
800 if (!debug) {
801 mtx_unlock(&ctx->DebugMutex);
802 return NULL;
803 }
804
805 switch (pname) {
806 case GL_DEBUG_CALLBACK_FUNCTION_ARB:
807 val = (void *) debug->Callback;
808 break;
809 case GL_DEBUG_CALLBACK_USER_PARAM_ARB:
810 val = (void *) debug->CallbackData;
811 break;
812 default:
813 assert(!"unknown debug output param");
814 val = NULL;
815 break;
816 }
817
818 mtx_unlock(&ctx->DebugMutex);
819
820 return val;
821 }
822
823 /**
824 * Insert a debug message. The mutex is assumed to be locked, and will be
825 * unlocked by this call.
826 */
827 static void
828 log_msg_locked_and_unlock(struct gl_context *ctx,
829 enum mesa_debug_source source,
830 enum mesa_debug_type type, GLuint id,
831 enum mesa_debug_severity severity,
832 GLint len, const char *buf)
833 {
834 struct gl_debug_state *debug = ctx->Debug;
835
836 if (!debug_is_message_enabled(debug, source, type, id, severity)) {
837 _mesa_unlock_debug_state(ctx);
838 return;
839 }
840
841 if (ctx->Debug->Callback) {
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 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 static void
866 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 break;
922 case GL_DEBUG_TYPE_PUSH_GROUP:
923 case GL_DEBUG_TYPE_POP_GROUP:
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 {
961 if (length >= MAX_DEBUG_MESSAGE_LENGTH) {
962 _mesa_error(ctx, GL_INVALID_VALUE,
963 "%s(length=%d, which is not less than "
964 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr, length,
965 MAX_DEBUG_MESSAGE_LENGTH);
966 return GL_FALSE;
967 }
968
969 return GL_TRUE;
970 }
971
972
973 void GLAPIENTRY
974 _mesa_DebugMessageInsert(GLenum source, GLenum type, GLuint id,
975 GLenum severity, GLint length,
976 const GLchar *buf)
977 {
978 const char *callerstr = "glDebugMessageInsert";
979
980 GET_CURRENT_CONTEXT(ctx);
981
982 if (!validate_params(ctx, INSERT, callerstr, source, type, severity))
983 return; /* GL_INVALID_ENUM */
984
985 if (length < 0)
986 length = strlen(buf);
987 if (!validate_length(ctx, callerstr, length))
988 return; /* GL_INVALID_VALUE */
989
990 log_msg(ctx, gl_enum_to_debug_source(source),
991 gl_enum_to_debug_type(type), id,
992 gl_enum_to_debug_severity(severity),
993 length, buf);
994 }
995
996
997 GLuint GLAPIENTRY
998 _mesa_GetDebugMessageLog(GLuint count, GLsizei logSize, GLenum *sources,
999 GLenum *types, GLenum *ids, GLenum *severities,
1000 GLsizei *lengths, GLchar *messageLog)
1001 {
1002 GET_CURRENT_CONTEXT(ctx);
1003 struct gl_debug_state *debug;
1004 GLuint ret;
1005
1006 if (!messageLog)
1007 logSize = 0;
1008
1009 if (logSize < 0) {
1010 _mesa_error(ctx, GL_INVALID_VALUE,
1011 "glGetDebugMessageLog(logSize=%d : logSize must not be"
1012 " negative)", logSize);
1013 return 0;
1014 }
1015
1016 debug = _mesa_lock_debug_state(ctx);
1017 if (!debug)
1018 return 0;
1019
1020 for (ret = 0; ret < count; ret++) {
1021 const struct gl_debug_message *msg = debug_fetch_message(debug);
1022
1023 if (!msg)
1024 break;
1025
1026 if (logSize < msg->length && messageLog != NULL)
1027 break;
1028
1029 if (messageLog) {
1030 assert(msg->message[msg->length-1] == '\0');
1031 (void) strncpy(messageLog, msg->message, (size_t)msg->length);
1032
1033 messageLog += msg->length;
1034 logSize -= msg->length;
1035 }
1036
1037 if (lengths)
1038 *lengths++ = msg->length;
1039 if (severities)
1040 *severities++ = debug_severity_enums[msg->severity];
1041 if (sources)
1042 *sources++ = debug_source_enums[msg->source];
1043 if (types)
1044 *types++ = debug_type_enums[msg->type];
1045 if (ids)
1046 *ids++ = msg->id;
1047
1048 debug_delete_messages(debug, 1);
1049 }
1050
1051 _mesa_unlock_debug_state(ctx);
1052
1053 return ret;
1054 }
1055
1056
1057 void GLAPIENTRY
1058 _mesa_DebugMessageControl(GLenum gl_source, GLenum gl_type,
1059 GLenum gl_severity, GLsizei count,
1060 const GLuint *ids, GLboolean enabled)
1061 {
1062 GET_CURRENT_CONTEXT(ctx);
1063 enum mesa_debug_source source = gl_enum_to_debug_source(gl_source);
1064 enum mesa_debug_type type = gl_enum_to_debug_type(gl_type);
1065 enum mesa_debug_severity severity = gl_enum_to_debug_severity(gl_severity);
1066 const char *callerstr = "glDebugMessageControl";
1067 struct gl_debug_state *debug;
1068
1069 if (count < 0) {
1070 _mesa_error(ctx, GL_INVALID_VALUE,
1071 "%s(count=%d : count must not be negative)", callerstr,
1072 count);
1073 return;
1074 }
1075
1076 if (!validate_params(ctx, CONTROL, callerstr, gl_source, gl_type,
1077 gl_severity))
1078 return; /* GL_INVALID_ENUM */
1079
1080 if (count && (gl_severity != GL_DONT_CARE || gl_type == GL_DONT_CARE
1081 || gl_source == GL_DONT_CARE)) {
1082 _mesa_error(ctx, GL_INVALID_OPERATION,
1083 "%s(When passing an array of ids, severity must be"
1084 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.",
1085 callerstr);
1086 return;
1087 }
1088
1089 debug = _mesa_lock_debug_state(ctx);
1090 if (!debug)
1091 return;
1092
1093 if (count) {
1094 GLsizei i;
1095 for (i = 0; i < count; i++)
1096 debug_set_message_enable(debug, source, type, ids[i], enabled);
1097 }
1098 else {
1099 debug_set_message_enable_all(debug, source, type, severity, enabled);
1100 }
1101
1102 _mesa_unlock_debug_state(ctx);
1103 }
1104
1105
1106 void GLAPIENTRY
1107 _mesa_DebugMessageCallback(GLDEBUGPROC callback, const void *userParam)
1108 {
1109 GET_CURRENT_CONTEXT(ctx);
1110 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx);
1111 if (debug) {
1112 debug->Callback = callback;
1113 debug->CallbackData = userParam;
1114 _mesa_unlock_debug_state(ctx);
1115 }
1116 }
1117
1118
1119 void GLAPIENTRY
1120 _mesa_PushDebugGroup(GLenum source, GLuint id, GLsizei length,
1121 const GLchar *message)
1122 {
1123 GET_CURRENT_CONTEXT(ctx);
1124 const char *callerstr = "glPushDebugGroup";
1125 struct gl_debug_state *debug;
1126 struct gl_debug_message *emptySlot;
1127
1128 switch(source) {
1129 case GL_DEBUG_SOURCE_APPLICATION:
1130 case GL_DEBUG_SOURCE_THIRD_PARTY:
1131 break;
1132 default:
1133 _mesa_error(ctx, GL_INVALID_ENUM, "bad value passed to %s"
1134 "(source=0x%x)", callerstr, source);
1135 return;
1136 }
1137
1138 if (length < 0)
1139 length = strlen(message);
1140 if (!validate_length(ctx, callerstr, length))
1141 return; /* GL_INVALID_VALUE */
1142
1143 debug = _mesa_lock_debug_state(ctx);
1144 if (!debug)
1145 return;
1146
1147 if (debug->GroupStackDepth >= MAX_DEBUG_GROUP_STACK_DEPTH-1) {
1148 _mesa_unlock_debug_state(ctx);
1149 _mesa_error(ctx, GL_STACK_OVERFLOW, "%s", callerstr);
1150 return;
1151 }
1152
1153 /* pop reuses the message details from push so we store this */
1154 emptySlot = debug_get_group_message(debug);
1155 debug_message_store(emptySlot,
1156 gl_enum_to_debug_source(source),
1157 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP),
1158 id,
1159 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION),
1160 length, message);
1161
1162 debug_push_group(debug);
1163
1164 log_msg_locked_and_unlock(ctx,
1165 gl_enum_to_debug_source(source),
1166 MESA_DEBUG_TYPE_PUSH_GROUP, id,
1167 MESA_DEBUG_SEVERITY_NOTIFICATION, length,
1168 message);
1169 }
1170
1171
1172 void GLAPIENTRY
1173 _mesa_PopDebugGroup(void)
1174 {
1175 GET_CURRENT_CONTEXT(ctx);
1176 const char *callerstr = "glPopDebugGroup";
1177 struct gl_debug_state *debug;
1178 struct gl_debug_message *gdmessage, msg;
1179
1180 debug = _mesa_lock_debug_state(ctx);
1181 if (!debug)
1182 return;
1183
1184 if (debug->GroupStackDepth <= 0) {
1185 _mesa_unlock_debug_state(ctx);
1186 _mesa_error(ctx, GL_STACK_UNDERFLOW, "%s", callerstr);
1187 return;
1188 }
1189
1190 debug_pop_group(debug);
1191
1192 /* make a shallow copy */
1193 gdmessage = debug_get_group_message(debug);
1194 msg = *gdmessage;
1195 gdmessage->message = NULL;
1196 gdmessage->length = 0;
1197
1198 log_msg_locked_and_unlock(ctx,
1199 msg.source,
1200 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP),
1201 msg.id,
1202 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION),
1203 msg.length, msg.message);
1204
1205 debug_message_clear(&msg);
1206 }
1207
1208
1209 void
1210 _mesa_init_errors(struct gl_context *ctx)
1211 {
1212 mtx_init(&ctx->DebugMutex, mtx_plain);
1213 }
1214
1215
1216 void
1217 _mesa_free_errors_data(struct gl_context *ctx)
1218 {
1219 if (ctx->Debug) {
1220 debug_destroy(ctx->Debug);
1221 /* set to NULL just in case it is used before context is completely gone. */
1222 ctx->Debug = NULL;
1223 }
1224
1225 mtx_destroy(&ctx->DebugMutex);
1226 }
1227
1228
1229 /**********************************************************************/
1230 /** \name Diagnostics */
1231 /*@{*/
1232
1233 static void
1234 output_if_debug(const char *prefixString, const char *outputString,
1235 GLboolean newline)
1236 {
1237 static int debug = -1;
1238 static FILE *fout = NULL;
1239
1240 /* Init the local 'debug' var once.
1241 * Note: the _mesa_init_debug() function should have been called
1242 * by now so MESA_DEBUG_FLAGS will be initialized.
1243 */
1244 if (debug == -1) {
1245 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
1246 * etc to the named file. Otherwise, output to stderr.
1247 */
1248 const char *logFile = getenv("MESA_LOG_FILE");
1249 if (logFile)
1250 fout = fopen(logFile, "w");
1251 if (!fout)
1252 fout = stderr;
1253 #ifdef DEBUG
1254 /* in debug builds, print messages unless MESA_DEBUG="silent" */
1255 if (MESA_DEBUG_FLAGS & DEBUG_SILENT)
1256 debug = 0;
1257 else
1258 debug = 1;
1259 #else
1260 /* in release builds, be silent unless MESA_DEBUG is set */
1261 debug = getenv("MESA_DEBUG") != NULL;
1262 #endif
1263 }
1264
1265 /* Now only print the string if we're required to do so. */
1266 if (debug) {
1267 fprintf(fout, "%s: %s", prefixString, outputString);
1268 if (newline)
1269 fprintf(fout, "\n");
1270 fflush(fout);
1271
1272 #if defined(_WIN32)
1273 /* stderr from windows applications without console is not usually
1274 * visible, so communicate with the debugger instead */
1275 {
1276 char buf[4096];
1277 _mesa_snprintf(buf, sizeof(buf), "%s: %s%s", prefixString, outputString, newline ? "\n" : "");
1278 OutputDebugStringA(buf);
1279 }
1280 #endif
1281 }
1282 }
1283
1284
1285 /**
1286 * When a new type of error is recorded, print a message describing
1287 * previous errors which were accumulated.
1288 */
1289 static void
1290 flush_delayed_errors( struct gl_context *ctx )
1291 {
1292 char s[MAX_DEBUG_MESSAGE_LENGTH];
1293
1294 if (ctx->ErrorDebugCount) {
1295 _mesa_snprintf(s, MAX_DEBUG_MESSAGE_LENGTH, "%d similar %s errors",
1296 ctx->ErrorDebugCount,
1297 _mesa_lookup_enum_by_nr(ctx->ErrorValue));
1298
1299 output_if_debug("Mesa", s, GL_TRUE);
1300
1301 ctx->ErrorDebugCount = 0;
1302 }
1303 }
1304
1305
1306 /**
1307 * Report a warning (a recoverable error condition) to stderr if
1308 * either DEBUG is defined or the MESA_DEBUG env var is set.
1309 *
1310 * \param ctx GL context.
1311 * \param fmtString printf()-like format string.
1312 */
1313 void
1314 _mesa_warning( struct gl_context *ctx, const char *fmtString, ... )
1315 {
1316 char str[MAX_DEBUG_MESSAGE_LENGTH];
1317 va_list args;
1318 va_start( args, fmtString );
1319 (void) _mesa_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
1320 va_end( args );
1321
1322 if (ctx)
1323 flush_delayed_errors( ctx );
1324
1325 output_if_debug("Mesa warning", str, GL_TRUE);
1326 }
1327
1328
1329 /**
1330 * Report an internal implementation problem.
1331 * Prints the message to stderr via fprintf().
1332 *
1333 * \param ctx GL context.
1334 * \param fmtString problem description string.
1335 */
1336 void
1337 _mesa_problem( const struct gl_context *ctx, const char *fmtString, ... )
1338 {
1339 va_list args;
1340 char str[MAX_DEBUG_MESSAGE_LENGTH];
1341 static int numCalls = 0;
1342
1343 (void) ctx;
1344
1345 if (numCalls < 50) {
1346 numCalls++;
1347
1348 va_start( args, fmtString );
1349 _mesa_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
1350 va_end( args );
1351 fprintf(stderr, "Mesa %s implementation error: %s\n",
1352 PACKAGE_VERSION, str);
1353 fprintf(stderr, "Please report at " PACKAGE_BUGREPORT "\n");
1354 }
1355 }
1356
1357
1358 static GLboolean
1359 should_output(struct gl_context *ctx, GLenum error, const char *fmtString)
1360 {
1361 static GLint debug = -1;
1362
1363 /* Check debug environment variable only once:
1364 */
1365 if (debug == -1) {
1366 const char *debugEnv = getenv("MESA_DEBUG");
1367
1368 #ifdef DEBUG
1369 if (debugEnv && strstr(debugEnv, "silent"))
1370 debug = GL_FALSE;
1371 else
1372 debug = GL_TRUE;
1373 #else
1374 if (debugEnv)
1375 debug = GL_TRUE;
1376 else
1377 debug = GL_FALSE;
1378 #endif
1379 }
1380
1381 if (debug) {
1382 if (ctx->ErrorValue != error ||
1383 ctx->ErrorDebugFmtString != fmtString) {
1384 flush_delayed_errors( ctx );
1385 ctx->ErrorDebugFmtString = fmtString;
1386 ctx->ErrorDebugCount = 0;
1387 return GL_TRUE;
1388 }
1389 ctx->ErrorDebugCount++;
1390 }
1391 return GL_FALSE;
1392 }
1393
1394
1395 void
1396 _mesa_gl_debug(struct gl_context *ctx,
1397 GLuint *id,
1398 enum mesa_debug_source source,
1399 enum mesa_debug_type type,
1400 enum mesa_debug_severity severity,
1401 const char *fmtString, ...)
1402 {
1403 char s[MAX_DEBUG_MESSAGE_LENGTH];
1404 int len;
1405 va_list args;
1406
1407 debug_get_id(id);
1408
1409 va_start(args, fmtString);
1410 len = _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
1411 va_end(args);
1412
1413 log_msg(ctx, source, type, *id, severity, len, s);
1414 }
1415
1416
1417 /**
1418 * Record an OpenGL state error. These usually occur when the user
1419 * passes invalid parameters to a GL function.
1420 *
1421 * If debugging is enabled (either at compile-time via the DEBUG macro, or
1422 * run-time via the MESA_DEBUG environment variable), report the error with
1423 * _mesa_debug().
1424 *
1425 * \param ctx the GL context.
1426 * \param error the error value.
1427 * \param fmtString printf() style format string, followed by optional args
1428 */
1429 void
1430 _mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... )
1431 {
1432 GLboolean do_output, do_log;
1433 /* Ideally this would be set up by the caller, so that we had proper IDs
1434 * per different message.
1435 */
1436 static GLuint error_msg_id = 0;
1437
1438 debug_get_id(&error_msg_id);
1439
1440 do_output = should_output(ctx, error, fmtString);
1441
1442 mtx_lock(&ctx->DebugMutex);
1443 if (ctx->Debug) {
1444 do_log = debug_is_message_enabled(ctx->Debug,
1445 MESA_DEBUG_SOURCE_API,
1446 MESA_DEBUG_TYPE_ERROR,
1447 error_msg_id,
1448 MESA_DEBUG_SEVERITY_HIGH);
1449 }
1450 else {
1451 do_log = GL_FALSE;
1452 }
1453 mtx_unlock(&ctx->DebugMutex);
1454
1455 if (do_output || do_log) {
1456 char s[MAX_DEBUG_MESSAGE_LENGTH], s2[MAX_DEBUG_MESSAGE_LENGTH];
1457 int len;
1458 va_list args;
1459
1460 va_start(args, fmtString);
1461 len = _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
1462 va_end(args);
1463
1464 if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
1465 /* Too long error message. Whoever calls _mesa_error should use
1466 * shorter strings.
1467 */
1468 ASSERT(0);
1469 return;
1470 }
1471
1472 len = _mesa_snprintf(s2, MAX_DEBUG_MESSAGE_LENGTH, "%s in %s",
1473 _mesa_lookup_enum_by_nr(error), s);
1474 if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
1475 /* Same as above. */
1476 ASSERT(0);
1477 return;
1478 }
1479
1480 /* Print the error to stderr if needed. */
1481 if (do_output) {
1482 output_if_debug("Mesa: User error", s2, GL_TRUE);
1483 }
1484
1485 /* Log the error via ARB_debug_output if needed.*/
1486 if (do_log) {
1487 log_msg(ctx, MESA_DEBUG_SOURCE_API, MESA_DEBUG_TYPE_ERROR,
1488 error_msg_id, MESA_DEBUG_SEVERITY_HIGH, len, s2);
1489 }
1490 }
1491
1492 /* Set the GL context error state for glGetError. */
1493 _mesa_record_error(ctx, error);
1494 }
1495
1496 void
1497 _mesa_error_no_memory(const char *caller)
1498 {
1499 GET_CURRENT_CONTEXT(ctx);
1500 _mesa_error(ctx, GL_OUT_OF_MEMORY, "out of memory in %s", caller);
1501 }
1502
1503 /**
1504 * Report debug information. Print error message to stderr via fprintf().
1505 * No-op if DEBUG mode not enabled.
1506 *
1507 * \param ctx GL context.
1508 * \param fmtString printf()-style format string, followed by optional args.
1509 */
1510 void
1511 _mesa_debug( const struct gl_context *ctx, const char *fmtString, ... )
1512 {
1513 #ifdef DEBUG
1514 char s[MAX_DEBUG_MESSAGE_LENGTH];
1515 va_list args;
1516 va_start(args, fmtString);
1517 _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
1518 va_end(args);
1519 output_if_debug("Mesa", s, GL_FALSE);
1520 #endif /* DEBUG */
1521 (void) ctx;
1522 (void) fmtString;
1523 }
1524
1525
1526 /**
1527 * Report debug information from the shader compiler via GL_ARB_debug_output.
1528 *
1529 * \param ctx GL context.
1530 * \param type The namespace to which this message belongs.
1531 * \param id The message ID within the given namespace.
1532 * \param msg The message to output. Need not be null-terminated.
1533 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1534 */
1535 void
1536 _mesa_shader_debug( struct gl_context *ctx, GLenum type, GLuint *id,
1537 const char *msg, int len )
1538 {
1539 enum mesa_debug_source source = MESA_DEBUG_SOURCE_SHADER_COMPILER;
1540 enum mesa_debug_severity severity = MESA_DEBUG_SEVERITY_HIGH;
1541
1542 debug_get_id(id);
1543
1544 if (len < 0)
1545 len = strlen(msg);
1546
1547 /* Truncate the message if necessary. */
1548 if (len >= MAX_DEBUG_MESSAGE_LENGTH)
1549 len = MAX_DEBUG_MESSAGE_LENGTH - 1;
1550
1551 log_msg(ctx, source, type, *id, severity, len, msg);
1552 }
1553
1554 /*@}*/