1 /* 2 * Mesa 3-D graphics library 3 * 4 * Copyright (C) 1999-2016 Brian Paul, et al All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included 14 * in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25 26 #include <stdarg.h> 27 #include <stdio.h> 28 #include "context.h" 29 #include "debug_output.h" 30 #include "dispatch.h" 31 #include "enums.h" 32 #include "imports.h" 33 #include "hash.h" 34 #include "mtypes.h" 35 #include "version.h" 36 #include "util/hash_table.h" 37 #include "util/simple_list.h" 38 39 40 static simple_mtx_t DynamicIDMutex = _SIMPLE_MTX_INITIALIZER_NP; 41 static GLuint NextDynamicID = 1; 42 43 44 /** 45 * A namespace element. 46 */ 47 struct 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 56 57 struct gl_debug_namespace 58 { 59 struct simple_node Elements; 60 GLbitfield DefaultState; 61 }; 62 63 64 struct gl_debug_group { 65 struct gl_debug_namespace Namespaces[MESA_DEBUG_SOURCE_COUNT][MESA_DEBUG_TYPE_COUNT]; 66 }; 67 68 69 /** 70 * An error, warning, or other piece of debug information for an application 71 * to consume via GL_ARB_debug_output/GL_KHR_debug. 72 */ 73 struct gl_debug_message 74 { 75 enum mesa_debug_source source; 76 enum mesa_debug_type type; 77 GLuint id; 78 enum mesa_debug_severity severity; 79 /* length as given by the user - if message was explicitly null terminated, 80 * length can be negative */ 81 GLsizei length; 82 GLcharARB *message; 83 }; 84 85 86 /** 87 * Debug message log. It works like a ring buffer. 88 */ 89 struct gl_debug_log { 90 struct gl_debug_message Messages[MAX_DEBUG_LOGGED_MESSAGES]; 91 GLint NextMessage; 92 GLint NumMessages; 93 }; 94 95 96 struct gl_debug_state 97 { 98 GLDEBUGPROC Callback; 99 const void *CallbackData; 100 GLboolean SyncOutput; 101 GLboolean DebugOutput; 102 GLboolean LogToStderr; 103 104 struct gl_debug_group *Groups[MAX_DEBUG_GROUP_STACK_DEPTH]; 105 struct gl_debug_message GroupMessages[MAX_DEBUG_GROUP_STACK_DEPTH]; 106 GLint CurrentGroup; // GroupStackDepth - 1 107 108 struct gl_debug_log Log; 109 }; 110 111 112 static char out_of_memory[] = "Debugging error: out of memory"; 113 114 static const GLenum debug_source_enums[] = { 115 GL_DEBUG_SOURCE_API, 116 GL_DEBUG_SOURCE_WINDOW_SYSTEM, 117 GL_DEBUG_SOURCE_SHADER_COMPILER, 118 GL_DEBUG_SOURCE_THIRD_PARTY, 119 GL_DEBUG_SOURCE_APPLICATION, 120 GL_DEBUG_SOURCE_OTHER, 121 }; 122 123 static const GLenum debug_type_enums[] = { 124 GL_DEBUG_TYPE_ERROR, 125 GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR, 126 GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR, 127 GL_DEBUG_TYPE_PORTABILITY, 128 GL_DEBUG_TYPE_PERFORMANCE, 129 GL_DEBUG_TYPE_OTHER, 130 GL_DEBUG_TYPE_MARKER, 131 GL_DEBUG_TYPE_PUSH_GROUP, 132 GL_DEBUG_TYPE_POP_GROUP, 133 }; 134 135 static const GLenum debug_severity_enums[] = { 136 GL_DEBUG_SEVERITY_LOW, 137 GL_DEBUG_SEVERITY_MEDIUM, 138 GL_DEBUG_SEVERITY_HIGH, 139 GL_DEBUG_SEVERITY_NOTIFICATION, 140 }; 141 142 143 static enum mesa_debug_source 144 gl_enum_to_debug_source(GLenum e) 145 { 146 unsigned i; 147 148 for (i = 0; i < ARRAY_SIZE(debug_source_enums); i++) { 149 if (debug_source_enums[i] == e) 150 break; 151 } 152 return i; 153 } 154 155 static enum mesa_debug_type 156 gl_enum_to_debug_type(GLenum e) 157 { 158 unsigned i; 159 160 for (i = 0; i < ARRAY_SIZE(debug_type_enums); i++) { 161 if (debug_type_enums[i] == e) 162 break; 163 } 164 return i; 165 } 166 167 static enum mesa_debug_severity 168 gl_enum_to_debug_severity(GLenum e) 169 { 170 unsigned i; 171 172 for (i = 0; i < ARRAY_SIZE(debug_severity_enums); i++) { 173 if (debug_severity_enums[i] == e) 174 break; 175 } 176 return i; 177 } 178 179 180 /** 181 * Handles generating a GL_ARB_debug_output message ID generated by the GL or 182 * GLSL compiler. 183 * 184 * The GL API has this "ID" mechanism, where the intention is to allow a 185 * client to filter in/out messages based on source, type, and ID. Of course, 186 * building a giant enum list of all debug output messages that Mesa might 187 * generate is ridiculous, so instead we have our caller pass us a pointer to 188 * static storage where the ID should get stored. This ID will be shared 189 * across all contexts for that message (which seems like a desirable 190 * property, even if it's not expected by the spec), but note that it won't be 191 * the same between executions if messages aren't generated in the same order. 192 */ 193 void 194 _mesa_debug_get_id(GLuint *id) 195 { 196 if (!(*id)) { 197 simple_mtx_lock(&DynamicIDMutex); 198 if (!(*id)) 199 *id = NextDynamicID++; 200 simple_mtx_unlock(&DynamicIDMutex); 201 } 202 } 203 204 static void 205 debug_message_clear(struct gl_debug_message *msg) 206 { 207 if (msg->message != (char*)out_of_memory) 208 free(msg->message); 209 msg->message = NULL; 210 msg->length = 0; 211 } 212 213 static void 214 debug_message_store(struct gl_debug_message *msg, 215 enum mesa_debug_source source, 216 enum mesa_debug_type type, GLuint id, 217 enum mesa_debug_severity severity, 218 GLsizei len, const char *buf) 219 { 220 GLsizei length = len; 221 222 assert(!msg->message && !msg->length); 223 224 if (length < 0) 225 length = strlen(buf); 226 227 msg->message = malloc(length+1); 228 if (msg->message) { 229 (void) strncpy(msg->message, buf, (size_t)length); 230 msg->message[length] = '\0'; 231 232 msg->length = len; 233 msg->source = source; 234 msg->type = type; 235 msg->id = id; 236 msg->severity = severity; 237 } else { 238 static GLuint oom_msg_id = 0; 239 _mesa_debug_get_id(&oom_msg_id); 240 241 /* malloc failed! */ 242 msg->message = out_of_memory; 243 msg->length = -1; 244 msg->source = MESA_DEBUG_SOURCE_OTHER; 245 msg->type = MESA_DEBUG_TYPE_ERROR; 246 msg->id = oom_msg_id; 247 msg->severity = MESA_DEBUG_SEVERITY_HIGH; 248 } 249 } 250 251 static void 252 debug_namespace_init(struct gl_debug_namespace *ns) 253 { 254 make_empty_list(&ns->Elements); 255 256 /* Enable all the messages with severity HIGH or MEDIUM by default */ 257 ns->DefaultState = (1 << MESA_DEBUG_SEVERITY_MEDIUM ) | 258 (1 << MESA_DEBUG_SEVERITY_HIGH) | 259 (1 << MESA_DEBUG_SEVERITY_NOTIFICATION); 260 } 261 262 static void 263 debug_namespace_clear(struct gl_debug_namespace *ns) 264 { 265 struct simple_node *node, *tmp; 266 267 foreach_s(node, tmp, &ns->Elements) 268 free(node); 269 } 270 271 static bool 272 debug_namespace_copy(struct gl_debug_namespace *dst, 273 const struct gl_debug_namespace *src) 274 { 275 struct simple_node *node; 276 277 dst->DefaultState = src->DefaultState; 278 279 make_empty_list(&dst->Elements); 280 foreach(node, &src->Elements) { 281 const struct gl_debug_element *elem = 282 (const struct gl_debug_element *) node; 283 struct gl_debug_element *copy; 284 285 copy = malloc(sizeof(*copy)); 286 if (!copy) { 287 debug_namespace_clear(dst); 288 return false; 289 } 290 291 copy->ID = elem->ID; 292 copy->State = elem->State; 293 insert_at_tail(&dst->Elements, ©->link); 294 } 295 296 return true; 297 } 298 299 /** 300 * Set the state of \p id in the namespace. 301 */ 302 static bool 303 debug_namespace_set(struct gl_debug_namespace *ns, 304 GLuint id, bool enabled) 305 { 306 const uint32_t state = (enabled) ? 307 ((1 << MESA_DEBUG_SEVERITY_COUNT) - 1) : 0; 308 struct gl_debug_element *elem = NULL; 309 struct simple_node *node; 310 311 /* find the element */ 312 foreach(node, &ns->Elements) { 313 struct gl_debug_element *tmp = (struct gl_debug_element *) node; 314 if (tmp->ID == id) { 315 elem = tmp; 316 break; 317 } 318 } 319 320 /* we do not need the element if it has the default state */ 321 if (ns->DefaultState == state) { 322 if (elem) { 323 remove_from_list(&elem->link); 324 free(elem); 325 } 326 return true; 327 } 328 329 if (!elem) { 330 elem = malloc(sizeof(*elem)); 331 if (!elem) 332 return false; 333 334 elem->ID = id; 335 insert_at_tail(&ns->Elements, &elem->link); 336 } 337 338 elem->State = state; 339 340 return true; 341 } 342 343 /** 344 * Set the default state of the namespace for \p severity. When \p severity 345 * is MESA_DEBUG_SEVERITY_COUNT, the default values for all severities are 346 * updated. 347 */ 348 static void 349 debug_namespace_set_all(struct gl_debug_namespace *ns, 350 enum mesa_debug_severity severity, 351 bool enabled) 352 { 353 struct simple_node *node, *tmp; 354 uint32_t mask, val; 355 356 /* set all elements to the same state */ 357 if (severity == MESA_DEBUG_SEVERITY_COUNT) { 358 ns->DefaultState = (enabled) ? ((1 << severity) - 1) : 0; 359 debug_namespace_clear(ns); 360 make_empty_list(&ns->Elements); 361 return; 362 } 363 364 mask = 1 << severity; 365 val = (enabled) ? mask : 0; 366 367 ns->DefaultState = (ns->DefaultState & ~mask) | val; 368 369 foreach_s(node, tmp, &ns->Elements) { 370 struct gl_debug_element *elem = (struct gl_debug_element *) node; 371 372 elem->State = (elem->State & ~mask) | val; 373 if (elem->State == ns->DefaultState) { 374 remove_from_list(node); 375 free(node); 376 } 377 } 378 } 379 380 /** 381 * Get the state of \p id in the namespace. 382 */ 383 static bool 384 debug_namespace_get(const struct gl_debug_namespace *ns, GLuint id, 385 enum mesa_debug_severity severity) 386 { 387 struct simple_node *node; 388 uint32_t state; 389 390 state = ns->DefaultState; 391 foreach(node, &ns->Elements) { 392 struct gl_debug_element *elem = (struct gl_debug_element *) node; 393 394 if (elem->ID == id) { 395 state = elem->State; 396 break; 397 } 398 } 399 400 return (state & (1 << severity)); 401 } 402 403 /** 404 * Allocate and initialize context debug state. 405 */ 406 static struct gl_debug_state * 407 debug_create(void) 408 { 409 struct gl_debug_state *debug; 410 int s, t; 411 412 debug = CALLOC_STRUCT(gl_debug_state); 413 if (!debug) 414 return NULL; 415 416 debug->Groups[0] = malloc(sizeof(*debug->Groups[0])); 417 if (!debug->Groups[0]) { 418 free(debug); 419 return NULL; 420 } 421 422 /* Initialize state for filtering known debug messages. */ 423 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) { 424 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) 425 debug_namespace_init(&debug->Groups[0]->Namespaces[s][t]); 426 } 427 428 return debug; 429 } 430 431 /** 432 * Return true if the top debug group points to the group below it. 433 */ 434 static bool 435 debug_is_group_read_only(const struct gl_debug_state *debug) 436 { 437 const GLint gstack = debug->CurrentGroup; 438 return (gstack > 0 && debug->Groups[gstack] == debug->Groups[gstack - 1]); 439 } 440 441 /** 442 * Make the top debug group writable. 443 */ 444 static bool 445 debug_make_group_writable(struct gl_debug_state *debug) 446 { 447 const GLint gstack = debug->CurrentGroup; 448 const struct gl_debug_group *src = debug->Groups[gstack]; 449 struct gl_debug_group *dst; 450 int s, t; 451 452 if (!debug_is_group_read_only(debug)) 453 return true; 454 455 dst = malloc(sizeof(*dst)); 456 if (!dst) 457 return false; 458 459 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) { 460 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) { 461 if (!debug_namespace_copy(&dst->Namespaces[s][t], 462 &src->Namespaces[s][t])) { 463 /* error path! */ 464 for (t = t - 1; t >= 0; t--) 465 debug_namespace_clear(&dst->Namespaces[s][t]); 466 for (s = s - 1; s >= 0; s--) { 467 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) 468 debug_namespace_clear(&dst->Namespaces[s][t]); 469 } 470 free(dst); 471 return false; 472 } 473 } 474 } 475 476 debug->Groups[gstack] = dst; 477 478 return true; 479 } 480 481 /** 482 * Free the top debug group. 483 */ 484 static void 485 debug_clear_group(struct gl_debug_state *debug) 486 { 487 const GLint gstack = debug->CurrentGroup; 488 489 if (!debug_is_group_read_only(debug)) { 490 struct gl_debug_group *grp = debug->Groups[gstack]; 491 int s, t; 492 493 for (s = 0; s < MESA_DEBUG_SOURCE_COUNT; s++) { 494 for (t = 0; t < MESA_DEBUG_TYPE_COUNT; t++) 495 debug_namespace_clear(&grp->Namespaces[s][t]); 496 } 497 498 free(grp); 499 } 500 501 debug->Groups[gstack] = NULL; 502 } 503 504 /** 505 * Delete the oldest debug messages out of the log. 506 */ 507 static void 508 debug_delete_messages(struct gl_debug_state *debug, int count) 509 { 510 struct gl_debug_log *log = &debug->Log; 511 512 if (count > log->NumMessages) 513 count = log->NumMessages; 514 515 while (count--) { 516 struct gl_debug_message *msg = &log->Messages[log->NextMessage]; 517 518 debug_message_clear(msg); 519 520 log->NumMessages--; 521 log->NextMessage++; 522 log->NextMessage %= MAX_DEBUG_LOGGED_MESSAGES; 523 } 524 } 525 526 /** 527 * Loop through debug group stack tearing down states for 528 * filtering debug messages. Then free debug output state. 529 */ 530 static void 531 debug_destroy(struct gl_debug_state *debug) 532 { 533 while (debug->CurrentGroup > 0) { 534 debug_clear_group(debug); 535 debug->CurrentGroup--; 536 } 537 538 debug_clear_group(debug); 539 debug_delete_messages(debug, debug->Log.NumMessages); 540 free(debug); 541 } 542 543 /** 544 * Sets the state of the given message source/type/ID tuple. 545 */ 546 static void 547 debug_set_message_enable(struct gl_debug_state *debug, 548 enum mesa_debug_source source, 549 enum mesa_debug_type type, 550 GLuint id, GLboolean enabled) 551 { 552 const GLint gstack = debug->CurrentGroup; 553 struct gl_debug_namespace *ns; 554 555 debug_make_group_writable(debug); 556 ns = &debug->Groups[gstack]->Namespaces[source][type]; 557 558 debug_namespace_set(ns, id, enabled); 559 } 560 561 /* 562 * Set the state of all message IDs found in the given intersection of 563 * 'source', 'type', and 'severity'. The _COUNT enum can be used for 564 * GL_DONT_CARE (include all messages in the class). 565 * 566 * This requires both setting the state of all previously seen message 567 * IDs in the hash table, and setting the default state for all 568 * applicable combinations of source/type/severity, so that all the 569 * yet-unknown message IDs that may be used in the future will be 570 * impacted as if they were already known. 571 */ 572 static void 573 debug_set_message_enable_all(struct gl_debug_state *debug, 574 enum mesa_debug_source source, 575 enum mesa_debug_type type, 576 enum mesa_debug_severity severity, 577 GLboolean enabled) 578 { 579 const GLint gstack = debug->CurrentGroup; 580 int s, t, smax, tmax; 581 582 if (source == MESA_DEBUG_SOURCE_COUNT) { 583 source = 0; 584 smax = MESA_DEBUG_SOURCE_COUNT; 585 } else { 586 smax = source+1; 587 } 588 589 if (type == MESA_DEBUG_TYPE_COUNT) { 590 type = 0; 591 tmax = MESA_DEBUG_TYPE_COUNT; 592 } else { 593 tmax = type+1; 594 } 595 596 debug_make_group_writable(debug); 597 598 for (s = source; s < smax; s++) { 599 for (t = type; t < tmax; t++) { 600 struct gl_debug_namespace *nspace = 601 &debug->Groups[gstack]->Namespaces[s][t]; 602 debug_namespace_set_all(nspace, severity, enabled); 603 } 604 } 605 } 606 607 /** 608 * Returns if the given message source/type/ID tuple is enabled. 609 */ 610 bool 611 _mesa_debug_is_message_enabled(const struct gl_debug_state *debug, 612 enum mesa_debug_source source, 613 enum mesa_debug_type type, 614 GLuint id, 615 enum mesa_debug_severity severity) 616 { 617 const GLint gstack = debug->CurrentGroup; 618 struct gl_debug_group *grp = debug->Groups[gstack]; 619 struct gl_debug_namespace *nspace = &grp->Namespaces[source][type]; 620 621 if (!debug->DebugOutput) 622 return false; 623 624 return debug_namespace_get(nspace, id, severity); 625 } 626 627 /** 628 * 'buf' is not necessarily a null-terminated string. When logging, copy 629 * 'len' characters from it, store them in a new, null-terminated string, 630 * and remember the number of bytes used by that string, *including* 631 * the null terminator this time. 632 */ 633 static void 634 debug_log_message(struct gl_debug_state *debug, 635 enum mesa_debug_source source, 636 enum mesa_debug_type type, GLuint id, 637 enum mesa_debug_severity severity, 638 GLsizei len, const char *buf) 639 { 640 struct gl_debug_log *log = &debug->Log; 641 GLint nextEmpty; 642 struct gl_debug_message *emptySlot; 643 644 if (debug->LogToStderr) { 645 _mesa_log("Mesa debug output: %.*s\n", len, buf); 646 } 647 648 assert(len < MAX_DEBUG_MESSAGE_LENGTH); 649 650 if (log->NumMessages == MAX_DEBUG_LOGGED_MESSAGES) 651 return; 652 653 nextEmpty = (log->NextMessage + log->NumMessages) 654 % MAX_DEBUG_LOGGED_MESSAGES; 655 emptySlot = &log->Messages[nextEmpty]; 656 657 debug_message_store(emptySlot, source, type, 658 id, severity, len, buf); 659 660 log->NumMessages++; 661 } 662 663 /** 664 * Return the oldest debug message out of the log. 665 */ 666 static const struct gl_debug_message * 667 debug_fetch_message(const struct gl_debug_state *debug) 668 { 669 const struct gl_debug_log *log = &debug->Log; 670 671 return (log->NumMessages) ? &log->Messages[log->NextMessage] : NULL; 672 } 673 674 static struct gl_debug_message * 675 debug_get_group_message(struct gl_debug_state *debug) 676 { 677 return &debug->GroupMessages[debug->CurrentGroup]; 678 } 679 680 static void 681 debug_push_group(struct gl_debug_state *debug) 682 { 683 const GLint gstack = debug->CurrentGroup; 684 685 /* just point to the previous stack */ 686 debug->Groups[gstack + 1] = debug->Groups[gstack]; 687 debug->CurrentGroup++; 688 } 689 690 static void 691 debug_pop_group(struct gl_debug_state *debug) 692 { 693 debug_clear_group(debug); 694 debug->CurrentGroup--; 695 } 696 697 698 /** 699 * Lock and return debug state for the context. The debug state will be 700 * allocated and initialized upon the first call. When NULL is returned, the 701 * debug state is not locked. 702 */ 703 static struct gl_debug_state * 704 _mesa_lock_debug_state(struct gl_context *ctx) 705 { 706 simple_mtx_lock(&ctx->DebugMutex); 707 708 if (!ctx->Debug) { 709 ctx->Debug = debug_create(); 710 if (!ctx->Debug) { 711 GET_CURRENT_CONTEXT(cur); 712 simple_mtx_unlock(&ctx->DebugMutex); 713 714 /* 715 * This function may be called from other threads. When that is the 716 * case, we cannot record this OOM error. 717 */ 718 if (ctx == cur) 719 _mesa_error(ctx, GL_OUT_OF_MEMORY, "allocating debug state"); 720 721 return NULL; 722 } 723 } 724 725 return ctx->Debug; 726 } 727 728 static void 729 _mesa_unlock_debug_state(struct gl_context *ctx) 730 { 731 simple_mtx_unlock(&ctx->DebugMutex); 732 } 733 734 /** 735 * Set the integer debug state specified by \p pname. This can be called from 736 * _mesa_set_enable for example. 737 */ 738 bool 739 _mesa_set_debug_state_int(struct gl_context *ctx, GLenum pname, GLint val) 740 { 741 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx); 742 743 if (!debug) 744 return false; 745 746 switch (pname) { 747 case GL_DEBUG_OUTPUT: 748 debug->DebugOutput = (val != 0); 749 break; 750 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB: 751 debug->SyncOutput = (val != 0); 752 break; 753 default: 754 assert(!"unknown debug output param"); 755 break; 756 } 757 758 _mesa_unlock_debug_state(ctx); 759 760 return true; 761 } 762 763 /** 764 * Query the integer debug state specified by \p pname. This can be called 765 * _mesa_GetIntegerv for example. 766 */ 767 GLint 768 _mesa_get_debug_state_int(struct gl_context *ctx, GLenum pname) 769 { 770 GLint val; 771 772 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx); 773 if (!debug) 774 return 0; 775 776 switch (pname) { 777 case GL_DEBUG_OUTPUT: 778 val = debug->DebugOutput; 779 break; 780 case GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB: 781 val = debug->SyncOutput; 782 break; 783 case GL_DEBUG_LOGGED_MESSAGES: 784 val = debug->Log.NumMessages; 785 break; 786 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH: 787 val = (debug->Log.NumMessages) ? 788 debug->Log.Messages[debug->Log.NextMessage].length + 1 : 0; 789 break; 790 case GL_DEBUG_GROUP_STACK_DEPTH: 791 val = debug->CurrentGroup + 1; 792 break; 793 default: 794 assert(!"unknown debug output param"); 795 val = 0; 796 break; 797 } 798 799 _mesa_unlock_debug_state(ctx); 800 801 return val; 802 } 803 804 /** 805 * Query the pointer debug state specified by \p pname. This can be called 806 * _mesa_GetPointerv for example. 807 */ 808 void * 809 _mesa_get_debug_state_ptr(struct gl_context *ctx, GLenum pname) 810 { 811 void *val; 812 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx); 813 814 if (!debug) 815 return NULL; 816 817 switch (pname) { 818 case GL_DEBUG_CALLBACK_FUNCTION_ARB: 819 val = (void *) debug->Callback; 820 break; 821 case GL_DEBUG_CALLBACK_USER_PARAM_ARB: 822 val = (void *) debug->CallbackData; 823 break; 824 default: 825 assert(!"unknown debug output param"); 826 val = NULL; 827 break; 828 } 829 830 _mesa_unlock_debug_state(ctx); 831 832 return val; 833 } 834 835 /** 836 * Insert a debug message. The mutex is assumed to be locked, and will be 837 * unlocked by this call. 838 */ 839 static void 840 log_msg_locked_and_unlock(struct gl_context *ctx, 841 enum mesa_debug_source source, 842 enum mesa_debug_type type, GLuint id, 843 enum mesa_debug_severity severity, 844 GLint len, const char *buf) 845 { 846 struct gl_debug_state *debug = ctx->Debug; 847 848 if (!_mesa_debug_is_message_enabled(debug, source, type, id, severity)) { 849 _mesa_unlock_debug_state(ctx); 850 return; 851 } 852 853 if (ctx->Debug->Callback) { 854 /* Call the user's callback function */ 855 GLenum gl_source = debug_source_enums[source]; 856 GLenum gl_type = debug_type_enums[type]; 857 GLenum gl_severity = debug_severity_enums[severity]; 858 GLDEBUGPROC callback = ctx->Debug->Callback; 859 const void *data = ctx->Debug->CallbackData; 860 861 /* 862 * When ctx->Debug->SyncOutput is GL_FALSE, the client is prepared for 863 * unsynchronous calls. When it is GL_TRUE, we will not spawn threads. 864 * In either case, we can call the callback unlocked. 865 */ 866 _mesa_unlock_debug_state(ctx); 867 callback(gl_source, gl_type, id, gl_severity, len, buf, data); 868 } 869 else { 870 /* add debug message to queue */ 871 debug_log_message(ctx->Debug, source, type, id, severity, len, buf); 872 _mesa_unlock_debug_state(ctx); 873 } 874 } 875 876 /** 877 * Log a client or driver debug message. 878 */ 879 void 880 _mesa_log_msg(struct gl_context *ctx, enum mesa_debug_source source, 881 enum mesa_debug_type type, GLuint id, 882 enum mesa_debug_severity severity, GLint len, const char *buf) 883 { 884 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx); 885 886 if (!debug) 887 return; 888 889 log_msg_locked_and_unlock(ctx, source, type, id, severity, len, buf); 890 } 891 892 893 /** 894 * Verify that source, type, and severity are valid enums. 895 * 896 * The 'caller' param is used for handling values available 897 * only in glDebugMessageInsert or glDebugMessageControl 898 */ 899 static GLboolean 900 validate_params(struct gl_context *ctx, unsigned caller, 901 const char *callerstr, GLenum source, GLenum type, 902 GLenum severity) 903 { 904 #define INSERT 1 905 #define CONTROL 2 906 switch(source) { 907 case GL_DEBUG_SOURCE_APPLICATION_ARB: 908 case GL_DEBUG_SOURCE_THIRD_PARTY_ARB: 909 break; 910 case GL_DEBUG_SOURCE_API_ARB: 911 case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB: 912 case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB: 913 case GL_DEBUG_SOURCE_OTHER_ARB: 914 if (caller != INSERT) 915 break; 916 else 917 goto error; 918 case GL_DONT_CARE: 919 if (caller == CONTROL) 920 break; 921 else 922 goto error; 923 default: 924 goto error; 925 } 926 927 switch(type) { 928 case GL_DEBUG_TYPE_ERROR_ARB: 929 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB: 930 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB: 931 case GL_DEBUG_TYPE_PERFORMANCE_ARB: 932 case GL_DEBUG_TYPE_PORTABILITY_ARB: 933 case GL_DEBUG_TYPE_OTHER_ARB: 934 case GL_DEBUG_TYPE_MARKER: 935 case GL_DEBUG_TYPE_PUSH_GROUP: 936 case GL_DEBUG_TYPE_POP_GROUP: 937 break; 938 case GL_DONT_CARE: 939 if (caller == CONTROL) 940 break; 941 else 942 goto error; 943 default: 944 goto error; 945 } 946 947 switch(severity) { 948 case GL_DEBUG_SEVERITY_HIGH_ARB: 949 case GL_DEBUG_SEVERITY_MEDIUM_ARB: 950 case GL_DEBUG_SEVERITY_LOW_ARB: 951 case GL_DEBUG_SEVERITY_NOTIFICATION: 952 break; 953 case GL_DONT_CARE: 954 if (caller == CONTROL) 955 break; 956 else 957 goto error; 958 default: 959 goto error; 960 } 961 return GL_TRUE; 962 963 error: 964 _mesa_error(ctx, GL_INVALID_ENUM, "bad values passed to %s" 965 "(source=0x%x, type=0x%x, severity=0x%x)", callerstr, 966 source, type, severity); 967 968 return GL_FALSE; 969 } 970 971 972 static GLboolean 973 validate_length(struct gl_context *ctx, const char *callerstr, GLsizei length, 974 const GLchar *buf) 975 { 976 977 if (length < 0) { 978 GLsizei len = strlen(buf); 979 980 if (len >= MAX_DEBUG_MESSAGE_LENGTH) { 981 _mesa_error(ctx, GL_INVALID_VALUE, 982 "%s(null terminated string length=%d, is not less than " 983 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr, len, 984 MAX_DEBUG_MESSAGE_LENGTH); 985 return GL_FALSE; 986 } 987 } 988 989 if (length >= MAX_DEBUG_MESSAGE_LENGTH) { 990 _mesa_error(ctx, GL_INVALID_VALUE, 991 "%s(length=%d, which is not less than " 992 "GL_MAX_DEBUG_MESSAGE_LENGTH=%d)", callerstr, length, 993 MAX_DEBUG_MESSAGE_LENGTH); 994 return GL_FALSE; 995 } 996 997 return GL_TRUE; 998 } 999 1000 1001 void GLAPIENTRY 1002 _mesa_DebugMessageInsert(GLenum source, GLenum type, GLuint id, 1003 GLenum severity, GLint length, 1004 const GLchar *buf) 1005 { 1006 GET_CURRENT_CONTEXT(ctx); 1007 const char *callerstr; 1008 1009 if (_mesa_is_desktop_gl(ctx)) 1010 callerstr = "glDebugMessageInsert"; 1011 else 1012 callerstr = "glDebugMessageInsertKHR"; 1013 1014 if (!validate_params(ctx, INSERT, callerstr, source, type, severity)) 1015 return; /* GL_INVALID_ENUM */ 1016 1017 if (!validate_length(ctx, callerstr, length, buf)) 1018 return; /* GL_INVALID_VALUE */ 1019 1020 /* if length not specified, string will be null terminated: */ 1021 if (length < 0) 1022 length = strlen(buf); 1023 1024 _mesa_log_msg(ctx, gl_enum_to_debug_source(source), 1025 gl_enum_to_debug_type(type), id, 1026 gl_enum_to_debug_severity(severity), 1027 length, buf); 1028 1029 if (type == GL_DEBUG_TYPE_MARKER && ctx->Driver.EmitStringMarker) { 1030 ctx->Driver.EmitStringMarker(ctx, buf, length); 1031 } 1032 } 1033 1034 1035 GLuint GLAPIENTRY 1036 _mesa_GetDebugMessageLog(GLuint count, GLsizei logSize, GLenum *sources, 1037 GLenum *types, GLenum *ids, GLenum *severities, 1038 GLsizei *lengths, GLchar *messageLog) 1039 { 1040 GET_CURRENT_CONTEXT(ctx); 1041 struct gl_debug_state *debug; 1042 const char *callerstr; 1043 GLuint ret; 1044 1045 if (_mesa_is_desktop_gl(ctx)) 1046 callerstr = "glGetDebugMessageLog"; 1047 else 1048 callerstr = "glGetDebugMessageLogKHR"; 1049 1050 if (!messageLog) 1051 logSize = 0; 1052 1053 if (logSize < 0) { 1054 _mesa_error(ctx, GL_INVALID_VALUE, 1055 "%s(logSize=%d : logSize must not be negative)", 1056 callerstr, logSize); 1057 return 0; 1058 } 1059 1060 debug = _mesa_lock_debug_state(ctx); 1061 if (!debug) 1062 return 0; 1063 1064 for (ret = 0; ret < count; ret++) { 1065 const struct gl_debug_message *msg = debug_fetch_message(debug); 1066 GLsizei len; 1067 1068 if (!msg) 1069 break; 1070 1071 len = msg->length; 1072 if (len < 0) 1073 len = strlen(msg->message); 1074 1075 if (logSize < len+1 && messageLog != NULL) 1076 break; 1077 1078 if (messageLog) { 1079 assert(msg->message[len] == '\0'); 1080 (void) strncpy(messageLog, msg->message, (size_t)len+1); 1081 1082 messageLog += len+1; 1083 logSize -= len+1; 1084 } 1085 1086 if (lengths) 1087 *lengths++ = len+1; 1088 if (severities) 1089 *severities++ = debug_severity_enums[msg->severity]; 1090 if (sources) 1091 *sources++ = debug_source_enums[msg->source]; 1092 if (types) 1093 *types++ = debug_type_enums[msg->type]; 1094 if (ids) 1095 *ids++ = msg->id; 1096 1097 debug_delete_messages(debug, 1); 1098 } 1099 1100 _mesa_unlock_debug_state(ctx); 1101 1102 return ret; 1103 } 1104 1105 1106 void GLAPIENTRY 1107 _mesa_DebugMessageControl(GLenum gl_source, GLenum gl_type, 1108 GLenum gl_severity, GLsizei count, 1109 const GLuint *ids, GLboolean enabled) 1110 { 1111 GET_CURRENT_CONTEXT(ctx); 1112 enum mesa_debug_source source = gl_enum_to_debug_source(gl_source); 1113 enum mesa_debug_type type = gl_enum_to_debug_type(gl_type); 1114 enum mesa_debug_severity severity = gl_enum_to_debug_severity(gl_severity); 1115 const char *callerstr; 1116 struct gl_debug_state *debug; 1117 1118 if (_mesa_is_desktop_gl(ctx)) 1119 callerstr = "glDebugMessageControl"; 1120 else 1121 callerstr = "glDebugMessageControlKHR"; 1122 1123 if (count < 0) { 1124 _mesa_error(ctx, GL_INVALID_VALUE, 1125 "%s(count=%d : count must not be negative)", callerstr, 1126 count); 1127 return; 1128 } 1129 1130 if (!validate_params(ctx, CONTROL, callerstr, gl_source, gl_type, 1131 gl_severity)) 1132 return; /* GL_INVALID_ENUM */ 1133 1134 if (count && (gl_severity != GL_DONT_CARE || gl_type == GL_DONT_CARE 1135 || gl_source == GL_DONT_CARE)) { 1136 _mesa_error(ctx, GL_INVALID_OPERATION, 1137 "%s(When passing an array of ids, severity must be" 1138 " GL_DONT_CARE, and source and type must not be GL_DONT_CARE.", 1139 callerstr); 1140 return; 1141 } 1142 1143 debug = _mesa_lock_debug_state(ctx); 1144 if (!debug) 1145 return; 1146 1147 if (count) { 1148 GLsizei i; 1149 for (i = 0; i < count; i++) 1150 debug_set_message_enable(debug, source, type, ids[i], enabled); 1151 } 1152 else { 1153 debug_set_message_enable_all(debug, source, type, severity, enabled); 1154 } 1155 1156 _mesa_unlock_debug_state(ctx); 1157 } 1158 1159 1160 void GLAPIENTRY 1161 _mesa_DebugMessageCallback(GLDEBUGPROC callback, const void *userParam) 1162 { 1163 GET_CURRENT_CONTEXT(ctx); 1164 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx); 1165 if (debug) { 1166 debug->Callback = callback; 1167 debug->CallbackData = userParam; 1168 _mesa_unlock_debug_state(ctx); 1169 } 1170 } 1171 1172 1173 void GLAPIENTRY 1174 _mesa_PushDebugGroup(GLenum source, GLuint id, GLsizei length, 1175 const GLchar *message) 1176 { 1177 GET_CURRENT_CONTEXT(ctx); 1178 const char *callerstr; 1179 struct gl_debug_state *debug; 1180 struct gl_debug_message *emptySlot; 1181 1182 if (_mesa_is_desktop_gl(ctx)) 1183 callerstr = "glPushDebugGroup"; 1184 else 1185 callerstr = "glPushDebugGroupKHR"; 1186 1187 switch(source) { 1188 case GL_DEBUG_SOURCE_APPLICATION: 1189 case GL_DEBUG_SOURCE_THIRD_PARTY: 1190 break; 1191 default: 1192 _mesa_error(ctx, GL_INVALID_ENUM, "bad value passed to %s" 1193 "(source=0x%x)", callerstr, source); 1194 return; 1195 } 1196 1197 if (!validate_length(ctx, callerstr, length, message)) 1198 return; /* GL_INVALID_VALUE */ 1199 1200 if (length < 0) 1201 length = strlen(message); 1202 1203 debug = _mesa_lock_debug_state(ctx); 1204 if (!debug) 1205 return; 1206 1207 if (debug->CurrentGroup >= MAX_DEBUG_GROUP_STACK_DEPTH-1) { 1208 _mesa_unlock_debug_state(ctx); 1209 _mesa_error(ctx, GL_STACK_OVERFLOW, "%s", callerstr); 1210 return; 1211 } 1212 1213 /* pop reuses the message details from push so we store this */ 1214 emptySlot = debug_get_group_message(debug); 1215 debug_message_store(emptySlot, 1216 gl_enum_to_debug_source(source), 1217 gl_enum_to_debug_type(GL_DEBUG_TYPE_PUSH_GROUP), 1218 id, 1219 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION), 1220 length, message); 1221 1222 debug_push_group(debug); 1223 1224 log_msg_locked_and_unlock(ctx, 1225 gl_enum_to_debug_source(source), 1226 MESA_DEBUG_TYPE_PUSH_GROUP, id, 1227 MESA_DEBUG_SEVERITY_NOTIFICATION, length, 1228 message); 1229 } 1230 1231 1232 void GLAPIENTRY 1233 _mesa_PopDebugGroup(void) 1234 { 1235 GET_CURRENT_CONTEXT(ctx); 1236 const char *callerstr; 1237 struct gl_debug_state *debug; 1238 struct gl_debug_message *gdmessage, msg; 1239 1240 if (_mesa_is_desktop_gl(ctx)) 1241 callerstr = "glPopDebugGroup"; 1242 else 1243 callerstr = "glPopDebugGroupKHR"; 1244 1245 debug = _mesa_lock_debug_state(ctx); 1246 if (!debug) 1247 return; 1248 1249 if (debug->CurrentGroup <= 0) { 1250 _mesa_unlock_debug_state(ctx); 1251 _mesa_error(ctx, GL_STACK_UNDERFLOW, "%s", callerstr); 1252 return; 1253 } 1254 1255 debug_pop_group(debug); 1256 1257 /* make a shallow copy */ 1258 gdmessage = debug_get_group_message(debug); 1259 msg = *gdmessage; 1260 gdmessage->message = NULL; 1261 gdmessage->length = 0; 1262 1263 log_msg_locked_and_unlock(ctx, 1264 msg.source, 1265 gl_enum_to_debug_type(GL_DEBUG_TYPE_POP_GROUP), 1266 msg.id, 1267 gl_enum_to_debug_severity(GL_DEBUG_SEVERITY_NOTIFICATION), 1268 msg.length, msg.message); 1269 1270 debug_message_clear(&msg); 1271 } 1272 1273 1274 void 1275 _mesa_init_debug_output(struct gl_context *ctx) 1276 { 1277 simple_mtx_init(&ctx->DebugMutex, mtx_plain); 1278 1279 if (MESA_DEBUG_FLAGS & DEBUG_CONTEXT) { 1280 /* If the MESA_DEBUG env is set to "context", we'll turn on the 1281 * GL_CONTEXT_FLAG_DEBUG_BIT context flag and log debug output 1282 * messages to stderr (or whatever MESA_LOG_FILE points at). 1283 */ 1284 struct gl_debug_state *debug = _mesa_lock_debug_state(ctx); 1285 if (!debug) { 1286 return; 1287 } 1288 debug->DebugOutput = GL_TRUE; 1289 debug->LogToStderr = GL_TRUE; 1290 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT; 1291 _mesa_unlock_debug_state(ctx); 1292 } 1293 } 1294 1295 1296 void 1297 _mesa_free_errors_data(struct gl_context *ctx) 1298 { 1299 if (ctx->Debug) { 1300 debug_destroy(ctx->Debug); 1301 /* set to NULL just in case it is used before context is completely gone. */ 1302 ctx->Debug = NULL; 1303 } 1304 1305 simple_mtx_destroy(&ctx->DebugMutex); 1306 } 1307 1308 void GLAPIENTRY 1309 _mesa_StringMarkerGREMEDY(GLsizei len, const GLvoid *string) 1310 { 1311 GET_CURRENT_CONTEXT(ctx); 1312 if (ctx->Extensions.GREMEDY_string_marker) { 1313 /* if length not specified, string will be null terminated: */ 1314 if (len <= 0) 1315 len = strlen(string); 1316 ctx->Driver.EmitStringMarker(ctx, string, len); 1317 } else { 1318 _mesa_error(ctx, GL_INVALID_OPERATION, "StringMarkerGREMEDY"); 1319 } 1320 } 1321