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