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