ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 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. |
| 15 | // |
| 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. |
| 27 | |
| 28 | // Default number of frames to include in the response to backtrace request. |
| 29 | const kDefaultBacktraceLength = 10; |
| 30 | |
| 31 | const Debug = {}; |
| 32 | |
| 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 |
| 35 | // comments. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 36 | const sourceLineBeginningSkip = /^(?:\s*(?:\/\*.*?\*\/)*)*/; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
| 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, |
| 41 | Exception: 2, |
| 42 | NewFunction: 3, |
| 43 | BeforeCompile: 4, |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 44 | AfterCompile: 5, |
| 45 | ScriptCollected: 6 }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | |
| 47 | // Types of exceptions that can be broken upon. |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 48 | Debug.ExceptionBreak = { Caught : 0, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 49 | Uncaught: 1 }; |
| 50 | |
| 51 | // The different types of steps. |
| 52 | Debug.StepAction = { StepOut: 0, |
| 53 | StepNext: 1, |
| 54 | StepIn: 2, |
| 55 | StepMin: 3, |
| 56 | StepInMin: 4 }; |
| 57 | |
| 58 | // The different types of scripts matching enum ScriptType in objects.h. |
| 59 | Debug.ScriptType = { Native: 0, |
| 60 | Extension: 1, |
| 61 | Normal: 2 }; |
| 62 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 63 | // The different types of script compilations matching enum |
| 64 | // Script::CompilationType in objects.h. |
| 65 | Debug.ScriptCompilationType = { Host: 0, |
| 66 | Eval: 1, |
| 67 | JSON: 2 }; |
| 68 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 69 | // The different script break point types. |
| 70 | Debug.ScriptBreakPointType = { ScriptId: 0, |
| 71 | ScriptName: 1 }; |
| 72 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 73 | function ScriptTypeFlag(type) { |
| 74 | return (1 << type); |
| 75 | } |
| 76 | |
| 77 | // Globals. |
| 78 | var next_response_seq = 0; |
| 79 | var next_break_point_number = 1; |
| 80 | var break_points = []; |
| 81 | var script_break_points = []; |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 82 | var debugger_flags = { |
| 83 | breakPointsActive: { |
| 84 | value: true, |
| 85 | getValue: function() { return this.value; }, |
| 86 | setValue: function(value) { |
| 87 | this.value = !!value; |
| 88 | %SetDisableBreak(!this.value); |
| 89 | } |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 90 | }, |
| 91 | breakOnCaughtException: { |
| 92 | getValue: function() { return Debug.isBreakOnException(); }, |
| 93 | setValue: function(value) { |
| 94 | if (value) { |
| 95 | Debug.setBreakOnException(); |
| 96 | } else { |
| 97 | Debug.clearBreakOnException(); |
| 98 | } |
| 99 | } |
| 100 | }, |
| 101 | breakOnUncaughtException: { |
| 102 | getValue: function() { return Debug.isBreakOnUncaughtException(); }, |
| 103 | setValue: function(value) { |
| 104 | if (value) { |
| 105 | Debug.setBreakOnUncaughtException(); |
| 106 | } else { |
| 107 | Debug.clearBreakOnUncaughtException(); |
| 108 | } |
| 109 | } |
| 110 | }, |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 111 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 112 | |
| 113 | |
| 114 | // Create a new break point object and add it to the list of break points. |
| 115 | function MakeBreakPoint(source_position, opt_line, opt_column, opt_script_break_point) { |
| 116 | var break_point = new BreakPoint(source_position, opt_line, opt_column, opt_script_break_point); |
| 117 | break_points.push(break_point); |
| 118 | return break_point; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 119 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | |
| 121 | |
| 122 | // Object representing a break point. |
| 123 | // NOTE: This object does not have a reference to the function having break |
| 124 | // point as this would cause function not to be garbage collected when it is |
| 125 | // not used any more. We do not want break points to keep functions alive. |
| 126 | function BreakPoint(source_position, opt_line, opt_column, opt_script_break_point) { |
| 127 | this.source_position_ = source_position; |
| 128 | this.source_line_ = opt_line; |
| 129 | this.source_column_ = opt_column; |
| 130 | if (opt_script_break_point) { |
| 131 | this.script_break_point_ = opt_script_break_point; |
| 132 | } else { |
| 133 | this.number_ = next_break_point_number++; |
| 134 | } |
| 135 | this.hit_count_ = 0; |
| 136 | this.active_ = true; |
| 137 | this.condition_ = null; |
| 138 | this.ignoreCount_ = 0; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 139 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | |
| 141 | |
| 142 | BreakPoint.prototype.number = function() { |
| 143 | return this.number_; |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | BreakPoint.prototype.func = function() { |
| 148 | return this.func_; |
| 149 | }; |
| 150 | |
| 151 | |
| 152 | BreakPoint.prototype.source_position = function() { |
| 153 | return this.source_position_; |
| 154 | }; |
| 155 | |
| 156 | |
| 157 | BreakPoint.prototype.hit_count = function() { |
| 158 | return this.hit_count_; |
| 159 | }; |
| 160 | |
| 161 | |
| 162 | BreakPoint.prototype.active = function() { |
| 163 | if (this.script_break_point()) { |
| 164 | return this.script_break_point().active(); |
| 165 | } |
| 166 | return this.active_; |
| 167 | }; |
| 168 | |
| 169 | |
| 170 | BreakPoint.prototype.condition = function() { |
| 171 | if (this.script_break_point() && this.script_break_point().condition()) { |
| 172 | return this.script_break_point().condition(); |
| 173 | } |
| 174 | return this.condition_; |
| 175 | }; |
| 176 | |
| 177 | |
| 178 | BreakPoint.prototype.ignoreCount = function() { |
| 179 | return this.ignoreCount_; |
| 180 | }; |
| 181 | |
| 182 | |
| 183 | BreakPoint.prototype.script_break_point = function() { |
| 184 | return this.script_break_point_; |
| 185 | }; |
| 186 | |
| 187 | |
| 188 | BreakPoint.prototype.enable = function() { |
| 189 | this.active_ = true; |
| 190 | }; |
| 191 | |
| 192 | |
| 193 | BreakPoint.prototype.disable = function() { |
| 194 | this.active_ = false; |
| 195 | }; |
| 196 | |
| 197 | |
| 198 | BreakPoint.prototype.setCondition = function(condition) { |
| 199 | this.condition_ = condition; |
| 200 | }; |
| 201 | |
| 202 | |
| 203 | BreakPoint.prototype.setIgnoreCount = function(ignoreCount) { |
| 204 | this.ignoreCount_ = ignoreCount; |
| 205 | }; |
| 206 | |
| 207 | |
| 208 | BreakPoint.prototype.isTriggered = function(exec_state) { |
| 209 | // Break point not active - not triggered. |
| 210 | if (!this.active()) return false; |
| 211 | |
| 212 | // Check for conditional break point. |
| 213 | if (this.condition()) { |
| 214 | // If break point has condition try to evaluate it in the top frame. |
| 215 | try { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 216 | var mirror = exec_state.frame(0).evaluate(this.condition()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 217 | // If no sensible mirror or non true value break point not triggered. |
| 218 | if (!(mirror instanceof ValueMirror) || !%ToBoolean(mirror.value_)) { |
| 219 | return false; |
| 220 | } |
| 221 | } catch (e) { |
| 222 | // Exception evaluating condition counts as not triggered. |
| 223 | return false; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Update the hit count. |
| 228 | this.hit_count_++; |
| 229 | if (this.script_break_point_) { |
| 230 | this.script_break_point_.hit_count_++; |
| 231 | } |
| 232 | |
| 233 | // If the break point has an ignore count it is not triggered. |
| 234 | if (this.ignoreCount_ > 0) { |
| 235 | this.ignoreCount_--; |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | // Break point triggered. |
| 240 | return true; |
| 241 | }; |
| 242 | |
| 243 | |
| 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)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 248 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 249 | |
| 250 | |
| 251 | // Object representing a script break point. The script is referenced by its |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 252 | // script name or script id and the break point is represented as line and |
| 253 | // column. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 254 | function ScriptBreakPoint(type, script_id_or_name, opt_line, opt_column, |
| 255 | opt_groupId) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 256 | this.type_ = type; |
| 257 | if (type == Debug.ScriptBreakPointType.ScriptId) { |
| 258 | this.script_id_ = script_id_or_name; |
| 259 | } else { // type == Debug.ScriptBreakPointType.ScriptName |
| 260 | this.script_name_ = script_id_or_name; |
| 261 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 262 | this.line_ = opt_line || 0; |
| 263 | this.column_ = opt_column; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 264 | this.groupId_ = opt_groupId; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | this.hit_count_ = 0; |
| 266 | this.active_ = true; |
| 267 | this.condition_ = null; |
| 268 | this.ignoreCount_ = 0; |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 269 | this.break_points_ = []; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 270 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 271 | |
| 272 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 273 | //Creates a clone of script breakpoint that is linked to another script. |
| 274 | ScriptBreakPoint.prototype.cloneForOtherScript = function (other_script) { |
| 275 | var copy = new ScriptBreakPoint(Debug.ScriptBreakPointType.ScriptId, |
| 276 | other_script.id, this.line_, this.column_, this.groupId_); |
| 277 | copy.number_ = next_break_point_number++; |
| 278 | script_break_points.push(copy); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 279 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 280 | copy.hit_count_ = this.hit_count_; |
| 281 | copy.active_ = this.active_; |
| 282 | copy.condition_ = this.condition_; |
| 283 | copy.ignoreCount_ = this.ignoreCount_; |
| 284 | return copy; |
| 285 | } |
| 286 | |
| 287 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 288 | ScriptBreakPoint.prototype.number = function() { |
| 289 | return this.number_; |
| 290 | }; |
| 291 | |
| 292 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 293 | ScriptBreakPoint.prototype.groupId = function() { |
| 294 | return this.groupId_; |
| 295 | }; |
| 296 | |
| 297 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 298 | ScriptBreakPoint.prototype.type = function() { |
| 299 | return this.type_; |
| 300 | }; |
| 301 | |
| 302 | |
| 303 | ScriptBreakPoint.prototype.script_id = function() { |
| 304 | return this.script_id_; |
| 305 | }; |
| 306 | |
| 307 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 308 | ScriptBreakPoint.prototype.script_name = function() { |
| 309 | return this.script_name_; |
| 310 | }; |
| 311 | |
| 312 | |
| 313 | ScriptBreakPoint.prototype.line = function() { |
| 314 | return this.line_; |
| 315 | }; |
| 316 | |
| 317 | |
| 318 | ScriptBreakPoint.prototype.column = function() { |
| 319 | return this.column_; |
| 320 | }; |
| 321 | |
| 322 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 323 | ScriptBreakPoint.prototype.actual_locations = function() { |
| 324 | var locations = []; |
| 325 | for (var i = 0; i < this.break_points_.length; i++) { |
| 326 | locations.push(this.break_points_[i].actual_location); |
| 327 | } |
| 328 | return locations; |
| 329 | } |
| 330 | |
| 331 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 332 | ScriptBreakPoint.prototype.update_positions = function(line, column) { |
| 333 | this.line_ = line; |
| 334 | this.column_ = column; |
| 335 | } |
| 336 | |
| 337 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 338 | ScriptBreakPoint.prototype.hit_count = function() { |
| 339 | return this.hit_count_; |
| 340 | }; |
| 341 | |
| 342 | |
| 343 | ScriptBreakPoint.prototype.active = function() { |
| 344 | return this.active_; |
| 345 | }; |
| 346 | |
| 347 | |
| 348 | ScriptBreakPoint.prototype.condition = function() { |
| 349 | return this.condition_; |
| 350 | }; |
| 351 | |
| 352 | |
| 353 | ScriptBreakPoint.prototype.ignoreCount = function() { |
| 354 | return this.ignoreCount_; |
| 355 | }; |
| 356 | |
| 357 | |
| 358 | ScriptBreakPoint.prototype.enable = function() { |
| 359 | this.active_ = true; |
| 360 | }; |
| 361 | |
| 362 | |
| 363 | ScriptBreakPoint.prototype.disable = function() { |
| 364 | this.active_ = false; |
| 365 | }; |
| 366 | |
| 367 | |
| 368 | ScriptBreakPoint.prototype.setCondition = function(condition) { |
| 369 | this.condition_ = condition; |
| 370 | }; |
| 371 | |
| 372 | |
| 373 | ScriptBreakPoint.prototype.setIgnoreCount = function(ignoreCount) { |
| 374 | this.ignoreCount_ = ignoreCount; |
| 375 | |
| 376 | // Set ignore count on all break points created from this script break point. |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 377 | for (var i = 0; i < this.break_points_.length; i++) { |
| 378 | this.break_points_[i].setIgnoreCount(ignoreCount); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 379 | } |
| 380 | }; |
| 381 | |
| 382 | |
| 383 | // Check whether a script matches this script break point. Currently this is |
| 384 | // only based on script name. |
| 385 | ScriptBreakPoint.prototype.matchesScript = function(script) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 386 | if (this.type_ == Debug.ScriptBreakPointType.ScriptId) { |
| 387 | return this.script_id_ == script.id; |
| 388 | } else { // this.type_ == Debug.ScriptBreakPointType.ScriptName |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 389 | return this.script_name_ == script.nameOrSourceURL() && |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 390 | script.line_offset <= this.line_ && |
| 391 | this.line_ < script.line_offset + script.lineCount(); |
| 392 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 393 | }; |
| 394 | |
| 395 | |
| 396 | // Set the script break point in a script. |
| 397 | ScriptBreakPoint.prototype.set = function (script) { |
| 398 | var column = this.column(); |
| 399 | var line = this.line(); |
| 400 | // If the column is undefined the break is on the line. To help locate the |
| 401 | // first piece of breakable code on the line try to find the column on the |
| 402 | // line which contains some source. |
| 403 | if (IS_UNDEFINED(column)) { |
| 404 | var source_line = script.sourceLine(this.line()); |
| 405 | |
| 406 | // Allocate array for caching the columns where the actual source starts. |
| 407 | if (!script.sourceColumnStart_) { |
| 408 | script.sourceColumnStart_ = new Array(script.lineCount()); |
| 409 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 410 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 411 | // Fill cache if needed and get column where the actual source starts. |
| 412 | if (IS_UNDEFINED(script.sourceColumnStart_[line])) { |
| 413 | script.sourceColumnStart_[line] = |
| 414 | source_line.match(sourceLineBeginningSkip)[0].length; |
| 415 | } |
| 416 | column = script.sourceColumnStart_[line]; |
| 417 | } |
| 418 | |
| 419 | // Convert the line and column into an absolute position within the script. |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 420 | var position = Debug.findScriptSourcePosition(script, this.line(), column); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 421 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 422 | // If the position is not found in the script (the script might be shorter |
| 423 | // than it used to be) just ignore it. |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 424 | if (position === null) return; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 425 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 426 | // Create a break point object and set the break point. |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 427 | break_point = MakeBreakPoint(position, this.line(), this.column(), this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 428 | break_point.setIgnoreCount(this.ignoreCount()); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 429 | var actual_position = %SetScriptBreakPoint(script, position, break_point); |
| 430 | if (IS_UNDEFINED(actual_position)) { |
| 431 | actual_position = position; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 432 | } |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 433 | var actual_location = script.locationFromPosition(actual_position, true); |
| 434 | break_point.actual_location = { line: actual_location.line, |
| 435 | column: actual_location.column }; |
| 436 | this.break_points_.push(break_point); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 437 | return break_point; |
| 438 | }; |
| 439 | |
| 440 | |
| 441 | // Clear all the break points created from this script break point |
| 442 | ScriptBreakPoint.prototype.clear = function () { |
| 443 | var remaining_break_points = []; |
| 444 | for (var i = 0; i < break_points.length; i++) { |
| 445 | if (break_points[i].script_break_point() && |
| 446 | break_points[i].script_break_point() === this) { |
| 447 | %ClearBreakPoint(break_points[i]); |
| 448 | } else { |
| 449 | remaining_break_points.push(break_points[i]); |
| 450 | } |
| 451 | } |
| 452 | break_points = remaining_break_points; |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 453 | this.break_points_ = []; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | |
| 457 | // Function called from runtime when a new script is compiled to set any script |
| 458 | // break points set in this script. |
| 459 | function UpdateScriptBreakPoints(script) { |
| 460 | for (var i = 0; i < script_break_points.length; i++) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 461 | if (script_break_points[i].type() == Debug.ScriptBreakPointType.ScriptName && |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 462 | script_break_points[i].matchesScript(script)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 463 | script_break_points[i].set(script); |
| 464 | } |
| 465 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 466 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 467 | |
| 468 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 469 | function GetScriptBreakPoints(script) { |
| 470 | var result = []; |
| 471 | for (var i = 0; i < script_break_points.length; i++) { |
| 472 | if (script_break_points[i].matchesScript(script)) { |
| 473 | result.push(script_break_points[i]); |
| 474 | } |
| 475 | } |
| 476 | return result; |
| 477 | } |
| 478 | |
| 479 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 480 | Debug.setListener = function(listener, opt_data) { |
| 481 | if (!IS_FUNCTION(listener) && !IS_UNDEFINED(listener) && !IS_NULL(listener)) { |
| 482 | throw new Error('Parameters have wrong types.'); |
| 483 | } |
| 484 | %SetDebugEventListener(listener, opt_data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 485 | }; |
| 486 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 487 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 488 | Debug.breakExecution = function(f) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 489 | %Break(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 490 | }; |
| 491 | |
| 492 | Debug.breakLocations = function(f) { |
| 493 | if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.'); |
| 494 | return %GetBreakLocations(f); |
| 495 | }; |
| 496 | |
| 497 | // Returns a Script object. If the parameter is a function the return value |
| 498 | // is the script in which the function is defined. If the parameter is a string |
| 499 | // the return value is the script for which the script name has that string |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 500 | // value. If it is a regexp and there is a unique script whose name matches |
| 501 | // we return that, otherwise undefined. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 502 | Debug.findScript = function(func_or_script_name) { |
| 503 | if (IS_FUNCTION(func_or_script_name)) { |
| 504 | return %FunctionGetScript(func_or_script_name); |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 505 | } else if (IS_REGEXP(func_or_script_name)) { |
| 506 | var scripts = Debug.scripts(); |
| 507 | var last_result = null; |
| 508 | var result_count = 0; |
| 509 | for (var i in scripts) { |
| 510 | var script = scripts[i]; |
| 511 | if (func_or_script_name.test(script.name)) { |
| 512 | last_result = script; |
| 513 | result_count++; |
| 514 | } |
| 515 | } |
| 516 | // Return the unique script matching the regexp. If there are more |
| 517 | // than one we don't return a value since there is no good way to |
| 518 | // decide which one to return. Returning a "random" one, say the |
| 519 | // first, would introduce nondeterminism (or something close to it) |
| 520 | // because the order is the heap iteration order. |
| 521 | if (result_count == 1) { |
| 522 | return last_result; |
| 523 | } else { |
| 524 | return undefined; |
| 525 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 526 | } else { |
| 527 | return %GetScript(func_or_script_name); |
| 528 | } |
| 529 | }; |
| 530 | |
| 531 | // Returns the script source. If the parameter is a function the return value |
| 532 | // is the script source for the script in which the function is defined. If the |
| 533 | // parameter is a string the return value is the script for which the script |
| 534 | // name has that string value. |
| 535 | Debug.scriptSource = function(func_or_script_name) { |
| 536 | return this.findScript(func_or_script_name).source; |
| 537 | }; |
| 538 | |
| 539 | Debug.source = function(f) { |
| 540 | if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.'); |
| 541 | return %FunctionGetSourceCode(f); |
| 542 | }; |
| 543 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 544 | Debug.disassemble = function(f) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 545 | if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.'); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 546 | return %DebugDisassembleFunction(f); |
| 547 | }; |
| 548 | |
| 549 | Debug.disassembleConstructor = function(f) { |
| 550 | if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.'); |
| 551 | return %DebugDisassembleConstructor(f); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 552 | }; |
| 553 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 554 | Debug.ExecuteInDebugContext = function(f, without_debugger) { |
| 555 | if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.'); |
| 556 | return %ExecuteInDebugContext(f, !!without_debugger); |
| 557 | }; |
| 558 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 559 | Debug.sourcePosition = function(f) { |
| 560 | if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.'); |
| 561 | return %FunctionGetScriptSourcePosition(f); |
| 562 | }; |
| 563 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 564 | |
| 565 | Debug.findFunctionSourceLocation = function(func, opt_line, opt_column) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 566 | var script = %FunctionGetScript(func); |
| 567 | var script_offset = %FunctionGetScriptSourcePosition(func); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 568 | return script.locationFromLine(opt_line, opt_column, script_offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
| 572 | // Returns the character position in a script based on a line number and an |
| 573 | // optional position within that line. |
| 574 | Debug.findScriptSourcePosition = function(script, opt_line, opt_column) { |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 575 | var location = script.locationFromLine(opt_line, opt_column); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 576 | return location ? location.position : null; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | |
| 580 | Debug.findBreakPoint = function(break_point_number, remove) { |
| 581 | var break_point; |
| 582 | for (var i = 0; i < break_points.length; i++) { |
| 583 | if (break_points[i].number() == break_point_number) { |
| 584 | break_point = break_points[i]; |
| 585 | // Remove the break point from the list if requested. |
| 586 | if (remove) { |
| 587 | break_points.splice(i, 1); |
| 588 | } |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | if (break_point) { |
| 593 | return break_point; |
| 594 | } else { |
| 595 | return this.findScriptBreakPoint(break_point_number, remove); |
| 596 | } |
| 597 | }; |
| 598 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 599 | Debug.findBreakPointActualLocations = function(break_point_number) { |
| 600 | for (var i = 0; i < script_break_points.length; i++) { |
| 601 | if (script_break_points[i].number() == break_point_number) { |
| 602 | return script_break_points[i].actual_locations(); |
| 603 | } |
| 604 | } |
| 605 | for (var i = 0; i < break_points.length; i++) { |
| 606 | if (break_points[i].number() == break_point_number) { |
| 607 | return [break_points[i].actual_location]; |
| 608 | } |
| 609 | } |
| 610 | return []; |
| 611 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 612 | |
| 613 | Debug.setBreakPoint = function(func, opt_line, opt_column, opt_condition) { |
| 614 | if (!IS_FUNCTION(func)) throw new Error('Parameters have wrong types.'); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 615 | // Break points in API functions are not supported. |
| 616 | if (%FunctionIsAPIFunction(func)) { |
| 617 | throw new Error('Cannot set break point in native code.'); |
| 618 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 619 | // Find source position relative to start of the function |
| 620 | var break_position = |
| 621 | this.findFunctionSourceLocation(func, opt_line, opt_column).position; |
| 622 | var source_position = break_position - this.sourcePosition(func); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 623 | // Find the script for the function. |
| 624 | var script = %FunctionGetScript(func); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 625 | // Break in builtin JavaScript code is not supported. |
| 626 | if (script.type == Debug.ScriptType.Native) { |
| 627 | throw new Error('Cannot set break point in native code.'); |
| 628 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 629 | // If the script for the function has a name convert this to a script break |
| 630 | // point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 631 | if (script && script.id) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 632 | // Adjust the source position to be script relative. |
| 633 | source_position += %FunctionGetScriptSourcePosition(func); |
| 634 | // Find line and column for the position in the script and set a script |
| 635 | // break point from that. |
ager@chromium.org | 3a6061e | 2009-03-12 14:24:36 +0000 | [diff] [blame] | 636 | var location = script.locationFromPosition(source_position, false); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 637 | return this.setScriptBreakPointById(script.id, |
| 638 | location.line, location.column, |
| 639 | opt_condition); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 640 | } else { |
| 641 | // Set a break point directly on the function. |
| 642 | var break_point = MakeBreakPoint(source_position, opt_line, opt_column); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 643 | var actual_position = |
| 644 | %SetFunctionBreakPoint(func, source_position, break_point); |
| 645 | actual_position += this.sourcePosition(func); |
| 646 | var actual_location = script.locationFromPosition(actual_position, true); |
| 647 | break_point.actual_location = { line: actual_location.line, |
| 648 | column: actual_location.column }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 649 | break_point.setCondition(opt_condition); |
| 650 | return break_point.number(); |
| 651 | } |
| 652 | }; |
| 653 | |
| 654 | |
| 655 | Debug.enableBreakPoint = function(break_point_number) { |
| 656 | var break_point = this.findBreakPoint(break_point_number, false); |
| 657 | break_point.enable(); |
| 658 | }; |
| 659 | |
| 660 | |
| 661 | Debug.disableBreakPoint = function(break_point_number) { |
| 662 | var break_point = this.findBreakPoint(break_point_number, false); |
| 663 | break_point.disable(); |
| 664 | }; |
| 665 | |
| 666 | |
| 667 | Debug.changeBreakPointCondition = function(break_point_number, condition) { |
| 668 | var break_point = this.findBreakPoint(break_point_number, false); |
| 669 | break_point.setCondition(condition); |
| 670 | }; |
| 671 | |
| 672 | |
| 673 | Debug.changeBreakPointIgnoreCount = function(break_point_number, ignoreCount) { |
| 674 | if (ignoreCount < 0) { |
| 675 | throw new Error('Invalid argument'); |
| 676 | } |
| 677 | var break_point = this.findBreakPoint(break_point_number, false); |
| 678 | break_point.setIgnoreCount(ignoreCount); |
| 679 | }; |
| 680 | |
| 681 | |
| 682 | Debug.clearBreakPoint = function(break_point_number) { |
| 683 | var break_point = this.findBreakPoint(break_point_number, true); |
| 684 | if (break_point) { |
| 685 | return %ClearBreakPoint(break_point); |
| 686 | } else { |
| 687 | break_point = this.findScriptBreakPoint(break_point_number, true); |
| 688 | if (!break_point) { |
| 689 | throw new Error('Invalid breakpoint'); |
| 690 | } |
| 691 | } |
| 692 | }; |
| 693 | |
| 694 | |
| 695 | Debug.clearAllBreakPoints = function() { |
| 696 | for (var i = 0; i < break_points.length; i++) { |
| 697 | break_point = break_points[i]; |
| 698 | %ClearBreakPoint(break_point); |
| 699 | } |
| 700 | break_points = []; |
| 701 | }; |
| 702 | |
| 703 | |
| 704 | Debug.findScriptBreakPoint = function(break_point_number, remove) { |
| 705 | var script_break_point; |
| 706 | for (var i = 0; i < script_break_points.length; i++) { |
| 707 | if (script_break_points[i].number() == break_point_number) { |
| 708 | script_break_point = script_break_points[i]; |
| 709 | // Remove the break point from the list if requested. |
| 710 | if (remove) { |
| 711 | script_break_point.clear(); |
| 712 | script_break_points.splice(i,1); |
| 713 | } |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | return script_break_point; |
| 718 | } |
| 719 | |
| 720 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 721 | // Sets a breakpoint in a script identified through id or name at the |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 722 | // specified source line and column within that line. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 723 | Debug.setScriptBreakPoint = function(type, script_id_or_name, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 724 | opt_line, opt_column, opt_condition, |
| 725 | opt_groupId) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 726 | // Create script break point object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 727 | var script_break_point = |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 728 | new ScriptBreakPoint(type, script_id_or_name, opt_line, opt_column, |
| 729 | opt_groupId); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 730 | |
| 731 | // Assign number to the new script break point and add it. |
| 732 | script_break_point.number_ = next_break_point_number++; |
| 733 | script_break_point.setCondition(opt_condition); |
| 734 | script_break_points.push(script_break_point); |
| 735 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 736 | // Run through all scripts to see if this script break point matches any |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 737 | // loaded scripts. |
| 738 | var scripts = this.scripts(); |
| 739 | for (var i = 0; i < scripts.length; i++) { |
| 740 | if (script_break_point.matchesScript(scripts[i])) { |
| 741 | script_break_point.set(scripts[i]); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | return script_break_point.number(); |
| 746 | } |
| 747 | |
| 748 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 749 | Debug.setScriptBreakPointById = function(script_id, |
| 750 | opt_line, opt_column, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 751 | opt_condition, opt_groupId) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 752 | return this.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptId, |
| 753 | script_id, opt_line, opt_column, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 754 | opt_condition, opt_groupId); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | |
| 758 | Debug.setScriptBreakPointByName = function(script_name, |
| 759 | opt_line, opt_column, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 760 | opt_condition, opt_groupId) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 761 | return this.setScriptBreakPoint(Debug.ScriptBreakPointType.ScriptName, |
| 762 | script_name, opt_line, opt_column, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 763 | opt_condition, opt_groupId); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 767 | Debug.enableScriptBreakPoint = function(break_point_number) { |
| 768 | var script_break_point = this.findScriptBreakPoint(break_point_number, false); |
| 769 | script_break_point.enable(); |
| 770 | }; |
| 771 | |
| 772 | |
| 773 | Debug.disableScriptBreakPoint = function(break_point_number) { |
| 774 | var script_break_point = this.findScriptBreakPoint(break_point_number, false); |
| 775 | script_break_point.disable(); |
| 776 | }; |
| 777 | |
| 778 | |
| 779 | Debug.changeScriptBreakPointCondition = function(break_point_number, condition) { |
| 780 | var script_break_point = this.findScriptBreakPoint(break_point_number, false); |
| 781 | script_break_point.setCondition(condition); |
| 782 | }; |
| 783 | |
| 784 | |
| 785 | Debug.changeScriptBreakPointIgnoreCount = function(break_point_number, ignoreCount) { |
| 786 | if (ignoreCount < 0) { |
| 787 | throw new Error('Invalid argument'); |
| 788 | } |
| 789 | var script_break_point = this.findScriptBreakPoint(break_point_number, false); |
| 790 | script_break_point.setIgnoreCount(ignoreCount); |
| 791 | }; |
| 792 | |
| 793 | |
| 794 | Debug.scriptBreakPoints = function() { |
| 795 | return script_break_points; |
| 796 | } |
| 797 | |
| 798 | |
| 799 | Debug.clearStepping = function() { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 800 | %ClearStepping(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | Debug.setBreakOnException = function() { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 804 | return %ChangeBreakOnException(Debug.ExceptionBreak.Caught, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 805 | }; |
| 806 | |
| 807 | Debug.clearBreakOnException = function() { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 808 | return %ChangeBreakOnException(Debug.ExceptionBreak.Caught, false); |
| 809 | }; |
| 810 | |
| 811 | Debug.isBreakOnException = function() { |
| 812 | return !!%IsBreakOnException(Debug.ExceptionBreak.Caught); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 813 | }; |
| 814 | |
| 815 | Debug.setBreakOnUncaughtException = function() { |
| 816 | return %ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, true); |
| 817 | }; |
| 818 | |
| 819 | Debug.clearBreakOnUncaughtException = function() { |
| 820 | return %ChangeBreakOnException(Debug.ExceptionBreak.Uncaught, false); |
| 821 | }; |
| 822 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 823 | Debug.isBreakOnUncaughtException = function() { |
| 824 | return !!%IsBreakOnException(Debug.ExceptionBreak.Uncaught); |
| 825 | }; |
| 826 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 827 | Debug.showBreakPoints = function(f, full) { |
| 828 | if (!IS_FUNCTION(f)) throw new Error('Parameters have wrong types.'); |
| 829 | var source = full ? this.scriptSource(f) : this.source(f); |
| 830 | var offset = full ? this.sourcePosition(f) : 0; |
| 831 | var locations = this.breakLocations(f); |
| 832 | if (!locations) return source; |
| 833 | locations.sort(function(x, y) { return x - y; }); |
| 834 | var result = ""; |
| 835 | var prev_pos = 0; |
| 836 | var pos; |
| 837 | for (var i = 0; i < locations.length; i++) { |
| 838 | pos = locations[i] - offset; |
| 839 | result += source.slice(prev_pos, pos); |
| 840 | result += "[B" + i + "]"; |
| 841 | prev_pos = pos; |
| 842 | } |
| 843 | pos = source.length; |
| 844 | result += source.substring(prev_pos, pos); |
| 845 | return result; |
| 846 | }; |
| 847 | |
| 848 | |
| 849 | // Get all the scripts currently loaded. Locating all the scripts is based on |
| 850 | // scanning the heap. |
| 851 | Debug.scripts = function() { |
| 852 | // Collect all scripts in the heap. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 853 | return %DebugGetLoadedScripts(); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 854 | }; |
| 855 | |
| 856 | |
| 857 | Debug.debuggerFlags = function() { |
| 858 | return debugger_flags; |
| 859 | }; |
| 860 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 861 | |
| 862 | function MakeExecutionState(break_id) { |
| 863 | return new ExecutionState(break_id); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 864 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 865 | |
| 866 | function ExecutionState(break_id) { |
| 867 | this.break_id = break_id; |
| 868 | this.selected_frame = 0; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 869 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 870 | |
| 871 | ExecutionState.prototype.prepareStep = function(opt_action, opt_count) { |
| 872 | var action = Debug.StepAction.StepIn; |
| 873 | if (!IS_UNDEFINED(opt_action)) action = %ToNumber(opt_action); |
| 874 | var count = opt_count ? %ToNumber(opt_count) : 1; |
| 875 | |
| 876 | return %PrepareStep(this.break_id, action, count); |
| 877 | } |
| 878 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 879 | ExecutionState.prototype.evaluateGlobal = function(source, disable_break) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 880 | return MakeMirror( |
| 881 | %DebugEvaluateGlobal(this.break_id, source, Boolean(disable_break))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 882 | }; |
| 883 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 884 | ExecutionState.prototype.frameCount = function() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 885 | return %GetFrameCount(this.break_id); |
| 886 | }; |
| 887 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 888 | ExecutionState.prototype.threadCount = function() { |
| 889 | return %GetThreadCount(this.break_id); |
| 890 | }; |
| 891 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 892 | ExecutionState.prototype.frame = function(opt_index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 893 | // If no index supplied return the selected frame. |
| 894 | if (opt_index == null) opt_index = this.selected_frame; |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 895 | if (opt_index < 0 || opt_index >= this.frameCount()) |
| 896 | throw new Error('Illegal frame index.'); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 897 | return new FrameMirror(this.break_id, opt_index); |
| 898 | }; |
| 899 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 900 | ExecutionState.prototype.setSelectedFrame = function(index) { |
| 901 | var i = %ToNumber(index); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 902 | if (i < 0 || i >= this.frameCount()) throw new Error('Illegal frame index.'); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 903 | this.selected_frame = i; |
| 904 | }; |
| 905 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 906 | ExecutionState.prototype.selectedFrame = function() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 907 | return this.selected_frame; |
| 908 | }; |
| 909 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 910 | ExecutionState.prototype.debugCommandProcessor = function(opt_is_running) { |
| 911 | return new DebugCommandProcessor(this, opt_is_running); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 912 | }; |
| 913 | |
| 914 | |
| 915 | function MakeBreakEvent(exec_state, break_points_hit) { |
| 916 | return new BreakEvent(exec_state, break_points_hit); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 917 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 918 | |
| 919 | |
| 920 | function BreakEvent(exec_state, break_points_hit) { |
| 921 | this.exec_state_ = exec_state; |
| 922 | this.break_points_hit_ = break_points_hit; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 923 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 924 | |
| 925 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 926 | BreakEvent.prototype.executionState = function() { |
| 927 | return this.exec_state_; |
| 928 | }; |
| 929 | |
| 930 | |
| 931 | BreakEvent.prototype.eventType = function() { |
| 932 | return Debug.DebugEvent.Break; |
| 933 | }; |
| 934 | |
| 935 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 936 | BreakEvent.prototype.func = function() { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 937 | return this.exec_state_.frame(0).func(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 938 | }; |
| 939 | |
| 940 | |
| 941 | BreakEvent.prototype.sourceLine = function() { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 942 | return this.exec_state_.frame(0).sourceLine(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 943 | }; |
| 944 | |
| 945 | |
| 946 | BreakEvent.prototype.sourceColumn = function() { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 947 | return this.exec_state_.frame(0).sourceColumn(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 948 | }; |
| 949 | |
| 950 | |
| 951 | BreakEvent.prototype.sourceLineText = function() { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 952 | return this.exec_state_.frame(0).sourceLineText(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 953 | }; |
| 954 | |
| 955 | |
| 956 | BreakEvent.prototype.breakPointsHit = function() { |
| 957 | return this.break_points_hit_; |
| 958 | }; |
| 959 | |
| 960 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 961 | BreakEvent.prototype.toJSONProtocol = function() { |
| 962 | var o = { seq: next_response_seq++, |
| 963 | type: "event", |
| 964 | event: "break", |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 965 | body: { invocationText: this.exec_state_.frame(0).invocationText(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 966 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 967 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 968 | |
| 969 | // Add script related information to the event if available. |
| 970 | var script = this.func().script(); |
| 971 | if (script) { |
| 972 | o.body.sourceLine = this.sourceLine(), |
| 973 | o.body.sourceColumn = this.sourceColumn(), |
| 974 | o.body.sourceLineText = this.sourceLineText(), |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 975 | o.body.script = MakeScriptObject_(script, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | // Add an Array of break points hit if any. |
| 979 | if (this.breakPointsHit()) { |
| 980 | o.body.breakpoints = []; |
| 981 | for (var i = 0; i < this.breakPointsHit().length; i++) { |
| 982 | // Find the break point number. For break points originating from a |
| 983 | // script break point supply the script break point number. |
| 984 | var breakpoint = this.breakPointsHit()[i]; |
| 985 | var script_break_point = breakpoint.script_break_point(); |
| 986 | var number; |
| 987 | if (script_break_point) { |
| 988 | number = script_break_point.number(); |
| 989 | } else { |
| 990 | number = breakpoint.number(); |
| 991 | } |
| 992 | o.body.breakpoints.push(number); |
| 993 | } |
| 994 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 995 | return JSON.stringify(ObjectToProtocolObject_(o)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 996 | }; |
| 997 | |
| 998 | |
| 999 | function MakeExceptionEvent(exec_state, exception, uncaught) { |
| 1000 | return new ExceptionEvent(exec_state, exception, uncaught); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1001 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1002 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1003 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1004 | function ExceptionEvent(exec_state, exception, uncaught) { |
| 1005 | this.exec_state_ = exec_state; |
| 1006 | this.exception_ = exception; |
| 1007 | this.uncaught_ = uncaught; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1008 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1009 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1010 | |
| 1011 | ExceptionEvent.prototype.executionState = function() { |
| 1012 | return this.exec_state_; |
| 1013 | }; |
| 1014 | |
| 1015 | |
| 1016 | ExceptionEvent.prototype.eventType = function() { |
| 1017 | return Debug.DebugEvent.Exception; |
| 1018 | }; |
| 1019 | |
| 1020 | |
| 1021 | ExceptionEvent.prototype.exception = function() { |
| 1022 | return this.exception_; |
| 1023 | } |
| 1024 | |
| 1025 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1026 | ExceptionEvent.prototype.uncaught = function() { |
| 1027 | return this.uncaught_; |
| 1028 | } |
| 1029 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1030 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1031 | ExceptionEvent.prototype.func = function() { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1032 | return this.exec_state_.frame(0).func(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1033 | }; |
| 1034 | |
| 1035 | |
| 1036 | ExceptionEvent.prototype.sourceLine = function() { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1037 | return this.exec_state_.frame(0).sourceLine(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1038 | }; |
| 1039 | |
| 1040 | |
| 1041 | ExceptionEvent.prototype.sourceColumn = function() { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1042 | return this.exec_state_.frame(0).sourceColumn(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1043 | }; |
| 1044 | |
| 1045 | |
| 1046 | ExceptionEvent.prototype.sourceLineText = function() { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1047 | return this.exec_state_.frame(0).sourceLineText(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1048 | }; |
| 1049 | |
| 1050 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1051 | ExceptionEvent.prototype.toJSONProtocol = function() { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1052 | var o = new ProtocolMessage(); |
| 1053 | o.event = "exception"; |
| 1054 | o.body = { uncaught: this.uncaught_, |
| 1055 | exception: MakeMirror(this.exception_) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1056 | }; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1057 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1058 | // Exceptions might happen whithout any JavaScript frames. |
| 1059 | if (this.exec_state_.frameCount() > 0) { |
| 1060 | o.body.sourceLine = this.sourceLine(); |
| 1061 | o.body.sourceColumn = this.sourceColumn(); |
| 1062 | o.body.sourceLineText = this.sourceLineText(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1063 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1064 | // Add script information to the event if available. |
| 1065 | var script = this.func().script(); |
| 1066 | if (script) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1067 | o.body.script = MakeScriptObject_(script, false); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1068 | } |
| 1069 | } else { |
| 1070 | o.body.sourceLine = -1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1073 | return o.toJSONProtocol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1074 | }; |
| 1075 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1076 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1077 | function MakeCompileEvent(exec_state, script, before) { |
| 1078 | return new CompileEvent(exec_state, script, before); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1079 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1080 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1081 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1082 | function CompileEvent(exec_state, script, before) { |
| 1083 | this.exec_state_ = exec_state; |
| 1084 | this.script_ = MakeMirror(script); |
| 1085 | this.before_ = before; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1086 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1087 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1088 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1089 | CompileEvent.prototype.executionState = function() { |
| 1090 | return this.exec_state_; |
| 1091 | }; |
| 1092 | |
| 1093 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1094 | CompileEvent.prototype.eventType = function() { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1095 | if (this.before_) { |
| 1096 | return Debug.DebugEvent.BeforeCompile; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1097 | } else { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1098 | return Debug.DebugEvent.AfterCompile; |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1099 | } |
| 1100 | }; |
| 1101 | |
| 1102 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1103 | CompileEvent.prototype.script = function() { |
| 1104 | return this.script_; |
| 1105 | }; |
| 1106 | |
| 1107 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1108 | CompileEvent.prototype.toJSONProtocol = function() { |
| 1109 | var o = new ProtocolMessage(); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1110 | o.running = true; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1111 | if (this.before_) { |
| 1112 | o.event = "beforeCompile"; |
| 1113 | } else { |
| 1114 | o.event = "afterCompile"; |
| 1115 | } |
| 1116 | o.body = {}; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1117 | o.body.script = this.script_; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1118 | |
| 1119 | return o.toJSONProtocol(); |
| 1120 | } |
| 1121 | |
| 1122 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1123 | function MakeNewFunctionEvent(func) { |
| 1124 | return new NewFunctionEvent(func); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1125 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1126 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1127 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1128 | function NewFunctionEvent(func) { |
| 1129 | this.func = func; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1130 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1131 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1132 | |
| 1133 | NewFunctionEvent.prototype.eventType = function() { |
| 1134 | return Debug.DebugEvent.NewFunction; |
| 1135 | }; |
| 1136 | |
| 1137 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1138 | NewFunctionEvent.prototype.name = function() { |
| 1139 | return this.func.name; |
| 1140 | }; |
| 1141 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1142 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1143 | NewFunctionEvent.prototype.setBreakPoint = function(p) { |
| 1144 | Debug.setBreakPoint(this.func, p || 0); |
| 1145 | }; |
| 1146 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1147 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1148 | function MakeScriptCollectedEvent(exec_state, id) { |
| 1149 | return new ScriptCollectedEvent(exec_state, id); |
| 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | function ScriptCollectedEvent(exec_state, id) { |
| 1154 | this.exec_state_ = exec_state; |
| 1155 | this.id_ = id; |
| 1156 | } |
| 1157 | |
| 1158 | |
| 1159 | ScriptCollectedEvent.prototype.id = function() { |
| 1160 | return this.id_; |
| 1161 | }; |
| 1162 | |
| 1163 | |
| 1164 | ScriptCollectedEvent.prototype.executionState = function() { |
| 1165 | return this.exec_state_; |
| 1166 | }; |
| 1167 | |
| 1168 | |
| 1169 | ScriptCollectedEvent.prototype.toJSONProtocol = function() { |
| 1170 | var o = new ProtocolMessage(); |
| 1171 | o.running = true; |
| 1172 | o.event = "scriptCollected"; |
| 1173 | o.body = {}; |
| 1174 | o.body.script = { id: this.id() }; |
| 1175 | return o.toJSONProtocol(); |
| 1176 | } |
| 1177 | |
| 1178 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1179 | function MakeScriptObject_(script, include_source) { |
| 1180 | var o = { id: script.id(), |
| 1181 | name: script.name(), |
| 1182 | lineOffset: script.lineOffset(), |
| 1183 | columnOffset: script.columnOffset(), |
| 1184 | lineCount: script.lineCount(), |
| 1185 | }; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1186 | if (!IS_UNDEFINED(script.data())) { |
| 1187 | o.data = script.data(); |
| 1188 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1189 | if (include_source) { |
| 1190 | o.source = script.source(); |
| 1191 | } |
| 1192 | return o; |
| 1193 | }; |
| 1194 | |
| 1195 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1196 | function DebugCommandProcessor(exec_state, opt_is_running) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1197 | this.exec_state_ = exec_state; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1198 | this.running_ = opt_is_running || false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1199 | }; |
| 1200 | |
| 1201 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1202 | DebugCommandProcessor.prototype.processDebugRequest = function (request) { |
| 1203 | return this.processDebugJSONRequest(request); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1207 | function ProtocolMessage(request) { |
| 1208 | // Update sequence number. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1209 | this.seq = next_response_seq++; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1210 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1211 | if (request) { |
| 1212 | // If message is based on a request this is a response. Fill the initial |
| 1213 | // response from the request. |
| 1214 | this.type = 'response'; |
| 1215 | this.request_seq = request.seq; |
| 1216 | this.command = request.command; |
| 1217 | } else { |
| 1218 | // If message is not based on a request it is a dabugger generated event. |
| 1219 | this.type = 'event'; |
| 1220 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1221 | this.success = true; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1222 | // Handler may set this field to control debugger state. |
| 1223 | this.running = undefined; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1224 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1225 | |
| 1226 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1227 | ProtocolMessage.prototype.setOption = function(name, value) { |
| 1228 | if (!this.options_) { |
| 1229 | this.options_ = {}; |
| 1230 | } |
| 1231 | this.options_[name] = value; |
| 1232 | } |
| 1233 | |
| 1234 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1235 | ProtocolMessage.prototype.failed = function(message) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1236 | this.success = false; |
| 1237 | this.message = message; |
| 1238 | } |
| 1239 | |
| 1240 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1241 | ProtocolMessage.prototype.toJSONProtocol = function() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1242 | // Encode the protocol header. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1243 | var json = {}; |
| 1244 | json.seq= this.seq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1245 | if (this.request_seq) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1246 | json.request_seq = this.request_seq; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1247 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1248 | json.type = this.type; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1249 | if (this.event) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1250 | json.event = this.event; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1251 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1252 | if (this.command) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1253 | json.command = this.command; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1254 | } |
| 1255 | if (this.success) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1256 | json.success = this.success; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1257 | } else { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1258 | json.success = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1259 | } |
| 1260 | if (this.body) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1261 | // Encode the body part. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1262 | var bodyJson; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 1263 | var serializer = MakeMirrorSerializer(true, this.options_); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1264 | if (this.body instanceof Mirror) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1265 | bodyJson = serializer.serializeValue(this.body); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1266 | } else if (this.body instanceof Array) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1267 | bodyJson = []; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1268 | for (var i = 0; i < this.body.length; i++) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1269 | if (this.body[i] instanceof Mirror) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1270 | bodyJson.push(serializer.serializeValue(this.body[i])); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1271 | } else { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1272 | bodyJson.push(ObjectToProtocolObject_(this.body[i], serializer)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1273 | } |
| 1274 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1275 | } else { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1276 | bodyJson = ObjectToProtocolObject_(this.body, serializer); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1277 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1278 | json.body = bodyJson; |
| 1279 | json.refs = serializer.serializeReferencedObjects(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1280 | } |
| 1281 | if (this.message) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1282 | json.message = this.message; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1283 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1284 | json.running = this.running; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1285 | return JSON.stringify(json); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | DebugCommandProcessor.prototype.createResponse = function(request) { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 1290 | return new ProtocolMessage(request); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1291 | }; |
| 1292 | |
| 1293 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1294 | DebugCommandProcessor.prototype.processDebugJSONRequest = function(json_request) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1295 | var request; // Current request. |
| 1296 | var response; // Generated response. |
| 1297 | try { |
| 1298 | try { |
| 1299 | // Convert the JSON string to an object. |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 1300 | request = %CompileString('(' + json_request + ')')(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1301 | |
| 1302 | // Create an initial response. |
| 1303 | response = this.createResponse(request); |
| 1304 | |
| 1305 | if (!request.type) { |
| 1306 | throw new Error('Type not specified'); |
| 1307 | } |
| 1308 | |
| 1309 | if (request.type != 'request') { |
| 1310 | throw new Error("Illegal type '" + request.type + "' in request"); |
| 1311 | } |
| 1312 | |
| 1313 | if (!request.command) { |
| 1314 | throw new Error('Command not specified'); |
| 1315 | } |
| 1316 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 1317 | if (request.arguments) { |
| 1318 | var args = request.arguments; |
| 1319 | // TODO(yurys): remove request.arguments.compactFormat check once |
| 1320 | // ChromeDevTools are switched to 'inlineRefs' |
| 1321 | if (args.inlineRefs || args.compactFormat) { |
| 1322 | response.setOption('inlineRefs', true); |
| 1323 | } |
| 1324 | if (!IS_UNDEFINED(args.maxStringLength)) { |
| 1325 | response.setOption('maxStringLength', args.maxStringLength); |
| 1326 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1329 | if (request.command == 'continue') { |
| 1330 | this.continueRequest_(request, response); |
| 1331 | } else if (request.command == 'break') { |
| 1332 | this.breakRequest_(request, response); |
| 1333 | } else if (request.command == 'setbreakpoint') { |
| 1334 | this.setBreakPointRequest_(request, response); |
| 1335 | } else if (request.command == 'changebreakpoint') { |
| 1336 | this.changeBreakPointRequest_(request, response); |
| 1337 | } else if (request.command == 'clearbreakpoint') { |
| 1338 | this.clearBreakPointRequest_(request, response); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1339 | } else if (request.command == 'clearbreakpointgroup') { |
| 1340 | this.clearBreakPointGroupRequest_(request, response); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1341 | } else if (request.command == 'listbreakpoints') { |
| 1342 | this.listBreakpointsRequest_(request, response); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1343 | } else if (request.command == 'backtrace') { |
| 1344 | this.backtraceRequest_(request, response); |
| 1345 | } else if (request.command == 'frame') { |
| 1346 | this.frameRequest_(request, response); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1347 | } else if (request.command == 'scopes') { |
| 1348 | this.scopesRequest_(request, response); |
| 1349 | } else if (request.command == 'scope') { |
| 1350 | this.scopeRequest_(request, response); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1351 | } else if (request.command == 'evaluate') { |
| 1352 | this.evaluateRequest_(request, response); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1353 | } else if (request.command == 'lookup') { |
| 1354 | this.lookupRequest_(request, response); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1355 | } else if (request.command == 'references') { |
| 1356 | this.referencesRequest_(request, response); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1357 | } else if (request.command == 'source') { |
| 1358 | this.sourceRequest_(request, response); |
| 1359 | } else if (request.command == 'scripts') { |
| 1360 | this.scriptsRequest_(request, response); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1361 | } else if (request.command == 'threads') { |
| 1362 | this.threadsRequest_(request, response); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1363 | } else if (request.command == 'suspend') { |
| 1364 | this.suspendRequest_(request, response); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1365 | } else if (request.command == 'version') { |
| 1366 | this.versionRequest_(request, response); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1367 | } else if (request.command == 'profile') { |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1368 | this.profileRequest_(request, response); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1369 | } else if (request.command == 'changelive') { |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1370 | this.changeLiveRequest_(request, response); |
| 1371 | } else if (request.command == 'flags') { |
| 1372 | this.debuggerFlagsRequest_(request, response); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1373 | } else { |
| 1374 | throw new Error('Unknown command "' + request.command + '" in request'); |
| 1375 | } |
| 1376 | } catch (e) { |
| 1377 | // If there is no response object created one (without command). |
| 1378 | if (!response) { |
| 1379 | response = this.createResponse(); |
| 1380 | } |
| 1381 | response.success = false; |
| 1382 | response.message = %ToString(e); |
| 1383 | } |
| 1384 | |
| 1385 | // Return the response as a JSON encoded string. |
| 1386 | try { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1387 | if (!IS_UNDEFINED(response.running)) { |
| 1388 | // Response controls running state. |
| 1389 | this.running_ = response.running; |
| 1390 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1391 | response.running = this.running_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1392 | return response.toJSONProtocol(); |
| 1393 | } catch (e) { |
| 1394 | // Failed to generate response - return generic error. |
| 1395 | return '{"seq":' + response.seq + ',' + |
| 1396 | '"request_seq":' + request.seq + ',' + |
| 1397 | '"type":"response",' + |
| 1398 | '"success":false,' + |
| 1399 | '"message":"Internal error: ' + %ToString(e) + '"}'; |
| 1400 | } |
| 1401 | } catch (e) { |
| 1402 | // Failed in one of the catch blocks above - most generic error. |
| 1403 | return '{"seq":0,"type":"response","success":false,"message":"Internal error"}'; |
| 1404 | } |
| 1405 | }; |
| 1406 | |
| 1407 | |
| 1408 | DebugCommandProcessor.prototype.continueRequest_ = function(request, response) { |
| 1409 | // Check for arguments for continue. |
| 1410 | if (request.arguments) { |
| 1411 | var count = 1; |
| 1412 | var action = Debug.StepAction.StepIn; |
| 1413 | |
| 1414 | // Pull out arguments. |
| 1415 | var stepaction = request.arguments.stepaction; |
| 1416 | var stepcount = request.arguments.stepcount; |
| 1417 | |
| 1418 | // Get the stepcount argument if any. |
| 1419 | if (stepcount) { |
| 1420 | count = %ToNumber(stepcount); |
| 1421 | if (count < 0) { |
| 1422 | throw new Error('Invalid stepcount argument "' + stepcount + '".'); |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | // Get the stepaction argument. |
| 1427 | if (stepaction) { |
| 1428 | if (stepaction == 'in') { |
| 1429 | action = Debug.StepAction.StepIn; |
| 1430 | } else if (stepaction == 'min') { |
| 1431 | action = Debug.StepAction.StepMin; |
| 1432 | } else if (stepaction == 'next') { |
| 1433 | action = Debug.StepAction.StepNext; |
| 1434 | } else if (stepaction == 'out') { |
| 1435 | action = Debug.StepAction.StepOut; |
| 1436 | } else { |
| 1437 | throw new Error('Invalid stepaction argument "' + stepaction + '".'); |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | // Setup the VM for stepping. |
| 1442 | this.exec_state_.prepareStep(action, count); |
| 1443 | } |
| 1444 | |
| 1445 | // VM should be running after executing this request. |
| 1446 | response.running = true; |
| 1447 | }; |
| 1448 | |
| 1449 | |
| 1450 | DebugCommandProcessor.prototype.breakRequest_ = function(request, response) { |
| 1451 | // Ignore as break command does not do anything when broken. |
| 1452 | }; |
| 1453 | |
| 1454 | |
| 1455 | DebugCommandProcessor.prototype.setBreakPointRequest_ = |
| 1456 | function(request, response) { |
| 1457 | // Check for legal request. |
| 1458 | if (!request.arguments) { |
| 1459 | response.failed('Missing arguments'); |
| 1460 | return; |
| 1461 | } |
| 1462 | |
| 1463 | // Pull out arguments. |
| 1464 | var type = request.arguments.type; |
| 1465 | var target = request.arguments.target; |
| 1466 | var line = request.arguments.line; |
| 1467 | var column = request.arguments.column; |
| 1468 | var enabled = IS_UNDEFINED(request.arguments.enabled) ? |
| 1469 | true : request.arguments.enabled; |
| 1470 | var condition = request.arguments.condition; |
| 1471 | var ignoreCount = request.arguments.ignoreCount; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1472 | var groupId = request.arguments.groupId; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1473 | |
| 1474 | // Check for legal arguments. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1475 | if (!type || IS_UNDEFINED(target)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1476 | response.failed('Missing argument "type" or "target"'); |
| 1477 | return; |
| 1478 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1479 | if (type != 'function' && type != 'handle' && |
| 1480 | type != 'script' && type != 'scriptId') { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1481 | response.failed('Illegal type "' + type + '"'); |
| 1482 | return; |
| 1483 | } |
| 1484 | |
| 1485 | // Either function or script break point. |
| 1486 | var break_point_number; |
| 1487 | if (type == 'function') { |
| 1488 | // Handle function break point. |
| 1489 | if (!IS_STRING(target)) { |
| 1490 | response.failed('Argument "target" is not a string value'); |
| 1491 | return; |
| 1492 | } |
| 1493 | var f; |
| 1494 | try { |
| 1495 | // Find the function through a global evaluate. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1496 | f = this.exec_state_.evaluateGlobal(target).value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1497 | } catch (e) { |
| 1498 | response.failed('Error: "' + %ToString(e) + |
| 1499 | '" evaluating "' + target + '"'); |
| 1500 | return; |
| 1501 | } |
| 1502 | if (!IS_FUNCTION(f)) { |
| 1503 | response.failed('"' + target + '" does not evaluate to a function'); |
| 1504 | return; |
| 1505 | } |
| 1506 | |
| 1507 | // Set function break point. |
| 1508 | break_point_number = Debug.setBreakPoint(f, line, column, condition); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1509 | } else if (type == 'handle') { |
| 1510 | // Find the object pointed by the specified handle. |
| 1511 | var handle = parseInt(target, 10); |
| 1512 | var mirror = LookupMirror(handle); |
| 1513 | if (!mirror) { |
| 1514 | return response.failed('Object #' + handle + '# not found'); |
| 1515 | } |
| 1516 | if (!mirror.isFunction()) { |
| 1517 | return response.failed('Object #' + handle + '# is not a function'); |
| 1518 | } |
| 1519 | |
| 1520 | // Set function break point. |
| 1521 | break_point_number = Debug.setBreakPoint(mirror.value(), |
| 1522 | line, column, condition); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1523 | } else if (type == 'script') { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1524 | // set script break point. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1525 | break_point_number = |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1526 | Debug.setScriptBreakPointByName(target, line, column, condition, |
| 1527 | groupId); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1528 | } else { // type == 'scriptId. |
| 1529 | break_point_number = |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1530 | Debug.setScriptBreakPointById(target, line, column, condition, groupId); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | // Set additional break point properties. |
| 1534 | var break_point = Debug.findBreakPoint(break_point_number); |
| 1535 | if (ignoreCount) { |
| 1536 | Debug.changeBreakPointIgnoreCount(break_point_number, ignoreCount); |
| 1537 | } |
| 1538 | if (!enabled) { |
| 1539 | Debug.disableBreakPoint(break_point_number); |
| 1540 | } |
| 1541 | |
| 1542 | // Add the break point number to the response. |
| 1543 | response.body = { type: type, |
| 1544 | breakpoint: break_point_number } |
| 1545 | |
| 1546 | // Add break point information to the response. |
| 1547 | if (break_point instanceof ScriptBreakPoint) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1548 | if (break_point.type() == Debug.ScriptBreakPointType.ScriptId) { |
| 1549 | response.body.type = 'scriptId'; |
| 1550 | response.body.script_id = break_point.script_id(); |
| 1551 | } else { |
| 1552 | response.body.type = 'scriptName'; |
| 1553 | response.body.script_name = break_point.script_name(); |
| 1554 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1555 | response.body.line = break_point.line(); |
| 1556 | response.body.column = break_point.column(); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 1557 | response.body.actual_locations = break_point.actual_locations(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1558 | } else { |
| 1559 | response.body.type = 'function'; |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 1560 | response.body.actual_locations = [break_point.actual_location]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1561 | } |
| 1562 | }; |
| 1563 | |
| 1564 | |
| 1565 | DebugCommandProcessor.prototype.changeBreakPointRequest_ = function(request, response) { |
| 1566 | // Check for legal request. |
| 1567 | if (!request.arguments) { |
| 1568 | response.failed('Missing arguments'); |
| 1569 | return; |
| 1570 | } |
| 1571 | |
| 1572 | // Pull out arguments. |
| 1573 | var break_point = %ToNumber(request.arguments.breakpoint); |
| 1574 | var enabled = request.arguments.enabled; |
| 1575 | var condition = request.arguments.condition; |
| 1576 | var ignoreCount = request.arguments.ignoreCount; |
| 1577 | |
| 1578 | // Check for legal arguments. |
| 1579 | if (!break_point) { |
| 1580 | response.failed('Missing argument "breakpoint"'); |
| 1581 | return; |
| 1582 | } |
| 1583 | |
| 1584 | // Change enabled state if supplied. |
| 1585 | if (!IS_UNDEFINED(enabled)) { |
| 1586 | if (enabled) { |
| 1587 | Debug.enableBreakPoint(break_point); |
| 1588 | } else { |
| 1589 | Debug.disableBreakPoint(break_point); |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | // Change condition if supplied |
| 1594 | if (!IS_UNDEFINED(condition)) { |
| 1595 | Debug.changeBreakPointCondition(break_point, condition); |
| 1596 | } |
| 1597 | |
| 1598 | // Change ignore count if supplied |
| 1599 | if (!IS_UNDEFINED(ignoreCount)) { |
| 1600 | Debug.changeBreakPointIgnoreCount(break_point, ignoreCount); |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1605 | DebugCommandProcessor.prototype.clearBreakPointGroupRequest_ = function(request, response) { |
| 1606 | // Check for legal request. |
| 1607 | if (!request.arguments) { |
| 1608 | response.failed('Missing arguments'); |
| 1609 | return; |
| 1610 | } |
| 1611 | |
| 1612 | // Pull out arguments. |
| 1613 | var group_id = request.arguments.groupId; |
| 1614 | |
| 1615 | // Check for legal arguments. |
| 1616 | if (!group_id) { |
| 1617 | response.failed('Missing argument "groupId"'); |
| 1618 | return; |
| 1619 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1620 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1621 | var cleared_break_points = []; |
| 1622 | var new_script_break_points = []; |
| 1623 | for (var i = 0; i < script_break_points.length; i++) { |
| 1624 | var next_break_point = script_break_points[i]; |
| 1625 | if (next_break_point.groupId() == group_id) { |
| 1626 | cleared_break_points.push(next_break_point.number()); |
| 1627 | next_break_point.clear(); |
| 1628 | } else { |
| 1629 | new_script_break_points.push(next_break_point); |
| 1630 | } |
| 1631 | } |
| 1632 | script_break_points = new_script_break_points; |
| 1633 | |
| 1634 | // Add the cleared break point numbers to the response. |
| 1635 | response.body = { breakpoints: cleared_break_points }; |
| 1636 | } |
| 1637 | |
| 1638 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1639 | DebugCommandProcessor.prototype.clearBreakPointRequest_ = function(request, response) { |
| 1640 | // Check for legal request. |
| 1641 | if (!request.arguments) { |
| 1642 | response.failed('Missing arguments'); |
| 1643 | return; |
| 1644 | } |
| 1645 | |
| 1646 | // Pull out arguments. |
| 1647 | var break_point = %ToNumber(request.arguments.breakpoint); |
| 1648 | |
| 1649 | // Check for legal arguments. |
| 1650 | if (!break_point) { |
| 1651 | response.failed('Missing argument "breakpoint"'); |
| 1652 | return; |
| 1653 | } |
| 1654 | |
| 1655 | // Clear break point. |
| 1656 | Debug.clearBreakPoint(break_point); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1657 | |
| 1658 | // Add the cleared break point number to the response. |
| 1659 | response.body = { breakpoint: break_point } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1662 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1663 | DebugCommandProcessor.prototype.listBreakpointsRequest_ = function(request, response) { |
| 1664 | var array = []; |
| 1665 | for (var i = 0; i < script_break_points.length; i++) { |
| 1666 | var break_point = script_break_points[i]; |
| 1667 | |
| 1668 | var description = { |
| 1669 | number: break_point.number(), |
| 1670 | line: break_point.line(), |
| 1671 | column: break_point.column(), |
| 1672 | groupId: break_point.groupId(), |
| 1673 | hit_count: break_point.hit_count(), |
| 1674 | active: break_point.active(), |
| 1675 | condition: break_point.condition(), |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 1676 | ignoreCount: break_point.ignoreCount(), |
| 1677 | actual_locations: break_point.actual_locations() |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1678 | } |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1679 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1680 | if (break_point.type() == Debug.ScriptBreakPointType.ScriptId) { |
| 1681 | description.type = 'scriptId'; |
| 1682 | description.script_id = break_point.script_id(); |
| 1683 | } else { |
| 1684 | description.type = 'scriptName'; |
| 1685 | description.script_name = break_point.script_name(); |
| 1686 | } |
| 1687 | array.push(description); |
| 1688 | } |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 1689 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1690 | response.body = { breakpoints: array } |
| 1691 | } |
| 1692 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1693 | |
| 1694 | DebugCommandProcessor.prototype.backtraceRequest_ = function(request, response) { |
| 1695 | // Get the number of frames. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1696 | var total_frames = this.exec_state_.frameCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1697 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 1698 | // Create simple response if there are no frames. |
| 1699 | if (total_frames == 0) { |
| 1700 | response.body = { |
| 1701 | totalFrames: total_frames |
| 1702 | } |
| 1703 | return; |
| 1704 | } |
| 1705 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1706 | // Default frame range to include in backtrace. |
| 1707 | var from_index = 0 |
| 1708 | var to_index = kDefaultBacktraceLength; |
| 1709 | |
| 1710 | // Get the range from the arguments. |
| 1711 | if (request.arguments) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1712 | if (request.arguments.fromFrame) { |
| 1713 | from_index = request.arguments.fromFrame; |
| 1714 | } |
| 1715 | if (request.arguments.toFrame) { |
| 1716 | to_index = request.arguments.toFrame; |
| 1717 | } |
| 1718 | if (request.arguments.bottom) { |
| 1719 | var tmp_index = total_frames - from_index; |
| 1720 | from_index = total_frames - to_index |
| 1721 | to_index = tmp_index; |
| 1722 | } |
| 1723 | if (from_index < 0 || to_index < 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1724 | return response.failed('Invalid frame number'); |
| 1725 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | // Adjust the index. |
| 1729 | to_index = Math.min(total_frames, to_index); |
| 1730 | |
| 1731 | if (to_index <= from_index) { |
| 1732 | var error = 'Invalid frame range'; |
| 1733 | return response.failed(error); |
| 1734 | } |
| 1735 | |
| 1736 | // Create the response body. |
| 1737 | var frames = []; |
| 1738 | for (var i = from_index; i < to_index; i++) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1739 | frames.push(this.exec_state_.frame(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1740 | } |
| 1741 | response.body = { |
| 1742 | fromFrame: from_index, |
| 1743 | toFrame: to_index, |
| 1744 | totalFrames: total_frames, |
| 1745 | frames: frames |
| 1746 | } |
| 1747 | }; |
| 1748 | |
| 1749 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1750 | DebugCommandProcessor.prototype.frameRequest_ = function(request, response) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1751 | // No frames no source. |
| 1752 | if (this.exec_state_.frameCount() == 0) { |
| 1753 | return response.failed('No frames'); |
| 1754 | } |
| 1755 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1756 | // With no arguments just keep the selected frame. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1757 | if (request.arguments) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1758 | var index = request.arguments.number; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1759 | if (index < 0 || this.exec_state_.frameCount() <= index) { |
| 1760 | return response.failed('Invalid frame number'); |
| 1761 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1762 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1763 | this.exec_state_.setSelectedFrame(request.arguments.number); |
| 1764 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1765 | response.body = this.exec_state_.frame(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1766 | }; |
| 1767 | |
| 1768 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1769 | DebugCommandProcessor.prototype.frameForScopeRequest_ = function(request) { |
| 1770 | // Get the frame for which the scope or scopes are requested. With no frameNumber |
| 1771 | // argument use the currently selected frame. |
| 1772 | if (request.arguments && !IS_UNDEFINED(request.arguments.frameNumber)) { |
| 1773 | frame_index = request.arguments.frameNumber; |
| 1774 | if (frame_index < 0 || this.exec_state_.frameCount() <= frame_index) { |
| 1775 | return response.failed('Invalid frame number'); |
| 1776 | } |
| 1777 | return this.exec_state_.frame(frame_index); |
| 1778 | } else { |
| 1779 | return this.exec_state_.frame(); |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | |
| 1784 | DebugCommandProcessor.prototype.scopesRequest_ = function(request, response) { |
| 1785 | // No frames no scopes. |
| 1786 | if (this.exec_state_.frameCount() == 0) { |
| 1787 | return response.failed('No scopes'); |
| 1788 | } |
| 1789 | |
| 1790 | // Get the frame for which the scopes are requested. |
| 1791 | var frame = this.frameForScopeRequest_(request); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1792 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1793 | // Fill all scopes for this frame. |
| 1794 | var total_scopes = frame.scopeCount(); |
| 1795 | var scopes = []; |
| 1796 | for (var i = 0; i < total_scopes; i++) { |
| 1797 | scopes.push(frame.scope(i)); |
| 1798 | } |
| 1799 | response.body = { |
| 1800 | fromScope: 0, |
| 1801 | toScope: total_scopes, |
| 1802 | totalScopes: total_scopes, |
| 1803 | scopes: scopes |
| 1804 | } |
| 1805 | }; |
| 1806 | |
| 1807 | |
| 1808 | DebugCommandProcessor.prototype.scopeRequest_ = function(request, response) { |
| 1809 | // No frames no scopes. |
| 1810 | if (this.exec_state_.frameCount() == 0) { |
| 1811 | return response.failed('No scopes'); |
| 1812 | } |
| 1813 | |
| 1814 | // Get the frame for which the scope is requested. |
| 1815 | var frame = this.frameForScopeRequest_(request); |
| 1816 | |
| 1817 | // With no scope argument just return top scope. |
| 1818 | var scope_index = 0; |
| 1819 | if (request.arguments && !IS_UNDEFINED(request.arguments.number)) { |
| 1820 | scope_index = %ToNumber(request.arguments.number); |
| 1821 | if (scope_index < 0 || frame.scopeCount() <= scope_index) { |
| 1822 | return response.failed('Invalid scope number'); |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | response.body = frame.scope(scope_index); |
| 1827 | }; |
| 1828 | |
| 1829 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1830 | DebugCommandProcessor.prototype.evaluateRequest_ = function(request, response) { |
| 1831 | if (!request.arguments) { |
| 1832 | return response.failed('Missing arguments'); |
| 1833 | } |
| 1834 | |
| 1835 | // Pull out arguments. |
| 1836 | var expression = request.arguments.expression; |
| 1837 | var frame = request.arguments.frame; |
| 1838 | var global = request.arguments.global; |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 1839 | var disable_break = request.arguments.disable_break; |
| 1840 | |
| 1841 | // The expression argument could be an integer so we convert it to a |
| 1842 | // string. |
| 1843 | try { |
| 1844 | expression = String(expression); |
| 1845 | } catch(e) { |
| 1846 | return response.failed('Failed to convert expression argument to string'); |
| 1847 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1848 | |
| 1849 | // Check for legal arguments. |
| 1850 | if (!IS_UNDEFINED(frame) && global) { |
| 1851 | return response.failed('Arguments "frame" and "global" are exclusive'); |
| 1852 | } |
| 1853 | |
| 1854 | // Global evaluate. |
| 1855 | if (global) { |
| 1856 | // Evaluate in the global context. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1857 | response.body = |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1858 | this.exec_state_.evaluateGlobal(expression, Boolean(disable_break)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1859 | return; |
| 1860 | } |
| 1861 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 1862 | // Default value for disable_break is true. |
| 1863 | if (IS_UNDEFINED(disable_break)) { |
| 1864 | disable_break = true; |
| 1865 | } |
| 1866 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1867 | // No frames no evaluate in frame. |
| 1868 | if (this.exec_state_.frameCount() == 0) { |
| 1869 | return response.failed('No frames'); |
| 1870 | } |
| 1871 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1872 | // Check whether a frame was specified. |
| 1873 | if (!IS_UNDEFINED(frame)) { |
| 1874 | var frame_number = %ToNumber(frame); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1875 | if (frame_number < 0 || frame_number >= this.exec_state_.frameCount()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1876 | return response.failed('Invalid frame "' + frame + '"'); |
| 1877 | } |
| 1878 | // Evaluate in the specified frame. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1879 | response.body = this.exec_state_.frame(frame_number).evaluate( |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 1880 | expression, Boolean(disable_break)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1881 | return; |
| 1882 | } else { |
| 1883 | // Evaluate in the selected frame. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1884 | response.body = this.exec_state_.frame().evaluate( |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 1885 | expression, Boolean(disable_break)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1886 | return; |
| 1887 | } |
| 1888 | }; |
| 1889 | |
| 1890 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1891 | DebugCommandProcessor.prototype.lookupRequest_ = function(request, response) { |
| 1892 | if (!request.arguments) { |
| 1893 | return response.failed('Missing arguments'); |
| 1894 | } |
| 1895 | |
| 1896 | // Pull out arguments. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1897 | var handles = request.arguments.handles; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1898 | |
| 1899 | // Check for legal arguments. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1900 | if (IS_UNDEFINED(handles)) { |
| 1901 | return response.failed('Argument "handles" missing'); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1904 | // Set 'includeSource' option for script lookup. |
| 1905 | if (!IS_UNDEFINED(request.arguments.includeSource)) { |
| 1906 | includeSource = %ToBoolean(request.arguments.includeSource); |
| 1907 | response.setOption('includeSource', includeSource); |
| 1908 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1909 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1910 | // Lookup handles. |
| 1911 | var mirrors = {}; |
| 1912 | for (var i = 0; i < handles.length; i++) { |
| 1913 | var handle = handles[i]; |
| 1914 | var mirror = LookupMirror(handle); |
| 1915 | if (!mirror) { |
| 1916 | return response.failed('Object #' + handle + '# not found'); |
| 1917 | } |
| 1918 | mirrors[handle] = mirror; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1919 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1920 | response.body = mirrors; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1921 | }; |
| 1922 | |
| 1923 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1924 | DebugCommandProcessor.prototype.referencesRequest_ = |
| 1925 | function(request, response) { |
| 1926 | if (!request.arguments) { |
| 1927 | return response.failed('Missing arguments'); |
| 1928 | } |
| 1929 | |
| 1930 | // Pull out arguments. |
| 1931 | var type = request.arguments.type; |
| 1932 | var handle = request.arguments.handle; |
| 1933 | |
| 1934 | // Check for legal arguments. |
| 1935 | if (IS_UNDEFINED(type)) { |
| 1936 | return response.failed('Argument "type" missing'); |
| 1937 | } |
| 1938 | if (IS_UNDEFINED(handle)) { |
| 1939 | return response.failed('Argument "handle" missing'); |
| 1940 | } |
| 1941 | if (type != 'referencedBy' && type != 'constructedBy') { |
| 1942 | return response.failed('Invalid type "' + type + '"'); |
| 1943 | } |
| 1944 | |
| 1945 | // Lookup handle and return objects with references the object. |
| 1946 | var mirror = LookupMirror(handle); |
| 1947 | if (mirror) { |
| 1948 | if (type == 'referencedBy') { |
| 1949 | response.body = mirror.referencedBy(); |
| 1950 | } else { |
| 1951 | response.body = mirror.constructedBy(); |
| 1952 | } |
| 1953 | } else { |
| 1954 | return response.failed('Object #' + handle + '# not found'); |
| 1955 | } |
| 1956 | }; |
| 1957 | |
| 1958 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1959 | DebugCommandProcessor.prototype.sourceRequest_ = function(request, response) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1960 | // No frames no source. |
| 1961 | if (this.exec_state_.frameCount() == 0) { |
| 1962 | return response.failed('No source'); |
| 1963 | } |
| 1964 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1965 | var from_line; |
| 1966 | var to_line; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1967 | var frame = this.exec_state_.frame(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1968 | if (request.arguments) { |
| 1969 | // Pull out arguments. |
| 1970 | from_line = request.arguments.fromLine; |
| 1971 | to_line = request.arguments.toLine; |
| 1972 | |
| 1973 | if (!IS_UNDEFINED(request.arguments.frame)) { |
| 1974 | var frame_number = %ToNumber(request.arguments.frame); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1975 | if (frame_number < 0 || frame_number >= this.exec_state_.frameCount()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1976 | return response.failed('Invalid frame "' + frame + '"'); |
| 1977 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1978 | frame = this.exec_state_.frame(frame_number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | // Get the script selected. |
| 1983 | var script = frame.func().script(); |
| 1984 | if (!script) { |
| 1985 | return response.failed('No source'); |
| 1986 | } |
| 1987 | |
| 1988 | // Get the source slice and fill it into the response. |
| 1989 | var slice = script.sourceSlice(from_line, to_line); |
| 1990 | if (!slice) { |
| 1991 | return response.failed('Invalid line interval'); |
| 1992 | } |
| 1993 | response.body = {}; |
| 1994 | response.body.source = slice.sourceText(); |
| 1995 | response.body.fromLine = slice.from_line; |
| 1996 | response.body.toLine = slice.to_line; |
| 1997 | response.body.fromPosition = slice.from_position; |
| 1998 | response.body.toPosition = slice.to_position; |
| 1999 | response.body.totalLines = script.lineCount(); |
| 2000 | }; |
| 2001 | |
| 2002 | |
| 2003 | DebugCommandProcessor.prototype.scriptsRequest_ = function(request, response) { |
| 2004 | var types = ScriptTypeFlag(Debug.ScriptType.Normal); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 2005 | var includeSource = false; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2006 | var idsToInclude = null; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2007 | if (request.arguments) { |
| 2008 | // Pull out arguments. |
| 2009 | if (!IS_UNDEFINED(request.arguments.types)) { |
| 2010 | types = %ToNumber(request.arguments.types); |
| 2011 | if (isNaN(types) || types < 0) { |
| 2012 | return response.failed('Invalid types "' + request.arguments.types + '"'); |
| 2013 | } |
| 2014 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2015 | |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 2016 | if (!IS_UNDEFINED(request.arguments.includeSource)) { |
| 2017 | includeSource = %ToBoolean(request.arguments.includeSource); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 2018 | response.setOption('includeSource', includeSource); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 2019 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2020 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2021 | if (IS_ARRAY(request.arguments.ids)) { |
| 2022 | idsToInclude = {}; |
| 2023 | var ids = request.arguments.ids; |
| 2024 | for (var i = 0; i < ids.length; i++) { |
| 2025 | idsToInclude[ids[i]] = true; |
| 2026 | } |
| 2027 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | // Collect all scripts in the heap. |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2031 | var scripts = %DebugGetLoadedScripts(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2032 | |
| 2033 | response.body = []; |
| 2034 | |
| 2035 | for (var i = 0; i < scripts.length; i++) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2036 | if (idsToInclude && !idsToInclude[scripts[i].id]) { |
| 2037 | continue; |
| 2038 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2039 | if (types & ScriptTypeFlag(scripts[i].type)) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 2040 | response.body.push(MakeMirror(scripts[i])); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2041 | } |
| 2042 | } |
| 2043 | }; |
| 2044 | |
| 2045 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2046 | DebugCommandProcessor.prototype.threadsRequest_ = function(request, response) { |
| 2047 | // Get the number of threads. |
| 2048 | var total_threads = this.exec_state_.threadCount(); |
| 2049 | |
| 2050 | // Get information for all threads. |
| 2051 | var threads = []; |
| 2052 | for (var i = 0; i < total_threads; i++) { |
| 2053 | var details = %GetThreadDetails(this.exec_state_.break_id, i); |
| 2054 | var thread_info = { current: details[0], |
| 2055 | id: details[1] |
| 2056 | } |
| 2057 | threads.push(thread_info); |
| 2058 | } |
| 2059 | |
| 2060 | // Create the response body. |
| 2061 | response.body = { |
| 2062 | totalThreads: total_threads, |
| 2063 | threads: threads |
| 2064 | } |
| 2065 | }; |
| 2066 | |
| 2067 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2068 | DebugCommandProcessor.prototype.suspendRequest_ = function(request, response) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2069 | response.running = false; |
| 2070 | }; |
| 2071 | |
| 2072 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2073 | DebugCommandProcessor.prototype.versionRequest_ = function(request, response) { |
| 2074 | response.body = { |
| 2075 | V8Version: %GetV8Version() |
| 2076 | } |
| 2077 | }; |
| 2078 | |
| 2079 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2080 | DebugCommandProcessor.prototype.profileRequest_ = function(request, response) { |
| 2081 | if (!request.arguments) { |
| 2082 | return response.failed('Missing arguments'); |
| 2083 | } |
| 2084 | var modules = parseInt(request.arguments.modules); |
| 2085 | if (isNaN(modules)) { |
| 2086 | return response.failed('Modules is not an integer'); |
| 2087 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2088 | var tag = parseInt(request.arguments.tag); |
| 2089 | if (isNaN(tag)) { |
| 2090 | tag = 0; |
| 2091 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2092 | if (request.arguments.command == 'resume') { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2093 | %ProfilerResume(modules, tag); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2094 | } else if (request.arguments.command == 'pause') { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2095 | %ProfilerPause(modules, tag); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2096 | } else { |
| 2097 | return response.failed('Unknown command'); |
| 2098 | } |
| 2099 | response.body = {}; |
| 2100 | }; |
| 2101 | |
| 2102 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2103 | DebugCommandProcessor.prototype.changeLiveRequest_ = function(request, response) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 2104 | if (!Debug.LiveEdit) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2105 | return response.failed('LiveEdit feature is not supported'); |
| 2106 | } |
| 2107 | if (!request.arguments) { |
| 2108 | return response.failed('Missing arguments'); |
| 2109 | } |
| 2110 | var script_id = request.arguments.script_id; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2111 | var preview_only = !!request.arguments.preview_only; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2112 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2113 | var scripts = %DebugGetLoadedScripts(); |
| 2114 | |
| 2115 | var the_script = null; |
| 2116 | for (var i = 0; i < scripts.length; i++) { |
| 2117 | if (scripts[i].id == script_id) { |
| 2118 | the_script = scripts[i]; |
| 2119 | } |
| 2120 | } |
| 2121 | if (!the_script) { |
| 2122 | response.failed('Script not found'); |
| 2123 | return; |
| 2124 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2125 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2126 | var change_log = new Array(); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 2127 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 2128 | if (!IS_STRING(request.arguments.new_source)) { |
| 2129 | throw "new_source argument expected"; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 2132 | var new_source = request.arguments.new_source; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2133 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 2134 | var result_description = Debug.LiveEdit.SetScriptSource(the_script, |
| 2135 | new_source, preview_only, change_log); |
| 2136 | response.body = {change_log: change_log, result: result_description}; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2137 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 2138 | if (!preview_only && !this.running_ && result_description.stack_modified) { |
| 2139 | response.body.stepin_recommended = true; |
| 2140 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2141 | }; |
| 2142 | |
| 2143 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 2144 | DebugCommandProcessor.prototype.debuggerFlagsRequest_ = function(request, |
| 2145 | response) { |
| 2146 | // Check for legal request. |
| 2147 | if (!request.arguments) { |
| 2148 | response.failed('Missing arguments'); |
| 2149 | return; |
| 2150 | } |
| 2151 | |
| 2152 | // Pull out arguments. |
| 2153 | var flags = request.arguments.flags; |
| 2154 | |
| 2155 | response.body = { flags: [] }; |
| 2156 | if (!IS_UNDEFINED(flags)) { |
| 2157 | for (var i = 0; i < flags.length; i++) { |
| 2158 | var name = flags[i].name; |
| 2159 | var debugger_flag = debugger_flags[name]; |
| 2160 | if (!debugger_flag) { |
| 2161 | continue; |
| 2162 | } |
| 2163 | if ('value' in flags[i]) { |
| 2164 | debugger_flag.setValue(flags[i].value); |
| 2165 | } |
| 2166 | response.body.flags.push({ name: name, value: debugger_flag.getValue() }); |
| 2167 | } |
| 2168 | } else { |
| 2169 | for (var name in debugger_flags) { |
| 2170 | var value = debugger_flags[name].getValue(); |
| 2171 | response.body.flags.push({ name: name, value: value }); |
| 2172 | } |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 2177 | // Check whether the previously processed command caused the VM to become |
| 2178 | // running. |
| 2179 | DebugCommandProcessor.prototype.isRunning = function() { |
| 2180 | return this.running_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | |
| 2184 | DebugCommandProcessor.prototype.systemBreak = function(cmd, args) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 2185 | return %SystemBreak(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2186 | }; |
| 2187 | |
| 2188 | |
| 2189 | function NumberToHex8Str(n) { |
| 2190 | var r = ""; |
| 2191 | for (var i = 0; i < 8; ++i) { |
| 2192 | var c = hexCharArray[n & 0x0F]; // hexCharArray is defined in uri.js |
| 2193 | r = c + r; |
| 2194 | n = n >>> 4; |
| 2195 | } |
| 2196 | return r; |
| 2197 | }; |
| 2198 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2199 | |
| 2200 | /** |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2201 | * Convert an Object to its debugger protocol representation. The representation |
| 2202 | * may be serilized to a JSON object using JSON.stringify(). |
| 2203 | * This implementation simply runs through all string property names, converts |
| 2204 | * each property value to a protocol value and adds the property to the result |
| 2205 | * object. For type "object" the function will be called recursively. Note that |
| 2206 | * circular structures will cause infinite recursion. |
| 2207 | * @param {Object} object The object to format as protocol object. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2208 | * @param {MirrorSerializer} mirror_serializer The serializer to use if any |
| 2209 | * mirror objects are encountered. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2210 | * @return {Object} Protocol object value. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2211 | */ |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2212 | function ObjectToProtocolObject_(object, mirror_serializer) { |
| 2213 | var content = {}; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2214 | for (var key in object) { |
| 2215 | // Only consider string keys. |
| 2216 | if (typeof key == 'string') { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2217 | // Format the value based on its type. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2218 | var property_value_json = ValueToProtocolValue_(object[key], |
| 2219 | mirror_serializer); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2220 | // Add the property if relevant. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2221 | if (!IS_UNDEFINED(property_value_json)) { |
| 2222 | content[key] = property_value_json; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2226 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2227 | return content; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2228 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2229 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2230 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2231 | /** |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2232 | * Convert an array to its debugger protocol representation. It will convert |
| 2233 | * each array element to a protocol value. |
| 2234 | * @param {Array} array The array to format as protocol array. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2235 | * @param {MirrorSerializer} mirror_serializer The serializer to use if any |
| 2236 | * mirror objects are encountered. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2237 | * @return {Array} Protocol array value. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2238 | */ |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2239 | function ArrayToProtocolArray_(array, mirror_serializer) { |
| 2240 | var json = []; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2241 | for (var i = 0; i < array.length; i++) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2242 | json.push(ValueToProtocolValue_(array[i], mirror_serializer)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2243 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2244 | return json; |
| 2245 | } |
| 2246 | |
| 2247 | |
| 2248 | /** |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2249 | * Convert a value to its debugger protocol representation. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2250 | * @param {*} value The value to format as protocol value. |
| 2251 | * @param {MirrorSerializer} mirror_serializer The serializer to use if any |
| 2252 | * mirror objects are encountered. |
| 2253 | * @return {*} Protocol value. |
| 2254 | */ |
| 2255 | function ValueToProtocolValue_(value, mirror_serializer) { |
| 2256 | // Format the value based on its type. |
| 2257 | var json; |
| 2258 | switch (typeof value) { |
| 2259 | case 'object': |
| 2260 | if (value instanceof Mirror) { |
| 2261 | json = mirror_serializer.serializeValue(value); |
| 2262 | } else if (IS_ARRAY(value)){ |
| 2263 | json = ArrayToProtocolArray_(value, mirror_serializer); |
| 2264 | } else { |
| 2265 | json = ObjectToProtocolObject_(value, mirror_serializer); |
| 2266 | } |
| 2267 | break; |
| 2268 | |
| 2269 | case 'boolean': |
| 2270 | case 'string': |
| 2271 | case 'number': |
| 2272 | json = value; |
| 2273 | break |
| 2274 | |
| 2275 | default: |
| 2276 | json = null; |
| 2277 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2278 | return json; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2279 | } |