1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // Default number of frames to include in the response to backtrace request.
29 var kDefaultBacktraceLength = 10;
33 // Regular expression to skip "crud" at the beginning of a source line which is
34 // not really code. Currently the regular expression matches whitespace and
36 var sourceLineBeginningSkip = /^(?:\s*(?:\/\*.*?\*\/)*)*/;
38 // Debug events which can occour in the V8 JavaScript engine. These originate
39 // from the API include file debug.h.
40 Debug.DebugEvent = { Break: 1,
47 // Types of exceptions that can be broken upon.
48 Debug.ExceptionBreak = { Caught : 0,
51 // The different types of steps.
52 Debug.StepAction = { StepOut: 0,
58 // The different types of scripts matching enum ScriptType in objects.h.
59 Debug.ScriptType = { Native: 0,
63 // The different types of script compilations matching enum
64 // Script::CompilationType in objects.h.
65 Debug.ScriptCompilationType = { Host: 0,
69 // The different script break point types.
70 Debug.ScriptBreakPointType = { ScriptId: 0,
74 function ScriptTypeFlag(type) {
79 var next_response_seq = 0;
80 var next_break_point_number = 1;
81 var break_points = [];
82 var script_break_points = [];
83 var debugger_flags = {
86 getValue: function() { return this.value; },
87 setValue: function(value) {
89 %SetDisableBreak(!this.value);
92 breakOnCaughtException: {
93 getValue: function() { return Debug.isBreakOnException(); },
94 setValue: function(value) {
96 Debug.setBreakOnException();
98 Debug.clearBreakOnException();
102 breakOnUncaughtException: {
103 getValue: function() { return Debug.isBreakOnUncaughtException(); },
104 setValue: function(value) {
106 Debug.setBreakOnUncaughtException();
108 Debug.clearBreakOnUncaughtException();
113 var lol_is_enabled = %HasLOLEnabled();
116 // Create a new break point object and add it to the list of break points.
117 function MakeBreakPoint(source_position, opt_script_break_point) {
118 var break_point = new BreakPoint(source_position, opt_script_break_point);
119 break_points.push(break_point);
124 // Object representing a break point.
125 // NOTE: This object does not have a reference to the function having break
126 // point as this would cause function not to be garbage collected when it is
127 // not used any more. We do not want break points to keep functions alive.
128 function BreakPoint(source_position, opt_script_break_point) {
129 this.source_position_ = source_position;
130 if (opt_script_break_point) {
131 this.script_break_point_ = opt_script_break_point;
133 this.number_ = next_break_point_number++;
137 this.condition_ = null;
138 this.ignoreCount_ = 0;
142 BreakPoint.prototype.number = function() {
147 BreakPoint.prototype.func = function() {
152 BreakPoint.prototype.source_position = function() {
153 return this.source_position_;
157 BreakPoint.prototype.hit_count = function() {
158 return this.hit_count_;
162 BreakPoint.prototype.active = function() {
163 if (this.script_break_point()) {
164 return this.script_break_point().active();
170 BreakPoint.prototype.condition = function() {
171 if (this.script_break_point() && this.script_break_point().condition()) {
172 return this.script_break_point().condition();
174 return this.condition_;
178 BreakPoint.prototype.ignoreCount = function() {
179 return this.ignoreCount_;
183 BreakPoint.prototype.script_break_point = function() {
184 return this.script_break_point_;
188 BreakPoint.prototype.enable = function() {
193 BreakPoint.prototype.disable = function() {
194 this.active_ = false;
198 BreakPoint.prototype.setCondition = function(condition) {
199 this.condition_ = condition;
203 BreakPoint.prototype.setIgnoreCount = function(ignoreCount) {
204 this.ignoreCount_ = ignoreCount;
208 BreakPoint.prototype.isTriggered = function(exec_state) {
209 // Break point not active - not triggered.
210 if (!this.active()) return false;
212 // Check for conditional break point.
213 if (this.condition()) {
214 // If break point has condition try to evaluate it in the top frame.
216 var mirror = exec_state.frame(0).evaluate(this.condition());
217 // If no sensible mirror or non true value break point not triggered.
218 if (!(mirror instanceof ValueMirror) || !%ToBoolean(mirror.value_)) {
222 // Exception evaluating condition counts as not triggered.
227 // Update the hit count.
229 if (this.script_break_point_) {
230 this.script_break_point_.hit_count_++;
233 // If the break point has an ignore count it is not triggered.
234 if (this.ignoreCount_ > 0) {
239 // Break point triggered.
244 // Function called from the runtime when a break point is hit. Returns true if
245 // the break point is triggered and supposed to break execution.
246 function IsBreakPointTriggered(break_id, break_point) {
247 return break_point.isTriggered(MakeExecutionState(break_id));
251 // Object representing a script break point. The script is referenced by its
252 // script name or script id and the break point is represented as line and
254 function ScriptBreakPoint(type, script_id_or_name, opt_line, opt_column,
257 if (type == Debug.ScriptBreakPointType.ScriptId) {
258 this.script_id_ = script_id_or_name;
259 } else if (type == Debug.ScriptBreakPointType.ScriptName) {
260 this.script_name_ = script_id_or_name;
261 } else if (type == Debug.ScriptBreakPointType.ScriptRegExp) {
262 this.script_regexp_object_ = new RegExp(script_id_or_name);
264 throw new Error("Unexpected breakpoint type " + type);
266 this.line_ = opt_line || 0;
267 this.column_ = opt_column;
268 this.groupId_ = opt_groupId;
271 this.condition_ = null;
272 this.ignoreCount_ = 0;
273 this.break_points_ = [];
277 //Creates a clone of script breakpoint that is linked to another script.
278 ScriptBreakPoint.prototype.cloneForOtherScript = function (other_script) {
279 var copy = new ScriptBreakPoint(Debug.ScriptBreakPointType.ScriptId,
280 other_script.id, this.line_, this.column_, this.groupId_);
281 copy.number_ = next_break_point_number++;
282 script_break_points.push(copy);
284 copy.hit_count_ = this.hit_count_;
285 copy.active_ = this.active_;
286 copy.condition_ = this.condition_;
287 copy.ignoreCount_ = this.ignoreCount_;
292 ScriptBreakPoint.prototype.number = function() {
297 ScriptBreakPoint.prototype.groupId = function() {
298 return this.groupId_;
302 ScriptBreakPoint.prototype.type = function() {
307 ScriptBreakPoint.prototype.script_id = function() {
308 return this.script_id_;
312 ScriptBreakPoint.prototype.script_name = function() {
313 return this.script_name_;
317 ScriptBreakPoint.prototype.script_regexp_object = function() {
318 return this.script_regexp_object_;
322 ScriptBreakPoint.prototype.line = function() {
327 ScriptBreakPoint.prototype.column = function() {
332 ScriptBreakPoint.prototype.actual_locations = function() {
334 for (var i = 0; i < this.break_points_.length; i++) {
335 locations.push(this.break_points_[i].actual_location);
341 ScriptBreakPoint.prototype.update_positions = function(line, column) {
343 this.column_ = column;
347 ScriptBreakPoint.prototype.hit_count = function() {
348 return this.hit_count_;
352 ScriptBreakPoint.prototype.active = function() {
357 ScriptBreakPoint.prototype.condition = function() {
358 return this.condition_;
362 ScriptBreakPoint.prototype.ignoreCount = function() {
363 return this.ignoreCount_;
367 ScriptBreakPoint.prototype.enable = function() {
372 ScriptBreakPoint.prototype.disable = function() {
373 this.active_ = false;
377 ScriptBreakPoint.prototype.setCondition = function(condition) {
378 this.condition_ = condition;
382 ScriptBreakPoint.prototype.setIgnoreCount = function(ignoreCount) {
383 this.ignoreCount_ = ignoreCount;
385 // Set ignore count on all break points created from this script break point.
386 for (var i = 0; i < this.break_points_.length; i++) {
387 this.break_points_[i].setIgnoreCount(ignoreCount);
392 // Check whether a script matches this script break point. Currently this is
393 // only based on script name.
394 ScriptBreakPoint.prototype.matchesScript = function(script) {
395 if (this.type_ == Debug.ScriptBreakPointType.ScriptId) {
396 return this.script_id_ == script.id;
398 // We might want to account columns here as well.
399 if (!(script.line_offset <= this.line_ &&
400 this.line_ < script.line_offset + script.lineCount())) {
403 if (this.type_ == Debug.ScriptBreakPointType.ScriptName) {
404 return this.script_name_ == script.nameOrSourceURL();
405 } else if (this.type_ == Debug.ScriptBreakPointType.ScriptRegExp) {
406 return this.script_regexp_object_.test(script.nameOrSourceURL());
408 throw new Error("Unexpected breakpoint type " + this.type_);
414 // Set the script break point in a script.
415 ScriptBreakPoint.prototype.set = function (script) {
416 var column = this.column();
417 var line = this.line();
418 // If the column is undefined the break is on the line. To help locate the
419 // first piece of breakable code on the line try to find the column on the
420 // line which contains some source.
421 if (IS_UNDEFINED(column)) {
422 var source_line = script.sourceLine(this.line());
424 // Allocate array for caching the columns where the actual source starts.
425 if (!script.sourceColumnStart_) {
426 script.sourceColumnStart_ = new Array(script.lineCount());
429 // Fill cache if needed and get column where the actual source starts.
430 if (IS_UNDEFINED(script.sourceColumnStart_[line])) {
431 script.sourceColumnStart_[line] =
432 source_line.match(sourceLineBeginningSkip)[0].length;
434 column = script.sourceColumnStart_[line];
437 // Convert the line and column into an absolute position within the script.
438 var position = Debug.findScriptSourcePosition(script, this.line(), column);
440 // If the position is not found in the script (the script might be shorter
441 // than it used to be) just ignore it.
442 if (position === null) return;
444 // Create a break point object and set the break point.
445 break_point = MakeBreakPoint(position, this);
446 break_point.setIgnoreCount(this.ignoreCount());
447 var actual_position = %SetScriptBreakPoint(script, position, break_point);
448 if (IS_UNDEFINED(actual_position)) {
449 actual_position = position;
451 var actual_location = script.locationFromPosition(actual_position, true);
452 break_point.actual_location = { line: actual_location.line,
453 column: actual_location.column,
454 script_id: script.id };
455 this.break_points_.push(break_point);
460 // Clear all the break points created from this script break point
461 ScriptBreakPoint.prototype.clear = function () {
462 var remaining_break_points = [];
463 for (var i = 0; i < break_points.length; i++) {
464 if (break_points[i].script_break_point() &&
465 break_points[i].script_break_point() === this) {
466 %ClearBreakPoint(break_points[i]);
468 remaining_break_points.push(break_points[i]);
471 break_points = remaining_break_points;
472 this.break_points_ = [];
476 // Function called from runtime when a new script is compiled to set any script
477 // break points set in this script.
478 function UpdateScriptBreakPoints(script) {
479 for (var i = 0; i < script_break_points.length; i++) {
480 var break_point = script_break_points[i];
481 if ((break_point.type() == Debug.ScriptBreakPointType.ScriptName ||
482 break_point.type() == Debug.ScriptBreakPointType.ScriptRegExp) &&
483 break_point.matchesScript(script)) {
484 break_point.set(script);
490 function GetScriptBreakPoints(script) {
492 for (var i = 0; i < script_break_points.length; i++) {
493 if (script_break_points[i].matchesScript(script)) {
494 result.push(script_break_points[i]);
501 Debug.setListener = function(listener, opt_data) {
502 if (!IS_FUNCTION(listener) && !IS_UNDEFINED(listener) && !IS_NULL(listener)) {
503 throw new Error('Parameters have wrong types.');
505 %SetDebugEventListener(listener, opt_data);
509 Debug.breakExecution = function(f) {
513 Debug.breakLocations = function(f) {
514 if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
515 return %GetBreakLocations(f);
518 // Returns a Script object. If the parameter is a function the return value
519 // is the script in which the function is defined. If the parameter is a string
520 // the return value is the script for which the script name has that string
521 // value. If it is a regexp and there is a unique script whose name matches
522 // we return that, otherwise undefined.
523 Debug.findScript = function(func_or_script_name) {
524 if (IS_FUNCTION(func_or_script_name)) {
525 return %FunctionGetScript(func_or_script_name);
526 } else if (IS_REGEXP(func_or_script_name)) {
527 var scripts = Debug.scripts();
528 var last_result = null;
529 var result_count = 0;
530 for (var i in scripts) {
531 var script = scripts[i];
532 if (func_or_script_name.test(script.name)) {
533 last_result = script;
537 // Return the unique script matching the regexp. If there are more
538 // than one we don't return a value since there is no good way to
539 // decide which one to return. Returning a "random" one, say the
540 // first, would introduce nondeterminism (or something close to it)
541 // because the order is the heap iteration order.
542 if (result_count == 1) {
548 return %GetScript(func_or_script_name);
552 // Returns the script source. If the parameter is a function the return value
553 // is the script source for the script in which the function is defined. If the
554 // parameter is a string the return value is the script for which the script
555 // name has that string value.
556 Debug.scriptSource = function(func_or_script_name) {
557 return this.findScript(func_or_script_name).source;
560 Debug.source = function(f) {
561 if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
562 return %FunctionGetSourceCode(f);
565 Debug.disassemble = function(f) {
566 if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
567 return %DebugDisassembleFunction(f);
570 Debug.disassembleConstructor = function(f) {
571 if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
572 return %DebugDisassembleConstructor(f);
575 Debug.ExecuteInDebugContext = function(f, without_debugger) {
576 if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
577 return %ExecuteInDebugContext(f, !!without_debugger);
580 Debug.sourcePosition = function(f) {
581 if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
582 return %FunctionGetScriptSourcePosition(f);
586 Debug.findFunctionSourceLocation = function(func, opt_line, opt_column) {
587 var script = %FunctionGetScript(func);
588 var script_offset = %FunctionGetScriptSourcePosition(func);
589 return script.locationFromLine(opt_line, opt_column, script_offset);
593 // Returns the character position in a script based on a line number and an
594 // optional position within that line.
595 Debug.findScriptSourcePosition = function(script, opt_line, opt_column) {
596 var location = script.locationFromLine(opt_line, opt_column);
597 return location ? location.position : null;
601 Debug.findBreakPoint = function(break_point_number, remove) {
603 for (var i = 0; i < break_points.length; i++) {
604 if (break_points[i].number() == break_point_number) {
605 break_point = break_points[i];
606 // Remove the break point from the list if requested.
608 break_points.splice(i, 1);
616 return this.findScriptBreakPoint(break_point_number, remove);
620 Debug.findBreakPointActualLocations = function(break_point_number) {
621 for (var i = 0; i < script_break_points.length; i++) {
622 if (script_break_points[i].number() == break_point_number) {
623 return script_break_points[i].actual_locations();
626 for (var i = 0; i < break_points.length; i++) {
627 if (break_points[i].number() == break_point_number) {
628 return [break_points[i].actual_location];
634 Debug.setBreakPoint = function(func, opt_line, opt_column, opt_condition) {
635 if (!IS_FUNCTION(func)) throw new Error('Parameters have wrong types.');
636 // Break points in API functions are not supported.
637 if (%FunctionIsAPIFunction(func)) {
638 throw new Error('Cannot set break point in native code.');
640 // Find source position relative to start of the function
642 this.findFunctionSourceLocation(func, opt_line, opt_column).position;
643 var source_position = break_position - this.sourcePosition(func);
644 // Find the script for the function.
645 var script = %FunctionGetScript(func);
646 // Break in builtin JavaScript code is not supported.
647 if (script.type == Debug.ScriptType.Native) {
648 throw new Error('Cannot set break point in native code.');
650 // If the script for the function has a name convert this to a script break
652 if (script && script.id) {
653 // Adjust the source position to be script relative.
654 source_position += %FunctionGetScriptSourcePosition(func);
655 // Find line and column for the position in the script and set a script
656 // break point from that.
657 var location = script.locationFromPosition(source_position, false);
658 return this.setScriptBreakPointById(script.id,
659 location.line, location.column,
662 // Set a break point directly on the function.
663 var break_point = MakeBreakPoint(source_position);
664 var actual_position =
665 %SetFunctionBreakPoint(func, source_position, break_point);
666 actual_position += this.sourcePosition(func);
667 var actual_location = script.locationFromPosition(actual_position, true);
668 break_point.actual_location = { line: actual_location.line,
669 column: actual_location.column,
670 script_id: script.id };
671 break_point.setCondition(opt_condition);
672 return break_point.number();
677 Debug.setBreakPointByScriptIdAndPosition = function(script_id, position,
680 break_point = MakeBreakPoint(position);
681 break_point.setCondition(condition);
683 break_point.disable();
685 var scripts = this.scripts();
686 for (var i = 0; i < scripts.length; i++) {
687 if (script_id == scripts[i].id) {
688 break_point.actual_position = %SetScriptBreakPoint(scripts[i], position,
697 Debug.enableBreakPoint = function(break_point_number) {
698 var break_point = this.findBreakPoint(break_point_number, false);
699 // Only enable if the breakpoint hasn't been deleted:
701 break_point.enable();
706 Debug.disableBreakPoint = function(break_point_number) {
707 var break_point = this.findBreakPoint(break_point_number, false);
708 // Only enable if the breakpoint hasn't been deleted:
710 break_point.disable();
715 Debug.changeBreakPointCondition = function(break_point_number, condition) {
716 var break_point = this.findBreakPoint(break_point_number, false);
717 break_point.setCondition(condition);
721 Debug.changeBreakPointIgnoreCount = function(break_point_number, ignoreCount) {
722 if (ignoreCount < 0) {
723 throw new Error('Invalid argument');
725 var break_point = this.findBreakPoint(break_point_number, false);
726 break_point.setIgnoreCount(ignoreCount);
730 Debug.clearBreakPoint = function(break_point_number) {
731 var break_point = this.findBreakPoint(break_point_number, true);
733 return %ClearBreakPoint(break_point);
735 break_point = this.findScriptBreakPoint(break_point_number, true);
737 throw new Error('Invalid breakpoint');
743 Debug.clearAllBreakPoints = function() {
744 for (var i = 0; i < break_points.length; i++) {
745 break_point = break_points[i];
746 %ClearBreakPoint(break_point);
752 Debug.disableAllBreakPoints = function() {
753 // Disable all user defined breakpoints:
754 for (var i = 1; i < next_break_point_number; i++) {
755 Debug.disableBreakPoint(i);
757 // Disable all exception breakpoints:
758 %ChangeBreakOnException(Debug.ExceptionBreak.Caught, false);
759 %ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, false);
763 Debug.findScriptBreakPoint = function(break_point_number, remove) {
764 var script_break_point;
765 for (var i = 0; i < script_break_points.length; i++) {
766 if (script_break_points[i].number() == break_point_number) {
767 script_break_point = script_break_points[i];
768 // Remove the break point from the list if requested.
770 script_break_point.clear();
771 script_break_points.splice(i,1);
776 return script_break_point;
780 // Sets a breakpoint in a script identified through id or name at the
781 // specified source line and column within that line.
782 Debug.setScriptBreakPoint = function(type, script_id_or_name,
783 opt_line, opt_column, opt_condition,
785 // Create script break point object.
786 var script_break_point =
787 new ScriptBreakPoint(type, script_id_or_name, opt_line, opt_column,
790 // Assign number to the new script break point and add it.
791 script_break_point.number_ = next_break_point_number++;
792 script_break_point.setCondition(opt_condition);
793 script_break_points.push(script_break_point);
795 // Run through all scripts to see if this script break point matches any
797 var scripts = this.scripts();
798 for (var i = 0; i < scripts.length; i++) {
799 if (script_break_point.matchesScript(scripts[i])) {
800 script_break_point.set(scripts[i]);
804 return script_break_point.number();
808 Debug.setScriptBreakPointById = function(script_id,
809 opt_line, opt_column,
810 opt_condition, opt_groupId) {
811 return this.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptId,
812 script_id, opt_line, opt_column,
813 opt_condition, opt_groupId);
817 Debug.setScriptBreakPointByName = function(script_name,
818 opt_line, opt_column,
819 opt_condition, opt_groupId) {
820 return this.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptName,
821 script_name, opt_line, opt_column,
822 opt_condition, opt_groupId);
826 Debug.setScriptBreakPointByRegExp = function(script_regexp,
827 opt_line, opt_column,
828 opt_condition, opt_groupId) {
829 return this.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptRegExp,
830 script_regexp, opt_line, opt_column,
831 opt_condition, opt_groupId);
835 Debug.enableScriptBreakPoint = function(break_point_number) {
836 var script_break_point = this.findScriptBreakPoint(break_point_number, false);
837 script_break_point.enable();
841 Debug.disableScriptBreakPoint = function(break_point_number) {
842 var script_break_point = this.findScriptBreakPoint(break_point_number, false);
843 script_break_point.disable();
847 Debug.changeScriptBreakPointCondition = function(
848 break_point_number, condition) {
849 var script_break_point = this.findScriptBreakPoint(break_point_number, false);
850 script_break_point.setCondition(condition);
854 Debug.changeScriptBreakPointIgnoreCount = function(
855 break_point_number, ignoreCount) {
856 if (ignoreCount < 0) {
857 throw new Error('Invalid argument');
859 var script_break_point = this.findScriptBreakPoint(break_point_number, false);
860 script_break_point.setIgnoreCount(ignoreCount);
864 Debug.scriptBreakPoints = function() {
865 return script_break_points;
869 Debug.clearStepping = function() {
873 Debug.setBreakOnException = function() {
874 return %ChangeBreakOnException(Debug.ExceptionBreak.Caught, true);
877 Debug.clearBreakOnException = function() {
878 return %ChangeBreakOnException(Debug.ExceptionBreak.Caught, false);
881 Debug.isBreakOnException = function() {
882 return !!%IsBreakOnException(Debug.ExceptionBreak.Caught);
885 Debug.setBreakOnUncaughtException = function() {
886 return %ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, true);
889 Debug.clearBreakOnUncaughtException = function() {
890 return %ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, false);
893 Debug.isBreakOnUncaughtException = function() {
894 return !!%IsBreakOnException(Debug.ExceptionBreak.Uncaught);
897 Debug.showBreakPoints = function(f, full) {
898 if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.');
899 var source = full ? this.scriptSource(f) : this.source(f);
900 var offset = full ? this.sourcePosition(f) : 0;
901 var locations = this.breakLocations(f);
902 if (!locations) return source;
903 locations.sort(function(x, y) { return x - y; });
907 for (var i = 0; i < locations.length; i++) {
908 pos = locations[i] - offset;
909 result += source.slice(prev_pos, pos);
910 result += "[B" + i + "]";
914 result += source.substring(prev_pos, pos);
919 // Get all the scripts currently loaded. Locating all the scripts is based on
920 // scanning the heap.
921 Debug.scripts = function() {
922 // Collect all scripts in the heap.
923 return %DebugGetLoadedScripts();
927 Debug.debuggerFlags = function() {
928 return debugger_flags;
931 Debug.MakeMirror = MakeMirror;
933 function MakeExecutionState(break_id) {
934 return new ExecutionState(break_id);
937 function ExecutionState(break_id) {
938 this.break_id = break_id;
939 this.selected_frame = 0;
942 ExecutionState.prototype.prepareStep = function(opt_action, opt_count) {
943 var action = Debug.StepAction.StepIn;
944 if (!IS_UNDEFINED(opt_action)) action = %ToNumber(opt_action);
945 var count = opt_count ? %ToNumber(opt_count) : 1;
947 return %PrepareStep(this.break_id, action, count);
950 ExecutionState.prototype.evaluateGlobal = function(source, disable_break,
951 opt_additional_context) {
952 return MakeMirror(%DebugEvaluateGlobal(this.break_id, source,
953 Boolean(disable_break),
954 opt_additional_context));
957 ExecutionState.prototype.frameCount = function() {
958 return %GetFrameCount(this.break_id);
961 ExecutionState.prototype.threadCount = function() {
962 return %GetThreadCount(this.break_id);
965 ExecutionState.prototype.frame = function(opt_index) {
966 // If no index supplied return the selected frame.
967 if (opt_index == null) opt_index = this.selected_frame;
968 if (opt_index < 0 || opt_index >= this.frameCount()) {
969 throw new Error('Illegal frame index.');
971 return new FrameMirror(this.break_id, opt_index);
974 ExecutionState.prototype.setSelectedFrame = function(index) {
975 var i = %ToNumber(index);
976 if (i < 0 || i >= this.frameCount()) throw new Error('Illegal frame index.');
977 this.selected_frame = i;
980 ExecutionState.prototype.selectedFrame = function() {
981 return this.selected_frame;
984 ExecutionState.prototype.debugCommandProcessor = function(opt_is_running) {
985 return new DebugCommandProcessor(this, opt_is_running);
989 function MakeBreakEvent(exec_state, break_points_hit) {
990 return new BreakEvent(exec_state, break_points_hit);
994 function BreakEvent(exec_state, break_points_hit) {
995 this.exec_state_ = exec_state;
996 this.break_points_hit_ = break_points_hit;
1000 BreakEvent.prototype.executionState = function() {
1001 return this.exec_state_;
1005 BreakEvent.prototype.eventType = function() {
1006 return Debug.DebugEvent.Break;
1010 BreakEvent.prototype.func = function() {
1011 return this.exec_state_.frame(0).func();
1015 BreakEvent.prototype.sourceLine = function() {
1016 return this.exec_state_.frame(0).sourceLine();
1020 BreakEvent.prototype.sourceColumn = function() {
1021 return this.exec_state_.frame(0).sourceColumn();
1025 BreakEvent.prototype.sourceLineText = function() {
1026 return this.exec_state_.frame(0).sourceLineText();
1030 BreakEvent.prototype.breakPointsHit = function() {
1031 return this.break_points_hit_;
1035 BreakEvent.prototype.toJSONProtocol = function() {
1036 var o = { seq: next_response_seq++,
1039 body: { invocationText: this.exec_state_.frame(0).invocationText(),
1043 // Add script related information to the event if available.
1044 var script = this.func().script();
1046 o.body.sourceLine = this.sourceLine(),
1047 o.body.sourceColumn = this.sourceColumn(),
1048 o.body.sourceLineText = this.sourceLineText(),
1049 o.body.script = MakeScriptObject_(script, false);
1052 // Add an Array of break points hit if any.
1053 if (this.breakPointsHit()) {
1054 o.body.breakpoints = [];
1055 for (var i = 0; i < this.breakPointsHit().length; i++) {
1056 // Find the break point number. For break points originating from a
1057 // script break point supply the script break point number.
1058 var breakpoint = this.breakPointsHit()[i];
1059 var script_break_point = breakpoint.script_break_point();
1061 if (script_break_point) {
1062 number = script_break_point.number();
1064 number = breakpoint.number();
1066 o.body.breakpoints.push(number);
1069 return JSON.stringify(ObjectToProtocolObject_(o));
1073 function MakeExceptionEvent(exec_state, exception, uncaught) {
1074 return new ExceptionEvent(exec_state, exception, uncaught);
1078 function ExceptionEvent(exec_state, exception, uncaught) {
1079 this.exec_state_ = exec_state;
1080 this.exception_ = exception;
1081 this.uncaught_ = uncaught;
1085 ExceptionEvent.prototype.executionState = function() {
1086 return this.exec_state_;
1090 ExceptionEvent.prototype.eventType = function() {
1091 return Debug.DebugEvent.Exception;
1095 ExceptionEvent.prototype.exception = function() {
1096 return this.exception_;
1100 ExceptionEvent.prototype.uncaught = function() {
1101 return this.uncaught_;
1105 ExceptionEvent.prototype.func = function() {
1106 return this.exec_state_.frame(0).func();
1110 ExceptionEvent.prototype.sourceLine = function() {
1111 return this.exec_state_.frame(0).sourceLine();
1115 ExceptionEvent.prototype.sourceColumn = function() {
1116 return this.exec_state_.frame(0).sourceColumn();
1120 ExceptionEvent.prototype.sourceLineText = function() {
1121 return this.exec_state_.frame(0).sourceLineText();
1125 ExceptionEvent.prototype.toJSONProtocol = function() {
1126 var o = new ProtocolMessage();
1127 o.event = "exception";
1128 o.body = { uncaught: this.uncaught_,
1129 exception: MakeMirror(this.exception_)
1132 // Exceptions might happen whithout any JavaScript frames.
1133 if (this.exec_state_.frameCount() > 0) {
1134 o.body.sourceLine = this.sourceLine();
1135 o.body.sourceColumn = this.sourceColumn();
1136 o.body.sourceLineText = this.sourceLineText();
1138 // Add script information to the event if available.
1139 var script = this.func().script();
1141 o.body.script = MakeScriptObject_(script, false);
1144 o.body.sourceLine = -1;
1147 return o.toJSONProtocol();
1151 function MakeCompileEvent(exec_state, script, before) {
1152 return new CompileEvent(exec_state, script, before);
1156 function CompileEvent(exec_state, script, before) {
1157 this.exec_state_ = exec_state;
1158 this.script_ = MakeMirror(script);
1159 this.before_ = before;
1163 CompileEvent.prototype.executionState = function() {
1164 return this.exec_state_;
1168 CompileEvent.prototype.eventType = function() {
1170 return Debug.DebugEvent.BeforeCompile;
1172 return Debug.DebugEvent.AfterCompile;
1177 CompileEvent.prototype.script = function() {
1178 return this.script_;
1182 CompileEvent.prototype.toJSONProtocol = function() {
1183 var o = new ProtocolMessage();
1186 o.event = "beforeCompile";
1188 o.event = "afterCompile";
1191 o.body.script = this.script_;
1193 return o.toJSONProtocol();
1197 function MakeNewFunctionEvent(func) {
1198 return new NewFunctionEvent(func);
1202 function NewFunctionEvent(func) {
1207 NewFunctionEvent.prototype.eventType = function() {
1208 return Debug.DebugEvent.NewFunction;
1212 NewFunctionEvent.prototype.name = function() {
1213 return this.func.name;
1217 NewFunctionEvent.prototype.setBreakPoint = function(p) {
1218 Debug.setBreakPoint(this.func, p || 0);
1222 function MakeScriptCollectedEvent(exec_state, id) {
1223 return new ScriptCollectedEvent(exec_state, id);
1227 function ScriptCollectedEvent(exec_state, id) {
1228 this.exec_state_ = exec_state;
1233 ScriptCollectedEvent.prototype.id = function() {
1238 ScriptCollectedEvent.prototype.executionState = function() {
1239 return this.exec_state_;
1243 ScriptCollectedEvent.prototype.toJSONProtocol = function() {
1244 var o = new ProtocolMessage();
1246 o.event = "scriptCollected";
1248 o.body.script = { id: this.id() };
1249 return o.toJSONProtocol();
1253 function MakeScriptObject_(script, include_source) {
1254 var o = { id: script.id(),
1255 name: script.name(),
1256 lineOffset: script.lineOffset(),
1257 columnOffset: script.columnOffset(),
1258 lineCount: script.lineCount(),
1260 if (!IS_UNDEFINED(script.data())) {
1261 o.data = script.data();
1263 if (include_source) {
1264 o.source = script.source();
1270 function DebugCommandProcessor(exec_state, opt_is_running) {
1271 this.exec_state_ = exec_state;
1272 this.running_ = opt_is_running || false;
1276 DebugCommandProcessor.prototype.processDebugRequest = function (request) {
1277 return this.processDebugJSONRequest(request);
1281 function ProtocolMessage(request) {
1282 // Update sequence number.
1283 this.seq = next_response_seq++;
1286 // If message is based on a request this is a response. Fill the initial
1287 // response from the request.
1288 this.type = 'response';
1289 this.request_seq = request.seq;
1290 this.command = request.command;
1292 // If message is not based on a request it is a dabugger generated event.
1293 this.type = 'event';
1295 this.success = true;
1296 // Handler may set this field to control debugger state.
1297 this.running = undefined;
1301 ProtocolMessage.prototype.setOption = function(name, value) {
1302 if (!this.options_) {
1305 this.options_[name] = value;
1309 ProtocolMessage.prototype.failed = function(message) {
1310 this.success = false;
1311 this.message = message;
1315 ProtocolMessage.prototype.toJSONProtocol = function() {
1316 // Encode the protocol header.
1319 if (this.request_seq) {
1320 json.request_seq = this.request_seq;
1322 json.type = this.type;
1324 json.event = this.event;
1327 json.command = this.command;
1330 json.success = this.success;
1332 json.success = false;
1335 // Encode the body part.
1337 var serializer = MakeMirrorSerializer(true, this.options_);
1338 if (this.body instanceof Mirror) {
1339 bodyJson = serializer.serializeValue(this.body);
1340 } else if (this.body instanceof Array) {
1342 for (var i = 0; i < this.body.length; i++) {
1343 if (this.body[i] instanceof Mirror) {
1344 bodyJson.push(serializer.serializeValue(this.body[i]));
1346 bodyJson.push(ObjectToProtocolObject_(this.body[i], serializer));
1350 bodyJson = ObjectToProtocolObject_(this.body, serializer);
1352 json.body = bodyJson;
1353 json.refs = serializer.serializeReferencedObjects();
1356 json.message = this.message;
1358 json.running = this.running;
1359 return JSON.stringify(json);
1363 DebugCommandProcessor.prototype.createResponse = function(request) {
1364 return new ProtocolMessage(request);
1368 DebugCommandProcessor.prototype.processDebugJSONRequest = function(
1370 var request; // Current request.
1371 var response; // Generated response.
1374 // Convert the JSON string to an object.
1375 request = JSON.parse(json_request);
1377 // Create an initial response.
1378 response = this.createResponse(request);
1380 if (!request.type) {
1381 throw new Error('Type not specified');
1384 if (request.type != 'request') {
1385 throw new Error("Illegal type '" + request.type + "' in request");
1388 if (!request.command) {
1389 throw new Error('Command not specified');
1392 if (request.arguments) {
1393 var args = request.arguments;
1394 // TODO(yurys): remove request.arguments.compactFormat check once
1395 // ChromeDevTools are switched to 'inlineRefs'
1396 if (args.inlineRefs || args.compactFormat) {
1397 response.setOption('inlineRefs', true);
1399 if (!IS_UNDEFINED(args.maxStringLength)) {
1400 response.setOption('maxStringLength', args.maxStringLength);
1404 if (request.command == 'continue') {
1405 this.continueRequest_(request, response);
1406 } else if (request.command == 'break') {
1407 this.breakRequest_(request, response);
1408 } else if (request.command == 'setbreakpoint') {
1409 this.setBreakPointRequest_(request, response);
1410 } else if (request.command == 'changebreakpoint') {
1411 this.changeBreakPointRequest_(request, response);
1412 } else if (request.command == 'clearbreakpoint') {
1413 this.clearBreakPointRequest_(request, response);
1414 } else if (request.command == 'clearbreakpointgroup') {
1415 this.clearBreakPointGroupRequest_(request, response);
1416 } else if (request.command == 'disconnect') {
1417 this.disconnectRequest_(request, response);
1418 } else if (request.command == 'setexceptionbreak') {
1419 this.setExceptionBreakRequest_(request, response);
1420 } else if (request.command == 'listbreakpoints') {
1421 this.listBreakpointsRequest_(request, response);
1422 } else if (request.command == 'backtrace') {
1423 this.backtraceRequest_(request, response);
1424 } else if (request.command == 'frame') {
1425 this.frameRequest_(request, response);
1426 } else if (request.command == 'scopes') {
1427 this.scopesRequest_(request, response);
1428 } else if (request.command == 'scope') {
1429 this.scopeRequest_(request, response);
1430 } else if (request.command == 'evaluate') {
1431 this.evaluateRequest_(request, response);
1432 } else if (lol_is_enabled && request.command == 'getobj') {
1433 this.getobjRequest_(request, response);
1434 } else if (request.command == 'lookup') {
1435 this.lookupRequest_(request, response);
1436 } else if (request.command == 'references') {
1437 this.referencesRequest_(request, response);
1438 } else if (request.command == 'source') {
1439 this.sourceRequest_(request, response);
1440 } else if (request.command == 'scripts') {
1441 this.scriptsRequest_(request, response);
1442 } else if (request.command == 'threads') {
1443 this.threadsRequest_(request, response);
1444 } else if (request.command == 'suspend') {
1445 this.suspendRequest_(request, response);
1446 } else if (request.command == 'version') {
1447 this.versionRequest_(request, response);
1448 } else if (request.command == 'profile') {
1449 this.profileRequest_(request, response);
1450 } else if (request.command == 'changelive') {
1451 this.changeLiveRequest_(request, response);
1452 } else if (request.command == 'flags') {
1453 this.debuggerFlagsRequest_(request, response);
1454 } else if (request.command == 'v8flags') {
1455 this.v8FlagsRequest_(request, response);
1458 } else if (request.command == 'gc') {
1459 this.gcRequest_(request, response);
1461 // LiveObjectList tools:
1462 } else if (lol_is_enabled && request.command == 'lol-capture') {
1463 this.lolCaptureRequest_(request, response);
1464 } else if (lol_is_enabled && request.command == 'lol-delete') {
1465 this.lolDeleteRequest_(request, response);
1466 } else if (lol_is_enabled && request.command == 'lol-diff') {
1467 this.lolDiffRequest_(request, response);
1468 } else if (lol_is_enabled && request.command == 'lol-getid') {
1469 this.lolGetIdRequest_(request, response);
1470 } else if (lol_is_enabled && request.command == 'lol-info') {
1471 this.lolInfoRequest_(request, response);
1472 } else if (lol_is_enabled && request.command == 'lol-reset') {
1473 this.lolResetRequest_(request, response);
1474 } else if (lol_is_enabled && request.command == 'lol-retainers') {
1475 this.lolRetainersRequest_(request, response);
1476 } else if (lol_is_enabled && request.command == 'lol-path') {
1477 this.lolPathRequest_(request, response);
1478 } else if (lol_is_enabled && request.command == 'lol-print') {
1479 this.lolPrintRequest_(request, response);
1480 } else if (lol_is_enabled && request.command == 'lol-stats') {
1481 this.lolStatsRequest_(request, response);
1484 throw new Error('Unknown command "' + request.command + '" in request');
1487 // If there is no response object created one (without command).
1489 response = this.createResponse();
1491 response.success = false;
1492 response.message = %ToString(e);
1495 // Return the response as a JSON encoded string.
1497 if (!IS_UNDEFINED(response.running)) {
1498 // Response controls running state.
1499 this.running_ = response.running;
1501 response.running = this.running_;
1502 return response.toJSONProtocol();
1504 // Failed to generate response - return generic error.
1505 return '{"seq":' + response.seq + ',' +
1506 '"request_seq":' + request.seq + ',' +
1507 '"type":"response",' +
1508 '"success":false,' +
1509 '"message":"Internal error: ' + %ToString(e) + '"}';
1512 // Failed in one of the catch blocks above - most generic error.
1513 return '{"seq":0,"type":"response","success":false,"message":"Internal error"}';
1518 DebugCommandProcessor.prototype.continueRequest_ = function(request, response) {
1519 // Check for arguments for continue.
1520 if (request.arguments) {
1522 var action = Debug.StepAction.StepIn;
1524 // Pull out arguments.
1525 var stepaction = request.arguments.stepaction;
1526 var stepcount = request.arguments.stepcount;
1528 // Get the stepcount argument if any.
1530 count = %ToNumber(stepcount);
1532 throw new Error('Invalid stepcount argument "' + stepcount + '".');
1536 // Get the stepaction argument.
1538 if (stepaction == 'in') {
1539 action = Debug.StepAction.StepIn;
1540 } else if (stepaction == 'min') {
1541 action = Debug.StepAction.StepMin;
1542 } else if (stepaction == 'next') {
1543 action = Debug.StepAction.StepNext;
1544 } else if (stepaction == 'out') {
1545 action = Debug.StepAction.StepOut;
1547 throw new Error('Invalid stepaction argument "' + stepaction + '".');
1551 // Set up the VM for stepping.
1552 this.exec_state_.prepareStep(action, count);
1555 // VM should be running after executing this request.
1556 response.running = true;
1560 DebugCommandProcessor.prototype.breakRequest_ = function(request, response) {
1561 // Ignore as break command does not do anything when broken.
1565 DebugCommandProcessor.prototype.setBreakPointRequest_ =
1566 function(request, response) {
1567 // Check for legal request.
1568 if (!request.arguments) {
1569 response.failed('Missing arguments');
1573 // Pull out arguments.
1574 var type = request.arguments.type;
1575 var target = request.arguments.target;
1576 var line = request.arguments.line;
1577 var column = request.arguments.column;
1578 var enabled = IS_UNDEFINED(request.arguments.enabled) ?
1579 true : request.arguments.enabled;
1580 var condition = request.arguments.condition;
1581 var ignoreCount = request.arguments.ignoreCount;
1582 var groupId = request.arguments.groupId;
1584 // Check for legal arguments.
1585 if (!type || IS_UNDEFINED(target)) {
1586 response.failed('Missing argument "type" or "target"');
1590 // Either function or script break point.
1591 var break_point_number;
1592 if (type == 'function') {
1593 // Handle function break point.
1594 if (!IS_STRING(target)) {
1595 response.failed('Argument "target" is not a string value');
1600 // Find the function through a global evaluate.
1601 f = this.exec_state_.evaluateGlobal(target).value();
1603 response.failed('Error: "' + %ToString(e) +
1604 '" evaluating "' + target + '"');
1607 if (!IS_FUNCTION(f)) {
1608 response.failed('"' + target + '" does not evaluate to a function');
1612 // Set function break point.
1613 break_point_number = Debug.setBreakPoint(f, line, column, condition);
1614 } else if (type == 'handle') {
1615 // Find the object pointed by the specified handle.
1616 var handle = parseInt(target, 10);
1617 var mirror = LookupMirror(handle);
1619 return response.failed('Object #' + handle + '# not found');
1621 if (!mirror.isFunction()) {
1622 return response.failed('Object #' + handle + '# is not a function');
1625 // Set function break point.
1626 break_point_number = Debug.setBreakPoint(mirror.value(),
1627 line, column, condition);
1628 } else if (type == 'script') {
1629 // set script break point.
1630 break_point_number =
1631 Debug.setScriptBreakPointByName(target, line, column, condition,
1633 } else if (type == 'scriptId') {
1634 break_point_number =
1635 Debug.setScriptBreakPointById(target, line, column, condition, groupId);
1636 } else if (type == 'scriptRegExp') {
1637 break_point_number =
1638 Debug.setScriptBreakPointByRegExp(target, line, column, condition,
1641 response.failed('Illegal type "' + type + '"');
1645 // Set additional break point properties.
1646 var break_point = Debug.findBreakPoint(break_point_number);
1648 Debug.changeBreakPointIgnoreCount(break_point_number, ignoreCount);
1651 Debug.disableBreakPoint(break_point_number);
1654 // Add the break point number to the response.
1655 response.body = { type: type,
1656 breakpoint: break_point_number };
1658 // Add break point information to the response.
1659 if (break_point instanceof ScriptBreakPoint) {
1660 if (break_point.type() == Debug.ScriptBreakPointType.ScriptId) {
1661 response.body.type = 'scriptId';
1662 response.body.script_id = break_point.script_id();
1663 } else if (break_point.type() == Debug.ScriptBreakPointType.ScriptName) {
1664 response.body.type = 'scriptName';
1665 response.body.script_name = break_point.script_name();
1666 } else if (break_point.type() == Debug.ScriptBreakPointType.ScriptRegExp) {
1667 response.body.type = 'scriptRegExp';
1668 response.body.script_regexp = break_point.script_regexp_object().source;
1670 throw new Error("Internal error: Unexpected breakpoint type: " +
1671 break_point.type());
1673 response.body.line = break_point.line();
1674 response.body.column = break_point.column();
1675 response.body.actual_locations = break_point.actual_locations();
1677 response.body.type = 'function';
1678 response.body.actual_locations = [break_point.actual_location];
1683 DebugCommandProcessor.prototype.changeBreakPointRequest_ = function(
1684 request, response) {
1685 // Check for legal request.
1686 if (!request.arguments) {
1687 response.failed('Missing arguments');
1691 // Pull out arguments.
1692 var break_point = %ToNumber(request.arguments.breakpoint);
1693 var enabled = request.arguments.enabled;
1694 var condition = request.arguments.condition;
1695 var ignoreCount = request.arguments.ignoreCount;
1697 // Check for legal arguments.
1699 response.failed('Missing argument "breakpoint"');
1703 // Change enabled state if supplied.
1704 if (!IS_UNDEFINED(enabled)) {
1706 Debug.enableBreakPoint(break_point);
1708 Debug.disableBreakPoint(break_point);
1712 // Change condition if supplied
1713 if (!IS_UNDEFINED(condition)) {
1714 Debug.changeBreakPointCondition(break_point, condition);
1717 // Change ignore count if supplied
1718 if (!IS_UNDEFINED(ignoreCount)) {
1719 Debug.changeBreakPointIgnoreCount(break_point, ignoreCount);
1724 DebugCommandProcessor.prototype.clearBreakPointGroupRequest_ = function(
1725 request, response) {
1726 // Check for legal request.
1727 if (!request.arguments) {
1728 response.failed('Missing arguments');
1732 // Pull out arguments.
1733 var group_id = request.arguments.groupId;
1735 // Check for legal arguments.
1737 response.failed('Missing argument "groupId"');
1741 var cleared_break_points = [];
1742 var new_script_break_points = [];
1743 for (var i = 0; i < script_break_points.length; i++) {
1744 var next_break_point = script_break_points[i];
1745 if (next_break_point.groupId() == group_id) {
1746 cleared_break_points.push(next_break_point.number());
1747 next_break_point.clear();
1749 new_script_break_points.push(next_break_point);
1752 script_break_points = new_script_break_points;
1754 // Add the cleared break point numbers to the response.
1755 response.body = { breakpoints: cleared_break_points };
1759 DebugCommandProcessor.prototype.clearBreakPointRequest_ = function(
1760 request, response) {
1761 // Check for legal request.
1762 if (!request.arguments) {
1763 response.failed('Missing arguments');
1767 // Pull out arguments.
1768 var break_point = %ToNumber(request.arguments.breakpoint);
1770 // Check for legal arguments.
1772 response.failed('Missing argument "breakpoint"');
1776 // Clear break point.
1777 Debug.clearBreakPoint(break_point);
1779 // Add the cleared break point number to the response.
1780 response.body = { breakpoint: break_point };
1784 DebugCommandProcessor.prototype.listBreakpointsRequest_ = function(
1785 request, response) {
1787 for (var i = 0; i < script_break_points.length; i++) {
1788 var break_point = script_break_points[i];
1791 number: break_point.number(),
1792 line: break_point.line(),
1793 column: break_point.column(),
1794 groupId: break_point.groupId(),
1795 hit_count: break_point.hit_count(),
1796 active: break_point.active(),
1797 condition: break_point.condition(),
1798 ignoreCount: break_point.ignoreCount(),
1799 actual_locations: break_point.actual_locations()
1802 if (break_point.type() == Debug.ScriptBreakPointType.ScriptId) {
1803 description.type = 'scriptId';
1804 description.script_id = break_point.script_id();
1805 } else if (break_point.type() == Debug.ScriptBreakPointType.ScriptName) {
1806 description.type = 'scriptName';
1807 description.script_name = break_point.script_name();
1808 } else if (break_point.type() == Debug.ScriptBreakPointType.ScriptRegExp) {
1809 description.type = 'scriptRegExp';
1810 description.script_regexp = break_point.script_regexp_object().source;
1812 throw new Error("Internal error: Unexpected breakpoint type: " +
1813 break_point.type());
1815 array.push(description);
1820 breakOnExceptions: Debug.isBreakOnException(),
1821 breakOnUncaughtExceptions: Debug.isBreakOnUncaughtException()
1826 DebugCommandProcessor.prototype.disconnectRequest_ =
1827 function(request, response) {
1828 Debug.disableAllBreakPoints();
1829 this.continueRequest_(request, response);
1833 DebugCommandProcessor.prototype.setExceptionBreakRequest_ =
1834 function(request, response) {
1835 // Check for legal request.
1836 if (!request.arguments) {
1837 response.failed('Missing arguments');
1841 // Pull out and check the 'type' argument:
1842 var type = request.arguments.type;
1844 response.failed('Missing argument "type"');
1848 // Initialize the default value of enable:
1850 if (type == 'all') {
1851 enabled = !Debug.isBreakOnException();
1852 } else if (type == 'uncaught') {
1853 enabled = !Debug.isBreakOnUncaughtException();
1856 // Pull out and check the 'enabled' argument if present:
1857 if (!IS_UNDEFINED(request.arguments.enabled)) {
1858 enabled = request.arguments.enabled;
1859 if ((enabled != true) && (enabled != false)) {
1860 response.failed('Illegal value for "enabled":"' + enabled + '"');
1864 // Now set the exception break state:
1865 if (type == 'all') {
1866 %ChangeBreakOnException(Debug.ExceptionBreak.Caught, enabled);
1867 } else if (type == 'uncaught') {
1868 %ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, enabled);
1870 response.failed('Unknown "type":"' + type + '"');
1873 // Add the cleared break point number to the response.
1874 response.body = { 'type': type, 'enabled': enabled };
1878 DebugCommandProcessor.prototype.backtraceRequest_ = function(
1879 request, response) {
1880 // Get the number of frames.
1881 var total_frames = this.exec_state_.frameCount();
1883 // Create simple response if there are no frames.
1884 if (total_frames == 0) {
1886 totalFrames: total_frames
1891 // Default frame range to include in backtrace.
1893 var to_index = kDefaultBacktraceLength;
1895 // Get the range from the arguments.
1896 if (request.arguments) {
1897 if (request.arguments.fromFrame) {
1898 from_index = request.arguments.fromFrame;
1900 if (request.arguments.toFrame) {
1901 to_index = request.arguments.toFrame;
1903 if (request.arguments.bottom) {
1904 var tmp_index = total_frames - from_index;
1905 from_index = total_frames - to_index;
1906 to_index = tmp_index;
1908 if (from_index < 0 || to_index < 0) {
1909 return response.failed('Invalid frame number');
1913 // Adjust the index.
1914 to_index = Math.min(total_frames, to_index);
1916 if (to_index <= from_index) {
1917 var error = 'Invalid frame range';
1918 return response.failed(error);
1921 // Create the response body.
1923 for (var i = from_index; i < to_index; i++) {
1924 frames.push(this.exec_state_.frame(i));
1927 fromFrame: from_index,
1929 totalFrames: total_frames,
1935 DebugCommandProcessor.prototype.frameRequest_ = function(request, response) {
1936 // No frames no source.
1937 if (this.exec_state_.frameCount() == 0) {
1938 return response.failed('No frames');
1941 // With no arguments just keep the selected frame.
1942 if (request.arguments) {
1943 var index = request.arguments.number;
1944 if (index < 0 || this.exec_state_.frameCount() <= index) {
1945 return response.failed('Invalid frame number');
1948 this.exec_state_.setSelectedFrame(request.arguments.number);
1950 response.body = this.exec_state_.frame();
1954 DebugCommandProcessor.prototype.frameForScopeRequest_ = function(request) {
1955 // Get the frame for which the scope or scopes are requested.
1956 // With no frameNumber argument use the currently selected frame.
1957 if (request.arguments && !IS_UNDEFINED(request.arguments.frameNumber)) {
1958 frame_index = request.arguments.frameNumber;
1959 if (frame_index < 0 || this.exec_state_.frameCount() <= frame_index) {
1960 throw new Error('Invalid frame number');
1962 return this.exec_state_.frame(frame_index);
1964 return this.exec_state_.frame();
1969 // Gets scope host object from request. It is either a function
1970 // ('functionHandle' argument must be specified) or a stack frame
1971 // ('frameNumber' may be specified and the current frame is taken by default).
1972 DebugCommandProcessor.prototype.scopeHolderForScopeRequest_ =
1974 if (request.arguments && "functionHandle" in request.arguments) {
1975 if (!IS_NUMBER(request.arguments.functionHandle)) {
1976 throw new Error('Function handle must be a number');
1978 var function_mirror = LookupMirror(request.arguments.functionHandle);
1979 if (!function_mirror) {
1980 throw new Error('Failed to find function object by handle');
1982 if (!function_mirror.isFunction()) {
1983 throw new Error('Value of non-function type is found by handle');
1985 return function_mirror;
1987 // No frames no scopes.
1988 if (this.exec_state_.frameCount() == 0) {
1989 throw new Error('No scopes');
1992 // Get the frame for which the scopes are requested.
1993 var frame = this.frameForScopeRequest_(request);
1999 DebugCommandProcessor.prototype.scopesRequest_ = function(request, response) {
2000 var scope_holder = this.scopeHolderForScopeRequest_(request);
2002 // Fill all scopes for this frame or function.
2003 var total_scopes = scope_holder.scopeCount();
2005 for (var i = 0; i < total_scopes; i++) {
2006 scopes.push(scope_holder.scope(i));
2010 toScope: total_scopes,
2011 totalScopes: total_scopes,
2017 DebugCommandProcessor.prototype.scopeRequest_ = function(request, response) {
2018 // Get the frame or function for which the scope is requested.
2019 var scope_holder = this.scopeHolderForScopeRequest_(request);
2021 // With no scope argument just return top scope.
2022 var scope_index = 0;
2023 if (request.arguments && !IS_UNDEFINED(request.arguments.number)) {
2024 scope_index = %ToNumber(request.arguments.number);
2025 if (scope_index < 0 || scope_holder.scopeCount() <= scope_index) {
2026 return response.failed('Invalid scope number');
2030 response.body = scope_holder.scope(scope_index);
2034 DebugCommandProcessor.prototype.evaluateRequest_ = function(request, response) {
2035 if (!request.arguments) {
2036 return response.failed('Missing arguments');
2039 // Pull out arguments.
2040 var expression = request.arguments.expression;
2041 var frame = request.arguments.frame;
2042 var global = request.arguments.global;
2043 var disable_break = request.arguments.disable_break;
2044 var additional_context = request.arguments.additional_context;
2046 // The expression argument could be an integer so we convert it to a
2049 expression = String(expression);
2051 return response.failed('Failed to convert expression argument to string');
2054 // Check for legal arguments.
2055 if (!IS_UNDEFINED(frame) && global) {
2056 return response.failed('Arguments "frame" and "global" are exclusive');
2059 var additional_context_object;
2060 if (additional_context) {
2061 additional_context_object = {};
2062 for (var i = 0; i < additional_context.length; i++) {
2063 var mapping = additional_context[i];
2064 if (!IS_STRING(mapping.name) || !IS_NUMBER(mapping.handle)) {
2065 return response.failed("Context element #" + i +
2066 " must contain name:string and handle:number");
2068 var context_value_mirror = LookupMirror(mapping.handle);
2069 if (!context_value_mirror) {
2070 return response.failed("Context object '" + mapping.name +
2071 "' #" + mapping.handle + "# not found");
2073 additional_context_object[mapping.name] = context_value_mirror.value();
2079 // Evaluate in the global context.
2080 response.body = this.exec_state_.evaluateGlobal(
2081 expression, Boolean(disable_break), additional_context_object);
2085 // Default value for disable_break is true.
2086 if (IS_UNDEFINED(disable_break)) {
2087 disable_break = true;
2090 // No frames no evaluate in frame.
2091 if (this.exec_state_.frameCount() == 0) {
2092 return response.failed('No frames');
2095 // Check whether a frame was specified.
2096 if (!IS_UNDEFINED(frame)) {
2097 var frame_number = %ToNumber(frame);
2098 if (frame_number < 0 || frame_number >= this.exec_state_.frameCount()) {
2099 return response.failed('Invalid frame "' + frame + '"');
2101 // Evaluate in the specified frame.
2102 response.body = this.exec_state_.frame(frame_number).evaluate(
2103 expression, Boolean(disable_break), additional_context_object);
2106 // Evaluate in the selected frame.
2107 response.body = this.exec_state_.frame().evaluate(
2108 expression, Boolean(disable_break), additional_context_object);
2114 DebugCommandProcessor.prototype.getobjRequest_ = function(request, response) {
2115 if (!request.arguments) {
2116 return response.failed('Missing arguments');
2119 // Pull out arguments.
2120 var obj_id = request.arguments.obj_id;
2122 // Check for legal arguments.
2123 if (IS_UNDEFINED(obj_id)) {
2124 return response.failed('Argument "obj_id" missing');
2128 response.body = MakeMirror(%GetLOLObj(obj_id));
2132 DebugCommandProcessor.prototype.lookupRequest_ = function(request, response) {
2133 if (!request.arguments) {
2134 return response.failed('Missing arguments');
2137 // Pull out arguments.
2138 var handles = request.arguments.handles;
2140 // Check for legal arguments.
2141 if (IS_UNDEFINED(handles)) {
2142 return response.failed('Argument "handles" missing');
2145 // Set 'includeSource' option for script lookup.
2146 if (!IS_UNDEFINED(request.arguments.includeSource)) {
2147 includeSource = %ToBoolean(request.arguments.includeSource);
2148 response.setOption('includeSource', includeSource);
2153 for (var i = 0; i < handles.length; i++) {
2154 var handle = handles[i];
2155 var mirror = LookupMirror(handle);
2157 return response.failed('Object #' + handle + '# not found');
2159 mirrors[handle] = mirror;
2161 response.body = mirrors;
2165 DebugCommandProcessor.prototype.referencesRequest_ =
2166 function(request, response) {
2167 if (!request.arguments) {
2168 return response.failed('Missing arguments');
2171 // Pull out arguments.
2172 var type = request.arguments.type;
2173 var handle = request.arguments.handle;
2175 // Check for legal arguments.
2176 if (IS_UNDEFINED(type)) {
2177 return response.failed('Argument "type" missing');
2179 if (IS_UNDEFINED(handle)) {
2180 return response.failed('Argument "handle" missing');
2182 if (type != 'referencedBy' && type != 'constructedBy') {
2183 return response.failed('Invalid type "' + type + '"');
2186 // Lookup handle and return objects with references the object.
2187 var mirror = LookupMirror(handle);
2189 if (type == 'referencedBy') {
2190 response.body = mirror.referencedBy();
2192 response.body = mirror.constructedBy();
2195 return response.failed('Object #' + handle + '# not found');
2200 DebugCommandProcessor.prototype.sourceRequest_ = function(request, response) {
2201 // No frames no source.
2202 if (this.exec_state_.frameCount() == 0) {
2203 return response.failed('No source');
2208 var frame = this.exec_state_.frame();
2209 if (request.arguments) {
2210 // Pull out arguments.
2211 from_line = request.arguments.fromLine;
2212 to_line = request.arguments.toLine;
2214 if (!IS_UNDEFINED(request.arguments.frame)) {
2215 var frame_number = %ToNumber(request.arguments.frame);
2216 if (frame_number < 0 || frame_number >= this.exec_state_.frameCount()) {
2217 return response.failed('Invalid frame "' + frame + '"');
2219 frame = this.exec_state_.frame(frame_number);
2223 // Get the script selected.
2224 var script = frame.func().script();
2226 return response.failed('No source');
2229 // Get the source slice and fill it into the response.
2230 var slice = script.sourceSlice(from_line, to_line);
2232 return response.failed('Invalid line interval');
2235 response.body.source = slice.sourceText();
2236 response.body.fromLine = slice.from_line;
2237 response.body.toLine = slice.to_line;
2238 response.body.fromPosition = slice.from_position;
2239 response.body.toPosition = slice.to_position;
2240 response.body.totalLines = script.lineCount();
2244 DebugCommandProcessor.prototype.scriptsRequest_ = function(request, response) {
2245 var types = ScriptTypeFlag(Debug.ScriptType.Normal);
2246 var includeSource = false;
2247 var idsToInclude = null;
2248 if (request.arguments) {
2249 // Pull out arguments.
2250 if (!IS_UNDEFINED(request.arguments.types)) {
2251 types = %ToNumber(request.arguments.types);
2252 if (isNaN(types) || types < 0) {
2253 return response.failed('Invalid types "' +
2254 request.arguments.types + '"');
2258 if (!IS_UNDEFINED(request.arguments.includeSource)) {
2259 includeSource = %ToBoolean(request.arguments.includeSource);
2260 response.setOption('includeSource', includeSource);
2263 if (IS_ARRAY(request.arguments.ids)) {
2265 var ids = request.arguments.ids;
2266 for (var i = 0; i < ids.length; i++) {
2267 idsToInclude[ids[i]] = true;
2271 var filterStr = null;
2272 var filterNum = null;
2273 if (!IS_UNDEFINED(request.arguments.filter)) {
2274 var num = %ToNumber(request.arguments.filter);
2278 filterStr = request.arguments.filter;
2282 // Collect all scripts in the heap.
2283 var scripts = %DebugGetLoadedScripts();
2287 for (var i = 0; i < scripts.length; i++) {
2288 if (idsToInclude && !idsToInclude[scripts[i].id]) {
2291 if (filterStr || filterNum) {
2292 var script = scripts[i];
2294 if (filterNum && !found) {
2295 if (script.id && script.id === filterNum) {
2299 if (filterStr && !found) {
2300 if (script.name && script.name.indexOf(filterStr) >= 0) {
2304 if (!found) continue;
2306 if (types & ScriptTypeFlag(scripts[i].type)) {
2307 response.body.push(MakeMirror(scripts[i]));
2313 DebugCommandProcessor.prototype.threadsRequest_ = function(request, response) {
2314 // Get the number of threads.
2315 var total_threads = this.exec_state_.threadCount();
2317 // Get information for all threads.
2319 for (var i = 0; i < total_threads; i++) {
2320 var details = %GetThreadDetails(this.exec_state_.break_id, i);
2321 var thread_info = { current: details[0],
2324 threads.push(thread_info);
2327 // Create the response body.
2329 totalThreads: total_threads,
2335 DebugCommandProcessor.prototype.suspendRequest_ = function(request, response) {
2336 response.running = false;
2340 DebugCommandProcessor.prototype.versionRequest_ = function(request, response) {
2342 V8Version: %GetV8Version()
2347 DebugCommandProcessor.prototype.profileRequest_ = function(request, response) {
2348 if (request.arguments.command == 'resume') {
2350 } else if (request.arguments.command == 'pause') {
2353 return response.failed('Unknown command');
2359 DebugCommandProcessor.prototype.changeLiveRequest_ = function(
2360 request, response) {
2361 if (!Debug.LiveEdit) {
2362 return response.failed('LiveEdit feature is not supported');
2364 if (!request.arguments) {
2365 return response.failed('Missing arguments');
2367 var script_id = request.arguments.script_id;
2368 var preview_only = !!request.arguments.preview_only;
2370 var scripts = %DebugGetLoadedScripts();
2372 var the_script = null;
2373 for (var i = 0; i < scripts.length; i++) {
2374 if (scripts[i].id == script_id) {
2375 the_script = scripts[i];
2379 response.failed('Script not found');
2383 var change_log = new Array();
2385 if (!IS_STRING(request.arguments.new_source)) {
2386 throw "new_source argument expected";
2389 var new_source = request.arguments.new_source;
2391 var result_description = Debug.LiveEdit.SetScriptSource(the_script,
2392 new_source, preview_only, change_log);
2393 response.body = {change_log: change_log, result: result_description};
2395 if (!preview_only && !this.running_ && result_description.stack_modified) {
2396 response.body.stepin_recommended = true;
2401 DebugCommandProcessor.prototype.debuggerFlagsRequest_ = function(request,
2403 // Check for legal request.
2404 if (!request.arguments) {
2405 response.failed('Missing arguments');
2409 // Pull out arguments.
2410 var flags = request.arguments.flags;
2412 response.body = { flags: [] };
2413 if (!IS_UNDEFINED(flags)) {
2414 for (var i = 0; i < flags.length; i++) {
2415 var name = flags[i].name;
2416 var debugger_flag = debugger_flags[name];
2417 if (!debugger_flag) {
2420 if ('value' in flags[i]) {
2421 debugger_flag.setValue(flags[i].value);
2423 response.body.flags.push({ name: name, value: debugger_flag.getValue() });
2426 for (var name in debugger_flags) {
2427 var value = debugger_flags[name].getValue();
2428 response.body.flags.push({ name: name, value: value });
2434 DebugCommandProcessor.prototype.v8FlagsRequest_ = function(request, response) {
2435 var flags = request.arguments.flags;
2436 if (!flags) flags = '';
2441 DebugCommandProcessor.prototype.gcRequest_ = function(request, response) {
2442 var type = request.arguments.type;
2443 if (!type) type = 'all';
2445 var before = %GetHeapUsage();
2446 %CollectGarbage(type);
2447 var after = %GetHeapUsage();
2449 response.body = { "before": before, "after": after };
2453 DebugCommandProcessor.prototype.lolCaptureRequest_ =
2454 function(request, response) {
2455 response.body = %CaptureLOL();
2459 DebugCommandProcessor.prototype.lolDeleteRequest_ =
2460 function(request, response) {
2461 var id = request.arguments.id;
2462 var result = %DeleteLOL(id);
2464 response.body = { id: id };
2466 response.failed('Failed to delete: live object list ' + id + ' not found.');
2471 DebugCommandProcessor.prototype.lolDiffRequest_ = function(request, response) {
2472 var id1 = request.arguments.id1;
2473 var id2 = request.arguments.id2;
2474 var verbose = request.arguments.verbose;
2475 var filter = request.arguments.filter;
2476 if (verbose === true) {
2477 var start = request.arguments.start;
2478 var count = request.arguments.count;
2479 response.body = %DumpLOL(id1, id2, start, count, filter);
2481 response.body = %SummarizeLOL(id1, id2, filter);
2486 DebugCommandProcessor.prototype.lolGetIdRequest_ = function(request, response) {
2487 var address = request.arguments.address;
2489 response.body.id = %GetLOLObjId(address);
2493 DebugCommandProcessor.prototype.lolInfoRequest_ = function(request, response) {
2494 var start = request.arguments.start;
2495 var count = request.arguments.count;
2496 response.body = %InfoLOL(start, count);
2500 DebugCommandProcessor.prototype.lolResetRequest_ = function(request, response) {
2505 DebugCommandProcessor.prototype.lolRetainersRequest_ =
2506 function(request, response) {
2507 var id = request.arguments.id;
2508 var verbose = request.arguments.verbose;
2509 var start = request.arguments.start;
2510 var count = request.arguments.count;
2511 var filter = request.arguments.filter;
2513 response.body = %GetLOLObjRetainers(id, Mirror.prototype, verbose,
2514 start, count, filter);
2518 DebugCommandProcessor.prototype.lolPathRequest_ = function(request, response) {
2519 var id1 = request.arguments.id1;
2520 var id2 = request.arguments.id2;
2522 response.body.path = %GetLOLPath(id1, id2, Mirror.prototype);
2526 DebugCommandProcessor.prototype.lolPrintRequest_ = function(request, response) {
2527 var id = request.arguments.id;
2529 response.body.dump = %PrintLOLObj(id);
2533 // Check whether the previously processed command caused the VM to become
2535 DebugCommandProcessor.prototype.isRunning = function() {
2536 return this.running_;
2540 DebugCommandProcessor.prototype.systemBreak = function(cmd, args) {
2541 return %SystemBreak();
2545 function NumberToHex8Str(n) {
2547 for (var i = 0; i < 8; ++i) {
2548 var c = hexCharArray[n & 0x0F]; // hexCharArray is defined in uri.js
2557 * Convert an Object to its debugger protocol representation. The representation
2558 * may be serilized to a JSON object using JSON.stringify().
2559 * This implementation simply runs through all string property names, converts
2560 * each property value to a protocol value and adds the property to the result
2561 * object. For type "object" the function will be called recursively. Note that
2562 * circular structures will cause infinite recursion.
2563 * @param {Object} object The object to format as protocol object.
2564 * @param {MirrorSerializer} mirror_serializer The serializer to use if any
2565 * mirror objects are encountered.
2566 * @return {Object} Protocol object value.
2568 function ObjectToProtocolObject_(object, mirror_serializer) {
2570 for (var key in object) {
2571 // Only consider string keys.
2572 if (typeof key == 'string') {
2573 // Format the value based on its type.
2574 var property_value_json = ValueToProtocolValue_(object[key],
2576 // Add the property if relevant.
2577 if (!IS_UNDEFINED(property_value_json)) {
2578 content[key] = property_value_json;
2588 * Convert an array to its debugger protocol representation. It will convert
2589 * each array element to a protocol value.
2590 * @param {Array} array The array to format as protocol array.
2591 * @param {MirrorSerializer} mirror_serializer The serializer to use if any
2592 * mirror objects are encountered.
2593 * @return {Array} Protocol array value.
2595 function ArrayToProtocolArray_(array, mirror_serializer) {
2597 for (var i = 0; i < array.length; i++) {
2598 json.push(ValueToProtocolValue_(array[i], mirror_serializer));
2605 * Convert a value to its debugger protocol representation.
2606 * @param {*} value The value to format as protocol value.
2607 * @param {MirrorSerializer} mirror_serializer The serializer to use if any
2608 * mirror objects are encountered.
2609 * @return {*} Protocol value.
2611 function ValueToProtocolValue_(value, mirror_serializer) {
2612 // Format the value based on its type.
2614 switch (typeof value) {
2616 if (value instanceof Mirror) {
2617 json = mirror_serializer.serializeValue(value);
2618 } else if (IS_ARRAY(value)){
2619 json = ArrayToProtocolArray_(value, mirror_serializer);
2621 json = ObjectToProtocolObject_(value, mirror_serializer);