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