mesa: Add some constants and state variables for KHR_debug functions
[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 struct gl_debug_namespace *nspace =
193 &ctx->Debug.Namespaces[source][type];
194 uintptr_t state;
195
196 /* In addition to not being able to store zero as a value, HashTable also
197 can't use zero as a key. */
198 if (id)
199 state = (uintptr_t)_mesa_HashLookup(nspace->IDs, id);
200 else
201 state = nspace->ZeroID;
202
203 /* Only do this once for each ID. This makes sure the ID exists in,
204 at most, one list, and does not pointlessly appear multiple times. */
205 if (!(state & KNOWN_SEVERITY)) {
206 struct gl_debug_severity *entry;
207
208 if (state == NOT_FOUND) {
209 if (ctx->Debug.Defaults[severity][source][type])
210 state = ENABLED;
211 else
212 state = DISABLED;
213 }
214
215 entry = malloc(sizeof *entry);
216 if (!entry)
217 goto out;
218
219 state |= KNOWN_SEVERITY;
220
221 if (id)
222 _mesa_HashInsert(nspace->IDs, id, (void*)state);
223 else
224 nspace->ZeroID = state;
225
226 entry->ID = id;
227 insert_at_tail(&nspace->Severity[severity], &entry->link);
228 }
229
230 out:
231 return !!(state & ENABLED_BIT);
232 }
233
234 /**
235 * Sets the state of the given message source/type/ID tuple.
236 */
237 static void
238 set_message_state(struct gl_context *ctx,
239 enum mesa_debug_source source,
240 enum mesa_debug_type type,
241 GLuint id, GLboolean enabled)
242 {
243 struct gl_debug_namespace *nspace =
244 &ctx->Debug.Namespaces[source][type];
245 uintptr_t state;
246
247 /* In addition to not being able to store zero as a value, HashTable also
248 can't use zero as a key. */
249 if (id)
250 state = (uintptr_t)_mesa_HashLookup(nspace->IDs, id);
251 else
252 state = nspace->ZeroID;
253
254 if (state == NOT_FOUND)
255 state = enabled ? ENABLED : DISABLED;
256 else {
257 if (enabled)
258 state |= ENABLED_BIT;
259 else
260 state &= ~ENABLED_BIT;
261 }
262
263 if (id)
264 _mesa_HashInsert(nspace->IDs, id, (void*)state);
265 else
266 nspace->ZeroID = state;
267 }
268
269 /**
270 * 'buf' is not necessarily a null-terminated string. When logging, copy
271 * 'len' characters from it, store them in a new, null-terminated string,
272 * and remember the number of bytes used by that string, *including*
273 * the null terminator this time.
274 */
275 static void
276 _mesa_log_msg(struct gl_context *ctx, enum mesa_debug_source source,
277 enum mesa_debug_type type, GLuint id,
278 enum mesa_debug_severity severity, GLint len, const char *buf)
279 {
280 GLint nextEmpty;
281 struct gl_debug_msg *emptySlot;
282
283 assert(len >= 0 && len < MAX_DEBUG_MESSAGE_LENGTH);
284
285 if (!should_log(ctx, source, type, id, severity))
286 return;
287
288 if (ctx->Debug.Callback) {
289 ctx->Debug.Callback(debug_source_enums[source],
290 debug_type_enums[type],
291 id,
292 debug_severity_enums[severity],
293 len, buf, ctx->Debug.CallbackData);
294 return;
295 }
296
297 if (ctx->Debug.NumMessages == MAX_DEBUG_LOGGED_MESSAGES)
298 return;
299
300 nextEmpty = (ctx->Debug.NextMsg + ctx->Debug.NumMessages)
301 % MAX_DEBUG_LOGGED_MESSAGES;
302 emptySlot = &ctx->Debug.Log[nextEmpty];
303
304 assert(!emptySlot->message && !emptySlot->length);
305
306 emptySlot->message = malloc(len+1);
307 if (emptySlot->message) {
308 (void) strncpy(emptySlot->message, buf, (size_t)len);
309 emptySlot->message[len] = '\0';
310
311 emptySlot->length = len+1;
312 emptySlot->source = source;
313 emptySlot->type = type;
314 emptySlot->id = id;
315 emptySlot->severity = severity;
316 } else {
317 static GLuint oom_msg_id = 0;
318 debug_get_id(&oom_msg_id);
319
320 /* malloc failed! */
321 emptySlot->message = out_of_memory;
322 emptySlot->length = strlen(out_of_memory)+1;
323 emptySlot->source = MESA_DEBUG_SOURCE_OTHER;
324 emptySlot->type = MESA_DEBUG_TYPE_ERROR;
325 emptySlot->id = oom_msg_id;
326 emptySlot->severity = MESA_DEBUG_SEVERITY_HIGH;
327 }
328
329 if (ctx->Debug.NumMessages == 0)
330 ctx->Debug.NextMsgLength = ctx->Debug.Log[ctx->Debug.NextMsg].length;
331
332 ctx->Debug.NumMessages++;
333 }
334
335 /**
336 * Pop the oldest debug message out of the log.
337 * Writes the message string, including the null terminator, into 'buf',
338 * using up to 'bufSize' bytes. If 'bufSize' is too small, or
339 * if 'buf' is NULL, nothing is written.
340 *
341 * Returns the number of bytes written on success, or when 'buf' is NULL,
342 * the number that would have been written. A return value of 0
343 * indicates failure.
344 */
345 static GLsizei
346 _mesa_get_msg(struct gl_context *ctx, GLenum *source, GLenum *type,
347 GLuint *id, GLenum *severity, GLsizei bufSize, char *buf)
348 {
349 struct gl_debug_msg *msg;
350 GLsizei length;
351
352 if (ctx->Debug.NumMessages == 0)
353 return 0;
354
355 msg = &ctx->Debug.Log[ctx->Debug.NextMsg];
356 length = msg->length;
357
358 assert(length > 0 && length == ctx->Debug.NextMsgLength);
359
360 if (bufSize < length && buf != NULL)
361 return 0;
362
363 if (severity)
364 *severity = debug_severity_enums[msg->severity];
365 if (source)
366 *source = debug_source_enums[msg->source];
367 if (type)
368 *type = debug_type_enums[msg->type];
369 if (id)
370 *id = msg->id;
371
372 if (buf) {
373 assert(msg->message[length-1] == '\0');
374 (void) strncpy(buf, msg->message, (size_t)length);
375 }
376
377 if (msg->message != (char*)out_of_memory)
378 free(msg->message);
379 msg->message = NULL;
380 msg->length = 0;
381
382 ctx->Debug.NumMessages--;
383 ctx->Debug.NextMsg++;
384 ctx->Debug.NextMsg %= MAX_DEBUG_LOGGED_MESSAGES;
385 ctx->Debug.NextMsgLength = ctx->Debug.Log[ctx->Debug.NextMsg].length;
386
387 return length;
388 }
389
390 /**
391 * Verify that source, type, and severity are valid enums.
392 * glDebugMessageInsertARB only accepts two values for 'source',
393 * and glDebugMessageControlARB will additionally accept GL_DONT_CARE
394 * in any parameter, so handle those cases specially.
395 */
396 static GLboolean
397 validate_params(struct gl_context *ctx, unsigned caller,
398 GLenum source, GLenum type, GLenum severity)
399 {
400 #define INSERT 1
401 #define CONTROL 2
402 switch(source) {
403 case GL_DEBUG_SOURCE_APPLICATION_ARB:
404 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB:
405 break;
406 case GL_DEBUG_SOURCE_API_ARB:
407 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB:
408 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB:
409 case GL_DEBUG_SOURCE_OTHER_ARB:
410 if (caller != INSERT)
411 break;
412 case GL_DONT_CARE:
413 if (caller == CONTROL)
414 break;
415 default:
416 goto error;
417 }
418
419 switch(type) {
420 case GL_DEBUG_TYPE_ERROR_ARB:
421 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
422 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
423 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
424 case GL_DEBUG_TYPE_PORTABILITY_ARB:
425 case GL_DEBUG_TYPE_OTHER_ARB:
426 break;
427 case GL_DONT_CARE:
428 if (caller == CONTROL)
429 break;
430 default:
431 goto error;
432 }
433
434 switch(severity) {
435 case GL_DEBUG_SEVERITY_HIGH_ARB:
436 case GL_DEBUG_SEVERITY_MEDIUM_ARB:
437 case GL_DEBUG_SEVERITY_LOW_ARB:
438 break;
439 case GL_DONT_CARE:
440 if (caller == CONTROL)
441 break;
442 default:
443 goto error;
444 }
445 return GL_TRUE;
446
447 error:
448 {
449 const char *callerstr;
450 if (caller == INSERT)
451 callerstr = "glDebugMessageInsertARB";
452 else if (caller == CONTROL)
453 callerstr = "glDebugMessageControlARB";
454 else
455 return GL_FALSE;
456
457 _mesa_error( ctx, GL_INVALID_ENUM, "bad values passed to %s"
458 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr,
459 source, type, severity);
460 }
461 return GL_FALSE;
462 }
463
464 void GLAPIENTRY
465 _mesa_DebugMessageInsertARB(GLenum source, GLenum type, GLuint id,
466 GLenum severity, GLint length,
467 const GLcharARB* buf)
468 {
469 GET_CURRENT_CONTEXT(ctx);
470
471 if (!validate_params(ctx, INSERT, source, type, severity))
472 return; /* GL_INVALID_ENUM */
473
474 if (length < 0)
475 length = strlen(buf);
476
477 if (length >= MAX_DEBUG_MESSAGE_LENGTH) {
478 _mesa_error(ctx, GL_INVALID_VALUE, "glDebugMessageInsertARB"
479 "(length=%d, which is not less than "
480 "GL_MAX_DEBUG_MESSAGE_LENGTH_ARB=%d)", length,
481 MAX_DEBUG_MESSAGE_LENGTH);
482 return;
483 }
484
485 _mesa_log_msg(ctx,
486 gl_enum_to_debug_source(source),
487 gl_enum_to_debug_type(type), id,
488 gl_enum_to_debug_severity(severity), length, buf);
489 }
490
491 GLuint GLAPIENTRY
492 _mesa_GetDebugMessageLogARB(GLuint count, GLsizei logSize, GLenum* sources,
493 GLenum* types, GLenum* ids, GLenum* severities,
494 GLsizei* lengths, GLcharARB* messageLog)
495 {
496 GET_CURRENT_CONTEXT(ctx);
497 GLuint ret;
498
499 if (!messageLog)
500 logSize = 0;
501
502 if (logSize < 0) {
503 _mesa_error(ctx, GL_INVALID_VALUE, "glGetDebugMessageLogARB"
504 "(logSize=%d : logSize must not be negative)", logSize);
505 return 0;
506 }
507
508 for (ret = 0; ret < count; ret++) {
509 GLsizei written = _mesa_get_msg(ctx, sources, types, ids, severities,
510 logSize, messageLog);
511 if (!written)
512 break;
513
514 if (messageLog) {
515 messageLog += written;
516 logSize -= written;
517 }
518 if (lengths) {
519 *lengths = written;
520 lengths++;
521 }
522
523 if (severities)
524 severities++;
525 if (sources)
526 sources++;
527 if (types)
528 types++;
529 if (ids)
530 ids++;
531 }
532
533 return ret;
534 }
535
536 /**
537 * Set the state of all message IDs found in the given intersection of
538 * 'source', 'type', and 'severity'. The _COUNT enum can be used for
539 * GL_DONT_CARE (include all messages in the class).
540 *
541 * This requires both setting the state of all previously seen message
542 * IDs in the hash table, and setting the default state for all
543 * applicable combinations of source/type/severity, so that all the
544 * yet-unknown message IDs that may be used in the future will be
545 * impacted as if they were already known.
546 */
547 static void
548 control_messages(struct gl_context *ctx,
549 enum mesa_debug_source source,
550 enum mesa_debug_type type,
551 enum mesa_debug_severity severity,
552 GLboolean enabled)
553 {
554 int s, t, sev, smax, tmax, sevmax;
555
556 if (source == MESA_DEBUG_SOURCE_COUNT) {
557 source = 0;
558 smax = MESA_DEBUG_SOURCE_COUNT;
559 } else {
560 smax = source+1;
561 }
562
563 if (type == MESA_DEBUG_TYPE_COUNT) {
564 type = 0;
565 tmax = MESA_DEBUG_TYPE_COUNT;
566 } else {
567 tmax = type+1;
568 }
569
570 if (severity == MESA_DEBUG_SEVERITY_COUNT) {
571 severity = 0;
572 sevmax = MESA_DEBUG_SEVERITY_COUNT;
573 } else {
574 sevmax = severity+1;
575 }
576
577 for (sev = severity; sev < sevmax; sev++)
578 for (s = source; s < smax; s++)
579 for (t = type; t < tmax; t++) {
580 struct simple_node *node;
581 struct gl_debug_severity *entry;
582
583 /* change the default for IDs we've never seen before. */
584 ctx->Debug.Defaults[sev][s][t] = enabled;
585
586 /* Now change the state of IDs we *have* seen... */
587 foreach(node, &ctx->Debug.Namespaces[s][t].Severity[sev]) {
588 entry = (struct gl_debug_severity *)node;
589 set_message_state(ctx, s, t, entry->ID, enabled);
590 }
591 }
592 }
593
594 /**
595 * Debugging-message namespaces with the source APPLICATION or THIRD_PARTY
596 * require special handling, since the IDs in them are controlled by clients,
597 * not the OpenGL implementation.
598 *
599 * 'count' is the length of the array 'ids'. If 'count' is nonzero, all
600 * the given IDs in the namespace defined by 'esource' and 'etype'
601 * will be affected.
602 *
603 * If 'count' is zero, this sets the state of all IDs that match
604 * the combination of 'esource', 'etype', and 'eseverity'.
605 */
606 static void
607 control_app_messages(struct gl_context *ctx, GLenum esource, GLenum etype,
608 GLenum eseverity, GLsizei count, const GLuint *ids,
609 GLboolean enabled)
610 {
611 GLsizei i;
612 enum mesa_debug_source source = gl_enum_to_debug_source(esource);
613 enum mesa_debug_type type = gl_enum_to_debug_type(etype);
614 enum mesa_debug_severity severity = gl_enum_to_debug_severity(eseverity);
615
616 for (i = 0; i < count; i++)
617 set_message_state(ctx, source, type, ids[i], enabled);
618
619 if (count)
620 return;
621
622 control_messages(ctx, source, type, severity, enabled);
623 }
624
625 void GLAPIENTRY
626 _mesa_DebugMessageControlARB(GLenum gl_source, GLenum gl_type,
627 GLenum gl_severity,
628 GLsizei count, const GLuint *ids,
629 GLboolean enabled)
630 {
631 GET_CURRENT_CONTEXT(ctx);
632
633 if (count < 0) {
634 _mesa_error(ctx, GL_INVALID_VALUE, "glDebugMessageControlARB"
635 "(count=%d : count must not be negative)", count);
636 return;
637 }
638
639 if (!validate_params(ctx, CONTROL, gl_source, gl_type, gl_severity))
640 return; /* GL_INVALID_ENUM */
641
642 if (count && (gl_severity != GL_DONT_CARE || gl_type == GL_DONT_CARE
643 || gl_source == GL_DONT_CARE)) {
644 _mesa_error(ctx, GL_INVALID_OPERATION, "glDebugMessageControlARB"
645 "(When passing an array of ids, severity must be"
646 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.");
647 return;
648 }
649
650 control_app_messages(ctx, gl_source, gl_type, gl_severity,
651 count, ids, enabled);
652 }
653
654 void GLAPIENTRY
655 _mesa_DebugMessageCallbackARB(GLDEBUGPROCARB callback, const void *userParam)
656 {
657 GET_CURRENT_CONTEXT(ctx);
658 ctx->Debug.Callback = callback;
659 ctx->Debug.CallbackData = userParam;
660 }
661
662 void
663 _mesa_init_errors(struct gl_context *ctx)
664 {
665 int s, t, sev;
666
667 ctx->Debug.Callback = NULL;
668 ctx->Debug.SyncOutput = GL_FALSE;
669 ctx->Debug.Log[0].length = 0;
670 ctx->Debug.NumMessages = 0;
671 ctx->Debug.NextMsg = 0;
672 ctx->Debug.NextMsgLength = 0;
673
674 /* Enable all the messages with severity HIGH or MEDIUM by default. */
675 memset(ctx->Debug.Defaults[MESA_DEBUG_SEVERITY_HIGH], GL_TRUE,
676 sizeof ctx->Debug.Defaults[MESA_DEBUG_SEVERITY_HIGH]);
677 memset(ctx->Debug.Defaults[MESA_DEBUG_SEVERITY_MEDIUM], GL_TRUE,
678 sizeof ctx->Debug.Defaults[MESA_DEBUG_SEVERITY_MEDIUM]);
679 memset(ctx->Debug.Defaults[MESA_DEBUG_SEVERITY_LOW], GL_FALSE,
680 sizeof ctx->Debug.Defaults[MESA_DEBUG_SEVERITY_LOW]);
681
682 /* Initialize state for filtering known debug messages. */
683 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++)
684 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) {
685 ctx->Debug.Namespaces[s][t].IDs = _mesa_NewHashTable();
686 assert(ctx->Debug.Namespaces[s][t].IDs);
687
688 for (sev = 0; sev < MESA_DEBUG_SEVERITY_COUNT; sev++)
689 make_empty_list(&ctx->Debug.Namespaces[s][t].Severity[sev]);
690 }
691 }
692
693 static void
694 do_nothing(GLuint key, void *data, void *userData)
695 {
696 }
697
698 void
699 _mesa_free_errors_data(struct gl_context *ctx)
700 {
701 enum mesa_debug_type t;
702 enum mesa_debug_source s;
703 enum mesa_debug_severity sev;
704
705 /* Tear down state for filtering debug messages. */
706 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++)
707 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) {
708 _mesa_HashDeleteAll(ctx->Debug.Namespaces[s][t].IDs, do_nothing, NULL);
709 _mesa_DeleteHashTable(ctx->Debug.Namespaces[s][t].IDs);
710 for (sev = 0; sev < MESA_DEBUG_SEVERITY_COUNT; sev++) {
711 struct simple_node *node, *tmp;
712 struct gl_debug_severity *entry;
713
714 foreach_s(node, tmp, &ctx->Debug.Namespaces[s][t].Severity[sev]) {
715 entry = (struct gl_debug_severity *)node;
716 free(entry);
717 }
718 }
719 }
720 }
721
722 /**********************************************************************/
723 /** \name Diagnostics */
724 /*@{*/
725
726 static void
727 output_if_debug(const char *prefixString, const char *outputString,
728 GLboolean newline)
729 {
730 static int debug = -1;
731 static FILE *fout = NULL;
732
733 /* Init the local 'debug' var once.
734 * Note: the _mesa_init_debug() function should have been called
735 * by now so MESA_DEBUG_FLAGS will be initialized.
736 */
737 if (debug == -1) {
738 /* If MESA_LOG_FILE env var is set, log Mesa errors, warnings,
739 * etc to the named file. Otherwise, output to stderr.
740 */
741 const char *logFile = _mesa_getenv("MESA_LOG_FILE");
742 if (logFile)
743 fout = fopen(logFile, "w");
744 if (!fout)
745 fout = stderr;
746 #ifdef DEBUG
747 /* in debug builds, print messages unless MESA_DEBUG="silent" */
748 if (MESA_DEBUG_FLAGS & DEBUG_SILENT)
749 debug = 0;
750 else
751 debug = 1;
752 #else
753 /* in release builds, be silent unless MESA_DEBUG is set */
754 debug = _mesa_getenv("MESA_DEBUG") != NULL;
755 #endif
756 }
757
758 /* Now only print the string if we're required to do so. */
759 if (debug) {
760 fprintf(fout, "%s: %s", prefixString, outputString);
761 if (newline)
762 fprintf(fout, "\n");
763 fflush(fout);
764
765 #if defined(_WIN32) && !defined(_WIN32_WCE)
766 /* stderr from windows applications without console is not usually
767 * visible, so communicate with the debugger instead */
768 {
769 char buf[4096];
770 _mesa_snprintf(buf, sizeof(buf), "%s: %s%s", prefixString, outputString, newline ? "\n" : "");
771 OutputDebugStringA(buf);
772 }
773 #endif
774 }
775 }
776
777 /**
778 * When a new type of error is recorded, print a message describing
779 * previous errors which were accumulated.
780 */
781 static void
782 flush_delayed_errors( struct gl_context *ctx )
783 {
784 char s[MAX_DEBUG_MESSAGE_LENGTH];
785
786 if (ctx->ErrorDebugCount) {
787 _mesa_snprintf(s, MAX_DEBUG_MESSAGE_LENGTH, "%d similar %s errors",
788 ctx->ErrorDebugCount,
789 _mesa_lookup_enum_by_nr(ctx->ErrorValue));
790
791 output_if_debug("Mesa", s, GL_TRUE);
792
793 ctx->ErrorDebugCount = 0;
794 }
795 }
796
797
798 /**
799 * Report a warning (a recoverable error condition) to stderr if
800 * either DEBUG is defined or the MESA_DEBUG env var is set.
801 *
802 * \param ctx GL context.
803 * \param fmtString printf()-like format string.
804 */
805 void
806 _mesa_warning( struct gl_context *ctx, const char *fmtString, ... )
807 {
808 char str[MAX_DEBUG_MESSAGE_LENGTH];
809 va_list args;
810 va_start( args, fmtString );
811 (void) _mesa_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
812 va_end( args );
813
814 if (ctx)
815 flush_delayed_errors( ctx );
816
817 output_if_debug("Mesa warning", str, GL_TRUE);
818 }
819
820
821 /**
822 * Report an internal implementation problem.
823 * Prints the message to stderr via fprintf().
824 *
825 * \param ctx GL context.
826 * \param fmtString problem description string.
827 */
828 void
829 _mesa_problem( const struct gl_context *ctx, const char *fmtString, ... )
830 {
831 va_list args;
832 char str[MAX_DEBUG_MESSAGE_LENGTH];
833 static int numCalls = 0;
834
835 (void) ctx;
836
837 if (numCalls < 50) {
838 numCalls++;
839
840 va_start( args, fmtString );
841 _mesa_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
842 va_end( args );
843 fprintf(stderr, "Mesa %s implementation error: %s\n",
844 PACKAGE_VERSION, str);
845 fprintf(stderr, "Please report at " PACKAGE_BUGREPORT "\n");
846 }
847 }
848
849 static GLboolean
850 should_output(struct gl_context *ctx, GLenum error, const char *fmtString)
851 {
852 static GLint debug = -1;
853
854 /* Check debug environment variable only once:
855 */
856 if (debug == -1) {
857 const char *debugEnv = _mesa_getenv("MESA_DEBUG");
858
859 #ifdef DEBUG
860 if (debugEnv && strstr(debugEnv, "silent"))
861 debug = GL_FALSE;
862 else
863 debug = GL_TRUE;
864 #else
865 if (debugEnv)
866 debug = GL_TRUE;
867 else
868 debug = GL_FALSE;
869 #endif
870 }
871
872 if (debug) {
873 if (ctx->ErrorValue != error ||
874 ctx->ErrorDebugFmtString != fmtString) {
875 flush_delayed_errors( ctx );
876 ctx->ErrorDebugFmtString = fmtString;
877 ctx->ErrorDebugCount = 0;
878 return GL_TRUE;
879 }
880 ctx->ErrorDebugCount++;
881 }
882 return GL_FALSE;
883 }
884
885 void
886 _mesa_gl_debug(struct gl_context *ctx,
887 GLuint *id,
888 enum mesa_debug_type type,
889 enum mesa_debug_severity severity,
890 const char *fmtString, ...)
891 {
892 char s[MAX_DEBUG_MESSAGE_LENGTH];
893 int len;
894 va_list args;
895
896 debug_get_id(id);
897
898 va_start(args, fmtString);
899 len = _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
900 va_end(args);
901
902 _mesa_log_msg(ctx, MESA_DEBUG_SOURCE_API, type,
903 *id, severity, len, s);
904 }
905
906
907 /**
908 * Record an OpenGL state error. These usually occur when the user
909 * passes invalid parameters to a GL function.
910 *
911 * If debugging is enabled (either at compile-time via the DEBUG macro, or
912 * run-time via the MESA_DEBUG environment variable), report the error with
913 * _mesa_debug().
914 *
915 * \param ctx the GL context.
916 * \param error the error value.
917 * \param fmtString printf() style format string, followed by optional args
918 */
919 void
920 _mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... )
921 {
922 GLboolean do_output, do_log;
923 /* Ideally this would be set up by the caller, so that we had proper IDs
924 * per different message.
925 */
926 static GLuint error_msg_id = 0;
927
928 debug_get_id(&error_msg_id);
929
930 do_output = should_output(ctx, error, fmtString);
931 do_log = should_log(ctx,
932 MESA_DEBUG_SOURCE_API,
933 MESA_DEBUG_TYPE_ERROR,
934 error_msg_id,
935 MESA_DEBUG_SEVERITY_HIGH);
936
937 if (do_output || do_log) {
938 char s[MAX_DEBUG_MESSAGE_LENGTH], s2[MAX_DEBUG_MESSAGE_LENGTH];
939 int len;
940 va_list args;
941
942 va_start(args, fmtString);
943 len = _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
944 va_end(args);
945
946 if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
947 /* Too long error message. Whoever calls _mesa_error should use
948 * shorter strings. */
949 ASSERT(0);
950 return;
951 }
952
953 len = _mesa_snprintf(s2, MAX_DEBUG_MESSAGE_LENGTH, "%s in %s",
954 _mesa_lookup_enum_by_nr(error), s);
955 if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
956 /* Same as above. */
957 ASSERT(0);
958 return;
959 }
960
961 /* Print the error to stderr if needed. */
962 if (do_output) {
963 output_if_debug("Mesa: User error", s2, GL_TRUE);
964 }
965
966 /* Log the error via ARB_debug_output if needed.*/
967 if (do_log) {
968 _mesa_log_msg(ctx,
969 MESA_DEBUG_SOURCE_API,
970 MESA_DEBUG_TYPE_ERROR,
971 error_msg_id,
972 MESA_DEBUG_SEVERITY_HIGH, len, s2);
973 }
974 }
975
976 /* Set the GL context error state for glGetError. */
977 _mesa_record_error(ctx, error);
978 }
979
980
981 /**
982 * Report debug information. Print error message to stderr via fprintf().
983 * No-op if DEBUG mode not enabled.
984 *
985 * \param ctx GL context.
986 * \param fmtString printf()-style format string, followed by optional args.
987 */
988 void
989 _mesa_debug( const struct gl_context *ctx, const char *fmtString, ... )
990 {
991 #ifdef DEBUG
992 char s[MAX_DEBUG_MESSAGE_LENGTH];
993 va_list args;
994 va_start(args, fmtString);
995 _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
996 va_end(args);
997 output_if_debug("Mesa", s, GL_FALSE);
998 #endif /* DEBUG */
999 (void) ctx;
1000 (void) fmtString;
1001 }
1002
1003
1004 /**
1005 * Report debug information from the shader compiler via GL_ARB_debug_output.
1006 *
1007 * \param ctx GL context.
1008 * \param type The namespace to which this message belongs.
1009 * \param id The message ID within the given namespace.
1010 * \param msg The message to output. Need not be null-terminated.
1011 * \param len The length of 'msg'. If negative, 'msg' must be null-terminated.
1012 */
1013 void
1014 _mesa_shader_debug( struct gl_context *ctx, GLenum type, GLuint *id,
1015 const char *msg, int len )
1016 {
1017 enum mesa_debug_source source = MESA_DEBUG_SOURCE_SHADER_COMPILER;
1018 enum mesa_debug_severity severity = MESA_DEBUG_SEVERITY_HIGH;
1019
1020 debug_get_id(id);
1021
1022 if (len < 0)
1023 len = strlen(msg);
1024
1025 /* Truncate the message if necessary. */
1026 if (len >= MAX_DEBUG_MESSAGE_LENGTH)
1027 len = MAX_DEBUG_MESSAGE_LENGTH - 1;
1028
1029 _mesa_log_msg(ctx, source, type, *id, severity, len, msg);
1030 }
1031
1032 /*@}*/