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