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