-
Notifications
You must be signed in to change notification settings - Fork 30.1k
/
debug-debugger.js
2590 lines (2123 loc) Β· 74.8 KB
/
debug-debugger.js
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
// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
"use strict";
// Default number of frames to include in the response to backtrace request.
var kDefaultBacktraceLength = 10;
var Debug = {};
// Regular expression to skip "crud" at the beginning of a source line which is
// not really code. Currently the regular expression matches whitespace and
// comments.
var sourceLineBeginningSkip = /^(?:\s*(?:\/\*.*?\*\/)*)*/;
// Debug events which can occour in the V8 JavaScript engine. These originate
// from the API include file debug.h.
Debug.DebugEvent = { Break: 1,
Exception: 2,
NewFunction: 3,
BeforeCompile: 4,
AfterCompile: 5,
CompileError: 6,
PromiseEvent: 7,
AsyncTaskEvent: 8,
BreakForCommand: 9 };
// Types of exceptions that can be broken upon.
Debug.ExceptionBreak = { Caught : 0,
Uncaught: 1 };
// The different types of steps.
Debug.StepAction = { StepOut: 0,
StepNext: 1,
StepIn: 2,
StepMin: 3,
StepInMin: 4,
StepFrame: 5 };
// The different types of scripts matching enum ScriptType in objects.h.
Debug.ScriptType = { Native: 0,
Extension: 1,
Normal: 2 };
// The different types of script compilations matching enum
// Script::CompilationType in objects.h.
Debug.ScriptCompilationType = { Host: 0,
Eval: 1,
JSON: 2 };
// The different script break point types.
Debug.ScriptBreakPointType = { ScriptId: 0,
ScriptName: 1,
ScriptRegExp: 2 };
// The different types of breakpoint position alignments.
// Must match BreakPositionAlignment in debug.h.
Debug.BreakPositionAlignment = {
Statement: 0,
BreakPosition: 1
};
function ScriptTypeFlag(type) {
return (1 << type);
}
// Globals.
var next_response_seq = 0;
var next_break_point_number = 1;
var break_points = [];
var script_break_points = [];
var debugger_flags = {
breakPointsActive: {
value: true,
getValue: function() { return this.value; },
setValue: function(value) {
this.value = !!value;
%SetDisableBreak(!this.value);
}
},
breakOnCaughtException: {
getValue: function() { return Debug.isBreakOnException(); },
setValue: function(value) {
if (value) {
Debug.setBreakOnException();
} else {
Debug.clearBreakOnException();
}
}
},
breakOnUncaughtException: {
getValue: function() { return Debug.isBreakOnUncaughtException(); },
setValue: function(value) {
if (value) {
Debug.setBreakOnUncaughtException();
} else {
Debug.clearBreakOnUncaughtException();
}
}
},
};
// Create a new break point object and add it to the list of break points.
function MakeBreakPoint(source_position, opt_script_break_point) {
var break_point = new BreakPoint(source_position, opt_script_break_point);
break_points.push(break_point);
return break_point;
}
// Object representing a break point.
// NOTE: This object does not have a reference to the function having break
// point as this would cause function not to be garbage collected when it is
// not used any more. We do not want break points to keep functions alive.
function BreakPoint(source_position, opt_script_break_point) {
this.source_position_ = source_position;
if (opt_script_break_point) {
this.script_break_point_ = opt_script_break_point;
} else {
this.number_ = next_break_point_number++;
}
this.hit_count_ = 0;
this.active_ = true;
this.condition_ = null;
this.ignoreCount_ = 0;
}
BreakPoint.prototype.number = function() {
return this.number_;
};
BreakPoint.prototype.func = function() {
return this.func_;
};
BreakPoint.prototype.source_position = function() {
return this.source_position_;
};
BreakPoint.prototype.hit_count = function() {
return this.hit_count_;
};
BreakPoint.prototype.active = function() {
if (this.script_break_point()) {
return this.script_break_point().active();
}
return this.active_;
};
BreakPoint.prototype.condition = function() {
if (this.script_break_point() && this.script_break_point().condition()) {
return this.script_break_point().condition();
}
return this.condition_;
};
BreakPoint.prototype.ignoreCount = function() {
return this.ignoreCount_;
};
BreakPoint.prototype.script_break_point = function() {
return this.script_break_point_;
};
BreakPoint.prototype.enable = function() {
this.active_ = true;
};
BreakPoint.prototype.disable = function() {
this.active_ = false;
};
BreakPoint.prototype.setCondition = function(condition) {
this.condition_ = condition;
};
BreakPoint.prototype.setIgnoreCount = function(ignoreCount) {
this.ignoreCount_ = ignoreCount;
};
BreakPoint.prototype.isTriggered = function(exec_state) {
// Break point not active - not triggered.
if (!this.active()) return false;
// Check for conditional break point.
if (this.condition()) {
// If break point has condition try to evaluate it in the top frame.
try {
var mirror = exec_state.frame(0).evaluate(this.condition());
// If no sensible mirror or non true value break point not triggered.
if (!(mirror instanceof ValueMirror) || !%ToBoolean(mirror.value_)) {
return false;
}
} catch (e) {
// Exception evaluating condition counts as not triggered.
return false;
}
}
// Update the hit count.
this.hit_count_++;
if (this.script_break_point_) {
this.script_break_point_.hit_count_++;
}
// If the break point has an ignore count it is not triggered.
if (this.ignoreCount_ > 0) {
this.ignoreCount_--;
return false;
}
// Break point triggered.
return true;
};
// Function called from the runtime when a break point is hit. Returns true if
// the break point is triggered and supposed to break execution.
function IsBreakPointTriggered(break_id, break_point) {
return break_point.isTriggered(MakeExecutionState(break_id));
}
// Object representing a script break point. The script is referenced by its
// script name or script id and the break point is represented as line and
// column.
function ScriptBreakPoint(type, script_id_or_name, opt_line, opt_column,
opt_groupId, opt_position_alignment) {
this.type_ = type;
if (type == Debug.ScriptBreakPointType.ScriptId) {
this.script_id_ = script_id_or_name;
} else if (type == Debug.ScriptBreakPointType.ScriptName) {
this.script_name_ = script_id_or_name;
} else if (type == Debug.ScriptBreakPointType.ScriptRegExp) {
this.script_regexp_object_ = new RegExp(script_id_or_name);
} else {
throw new Error("Unexpected breakpoint type " + type);
}
this.line_ = opt_line || 0;
this.column_ = opt_column;
this.groupId_ = opt_groupId;
this.position_alignment_ = IS_UNDEFINED(opt_position_alignment)
? Debug.BreakPositionAlignment.Statement : opt_position_alignment;
this.hit_count_ = 0;
this.active_ = true;
this.condition_ = null;
this.ignoreCount_ = 0;
this.break_points_ = [];
}
//Creates a clone of script breakpoint that is linked to another script.
ScriptBreakPoint.prototype.cloneForOtherScript = function (other_script) {
var copy = new ScriptBreakPoint(Debug.ScriptBreakPointType.ScriptId,
other_script.id, this.line_, this.column_, this.groupId_,
this.position_alignment_);
copy.number_ = next_break_point_number++;
script_break_points.push(copy);
copy.hit_count_ = this.hit_count_;
copy.active_ = this.active_;
copy.condition_ = this.condition_;
copy.ignoreCount_ = this.ignoreCount_;
return copy;
};
ScriptBreakPoint.prototype.number = function() {
return this.number_;
};
ScriptBreakPoint.prototype.groupId = function() {
return this.groupId_;
};
ScriptBreakPoint.prototype.type = function() {
return this.type_;
};
ScriptBreakPoint.prototype.script_id = function() {
return this.script_id_;
};
ScriptBreakPoint.prototype.script_name = function() {
return this.script_name_;
};
ScriptBreakPoint.prototype.script_regexp_object = function() {
return this.script_regexp_object_;
};
ScriptBreakPoint.prototype.line = function() {
return this.line_;
};
ScriptBreakPoint.prototype.column = function() {
return this.column_;
};
ScriptBreakPoint.prototype.actual_locations = function() {
var locations = [];
for (var i = 0; i < this.break_points_.length; i++) {
locations.push(this.break_points_[i].actual_location);
}
return locations;
};
ScriptBreakPoint.prototype.update_positions = function(line, column) {
this.line_ = line;
this.column_ = column;
};
ScriptBreakPoint.prototype.hit_count = function() {
return this.hit_count_;
};
ScriptBreakPoint.prototype.active = function() {
return this.active_;
};
ScriptBreakPoint.prototype.condition = function() {
return this.condition_;
};
ScriptBreakPoint.prototype.ignoreCount = function() {
return this.ignoreCount_;
};
ScriptBreakPoint.prototype.enable = function() {
this.active_ = true;
};
ScriptBreakPoint.prototype.disable = function() {
this.active_ = false;
};
ScriptBreakPoint.prototype.setCondition = function(condition) {
this.condition_ = condition;
};
ScriptBreakPoint.prototype.setIgnoreCount = function(ignoreCount) {
this.ignoreCount_ = ignoreCount;
// Set ignore count on all break points created from this script break point.
for (var i = 0; i < this.break_points_.length; i++) {
this.break_points_[i].setIgnoreCount(ignoreCount);
}
};
// Check whether a script matches this script break point. Currently this is
// only based on script name.
ScriptBreakPoint.prototype.matchesScript = function(script) {
if (this.type_ == Debug.ScriptBreakPointType.ScriptId) {
return this.script_id_ == script.id;
} else {
// We might want to account columns here as well.
if (!(script.line_offset <= this.line_ &&
this.line_ < script.line_offset + script.lineCount())) {
return false;
}
if (this.type_ == Debug.ScriptBreakPointType.ScriptName) {
return this.script_name_ == script.nameOrSourceURL();
} else if (this.type_ == Debug.ScriptBreakPointType.ScriptRegExp) {
return this.script_regexp_object_.test(script.nameOrSourceURL());
} else {
throw new Error("Unexpected breakpoint type " + this.type_);
}
}
};
// Set the script break point in a script.
ScriptBreakPoint.prototype.set = function (script) {
var column = this.column();
var line = this.line();
// If the column is undefined the break is on the line. To help locate the
// first piece of breakable code on the line try to find the column on the
// line which contains some source.
if (IS_UNDEFINED(column)) {
var source_line = script.sourceLine(this.line());
// Allocate array for caching the columns where the actual source starts.
if (!script.sourceColumnStart_) {
script.sourceColumnStart_ = new Array(script.lineCount());
}
// Fill cache if needed and get column where the actual source starts.
if (IS_UNDEFINED(script.sourceColumnStart_[line])) {
script.sourceColumnStart_[line] =
source_line.match(sourceLineBeginningSkip)[0].length;
}
column = script.sourceColumnStart_[line];
}
// Convert the line and column into an absolute position within the script.
var position = Debug.findScriptSourcePosition(script, this.line(), column);
// If the position is not found in the script (the script might be shorter
// than it used to be) just ignore it.
if (IS_NULL(position)) return;
// Create a break point object and set the break point.
var break_point = MakeBreakPoint(position, this);
break_point.setIgnoreCount(this.ignoreCount());
var actual_position = %SetScriptBreakPoint(script, position,
this.position_alignment_,
break_point);
if (IS_UNDEFINED(actual_position)) {
actual_position = position;
}
var actual_location = script.locationFromPosition(actual_position, true);
break_point.actual_location = { line: actual_location.line,
column: actual_location.column,
script_id: script.id };
this.break_points_.push(break_point);
return break_point;
};
// Clear all the break points created from this script break point
ScriptBreakPoint.prototype.clear = function () {
var remaining_break_points = [];
for (var i = 0; i < break_points.length; i++) {
if (break_points[i].script_break_point() &&
break_points[i].script_break_point() === this) {
%ClearBreakPoint(break_points[i]);
} else {
remaining_break_points.push(break_points[i]);
}
}
break_points = remaining_break_points;
this.break_points_ = [];
};
// Function called from runtime when a new script is compiled to set any script
// break points set in this script.
function UpdateScriptBreakPoints(script) {
for (var i = 0; i < script_break_points.length; i++) {
var break_point = script_break_points[i];
if ((break_point.type() == Debug.ScriptBreakPointType.ScriptName ||
break_point.type() == Debug.ScriptBreakPointType.ScriptRegExp) &&
break_point.matchesScript(script)) {
break_point.set(script);
}
}
}
function GetScriptBreakPoints(script) {
var result = [];
for (var i = 0; i < script_break_points.length; i++) {
if (script_break_points[i].matchesScript(script)) {
result.push(script_break_points[i]);
}
}
return result;
}
Debug.setListener = function(listener, opt_data) {
if (!IS_FUNCTION(listener) && !IS_UNDEFINED(listener) && !IS_NULL(listener)) {
throw new Error('Parameters have wrong types.');
}
%SetDebugEventListener(listener, opt_data);
};
Debug.breakExecution = function(f) {
%Break();
};
Debug.breakLocations = function(f, opt_position_aligment) {
if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
var position_aligment = IS_UNDEFINED(opt_position_aligment)
? Debug.BreakPositionAlignment.Statement : opt_position_aligment;
return %GetBreakLocations(f, position_aligment);
};
// Returns a Script object. If the parameter is a function the return value
// is the script in which the function is defined. If the parameter is a string
// the return value is the script for which the script name has that string
// value. If it is a regexp and there is a unique script whose name matches
// we return that, otherwise undefined.
Debug.findScript = function(func_or_script_name) {
if (IS_FUNCTION(func_or_script_name)) {
return %FunctionGetScript(func_or_script_name);
} else if (IS_REGEXP(func_or_script_name)) {
var scripts = Debug.scripts();
var last_result = null;
var result_count = 0;
for (var i in scripts) {
var script = scripts[i];
if (func_or_script_name.test(script.name)) {
last_result = script;
result_count++;
}
}
// Return the unique script matching the regexp. If there are more
// than one we don't return a value since there is no good way to
// decide which one to return. Returning a "random" one, say the
// first, would introduce nondeterminism (or something close to it)
// because the order is the heap iteration order.
if (result_count == 1) {
return last_result;
} else {
return undefined;
}
} else {
return %GetScript(func_or_script_name);
}
};
// Returns the script source. If the parameter is a function the return value
// is the script source for the script in which the function is defined. If the
// parameter is a string the return value is the script for which the script
// name has that string value.
Debug.scriptSource = function(func_or_script_name) {
return this.findScript(func_or_script_name).source;
};
Debug.source = function(f) {
if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
return %FunctionGetSourceCode(f);
};
Debug.disassemble = function(f) {
if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
return %DebugDisassembleFunction(f);
};
Debug.disassembleConstructor = function(f) {
if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
return %DebugDisassembleConstructor(f);
};
Debug.ExecuteInDebugContext = function(f, without_debugger) {
if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
return %ExecuteInDebugContext(f, !!without_debugger);
};
Debug.sourcePosition = function(f) {
if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
return %FunctionGetScriptSourcePosition(f);
};
Debug.findFunctionSourceLocation = function(func, opt_line, opt_column) {
var script = %FunctionGetScript(func);
var script_offset = %FunctionGetScriptSourcePosition(func);
return script.locationFromLine(opt_line, opt_column, script_offset);
};
// Returns the character position in a script based on a line number and an
// optional position within that line.
Debug.findScriptSourcePosition = function(script, opt_line, opt_column) {
var location = script.locationFromLine(opt_line, opt_column);
return location ? location.position : null;
};
Debug.findBreakPoint = function(break_point_number, remove) {
var break_point;
for (var i = 0; i < break_points.length; i++) {
if (break_points[i].number() == break_point_number) {
break_point = break_points[i];
// Remove the break point from the list if requested.
if (remove) {
break_points.splice(i, 1);
}
break;
}
}
if (break_point) {
return break_point;
} else {
return this.findScriptBreakPoint(break_point_number, remove);
}
};
Debug.findBreakPointActualLocations = function(break_point_number) {
for (var i = 0; i < script_break_points.length; i++) {
if (script_break_points[i].number() == break_point_number) {
return script_break_points[i].actual_locations();
}
}
for (var i = 0; i < break_points.length; i++) {
if (break_points[i].number() == break_point_number) {
return [break_points[i].actual_location];
}
}
return [];
};
Debug.setBreakPoint = function(func, opt_line, opt_column, opt_condition) {
if (!IS_FUNCTION(func)) throw new Error('Parameters have wrong types.');
// Break points in API functions are not supported.
if (%FunctionIsAPIFunction(func)) {
throw new Error('Cannot set break point in native code.');
}
// Find source position relative to start of the function
var break_position =
this.findFunctionSourceLocation(func, opt_line, opt_column).position;
var source_position = break_position - this.sourcePosition(func);
// Find the script for the function.
var script = %FunctionGetScript(func);
// Break in builtin JavaScript code is not supported.
if (script.type == Debug.ScriptType.Native) {
throw new Error('Cannot set break point in native code.');
}
// If the script for the function has a name convert this to a script break
// point.
if (script && script.id) {
// Adjust the source position to be script relative.
source_position += %FunctionGetScriptSourcePosition(func);
// Find line and column for the position in the script and set a script
// break point from that.
var location = script.locationFromPosition(source_position, false);
return this.setScriptBreakPointById(script.id,
location.line, location.column,
opt_condition);
} else {
// Set a break point directly on the function.
var break_point = MakeBreakPoint(source_position);
var actual_position =
%SetFunctionBreakPoint(func, source_position, break_point);
actual_position += this.sourcePosition(func);
var actual_location = script.locationFromPosition(actual_position, true);
break_point.actual_location = { line: actual_location.line,
column: actual_location.column,
script_id: script.id };
break_point.setCondition(opt_condition);
return break_point.number();
}
};
Debug.setBreakPointByScriptIdAndPosition = function(script_id, position,
condition, enabled,
opt_position_alignment)
{
var break_point = MakeBreakPoint(position);
break_point.setCondition(condition);
if (!enabled) {
break_point.disable();
}
var scripts = this.scripts();
var position_alignment = IS_UNDEFINED(opt_position_alignment)
? Debug.BreakPositionAlignment.Statement : opt_position_alignment;
for (var i = 0; i < scripts.length; i++) {
if (script_id == scripts[i].id) {
break_point.actual_position = %SetScriptBreakPoint(scripts[i], position,
position_alignment, break_point);
break;
}
}
return break_point;
};
Debug.enableBreakPoint = function(break_point_number) {
var break_point = this.findBreakPoint(break_point_number, false);
// Only enable if the breakpoint hasn't been deleted:
if (break_point) {
break_point.enable();
}
};
Debug.disableBreakPoint = function(break_point_number) {
var break_point = this.findBreakPoint(break_point_number, false);
// Only enable if the breakpoint hasn't been deleted:
if (break_point) {
break_point.disable();
}
};
Debug.changeBreakPointCondition = function(break_point_number, condition) {
var break_point = this.findBreakPoint(break_point_number, false);
break_point.setCondition(condition);
};
Debug.changeBreakPointIgnoreCount = function(break_point_number, ignoreCount) {
if (ignoreCount < 0) {
throw new Error('Invalid argument');
}
var break_point = this.findBreakPoint(break_point_number, false);
break_point.setIgnoreCount(ignoreCount);
};
Debug.clearBreakPoint = function(break_point_number) {
var break_point = this.findBreakPoint(break_point_number, true);
if (break_point) {
return %ClearBreakPoint(break_point);
} else {
break_point = this.findScriptBreakPoint(break_point_number, true);
if (!break_point) {
throw new Error('Invalid breakpoint');
}
}
};
Debug.clearAllBreakPoints = function() {
for (var i = 0; i < break_points.length; i++) {
var break_point = break_points[i];
%ClearBreakPoint(break_point);
}
break_points = [];
};
Debug.disableAllBreakPoints = function() {
// Disable all user defined breakpoints:
for (var i = 1; i < next_break_point_number; i++) {
Debug.disableBreakPoint(i);
}
// Disable all exception breakpoints:
%ChangeBreakOnException(Debug.ExceptionBreak.Caught, false);
%ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, false);
};
Debug.findScriptBreakPoint = function(break_point_number, remove) {
var script_break_point;
for (var i = 0; i < script_break_points.length; i++) {
if (script_break_points[i].number() == break_point_number) {
script_break_point = script_break_points[i];
// Remove the break point from the list if requested.
if (remove) {
script_break_point.clear();
script_break_points.splice(i,1);
}
break;
}
}
return script_break_point;
};
// Sets a breakpoint in a script identified through id or name at the
// specified source line and column within that line.
Debug.setScriptBreakPoint = function(type, script_id_or_name,
opt_line, opt_column, opt_condition,
opt_groupId, opt_position_alignment) {
// Create script break point object.
var script_break_point =
new ScriptBreakPoint(type, script_id_or_name, opt_line, opt_column,
opt_groupId, opt_position_alignment);
// Assign number to the new script break point and add it.
script_break_point.number_ = next_break_point_number++;
script_break_point.setCondition(opt_condition);
script_break_points.push(script_break_point);
// Run through all scripts to see if this script break point matches any
// loaded scripts.
var scripts = this.scripts();
for (var i = 0; i < scripts.length; i++) {
if (script_break_point.matchesScript(scripts[i])) {
script_break_point.set(scripts[i]);
}
}
return script_break_point.number();
};
Debug.setScriptBreakPointById = function(script_id,
opt_line, opt_column,
opt_condition, opt_groupId,
opt_position_alignment) {
return this.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptId,
script_id, opt_line, opt_column,
opt_condition, opt_groupId,
opt_position_alignment);
};
Debug.setScriptBreakPointByName = function(script_name,
opt_line, opt_column,
opt_condition, opt_groupId) {
return this.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptName,
script_name, opt_line, opt_column,
opt_condition, opt_groupId);
};
Debug.setScriptBreakPointByRegExp = function(script_regexp,
opt_line, opt_column,
opt_condition, opt_groupId) {
return this.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptRegExp,
script_regexp, opt_line, opt_column,
opt_condition, opt_groupId);
};
Debug.enableScriptBreakPoint = function(break_point_number) {
var script_break_point = this.findScriptBreakPoint(break_point_number, false);
script_break_point.enable();
};
Debug.disableScriptBreakPoint = function(break_point_number) {
var script_break_point = this.findScriptBreakPoint(break_point_number, false);
script_break_point.disable();
};
Debug.changeScriptBreakPointCondition = function(
break_point_number, condition) {
var script_break_point = this.findScriptBreakPoint(break_point_number, false);
script_break_point.setCondition(condition);
};
Debug.changeScriptBreakPointIgnoreCount = function(
break_point_number, ignoreCount) {
if (ignoreCount < 0) {
throw new Error('Invalid argument');
}
var script_break_point = this.findScriptBreakPoint(break_point_number, false);
script_break_point.setIgnoreCount(ignoreCount);
};
Debug.scriptBreakPoints = function() {
return script_break_points;
};
Debug.clearStepping = function() {
%ClearStepping();
};
Debug.setBreakOnException = function() {
return %ChangeBreakOnException(Debug.ExceptionBreak.Caught, true);
};
Debug.clearBreakOnException = function() {
return %ChangeBreakOnException(Debug.ExceptionBreak.Caught, false);
};
Debug.isBreakOnException = function() {
return !!%IsBreakOnException(Debug.ExceptionBreak.Caught);
};
Debug.setBreakOnUncaughtException = function() {
return %ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, true);
};
Debug.clearBreakOnUncaughtException = function() {
return %ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, false);
};
Debug.isBreakOnUncaughtException = function() {
return !!%IsBreakOnException(Debug.ExceptionBreak.Uncaught);
};
Debug.showBreakPoints = function(f, full, opt_position_alignment) {
if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
var source = full ? this.scriptSource(f) : this.source(f);
var offset = full ? this.sourcePosition(f) : 0;
var locations = this.breakLocations(f, opt_position_alignment);
if (!locations) return source;
locations.sort(function(x, y) { return x - y; });
var result = "";
var prev_pos = 0;
var pos;
for (var i = 0; i < locations.length; i++) {
pos = locations[i] - offset;
result += source.slice(prev_pos, pos);
result += "[B" + i + "]";
prev_pos = pos;
}
pos = source.length;
result += source.substring(prev_pos, pos);
return result;
};
// Get all the scripts currently loaded. Locating all the scripts is based on
// scanning the heap.
Debug.scripts = function() {
// Collect all scripts in the heap.
return %DebugGetLoadedScripts();
};
Debug.debuggerFlags = function() {
return debugger_flags;
};
Debug.MakeMirror = MakeMirror;
function MakeExecutionState(break_id) {
return new ExecutionState(break_id);
}
function ExecutionState(break_id) {
this.break_id = break_id;
this.selected_frame = 0;
}
ExecutionState.prototype.prepareStep = function(opt_action, opt_count,
opt_callframe) {
var action = Debug.StepAction.StepIn;
if (!IS_UNDEFINED(opt_action)) action = %ToNumber(opt_action);
var count = opt_count ? %ToNumber(opt_count) : 1;
var callFrameId = 0;
if (!IS_UNDEFINED(opt_callframe)) {
callFrameId = opt_callframe.details_.frameId();
}
return %PrepareStep(this.break_id, action, count, callFrameId);
};
ExecutionState.prototype.evaluateGlobal = function(source, disable_break,
opt_additional_context) {
return MakeMirror(%DebugEvaluateGlobal(this.break_id, source,
Boolean(disable_break),
opt_additional_context));
};
ExecutionState.prototype.frameCount = function() {
return %GetFrameCount(this.break_id);
};
ExecutionState.prototype.threadCount = function() {
return %GetThreadCount(this.break_id);
};
ExecutionState.prototype.frame = function(opt_index) {
// If no index supplied return the selected frame.
if (opt_index == null) opt_index = this.selected_frame;
if (opt_index < 0 || opt_index >= this.frameCount()) {
throw new Error('Illegal frame index.');
}
return new FrameMirror(this.break_id, opt_index);
};
ExecutionState.prototype.setSelectedFrame = function(index) {
var i = %ToNumber(index);
if (i < 0 || i >= this.frameCount()) throw new Error('Illegal frame index.');
this.selected_frame = i;
};
ExecutionState.prototype.selectedFrame = function() {
return this.selected_frame;
};
ExecutionState.prototype.debugCommandProcessor = function(opt_is_running) {
return new DebugCommandProcessor(this, opt_is_running);
};
function MakeBreakEvent(break_id, break_points_hit) {
return new BreakEvent(break_id, break_points_hit);
}
function BreakEvent(break_id, break_points_hit) {
this.frame_ = new FrameMirror(break_id, 0);