1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
|
/*
* Copyright (C) 2013-2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
WebInspector.DebuggerManager = class DebuggerManager extends WebInspector.Object
{
constructor()
{
super();
DebuggerAgent.enable();
WebInspector.notifications.addEventListener(WebInspector.Notification.DebugUIEnabledDidChange, this._debugUIEnabledDidChange, this);
WebInspector.Breakpoint.addEventListener(WebInspector.Breakpoint.Event.DisplayLocationDidChange, this._breakpointDisplayLocationDidChange, this);
WebInspector.Breakpoint.addEventListener(WebInspector.Breakpoint.Event.DisabledStateDidChange, this._breakpointDisabledStateDidChange, this);
WebInspector.Breakpoint.addEventListener(WebInspector.Breakpoint.Event.ConditionDidChange, this._breakpointEditablePropertyDidChange, this);
WebInspector.Breakpoint.addEventListener(WebInspector.Breakpoint.Event.IgnoreCountDidChange, this._breakpointEditablePropertyDidChange, this);
WebInspector.Breakpoint.addEventListener(WebInspector.Breakpoint.Event.AutoContinueDidChange, this._breakpointEditablePropertyDidChange, this);
WebInspector.Breakpoint.addEventListener(WebInspector.Breakpoint.Event.ActionsDidChange, this._breakpointEditablePropertyDidChange, this);
WebInspector.timelineManager.addEventListener(WebInspector.TimelineManager.Event.CapturingWillStart, this._timelineCapturingWillStart, this);
WebInspector.timelineManager.addEventListener(WebInspector.TimelineManager.Event.CapturingStopped, this._timelineCapturingStopped, this);
WebInspector.targetManager.addEventListener(WebInspector.TargetManager.Event.TargetRemoved, this._targetRemoved, this);
WebInspector.Frame.addEventListener(WebInspector.Frame.Event.MainResourceDidChange, this._mainResourceDidChange, this);
this._breakpointsSetting = new WebInspector.Setting("breakpoints", []);
this._breakpointsEnabledSetting = new WebInspector.Setting("breakpoints-enabled", true);
this._allExceptionsBreakpointEnabledSetting = new WebInspector.Setting("break-on-all-exceptions", false);
this._allUncaughtExceptionsBreakpointEnabledSetting = new WebInspector.Setting("break-on-all-uncaught-exceptions", false);
this._assertionsBreakpointEnabledSetting = new WebInspector.Setting("break-on-assertions", false);
this._asyncStackTraceDepthSetting = new WebInspector.Setting("async-stack-trace-depth", 200);
let specialBreakpointLocation = new WebInspector.SourceCodeLocation(null, Infinity, Infinity);
this._allExceptionsBreakpoint = new WebInspector.Breakpoint(specialBreakpointLocation, !this._allExceptionsBreakpointEnabledSetting.value);
this._allExceptionsBreakpoint.resolved = true;
this._allUncaughtExceptionsBreakpoint = new WebInspector.Breakpoint(specialBreakpointLocation, !this._allUncaughtExceptionsBreakpointEnabledSetting.value);
this._assertionsBreakpoint = new WebInspector.Breakpoint(specialBreakpointLocation, !this._assertionsBreakpointEnabledSetting.value);
this._assertionsBreakpoint.resolved = true;
this._breakpoints = [];
this._breakpointContentIdentifierMap = new Map;
this._breakpointScriptIdentifierMap = new Map;
this._breakpointIdMap = new Map;
this._breakOnExceptionsState = "none";
this._updateBreakOnExceptionsState();
this._nextBreakpointActionIdentifier = 1;
this._activeCallFrame = null;
this._internalWebKitScripts = [];
this._targetDebuggerDataMap = new Map;
this._targetDebuggerDataMap.set(WebInspector.mainTarget, new WebInspector.DebuggerData(WebInspector.mainTarget));
// Restore the correct breakpoints enabled setting if Web Inspector had
// previously been left in a state where breakpoints were temporarily disabled.
this._temporarilyDisabledBreakpointsRestoreSetting = new WebInspector.Setting("temporarily-disabled-breakpoints-restore", null);
if (this._temporarilyDisabledBreakpointsRestoreSetting.value !== null) {
this._breakpointsEnabledSetting.value = this._temporarilyDisabledBreakpointsRestoreSetting.value;
this._temporarilyDisabledBreakpointsRestoreSetting.value = null;
}
DebuggerAgent.setBreakpointsActive(this._breakpointsEnabledSetting.value);
DebuggerAgent.setPauseOnExceptions(this._breakOnExceptionsState);
// COMPATIBILITY (iOS 10): DebuggerAgent.setPauseOnAssertions did not exist yet.
if (DebuggerAgent.setPauseOnAssertions)
DebuggerAgent.setPauseOnAssertions(this._assertionsBreakpointEnabledSetting.value);
// COMPATIBILITY (iOS 10): Debugger.setAsyncStackTraceDepth did not exist yet.
if (DebuggerAgent.setAsyncStackTraceDepth)
DebuggerAgent.setAsyncStackTraceDepth(this._asyncStackTraceDepthSetting.value);
this._ignoreBreakpointDisplayLocationDidChangeEvent = false;
function restoreBreakpointsSoon() {
this._restoringBreakpoints = true;
for (let cookie of this._breakpointsSetting.value)
this.addBreakpoint(new WebInspector.Breakpoint(cookie));
this._restoringBreakpoints = false;
}
// Ensure that all managers learn about restored breakpoints,
// regardless of their initialization order.
setTimeout(restoreBreakpointsSoon.bind(this), 0);
}
// Public
get paused()
{
for (let [target, targetData] of this._targetDebuggerDataMap) {
if (targetData.paused)
return true;
}
return false;
}
get activeCallFrame()
{
return this._activeCallFrame;
}
set activeCallFrame(callFrame)
{
if (callFrame === this._activeCallFrame)
return;
this._activeCallFrame = callFrame || null;
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.ActiveCallFrameDidChange);
}
dataForTarget(target)
{
let targetData = this._targetDebuggerDataMap.get(target);
if (targetData)
return targetData;
targetData = new WebInspector.DebuggerData(target);
this._targetDebuggerDataMap.set(target, targetData);
return targetData;
}
get allExceptionsBreakpoint()
{
return this._allExceptionsBreakpoint;
}
get allUncaughtExceptionsBreakpoint()
{
return this._allUncaughtExceptionsBreakpoint;
}
get assertionsBreakpoint()
{
return this._assertionsBreakpoint;
}
get breakpoints()
{
return this._breakpoints;
}
breakpointForIdentifier(id)
{
return this._breakpointIdMap.get(id) || null;
}
breakpointsForSourceCode(sourceCode)
{
console.assert(sourceCode instanceof WebInspector.Resource || sourceCode instanceof WebInspector.Script);
if (sourceCode instanceof WebInspector.SourceMapResource) {
let originalSourceCodeBreakpoints = this.breakpointsForSourceCode(sourceCode.sourceMap.originalSourceCode);
return originalSourceCodeBreakpoints.filter(function(breakpoint) {
return breakpoint.sourceCodeLocation.displaySourceCode === sourceCode;
});
}
let contentIdentifierBreakpoints = this._breakpointContentIdentifierMap.get(sourceCode.contentIdentifier);
if (contentIdentifierBreakpoints) {
this._associateBreakpointsWithSourceCode(contentIdentifierBreakpoints, sourceCode);
return contentIdentifierBreakpoints;
}
if (sourceCode instanceof WebInspector.Script) {
let scriptIdentifierBreakpoints = this._breakpointScriptIdentifierMap.get(sourceCode.id);
if (scriptIdentifierBreakpoints) {
this._associateBreakpointsWithSourceCode(scriptIdentifierBreakpoints, sourceCode);
return scriptIdentifierBreakpoints;
}
}
return [];
}
isBreakpointRemovable(breakpoint)
{
return breakpoint !== this._allExceptionsBreakpoint
&& breakpoint !== this._allUncaughtExceptionsBreakpoint
&& breakpoint !== this._assertionsBreakpoint;
}
isBreakpointEditable(breakpoint)
{
return this.isBreakpointRemovable(breakpoint);
}
get breakpointsEnabled()
{
return this._breakpointsEnabledSetting.value;
}
set breakpointsEnabled(enabled)
{
if (this._breakpointsEnabledSetting.value === enabled)
return;
console.assert(!(enabled && this.breakpointsDisabledTemporarily), "Should not enable breakpoints when we are temporarily disabling breakpoints.");
if (enabled && this.breakpointsDisabledTemporarily)
return;
this._breakpointsEnabledSetting.value = enabled;
this._updateBreakOnExceptionsState();
for (let target of WebInspector.targets) {
target.DebuggerAgent.setBreakpointsActive(enabled);
target.DebuggerAgent.setPauseOnExceptions(this._breakOnExceptionsState);
}
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.BreakpointsEnabledDidChange);
}
get breakpointsDisabledTemporarily()
{
return this._temporarilyDisabledBreakpointsRestoreSetting.value !== null;
}
scriptForIdentifier(id, target)
{
console.assert(target instanceof WebInspector.Target);
return this.dataForTarget(target).scriptForIdentifier(id);
}
scriptsForURL(url, target)
{
// FIXME: This may not be safe. A Resource's URL may differ from a Script's URL.
console.assert(target instanceof WebInspector.Target);
return this.dataForTarget(target).scriptsForURL(url);
}
get searchableScripts()
{
return this.knownNonResourceScripts.filter((script) => !!script.contentIdentifier);
}
get knownNonResourceScripts()
{
let knownScripts = [];
for (let [target, targetData] of this._targetDebuggerDataMap) {
for (let script of targetData.scripts) {
if (script.resource)
continue;
if (!WebInspector.isDebugUIEnabled() && isWebKitInternalScript(script.sourceURL))
continue;
knownScripts.push(script);
}
}
return knownScripts;
}
get asyncStackTraceDepth()
{
return this._asyncStackTraceDepthSetting.value;
}
set asyncStackTraceDepth(x)
{
if (this._asyncStackTraceDepthSetting.value === x)
return;
this._asyncStackTraceDepthSetting.value = x;
for (let target of WebInspector.targets)
target.DebuggerAgent.setAsyncStackTraceDepth(this._asyncStackTraceDepthSetting.value);
}
pause()
{
if (this.paused)
return Promise.resolve();
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.WaitingToPause);
let listener = new WebInspector.EventListener(this, true);
let managerResult = new Promise(function(resolve, reject) {
listener.connect(WebInspector.debuggerManager, WebInspector.DebuggerManager.Event.Paused, resolve);
});
let promises = [];
for (let [target, targetData] of this._targetDebuggerDataMap)
promises.push(targetData.pauseIfNeeded());
return Promise.all([managerResult, ...promises]);
}
resume()
{
if (!this.paused)
return Promise.resolve();
let listener = new WebInspector.EventListener(this, true);
let managerResult = new Promise(function(resolve, reject) {
listener.connect(WebInspector.debuggerManager, WebInspector.DebuggerManager.Event.Resumed, resolve);
});
let promises = [];
for (let [target, targetData] of this._targetDebuggerDataMap)
promises.push(targetData.resumeIfNeeded());
return Promise.all([managerResult, ...promises]);
}
stepOver()
{
if (!this.paused)
return Promise.reject(new Error("Cannot step over because debugger is not paused."));
let listener = new WebInspector.EventListener(this, true);
let managerResult = new Promise(function(resolve, reject) {
listener.connect(WebInspector.debuggerManager, WebInspector.DebuggerManager.Event.ActiveCallFrameDidChange, resolve);
});
let protocolResult = this._activeCallFrame.target.DebuggerAgent.stepOver()
.catch(function(error) {
listener.disconnect();
console.error("DebuggerManager.stepOver failed: ", error);
throw error;
});
return Promise.all([managerResult, protocolResult]);
}
stepInto()
{
if (!this.paused)
return Promise.reject(new Error("Cannot step into because debugger is not paused."));
let listener = new WebInspector.EventListener(this, true);
let managerResult = new Promise(function(resolve, reject) {
listener.connect(WebInspector.debuggerManager, WebInspector.DebuggerManager.Event.ActiveCallFrameDidChange, resolve);
});
let protocolResult = this._activeCallFrame.target.DebuggerAgent.stepInto()
.catch(function(error) {
listener.disconnect();
console.error("DebuggerManager.stepInto failed: ", error);
throw error;
});
return Promise.all([managerResult, protocolResult]);
}
stepOut()
{
if (!this.paused)
return Promise.reject(new Error("Cannot step out because debugger is not paused."));
let listener = new WebInspector.EventListener(this, true);
let managerResult = new Promise(function(resolve, reject) {
listener.connect(WebInspector.debuggerManager, WebInspector.DebuggerManager.Event.ActiveCallFrameDidChange, resolve);
});
let protocolResult = this._activeCallFrame.target.DebuggerAgent.stepOut()
.catch(function(error) {
listener.disconnect();
console.error("DebuggerManager.stepOut failed: ", error);
throw error;
});
return Promise.all([managerResult, protocolResult]);
}
continueUntilNextRunLoop(target)
{
return this.dataForTarget(target).continueUntilNextRunLoop();
}
continueToLocation(script, lineNumber, columnNumber)
{
return script.target.DebuggerAgent.continueToLocation({scriptId: script.id, lineNumber, columnNumber});
}
addBreakpoint(breakpoint, shouldSpeculativelyResolve)
{
console.assert(breakpoint instanceof WebInspector.Breakpoint);
if (!breakpoint)
return;
if (breakpoint.contentIdentifier) {
let contentIdentifierBreakpoints = this._breakpointContentIdentifierMap.get(breakpoint.contentIdentifier);
if (!contentIdentifierBreakpoints) {
contentIdentifierBreakpoints = [];
this._breakpointContentIdentifierMap.set(breakpoint.contentIdentifier, contentIdentifierBreakpoints);
}
contentIdentifierBreakpoints.push(breakpoint);
}
if (breakpoint.scriptIdentifier) {
let scriptIdentifierBreakpoints = this._breakpointScriptIdentifierMap.get(breakpoint.scriptIdentifier);
if (!scriptIdentifierBreakpoints) {
scriptIdentifierBreakpoints = [];
this._breakpointScriptIdentifierMap.set(breakpoint.scriptIdentifier, scriptIdentifierBreakpoints);
}
scriptIdentifierBreakpoints.push(breakpoint);
}
this._breakpoints.push(breakpoint);
if (!breakpoint.disabled) {
const specificTarget = undefined;
this._setBreakpoint(breakpoint, specificTarget, () => {
if (shouldSpeculativelyResolve)
breakpoint.resolved = true;
});
}
this._saveBreakpoints();
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.BreakpointAdded, {breakpoint});
}
removeBreakpoint(breakpoint)
{
console.assert(breakpoint instanceof WebInspector.Breakpoint);
if (!breakpoint)
return;
console.assert(this.isBreakpointRemovable(breakpoint));
if (!this.isBreakpointRemovable(breakpoint))
return;
this._breakpoints.remove(breakpoint);
if (breakpoint.identifier)
this._removeBreakpoint(breakpoint);
if (breakpoint.contentIdentifier) {
let contentIdentifierBreakpoints = this._breakpointContentIdentifierMap.get(breakpoint.contentIdentifier);
if (contentIdentifierBreakpoints) {
contentIdentifierBreakpoints.remove(breakpoint);
if (!contentIdentifierBreakpoints.length)
this._breakpointContentIdentifierMap.delete(breakpoint.contentIdentifier);
}
}
if (breakpoint.scriptIdentifier) {
let scriptIdentifierBreakpoints = this._breakpointScriptIdentifierMap.get(breakpoint.scriptIdentifier);
if (scriptIdentifierBreakpoints) {
scriptIdentifierBreakpoints.remove(breakpoint);
if (!scriptIdentifierBreakpoints.length)
this._breakpointScriptIdentifierMap.delete(breakpoint.scriptIdentifier);
}
}
// Disable the breakpoint first, so removing actions doesn't re-add the breakpoint.
breakpoint.disabled = true;
breakpoint.clearActions();
this._saveBreakpoints();
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.BreakpointRemoved, {breakpoint});
}
nextBreakpointActionIdentifier()
{
return this._nextBreakpointActionIdentifier++;
}
initializeTarget(target)
{
let DebuggerAgent = target.DebuggerAgent;
let targetData = this.dataForTarget(target);
// Initialize global state.
DebuggerAgent.enable();
DebuggerAgent.setBreakpointsActive(this._breakpointsEnabledSetting.value);
DebuggerAgent.setPauseOnAssertions(this._assertionsBreakpointEnabledSetting.value);
DebuggerAgent.setPauseOnExceptions(this._breakOnExceptionsState);
DebuggerAgent.setAsyncStackTraceDepth(this._asyncStackTraceDepthSetting.value);
if (this.paused)
targetData.pauseIfNeeded();
// Initialize breakpoints.
this._restoringBreakpoints = true;
for (let breakpoint of this._breakpoints) {
if (breakpoint.disabled)
continue;
if (!breakpoint.contentIdentifier)
continue;
this._setBreakpoint(breakpoint, target);
}
this._restoringBreakpoints = false;
}
// Protected (Called from WebInspector.DebuggerObserver)
breakpointResolved(target, breakpointIdentifier, location)
{
// Called from WebInspector.DebuggerObserver.
let breakpoint = this._breakpointIdMap.get(breakpointIdentifier);
console.assert(breakpoint);
if (!breakpoint)
return;
console.assert(breakpoint.identifier === breakpointIdentifier);
if (!breakpoint.sourceCodeLocation.sourceCode) {
let sourceCodeLocation = this._sourceCodeLocationFromPayload(target, location);
breakpoint.sourceCodeLocation.sourceCode = sourceCodeLocation.sourceCode;
}
breakpoint.resolved = true;
}
reset()
{
// Called from WebInspector.DebuggerObserver.
let wasPaused = this.paused;
WebInspector.Script.resetUniqueDisplayNameNumbers();
this._internalWebKitScripts = [];
this._targetDebuggerDataMap.clear();
this._ignoreBreakpointDisplayLocationDidChangeEvent = true;
// Mark all the breakpoints as unresolved. They will be reported as resolved when
// breakpointResolved is called as the page loads.
for (let breakpoint of this._breakpoints) {
breakpoint.resolved = false;
if (breakpoint.sourceCodeLocation.sourceCode)
breakpoint.sourceCodeLocation.sourceCode = null;
}
this._ignoreBreakpointDisplayLocationDidChangeEvent = false;
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.ScriptsCleared);
if (wasPaused)
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.Resumed);
}
debuggerDidPause(target, callFramesPayload, reason, data, asyncStackTracePayload)
{
// Called from WebInspector.DebuggerObserver.
if (this._delayedResumeTimeout) {
clearTimeout(this._delayedResumeTimeout);
this._delayedResumeTimeout = undefined;
}
let wasPaused = this.paused;
let targetData = this._targetDebuggerDataMap.get(target);
let callFrames = [];
let pauseReason = this._pauseReasonFromPayload(reason);
let pauseData = data || null;
for (var i = 0; i < callFramesPayload.length; ++i) {
var callFramePayload = callFramesPayload[i];
var sourceCodeLocation = this._sourceCodeLocationFromPayload(target, callFramePayload.location);
// FIXME: There may be useful call frames without a source code location (native callframes), should we include them?
if (!sourceCodeLocation)
continue;
if (!sourceCodeLocation.sourceCode)
continue;
// Exclude the case where the call frame is in the inspector code.
if (!WebInspector.isDebugUIEnabled() && isWebKitInternalScript(sourceCodeLocation.sourceCode.sourceURL))
continue;
let scopeChain = this._scopeChainFromPayload(target, callFramePayload.scopeChain);
let callFrame = WebInspector.CallFrame.fromDebuggerPayload(target, callFramePayload, scopeChain, sourceCodeLocation);
callFrames.push(callFrame);
}
let activeCallFrame = callFrames[0];
if (!activeCallFrame) {
// FIXME: This may not be safe for multiple threads/targets.
// This indicates we were pausing in internal scripts only (Injected Scripts).
// Just resume and skip past this pause. We should be fixing the backend to
// not send such pauses.
if (wasPaused)
target.DebuggerAgent.continueUntilNextRunLoop();
else
target.DebuggerAgent.resume();
this._didResumeInternal(target);
return;
}
let asyncStackTrace = WebInspector.StackTrace.fromPayload(target, asyncStackTracePayload);
targetData.updateForPause(callFrames, pauseReason, pauseData, asyncStackTrace);
// Pause other targets because at least one target has paused.
// FIXME: Should this be done on the backend?
for (let [otherTarget, otherTargetData] of this._targetDebuggerDataMap)
otherTargetData.pauseIfNeeded();
let activeCallFrameDidChange = this._activeCallFrame && this._activeCallFrame.target === target;
if (activeCallFrameDidChange)
this._activeCallFrame = activeCallFrame;
else if (!wasPaused) {
this._activeCallFrame = activeCallFrame;
activeCallFrameDidChange = true;
}
if (!wasPaused)
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.Paused);
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.CallFramesDidChange, {target});
if (activeCallFrameDidChange)
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.ActiveCallFrameDidChange);
}
debuggerDidResume(target)
{
// Called from WebInspector.DebuggerObserver.
// COMPATIBILITY (iOS 10): Debugger.resumed event was ambiguous. When stepping
// we would receive a Debugger.resumed and we would not know if it really meant
// the backend resumed or would pause again due to a step. Legacy backends wait
// 50ms, and treat it as a real resume if we haven't paused in that time frame.
// This delay ensures the user interface does not flash between brief steps
// or successive breakpoints.
if (!DebuggerAgent.setPauseOnAssertions) {
this._delayedResumeTimeout = setTimeout(this._didResumeInternal.bind(this, target), 50);
return;
}
this._didResumeInternal(target);
}
playBreakpointActionSound(breakpointActionIdentifier)
{
// Called from WebInspector.DebuggerObserver.
InspectorFrontendHost.beep();
}
scriptDidParse(target, scriptIdentifier, url, startLine, startColumn, endLine, endColumn, isModule, isContentScript, sourceURL, sourceMapURL)
{
// Called from WebInspector.DebuggerObserver.
// Don't add the script again if it is already known.
let targetData = this.dataForTarget(target);
let existingScript = targetData.scriptForIdentifier(scriptIdentifier);
if (existingScript) {
console.assert(existingScript.url === (url || null));
console.assert(existingScript.range.startLine === startLine);
console.assert(existingScript.range.startColumn === startColumn);
console.assert(existingScript.range.endLine === endLine);
console.assert(existingScript.range.endColumn === endColumn);
return;
}
if (!WebInspector.isDebugUIEnabled() && isWebKitInternalScript(sourceURL))
return;
let range = new WebInspector.TextRange(startLine, startColumn, endLine, endColumn);
let sourceType = isModule ? WebInspector.Script.SourceType.Module : WebInspector.Script.SourceType.Program;
let script = new WebInspector.Script(target, scriptIdentifier, range, url, sourceType, isContentScript, sourceURL, sourceMapURL);
targetData.addScript(script);
if (target !== WebInspector.mainTarget && !target.mainResource) {
// FIXME: <https://webkit.org/b/164427> Web Inspector: WorkerTarget's mainResource should be a Resource not a Script
// We make the main resource of a WorkerTarget the Script instead of the Resource
// because the frontend may not be informed of the Resource. We should guarantee
// the frontend is informed of the Resource.
if (script.url === target.name) {
target.mainResource = script;
if (script.resource)
target.resourceCollection.remove(script.resource);
}
}
if (isWebKitInternalScript(script.sourceURL)) {
this._internalWebKitScripts.push(script);
if (!WebInspector.isDebugUIEnabled())
return;
}
// Console expressions are not added to the UI by default.
if (isWebInspectorConsoleEvaluationScript(script.sourceURL))
return;
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.ScriptAdded, {script});
if (target !== WebInspector.mainTarget && !script.isMainResource() && !script.resource)
target.addScript(script);
}
// Private
_sourceCodeLocationFromPayload(target, payload)
{
let targetData = this.dataForTarget(target);
let script = targetData.scriptForIdentifier(payload.scriptId);
if (!script)
return null;
return script.createSourceCodeLocation(payload.lineNumber, payload.columnNumber);
}
_scopeChainFromPayload(target, payload)
{
let scopeChain = [];
for (let i = 0; i < payload.length; ++i)
scopeChain.push(this._scopeChainNodeFromPayload(target, payload[i]));
return scopeChain;
}
_scopeChainNodeFromPayload(target, payload)
{
var type = null;
switch (payload.type) {
case DebuggerAgent.ScopeType.Global:
type = WebInspector.ScopeChainNode.Type.Global;
break;
case DebuggerAgent.ScopeType.With:
type = WebInspector.ScopeChainNode.Type.With;
break;
case DebuggerAgent.ScopeType.Closure:
type = WebInspector.ScopeChainNode.Type.Closure;
break;
case DebuggerAgent.ScopeType.Catch:
type = WebInspector.ScopeChainNode.Type.Catch;
break;
case DebuggerAgent.ScopeType.FunctionName:
type = WebInspector.ScopeChainNode.Type.FunctionName;
break;
case DebuggerAgent.ScopeType.NestedLexical:
type = WebInspector.ScopeChainNode.Type.Block;
break;
case DebuggerAgent.ScopeType.GlobalLexicalEnvironment:
type = WebInspector.ScopeChainNode.Type.GlobalLexicalEnvironment;
break;
// COMPATIBILITY (iOS 9): Debugger.ScopeType.Local used to be provided by the backend.
// Newer backends no longer send this enum value, it should be computed by the frontend.
// Map this to "Closure" type. The frontend can recalculate this when needed.
case DebuggerAgent.ScopeType.Local:
type = WebInspector.ScopeChainNode.Type.Closure;
break;
default:
console.error("Unknown type: " + payload.type);
}
let object = WebInspector.RemoteObject.fromPayload(payload.object, target);
return new WebInspector.ScopeChainNode(type, [object], payload.name, payload.location, payload.empty);
}
_pauseReasonFromPayload(payload)
{
// FIXME: Handle other backend pause reasons.
switch (payload) {
case DebuggerAgent.PausedReason.Assert:
return WebInspector.DebuggerManager.PauseReason.Assertion;
case DebuggerAgent.PausedReason.Breakpoint:
return WebInspector.DebuggerManager.PauseReason.Breakpoint;
case DebuggerAgent.PausedReason.CSPViolation:
return WebInspector.DebuggerManager.PauseReason.CSPViolation;
case DebuggerAgent.PausedReason.DebuggerStatement:
return WebInspector.DebuggerManager.PauseReason.DebuggerStatement;
case DebuggerAgent.PausedReason.Exception:
return WebInspector.DebuggerManager.PauseReason.Exception;
case DebuggerAgent.PausedReason.PauseOnNextStatement:
return WebInspector.DebuggerManager.PauseReason.PauseOnNextStatement;
default:
return WebInspector.DebuggerManager.PauseReason.Other;
}
}
_debuggerBreakpointActionType(type)
{
switch (type) {
case WebInspector.BreakpointAction.Type.Log:
return DebuggerAgent.BreakpointActionType.Log;
case WebInspector.BreakpointAction.Type.Evaluate:
return DebuggerAgent.BreakpointActionType.Evaluate;
case WebInspector.BreakpointAction.Type.Sound:
return DebuggerAgent.BreakpointActionType.Sound;
case WebInspector.BreakpointAction.Type.Probe:
return DebuggerAgent.BreakpointActionType.Probe;
default:
console.assert(false);
return DebuggerAgent.BreakpointActionType.Log;
}
}
_debuggerBreakpointOptions(breakpoint)
{
const templatePlaceholderRegex = /\$\{.*?\}/;
let options = breakpoint.options;
let invalidActions = [];
for (let action of options.actions) {
if (action.type !== WebInspector.BreakpointAction.Type.Log)
continue;
if (!templatePlaceholderRegex.test(action.data))
continue;
let lexer = new WebInspector.BreakpointLogMessageLexer;
let tokens = lexer.tokenize(action.data);
if (!tokens) {
invalidActions.push(action);
continue;
}
let templateLiteral = tokens.reduce((text, token) => {
if (token.type === WebInspector.BreakpointLogMessageLexer.TokenType.PlainText)
return text + token.data.escapeCharacters("`\\");
if (token.type === WebInspector.BreakpointLogMessageLexer.TokenType.Expression)
return text + "${" + token.data + "}";
return text;
}, "");
action.data = "console.log(`" + templateLiteral + "`)";
action.type = WebInspector.BreakpointAction.Type.Evaluate;
}
const onlyFirst = true;
for (let invalidAction of invalidActions)
options.actions.remove(invalidAction, onlyFirst);
return options;
}
_setBreakpoint(breakpoint, specificTarget, callback)
{
console.assert(!breakpoint.disabled);
if (breakpoint.disabled)
return;
if (!this._restoringBreakpoints && !this.breakpointsDisabledTemporarily) {
// Enable breakpoints since a breakpoint is being set. This eliminates
// a multi-step process for the user that can be confusing.
this.breakpointsEnabled = true;
}
function didSetBreakpoint(target, error, breakpointIdentifier, locations)
{
if (error)
return;
this._breakpointIdMap.set(breakpointIdentifier, breakpoint);
breakpoint.identifier = breakpointIdentifier;
// Debugger.setBreakpoint returns a single location.
if (!(locations instanceof Array))
locations = [locations];
for (let location of locations)
this.breakpointResolved(target, breakpointIdentifier, location);
if (typeof callback === "function")
callback();
}
// The breakpoint will be resolved again by calling DebuggerAgent, so mark it as unresolved.
// If something goes wrong it will stay unresolved and show up as such in the user interface.
// When setting for a new target, don't change the resolved target.
if (!specificTarget)
breakpoint.resolved = false;
// Convert BreakpointAction types to DebuggerAgent protocol types.
// NOTE: Breakpoint.options returns new objects each time, so it is safe to modify.
// COMPATIBILITY (iOS 7): Debugger.BreakpointActionType did not exist yet.
let options;
if (DebuggerAgent.BreakpointActionType) {
options = this._debuggerBreakpointOptions(breakpoint);
if (options.actions.length) {
for (let action of options.actions)
action.type = this._debuggerBreakpointActionType(action.type);
}
}
// COMPATIBILITY (iOS 7): iOS 7 and earlier, DebuggerAgent.setBreakpoint* took a "condition" string argument.
// This has been replaced with an "options" BreakpointOptions object.
if (breakpoint.contentIdentifier) {
let targets = specificTarget ? [specificTarget] : WebInspector.targets;
for (let target of targets) {
target.DebuggerAgent.setBreakpointByUrl.invoke({
lineNumber: breakpoint.sourceCodeLocation.lineNumber,
url: breakpoint.contentIdentifier,
urlRegex: undefined,
columnNumber: breakpoint.sourceCodeLocation.columnNumber,
condition: breakpoint.condition,
options
}, didSetBreakpoint.bind(this, target), target.DebuggerAgent);
}
} else if (breakpoint.scriptIdentifier) {
let target = breakpoint.target;
target.DebuggerAgent.setBreakpoint.invoke({
location: {scriptId: breakpoint.scriptIdentifier, lineNumber: breakpoint.sourceCodeLocation.lineNumber, columnNumber: breakpoint.sourceCodeLocation.columnNumber},
condition: breakpoint.condition,
options
}, didSetBreakpoint.bind(this, target), target.DebuggerAgent);
}
}
_removeBreakpoint(breakpoint, callback)
{
if (!breakpoint.identifier)
return;
function didRemoveBreakpoint(error)
{
if (error)
console.error(error);
this._breakpointIdMap.delete(breakpoint.identifier);
breakpoint.identifier = null;
// Don't reset resolved here since we want to keep disabled breakpoints looking like they
// are resolved in the user interface. They will get marked as unresolved in reset.
if (typeof callback === "function")
callback();
}
if (breakpoint.contentIdentifier) {
for (let target of WebInspector.targets)
target.DebuggerAgent.removeBreakpoint(breakpoint.identifier, didRemoveBreakpoint.bind(this));
} else if (breakpoint.scriptIdentifier) {
let target = breakpoint.target;
target.DebuggerAgent.removeBreakpoint(breakpoint.identifier, didRemoveBreakpoint.bind(this));
}
}
_breakpointDisplayLocationDidChange(event)
{
if (this._ignoreBreakpointDisplayLocationDidChangeEvent)
return;
let breakpoint = event.target;
if (!breakpoint.identifier || breakpoint.disabled)
return;
// Remove the breakpoint with its old id.
this._removeBreakpoint(breakpoint, breakpointRemoved.bind(this));
function breakpointRemoved()
{
// Add the breakpoint at its new lineNumber and get a new id.
this._setBreakpoint(breakpoint);
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.BreakpointMoved, {breakpoint});
}
}
_breakpointDisabledStateDidChange(event)
{
this._saveBreakpoints();
let breakpoint = event.target;
if (breakpoint === this._allExceptionsBreakpoint) {
if (!breakpoint.disabled && !this.breakpointsDisabledTemporarily)
this.breakpointsEnabled = true;
this._allExceptionsBreakpointEnabledSetting.value = !breakpoint.disabled;
this._updateBreakOnExceptionsState();
for (let target of WebInspector.targets)
target.DebuggerAgent.setPauseOnExceptions(this._breakOnExceptionsState);
return;
}
if (breakpoint === this._allUncaughtExceptionsBreakpoint) {
if (!breakpoint.disabled && !this.breakpointsDisabledTemporarily)
this.breakpointsEnabled = true;
this._allUncaughtExceptionsBreakpointEnabledSetting.value = !breakpoint.disabled;
this._updateBreakOnExceptionsState();
for (let target of WebInspector.targets)
target.DebuggerAgent.setPauseOnExceptions(this._breakOnExceptionsState);
return;
}
if (breakpoint === this._assertionsBreakpoint) {
if (!breakpoint.disabled && !this.breakpointsDisabledTemporarily)
this.breakpointsEnabled = true;
this._assertionsBreakpointEnabledSetting.value = !breakpoint.disabled;
for (let target of WebInspector.targets)
target.DebuggerAgent.setPauseOnAssertions(this._assertionsBreakpointEnabledSetting.value);
return;
}
if (breakpoint.disabled)
this._removeBreakpoint(breakpoint);
else
this._setBreakpoint(breakpoint);
}
_breakpointEditablePropertyDidChange(event)
{
this._saveBreakpoints();
let breakpoint = event.target;
if (breakpoint.disabled)
return;
console.assert(this.isBreakpointEditable(breakpoint));
if (!this.isBreakpointEditable(breakpoint))
return;
// Remove the breakpoint with its old id.
this._removeBreakpoint(breakpoint, breakpointRemoved.bind(this));
function breakpointRemoved()
{
// Add the breakpoint with its new properties and get a new id.
this._setBreakpoint(breakpoint);
}
}
_startDisablingBreakpointsTemporarily()
{
console.assert(!this.breakpointsDisabledTemporarily, "Already temporarily disabling breakpoints.");
if (this.breakpointsDisabledTemporarily)
return;
this._temporarilyDisabledBreakpointsRestoreSetting.value = this._breakpointsEnabledSetting.value;
this.breakpointsEnabled = false;
}
_stopDisablingBreakpointsTemporarily()
{
console.assert(this.breakpointsDisabledTemporarily, "Was not temporarily disabling breakpoints.");
if (!this.breakpointsDisabledTemporarily)
return;
let restoreState = this._temporarilyDisabledBreakpointsRestoreSetting.value;
this._temporarilyDisabledBreakpointsRestoreSetting.value = null;
this.breakpointsEnabled = restoreState;
}
_timelineCapturingWillStart(event)
{
this._startDisablingBreakpointsTemporarily();
if (this.paused)
this.resume();
}
_timelineCapturingStopped(event)
{
this._stopDisablingBreakpointsTemporarily();
}
_targetRemoved(event)
{
let wasPaused = this.paused;
this._targetDebuggerDataMap.delete(event.data.target);
if (!this.paused && wasPaused)
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.Resumed);
}
_mainResourceDidChange(event)
{
if (!event.target.isMainFrame())
return;
this._didResumeInternal(WebInspector.mainTarget);
}
_didResumeInternal(target)
{
if (!this.paused)
return;
if (this._delayedResumeTimeout) {
clearTimeout(this._delayedResumeTimeout);
this._delayedResumeTimeout = undefined;
}
let activeCallFrameDidChange = false;
if (this._activeCallFrame && this._activeCallFrame.target === target) {
this._activeCallFrame = null;
activeCallFrameDidChange = true;
}
this.dataForTarget(target).updateForResume();
if (!this.paused)
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.Resumed);
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.CallFramesDidChange, {target});
if (activeCallFrameDidChange)
this.dispatchEventToListeners(WebInspector.DebuggerManager.Event.ActiveCallFrameDidChange);
}
_updateBreakOnExceptionsState()
{
let state = "none";
if (this._breakpointsEnabledSetting.value) {
if (!this._allExceptionsBreakpoint.disabled)
state = "all";
else if (!this._allUncaughtExceptionsBreakpoint.disabled)
state = "uncaught";
}
this._breakOnExceptionsState = state;
switch (state) {
case "all":
// Mark the uncaught breakpoint as unresolved since "all" includes "uncaught".
// That way it is clear in the user interface that the breakpoint is ignored.
this._allUncaughtExceptionsBreakpoint.resolved = false;
break;
case "uncaught":
case "none":
// Mark the uncaught breakpoint as resolved again.
this._allUncaughtExceptionsBreakpoint.resolved = true;
break;
}
}
_saveBreakpoints()
{
if (this._restoringBreakpoints)
return;
let breakpointsToSave = this._breakpoints.filter((breakpoint) => !!breakpoint.contentIdentifier);
let serializedBreakpoints = breakpointsToSave.map((breakpoint) => breakpoint.info);
this._breakpointsSetting.value = serializedBreakpoints;
}
_associateBreakpointsWithSourceCode(breakpoints, sourceCode)
{
this._ignoreBreakpointDisplayLocationDidChangeEvent = true;
for (let breakpoint of breakpoints) {
if (!breakpoint.sourceCodeLocation.sourceCode)
breakpoint.sourceCodeLocation.sourceCode = sourceCode;
// SourceCodes can be unequal if the SourceCodeLocation is associated with a Script and we are looking at the Resource.
console.assert(breakpoint.sourceCodeLocation.sourceCode === sourceCode || breakpoint.sourceCodeLocation.sourceCode.contentIdentifier === sourceCode.contentIdentifier);
}
this._ignoreBreakpointDisplayLocationDidChangeEvent = false;
}
_debugUIEnabledDidChange()
{
let eventType = WebInspector.isDebugUIEnabled() ? WebInspector.DebuggerManager.Event.ScriptAdded : WebInspector.DebuggerManager.Event.ScriptRemoved;
for (let script of this._internalWebKitScripts)
this.dispatchEventToListeners(eventType, {script});
}
};
WebInspector.DebuggerManager.Event = {
BreakpointAdded: "debugger-manager-breakpoint-added",
BreakpointRemoved: "debugger-manager-breakpoint-removed",
BreakpointMoved: "debugger-manager-breakpoint-moved",
WaitingToPause: "debugger-manager-waiting-to-pause",
Paused: "debugger-manager-paused",
Resumed: "debugger-manager-resumed",
CallFramesDidChange: "debugger-manager-call-frames-did-change",
ActiveCallFrameDidChange: "debugger-manager-active-call-frame-did-change",
ScriptAdded: "debugger-manager-script-added",
ScriptRemoved: "debugger-manager-script-removed",
ScriptsCleared: "debugger-manager-scripts-cleared",
BreakpointsEnabledDidChange: "debugger-manager-breakpoints-enabled-did-change"
};
WebInspector.DebuggerManager.PauseReason = {
Assertion: "assertion",
Breakpoint: "breakpoint",
CSPViolation: "CSP-violation",
DebuggerStatement: "debugger-statement",
Exception: "exception",
PauseOnNextStatement: "pause-on-next-statement",
Other: "other",
};
|