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