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