kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 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 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 28 | String.prototype.startsWith = function (str) { |
| 29 | if (str.length > this.length) |
| 30 | return false; |
| 31 | return this.substr(0, str.length) == str; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | function log10(num) { |
| 35 | return Math.log(num)/Math.log(10); |
| 36 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 37 | |
| 38 | function ToInspectableObject(obj) { |
| 39 | if (!obj && typeof obj === 'object') { |
| 40 | return void 0; |
| 41 | } else { |
| 42 | return Object(obj); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | function GetCompletions(global, last, full) { |
| 47 | var full_tokens = full.split(); |
| 48 | full = full_tokens.pop(); |
| 49 | var parts = full.split('.'); |
| 50 | parts.pop(); |
| 51 | var current = global; |
| 52 | for (var i = 0; i < parts.length; i++) { |
| 53 | var part = parts[i]; |
| 54 | var next = current[part]; |
| 55 | if (!next) |
| 56 | return []; |
| 57 | current = next; |
| 58 | } |
| 59 | var result = []; |
| 60 | current = ToInspectableObject(current); |
| 61 | while (typeof current !== 'undefined') { |
| 62 | var mirror = new $debug.ObjectMirror(current); |
| 63 | var properties = mirror.properties(); |
| 64 | for (var i = 0; i < properties.length; i++) { |
| 65 | var name = properties[i].name(); |
| 66 | if (typeof name === 'string' && name.startsWith(last)) |
| 67 | result.push(name); |
| 68 | } |
| 69 | current = ToInspectableObject(current.__proto__); |
| 70 | } |
| 71 | return result; |
| 72 | } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | // Global object holding debugger related constants and state. |
| 76 | const Debug = {}; |
| 77 | |
| 78 | |
| 79 | // Debug events which can occour in the V8 JavaScript engine. These originate |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 80 | // from the API include file v8-debug.h. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 81 | Debug.DebugEvent = { Break: 1, |
| 82 | Exception: 2, |
| 83 | NewFunction: 3, |
| 84 | BeforeCompile: 4, |
| 85 | AfterCompile: 5 }; |
| 86 | |
| 87 | |
| 88 | // The different types of scripts matching enum ScriptType in objects.h. |
| 89 | Debug.ScriptType = { Native: 0, |
| 90 | Extension: 1, |
| 91 | Normal: 2 }; |
| 92 | |
| 93 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 94 | // The different types of script compilations matching enum |
| 95 | // Script::CompilationType in objects.h. |
| 96 | Debug.ScriptCompilationType = { Host: 0, |
| 97 | Eval: 1, |
| 98 | JSON: 2 }; |
| 99 | |
| 100 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 101 | // The different types of scopes matching constants runtime.cc. |
| 102 | Debug.ScopeType = { Global: 0, |
| 103 | Local: 1, |
| 104 | With: 2, |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 105 | Closure: 3, |
| 106 | Catch: 4 }; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 107 | |
| 108 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 109 | // Current debug state. |
| 110 | const kNoFrame = -1; |
| 111 | Debug.State = { |
| 112 | currentFrame: kNoFrame, |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 113 | displaySourceStartLine: -1, |
| 114 | displaySourceEndLine: -1, |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 115 | currentSourceLine: -1 |
| 116 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 117 | var trace_compile = false; // Tracing all compile events? |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 118 | var trace_debug_json = false; // Tracing all debug json packets? |
| 119 | var last_cmd_line = ''; |
| 120 | var repeat_cmd_line = ''; |
| 121 | var is_running = true; |
| 122 | |
| 123 | // Copied from debug-delay.js. This is needed below: |
| 124 | function ScriptTypeFlag(type) { |
| 125 | return (1 << type); |
| 126 | } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 127 | |
| 128 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 129 | // Process a debugger JSON message into a display text and a running status. |
| 130 | // This function returns an object with properties "text" and "running" holding |
| 131 | // this information. |
| 132 | function DebugMessageDetails(message) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 133 | if (trace_debug_json) { |
| 134 | print("received: '" + message + "'"); |
| 135 | } |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 136 | // Convert the JSON string to an object. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 137 | var response = new ProtocolPackage(message); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 138 | is_running = response.running(); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 139 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 140 | if (response.type() == 'event') { |
| 141 | return DebugEventDetails(response); |
| 142 | } else { |
| 143 | return DebugResponseDetails(response); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | function DebugEventDetails(response) { |
| 148 | details = {text:'', running:false} |
| 149 | |
| 150 | // Get the running state. |
| 151 | details.running = response.running(); |
| 152 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 153 | var body = response.body(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 154 | var result = ''; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 155 | switch (response.event()) { |
| 156 | case 'break': |
| 157 | if (body.breakpoints) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 158 | result += 'breakpoint'; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 159 | if (body.breakpoints.length > 1) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 160 | result += 's'; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 161 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 162 | result += ' #'; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 163 | for (var i = 0; i < body.breakpoints.length; i++) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 164 | if (i > 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 165 | result += ', #'; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 166 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 167 | result += body.breakpoints[i]; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 168 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 169 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 170 | result += 'break'; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 171 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 172 | result += ' in '; |
| 173 | result += body.invocationText; |
| 174 | result += ', '; |
| 175 | result += SourceInfo(body); |
| 176 | result += '\n'; |
| 177 | result += SourceUnderline(body.sourceLineText, body.sourceColumn); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 178 | Debug.State.currentSourceLine = body.sourceLine; |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 179 | Debug.State.displaySourceStartLine = -1; |
| 180 | Debug.State.displaySourceEndLine = -1; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 181 | Debug.State.currentFrame = 0; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 182 | details.text = result; |
| 183 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 184 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 185 | case 'exception': |
| 186 | if (body.uncaught) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 187 | result += 'Uncaught: '; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 188 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 189 | result += 'Exception: '; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 190 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 191 | result += '"'; |
| 192 | result += body.exception.text; |
| 193 | result += '"'; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 194 | if (body.sourceLine >= 0) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 195 | result += ', '; |
| 196 | result += SourceInfo(body); |
| 197 | result += '\n'; |
| 198 | result += SourceUnderline(body.sourceLineText, body.sourceColumn); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 199 | Debug.State.currentSourceLine = body.sourceLine; |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 200 | Debug.State.displaySourceStartLine = -1; |
| 201 | Debug.State.displaySourceEndLine = -1; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 202 | Debug.State.currentFrame = 0; |
| 203 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 204 | result += ' (empty stack)'; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 205 | Debug.State.currentSourceLine = -1; |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 206 | Debug.State.displaySourceStartLine = -1; |
| 207 | Debug.State.displaySourceEndLine = -1; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 208 | Debug.State.currentFrame = kNoFrame; |
| 209 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 210 | details.text = result; |
| 211 | break; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 212 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 213 | case 'afterCompile': |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 214 | if (trace_compile) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 215 | result = 'Source ' + body.script.name + ' compiled:\n' |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 216 | var source = body.script.source; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 217 | if (!(source[source.length - 1] == '\n')) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 218 | result += source; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 219 | } else { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 220 | result += source.substring(0, source.length - 1); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 221 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 222 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 223 | details.text = result; |
| 224 | break; |
| 225 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 226 | case 'scriptCollected': |
| 227 | details.text = result; |
| 228 | break; |
| 229 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 230 | default: |
| 231 | details.text = 'Unknown debug event ' + response.event(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 232 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 233 | |
| 234 | return details; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 238 | function SourceInfo(body) { |
| 239 | var result = ''; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 240 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 241 | if (body.script) { |
| 242 | if (body.script.name) { |
| 243 | result += body.script.name; |
| 244 | } else { |
| 245 | result += '[unnamed]'; |
| 246 | } |
| 247 | } |
| 248 | result += ' line '; |
| 249 | result += body.sourceLine + 1; |
| 250 | result += ' column '; |
| 251 | result += body.sourceColumn + 1; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 252 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 253 | return result; |
| 254 | } |
| 255 | |
| 256 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 257 | function SourceUnderline(source_text, position) { |
| 258 | if (!source_text) { |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | // Create an underline with a caret pointing to the source position. If the |
| 263 | // source contains a tab character the underline will have a tab character in |
| 264 | // the same place otherwise the underline will have a space character. |
| 265 | var underline = ''; |
| 266 | for (var i = 0; i < position; i++) { |
| 267 | if (source_text[i] == '\t') { |
| 268 | underline += '\t'; |
| 269 | } else { |
| 270 | underline += ' '; |
| 271 | } |
| 272 | } |
| 273 | underline += '^'; |
| 274 | |
| 275 | // Return the source line text with the underline beneath. |
| 276 | return source_text + '\n' + underline; |
| 277 | }; |
| 278 | |
| 279 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 280 | // Converts a text command to a JSON request. |
| 281 | function DebugCommandToJSONRequest(cmd_line) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 282 | var result = new DebugRequest(cmd_line).JSONRequest(); |
| 283 | if (trace_debug_json && result) { |
| 284 | print("sending: '" + result + "'"); |
| 285 | } |
| 286 | return result; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | |
| 290 | function DebugRequest(cmd_line) { |
| 291 | // If the very first character is a { assume that a JSON request have been |
| 292 | // entered as a command. Converting that to a JSON request is trivial. |
| 293 | if (cmd_line && cmd_line.length > 0 && cmd_line.charAt(0) == '{') { |
| 294 | this.request_ = cmd_line; |
| 295 | return; |
| 296 | } |
| 297 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 298 | // Check for a simple carriage return to repeat the last command: |
| 299 | var is_repeating = false; |
| 300 | if (cmd_line == '\n') { |
| 301 | if (is_running) { |
| 302 | cmd_line = 'break'; // Not in debugger mode, break with a frame request. |
| 303 | } else { |
| 304 | cmd_line = repeat_cmd_line; // use command to repeat. |
| 305 | is_repeating = true; |
| 306 | } |
| 307 | } |
| 308 | if (!is_running) { // Only save the command if in debugger mode. |
| 309 | repeat_cmd_line = cmd_line; // save last command. |
| 310 | } |
| 311 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 312 | // Trim string for leading and trailing whitespace. |
| 313 | cmd_line = cmd_line.replace(/^\s+|\s+$/g, ''); |
| 314 | |
| 315 | // Find the command. |
| 316 | var pos = cmd_line.indexOf(' '); |
| 317 | var cmd; |
| 318 | var args; |
| 319 | if (pos == -1) { |
| 320 | cmd = cmd_line; |
| 321 | args = ''; |
| 322 | } else { |
| 323 | cmd = cmd_line.slice(0, pos); |
| 324 | args = cmd_line.slice(pos).replace(/^\s+|\s+$/g, ''); |
| 325 | } |
| 326 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 327 | if ((cmd === undefined) || !cmd) { |
| 328 | this.request_ = void 0; |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | last_cmd = cmd; |
| 333 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 334 | // Switch on command. |
| 335 | switch (cmd) { |
| 336 | case 'continue': |
| 337 | case 'c': |
| 338 | this.request_ = this.continueCommandToJSONRequest_(args); |
| 339 | break; |
| 340 | |
| 341 | case 'step': |
| 342 | case 's': |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 343 | this.request_ = this.stepCommandToJSONRequest_(args, 'in'); |
| 344 | break; |
| 345 | |
| 346 | case 'stepi': |
| 347 | case 'si': |
| 348 | this.request_ = this.stepCommandToJSONRequest_(args, 'min'); |
| 349 | break; |
| 350 | |
| 351 | case 'next': |
| 352 | case 'n': |
| 353 | this.request_ = this.stepCommandToJSONRequest_(args, 'next'); |
| 354 | break; |
| 355 | |
| 356 | case 'finish': |
| 357 | case 'fin': |
| 358 | this.request_ = this.stepCommandToJSONRequest_(args, 'out'); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 359 | break; |
| 360 | |
| 361 | case 'backtrace': |
| 362 | case 'bt': |
| 363 | this.request_ = this.backtraceCommandToJSONRequest_(args); |
| 364 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 365 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 366 | case 'frame': |
| 367 | case 'f': |
| 368 | this.request_ = this.frameCommandToJSONRequest_(args); |
| 369 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 370 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 371 | case 'scopes': |
| 372 | this.request_ = this.scopesCommandToJSONRequest_(args); |
| 373 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 374 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 375 | case 'scope': |
| 376 | this.request_ = this.scopeCommandToJSONRequest_(args); |
| 377 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 378 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 379 | case 'disconnect': |
| 380 | case 'exit': |
| 381 | case 'quit': |
| 382 | this.request_ = this.disconnectCommandToJSONRequest_(args); |
| 383 | break; |
| 384 | |
| 385 | case 'up': |
| 386 | this.request_ = |
| 387 | this.frameCommandToJSONRequest_('' + |
| 388 | (Debug.State.currentFrame + 1)); |
| 389 | break; |
| 390 | |
| 391 | case 'down': |
| 392 | case 'do': |
| 393 | this.request_ = |
| 394 | this.frameCommandToJSONRequest_('' + |
| 395 | (Debug.State.currentFrame - 1)); |
| 396 | break; |
| 397 | |
| 398 | case 'set': |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 399 | case 'print': |
| 400 | case 'p': |
| 401 | this.request_ = this.printCommandToJSONRequest_(args); |
| 402 | break; |
| 403 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 404 | case 'dir': |
| 405 | this.request_ = this.dirCommandToJSONRequest_(args); |
| 406 | break; |
| 407 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 408 | case 'references': |
| 409 | this.request_ = this.referencesCommandToJSONRequest_(args); |
| 410 | break; |
| 411 | |
| 412 | case 'instances': |
| 413 | this.request_ = this.instancesCommandToJSONRequest_(args); |
| 414 | break; |
| 415 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 416 | case 'list': |
| 417 | case 'l': |
| 418 | this.request_ = this.listCommandToJSONRequest_(args); |
| 419 | break; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 420 | case 'source': |
| 421 | this.request_ = this.sourceCommandToJSONRequest_(args); |
| 422 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 423 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 424 | case 'scripts': |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 425 | case 'script': |
| 426 | case 'scr': |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 427 | this.request_ = this.scriptsCommandToJSONRequest_(args); |
| 428 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 429 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 430 | case 'break': |
| 431 | case 'b': |
| 432 | this.request_ = this.breakCommandToJSONRequest_(args); |
| 433 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 434 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 435 | case 'breakpoints': |
| 436 | case 'bb': |
| 437 | this.request_ = this.breakpointsCommandToJSONRequest_(args); |
| 438 | break; |
| 439 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 440 | case 'clear': |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 441 | case 'delete': |
| 442 | case 'd': |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 443 | this.request_ = this.clearCommandToJSONRequest_(args); |
| 444 | break; |
| 445 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 446 | case 'threads': |
| 447 | this.request_ = this.threadsCommandToJSONRequest_(args); |
| 448 | break; |
| 449 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 450 | case 'cond': |
| 451 | this.request_ = this.changeBreakpointCommandToJSONRequest_(args, 'cond'); |
| 452 | break; |
| 453 | |
| 454 | case 'enable': |
| 455 | case 'en': |
| 456 | this.request_ = |
| 457 | this.changeBreakpointCommandToJSONRequest_(args, 'enable'); |
| 458 | break; |
| 459 | |
| 460 | case 'disable': |
| 461 | case 'dis': |
| 462 | this.request_ = |
| 463 | this.changeBreakpointCommandToJSONRequest_(args, 'disable'); |
| 464 | break; |
| 465 | |
| 466 | case 'ignore': |
| 467 | this.request_ = |
| 468 | this.changeBreakpointCommandToJSONRequest_(args, 'ignore'); |
| 469 | break; |
| 470 | |
| 471 | case 'info': |
| 472 | case 'inf': |
| 473 | this.request_ = this.infoCommandToJSONRequest_(args); |
| 474 | break; |
| 475 | |
| 476 | case 'flags': |
| 477 | this.request_ = this.v8FlagsToJSONRequest_(args); |
| 478 | break; |
| 479 | |
| 480 | case 'gc': |
| 481 | this.request_ = this.gcToJSONRequest_(args); |
| 482 | break; |
| 483 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 484 | case 'trace': |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 485 | case 'tr': |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 486 | // Return undefined to indicate command handled internally (no JSON). |
| 487 | this.request_ = void 0; |
| 488 | this.traceCommand_(args); |
| 489 | break; |
| 490 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 491 | case 'help': |
| 492 | case '?': |
| 493 | this.helpCommand_(args); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 494 | // Return undefined to indicate command handled internally (no JSON). |
| 495 | this.request_ = void 0; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 496 | break; |
| 497 | |
| 498 | default: |
| 499 | throw new Error('Unknown command "' + cmd + '"'); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | DebugRequest.prototype.JSONRequest = function() { |
| 504 | return this.request_; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | function RequestPacket(command) { |
| 509 | this.seq = 0; |
| 510 | this.type = 'request'; |
| 511 | this.command = command; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | RequestPacket.prototype.toJSONProtocol = function() { |
| 516 | // Encode the protocol header. |
| 517 | var json = '{'; |
| 518 | json += '"seq":' + this.seq; |
| 519 | json += ',"type":"' + this.type + '"'; |
| 520 | if (this.command) { |
| 521 | json += ',"command":' + StringToJSON_(this.command); |
| 522 | } |
| 523 | if (this.arguments) { |
| 524 | json += ',"arguments":'; |
| 525 | // Encode the arguments part. |
| 526 | if (this.arguments.toJSONProtocol) { |
| 527 | json += this.arguments.toJSONProtocol() |
| 528 | } else { |
| 529 | json += SimpleObjectToJSON_(this.arguments); |
| 530 | } |
| 531 | } |
| 532 | json += '}'; |
| 533 | return json; |
| 534 | } |
| 535 | |
| 536 | |
| 537 | DebugRequest.prototype.createRequest = function(command) { |
| 538 | return new RequestPacket(command); |
| 539 | }; |
| 540 | |
| 541 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 542 | // Create a JSON request for the evaluation command. |
| 543 | DebugRequest.prototype.makeEvaluateJSONRequest_ = function(expression) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 544 | // Global varaible used to store whether a handle was requested. |
| 545 | lookup_handle = null; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 546 | // Check if the expression is a handle id in the form #<handle>#. |
| 547 | var handle_match = expression.match(/^#([0-9]*)#$/); |
| 548 | if (handle_match) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 549 | // Remember the handle requested in a global variable. |
| 550 | lookup_handle = parseInt(handle_match[1]); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 551 | // Build a lookup request. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 552 | var request = this.createRequest('lookup'); |
| 553 | request.arguments = {}; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 554 | request.arguments.handles = [ lookup_handle ]; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 555 | return request.toJSONProtocol(); |
| 556 | } else { |
| 557 | // Build an evaluate request. |
| 558 | var request = this.createRequest('evaluate'); |
| 559 | request.arguments = {}; |
| 560 | request.arguments.expression = expression; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 561 | // Request a global evaluation if there is no current frame. |
| 562 | if (Debug.State.currentFrame == kNoFrame) { |
| 563 | request.arguments.global = true; |
| 564 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 565 | return request.toJSONProtocol(); |
| 566 | } |
| 567 | }; |
| 568 | |
| 569 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 570 | // Create a JSON request for the references/instances command. |
| 571 | DebugRequest.prototype.makeReferencesJSONRequest_ = function(handle, type) { |
| 572 | // Build a references request. |
| 573 | var handle_match = handle.match(/^#([0-9]*)#$/); |
| 574 | if (handle_match) { |
| 575 | var request = this.createRequest('references'); |
| 576 | request.arguments = {}; |
| 577 | request.arguments.type = type; |
| 578 | request.arguments.handle = parseInt(handle_match[1]); |
| 579 | return request.toJSONProtocol(); |
| 580 | } else { |
| 581 | throw new Error('Invalid object id.'); |
| 582 | } |
| 583 | }; |
| 584 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 585 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 586 | // Create a JSON request for the continue command. |
| 587 | DebugRequest.prototype.continueCommandToJSONRequest_ = function(args) { |
| 588 | var request = this.createRequest('continue'); |
| 589 | return request.toJSONProtocol(); |
| 590 | }; |
| 591 | |
| 592 | |
| 593 | // Create a JSON request for the step command. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 594 | DebugRequest.prototype.stepCommandToJSONRequest_ = function(args, type) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 595 | // Requesting a step is through the continue command with additional |
| 596 | // arguments. |
| 597 | var request = this.createRequest('continue'); |
| 598 | request.arguments = {}; |
| 599 | |
| 600 | // Process arguments if any. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 601 | |
| 602 | // Only process args if the command is 'step' which is indicated by type being |
| 603 | // set to 'in'. For all other commands, ignore the args. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 604 | if (args && args.length > 0) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 605 | args = args.split(/\s+/g); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 606 | |
| 607 | if (args.length > 2) { |
| 608 | throw new Error('Invalid step arguments.'); |
| 609 | } |
| 610 | |
| 611 | if (args.length > 0) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 612 | // Check if we have a gdb stype step command. If so, the 1st arg would |
| 613 | // be the step count. If it's not a number, then assume that we're |
| 614 | // parsing for the legacy v8 step command. |
| 615 | var stepcount = Number(args[0]); |
| 616 | if (stepcount == Number.NaN) { |
| 617 | // No step count at arg 1. Process as legacy d8 step command: |
| 618 | if (args.length == 2) { |
| 619 | var stepcount = parseInt(args[1]); |
| 620 | if (isNaN(stepcount) || stepcount <= 0) { |
| 621 | throw new Error('Invalid step count argument "' + args[0] + '".'); |
| 622 | } |
| 623 | request.arguments.stepcount = stepcount; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 624 | } |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 625 | |
| 626 | // Get the step action. |
| 627 | switch (args[0]) { |
| 628 | case 'in': |
| 629 | case 'i': |
| 630 | request.arguments.stepaction = 'in'; |
| 631 | break; |
| 632 | |
| 633 | case 'min': |
| 634 | case 'm': |
| 635 | request.arguments.stepaction = 'min'; |
| 636 | break; |
| 637 | |
| 638 | case 'next': |
| 639 | case 'n': |
| 640 | request.arguments.stepaction = 'next'; |
| 641 | break; |
| 642 | |
| 643 | case 'out': |
| 644 | case 'o': |
| 645 | request.arguments.stepaction = 'out'; |
| 646 | break; |
| 647 | |
| 648 | default: |
| 649 | throw new Error('Invalid step argument "' + args[0] + '".'); |
| 650 | } |
| 651 | |
| 652 | } else { |
| 653 | // gdb style step commands: |
| 654 | request.arguments.stepaction = type; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 655 | request.arguments.stepcount = stepcount; |
| 656 | } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 657 | } |
| 658 | } else { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 659 | // Default is step of the specified type. |
| 660 | request.arguments.stepaction = type; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | return request.toJSONProtocol(); |
| 664 | }; |
| 665 | |
| 666 | |
| 667 | // Create a JSON request for the backtrace command. |
| 668 | DebugRequest.prototype.backtraceCommandToJSONRequest_ = function(args) { |
| 669 | // Build a backtrace request from the text command. |
| 670 | var request = this.createRequest('backtrace'); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 671 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 672 | // Default is to show top 10 frames. |
| 673 | request.arguments = {}; |
| 674 | request.arguments.fromFrame = 0; |
| 675 | request.arguments.toFrame = 10; |
| 676 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 677 | args = args.split(/\s*[ ]+\s*/g); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 678 | if (args.length == 1 && args[0].length > 0) { |
| 679 | var frameCount = parseInt(args[0]); |
| 680 | if (frameCount > 0) { |
| 681 | // Show top frames. |
| 682 | request.arguments.fromFrame = 0; |
| 683 | request.arguments.toFrame = frameCount; |
| 684 | } else { |
| 685 | // Show bottom frames. |
| 686 | request.arguments.fromFrame = 0; |
| 687 | request.arguments.toFrame = -frameCount; |
| 688 | request.arguments.bottom = true; |
| 689 | } |
| 690 | } else if (args.length == 2) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 691 | var fromFrame = parseInt(args[0]); |
| 692 | var toFrame = parseInt(args[1]); |
| 693 | if (isNaN(fromFrame) || fromFrame < 0) { |
| 694 | throw new Error('Invalid start frame argument "' + args[0] + '".'); |
| 695 | } |
| 696 | if (isNaN(toFrame) || toFrame < 0) { |
| 697 | throw new Error('Invalid end frame argument "' + args[1] + '".'); |
| 698 | } |
| 699 | if (fromFrame > toFrame) { |
| 700 | throw new Error('Invalid arguments start frame cannot be larger ' + |
| 701 | 'than end frame.'); |
| 702 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 703 | // Show frame range. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 704 | request.arguments.fromFrame = fromFrame; |
| 705 | request.arguments.toFrame = toFrame + 1; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 706 | } else if (args.length > 2) { |
| 707 | throw new Error('Invalid backtrace arguments.'); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 708 | } |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 709 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 710 | return request.toJSONProtocol(); |
| 711 | }; |
| 712 | |
| 713 | |
| 714 | // Create a JSON request for the frame command. |
| 715 | DebugRequest.prototype.frameCommandToJSONRequest_ = function(args) { |
| 716 | // Build a frame request from the text command. |
| 717 | var request = this.createRequest('frame'); |
| 718 | args = args.split(/\s*[ ]+\s*/g); |
| 719 | if (args.length > 0 && args[0].length > 0) { |
| 720 | request.arguments = {}; |
| 721 | request.arguments.number = args[0]; |
| 722 | } |
| 723 | return request.toJSONProtocol(); |
| 724 | }; |
| 725 | |
| 726 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 727 | // Create a JSON request for the scopes command. |
| 728 | DebugRequest.prototype.scopesCommandToJSONRequest_ = function(args) { |
| 729 | // Build a scopes request from the text command. |
| 730 | var request = this.createRequest('scopes'); |
| 731 | return request.toJSONProtocol(); |
| 732 | }; |
| 733 | |
| 734 | |
| 735 | // Create a JSON request for the scope command. |
| 736 | DebugRequest.prototype.scopeCommandToJSONRequest_ = function(args) { |
| 737 | // Build a scope request from the text command. |
| 738 | var request = this.createRequest('scope'); |
| 739 | args = args.split(/\s*[ ]+\s*/g); |
| 740 | if (args.length > 0 && args[0].length > 0) { |
| 741 | request.arguments = {}; |
| 742 | request.arguments.number = args[0]; |
| 743 | } |
| 744 | return request.toJSONProtocol(); |
| 745 | }; |
| 746 | |
| 747 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 748 | // Create a JSON request for the print command. |
| 749 | DebugRequest.prototype.printCommandToJSONRequest_ = function(args) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 750 | // Build an evaluate request from the text command. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 751 | if (args.length == 0) { |
| 752 | throw new Error('Missing expression.'); |
| 753 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 754 | return this.makeEvaluateJSONRequest_(args); |
| 755 | }; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 756 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 757 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 758 | // Create a JSON request for the dir command. |
| 759 | DebugRequest.prototype.dirCommandToJSONRequest_ = function(args) { |
| 760 | // Build an evaluate request from the text command. |
| 761 | if (args.length == 0) { |
| 762 | throw new Error('Missing expression.'); |
| 763 | } |
| 764 | return this.makeEvaluateJSONRequest_(args); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 765 | }; |
| 766 | |
| 767 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 768 | // Create a JSON request for the references command. |
| 769 | DebugRequest.prototype.referencesCommandToJSONRequest_ = function(args) { |
| 770 | // Build an evaluate request from the text command. |
| 771 | if (args.length == 0) { |
| 772 | throw new Error('Missing object id.'); |
| 773 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 774 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 775 | return this.makeReferencesJSONRequest_(args, 'referencedBy'); |
| 776 | }; |
| 777 | |
| 778 | |
| 779 | // Create a JSON request for the instances command. |
| 780 | DebugRequest.prototype.instancesCommandToJSONRequest_ = function(args) { |
| 781 | // Build an evaluate request from the text command. |
| 782 | if (args.length == 0) { |
| 783 | throw new Error('Missing object id.'); |
| 784 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 785 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 786 | // Build a references request. |
| 787 | return this.makeReferencesJSONRequest_(args, 'constructedBy'); |
| 788 | }; |
| 789 | |
| 790 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 791 | // Create a JSON request for the list command. |
| 792 | DebugRequest.prototype.listCommandToJSONRequest_ = function(args) { |
| 793 | |
| 794 | // Default is ten lines starting five lines before the current location. |
| 795 | if (Debug.State.displaySourceEndLine == -1) { |
| 796 | // If we list forwards, we will start listing after the last source end |
| 797 | // line. Set it to start from 5 lines before the current location. |
| 798 | Debug.State.displaySourceEndLine = Debug.State.currentSourceLine - 5; |
| 799 | // If we list backwards, we will start listing backwards from the last |
| 800 | // source start line. Set it to start from 1 lines before the current |
| 801 | // location. |
| 802 | Debug.State.displaySourceStartLine = Debug.State.currentSourceLine + 1; |
| 803 | } |
| 804 | |
| 805 | var from = Debug.State.displaySourceEndLine + 1; |
| 806 | var lines = 10; |
| 807 | |
| 808 | // Parse the arguments. |
| 809 | args = args.split(/\s*,\s*/g); |
| 810 | if (args == '') { |
| 811 | } else if ((args.length == 1) && (args[0] == '-')) { |
| 812 | from = Debug.State.displaySourceStartLine - lines; |
| 813 | } else if (args.length == 2) { |
| 814 | from = parseInt(args[0]); |
| 815 | lines = parseInt(args[1]) - from + 1; // inclusive of the ending line. |
| 816 | } else { |
| 817 | throw new Error('Invalid list arguments.'); |
| 818 | } |
| 819 | Debug.State.displaySourceStartLine = from; |
| 820 | Debug.State.displaySourceEndLine = from + lines - 1; |
| 821 | var sourceArgs = '' + from + ' ' + lines; |
| 822 | return this.sourceCommandToJSONRequest_(sourceArgs); |
| 823 | }; |
| 824 | |
| 825 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 826 | // Create a JSON request for the source command. |
| 827 | DebugRequest.prototype.sourceCommandToJSONRequest_ = function(args) { |
| 828 | // Build a evaluate request from the text command. |
| 829 | var request = this.createRequest('source'); |
| 830 | |
| 831 | // Default is ten lines starting five lines before the current location. |
| 832 | var from = Debug.State.currentSourceLine - 5; |
| 833 | var lines = 10; |
| 834 | |
| 835 | // Parse the arguments. |
| 836 | args = args.split(/\s*[ ]+\s*/g); |
| 837 | if (args.length > 1 && args[0].length > 0 && args[1].length > 0) { |
| 838 | from = parseInt(args[0]) - 1; |
| 839 | lines = parseInt(args[1]); |
| 840 | } else if (args.length > 0 && args[0].length > 0) { |
| 841 | from = parseInt(args[0]) - 1; |
| 842 | } |
| 843 | |
| 844 | if (from < 0) from = 0; |
| 845 | if (lines < 0) lines = 10; |
| 846 | |
| 847 | // Request source arround current source location. |
| 848 | request.arguments = {}; |
| 849 | request.arguments.fromLine = from; |
| 850 | request.arguments.toLine = from + lines; |
| 851 | |
| 852 | return request.toJSONProtocol(); |
| 853 | }; |
| 854 | |
| 855 | |
| 856 | // Create a JSON request for the scripts command. |
| 857 | DebugRequest.prototype.scriptsCommandToJSONRequest_ = function(args) { |
| 858 | // Build a evaluate request from the text command. |
| 859 | var request = this.createRequest('scripts'); |
| 860 | |
| 861 | // Process arguments if any. |
| 862 | if (args && args.length > 0) { |
| 863 | args = args.split(/\s*[ ]+\s*/g); |
| 864 | |
| 865 | if (args.length > 1) { |
| 866 | throw new Error('Invalid scripts arguments.'); |
| 867 | } |
| 868 | |
| 869 | request.arguments = {}; |
| 870 | switch (args[0]) { |
| 871 | case 'natives': |
| 872 | request.arguments.types = ScriptTypeFlag(Debug.ScriptType.Native); |
| 873 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 874 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 875 | case 'extensions': |
| 876 | request.arguments.types = ScriptTypeFlag(Debug.ScriptType.Extension); |
| 877 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 878 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 879 | case 'all': |
| 880 | request.arguments.types = |
| 881 | ScriptTypeFlag(Debug.ScriptType.Normal) | |
| 882 | ScriptTypeFlag(Debug.ScriptType.Native) | |
| 883 | ScriptTypeFlag(Debug.ScriptType.Extension); |
| 884 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 885 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 886 | default: |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 887 | // If the arg is not one of the know one aboves, then it must be a |
| 888 | // filter used for filtering the results: |
| 889 | request.arguments.filter = args[0]; |
| 890 | break; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | |
| 894 | return request.toJSONProtocol(); |
| 895 | }; |
| 896 | |
| 897 | |
| 898 | // Create a JSON request for the break command. |
| 899 | DebugRequest.prototype.breakCommandToJSONRequest_ = function(args) { |
| 900 | // Build a evaluate request from the text command. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 901 | // Process arguments if any. |
| 902 | if (args && args.length > 0) { |
| 903 | var target = args; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 904 | var type = 'function'; |
| 905 | var line; |
| 906 | var column; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 907 | var condition; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 908 | var pos; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 909 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 910 | var request = this.createRequest('setbreakpoint'); |
| 911 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 912 | // Break the args into target spec and condition if appropriate. |
| 913 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 914 | // Check for breakpoint condition. |
| 915 | pos = args.indexOf(' '); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 916 | if (pos > 0) { |
| 917 | target = args.substring(0, pos); |
| 918 | condition = args.substring(pos + 1, args.length); |
| 919 | } |
| 920 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 921 | // Check for script breakpoint (name:line[:column]). If no ':' in break |
| 922 | // specification it is considered a function break point. |
| 923 | pos = target.indexOf(':'); |
| 924 | if (pos > 0) { |
| 925 | type = 'script'; |
| 926 | var tmp = target.substring(pos + 1, target.length); |
| 927 | target = target.substring(0, pos); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 928 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 929 | // Check for both line and column. |
| 930 | pos = tmp.indexOf(':'); |
| 931 | if (pos > 0) { |
| 932 | column = parseInt(tmp.substring(pos + 1, tmp.length)) - 1; |
| 933 | line = parseInt(tmp.substring(0, pos)) - 1; |
| 934 | } else { |
| 935 | line = parseInt(tmp) - 1; |
| 936 | } |
| 937 | } else if (target[0] == '#' && target[target.length - 1] == '#') { |
| 938 | type = 'handle'; |
| 939 | target = target.substring(1, target.length - 1); |
| 940 | } else { |
| 941 | type = 'function'; |
| 942 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 943 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 944 | request.arguments = {}; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 945 | request.arguments.type = type; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 946 | request.arguments.target = target; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 947 | request.arguments.line = line; |
| 948 | request.arguments.column = column; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 949 | request.arguments.condition = condition; |
| 950 | } else { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 951 | var request = this.createRequest('suspend'); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | return request.toJSONProtocol(); |
| 955 | }; |
| 956 | |
| 957 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 958 | DebugRequest.prototype.breakpointsCommandToJSONRequest_ = function(args) { |
| 959 | if (args && args.length > 0) { |
| 960 | throw new Error('Unexpected arguments.'); |
| 961 | } |
| 962 | var request = this.createRequest('listbreakpoints'); |
| 963 | return request.toJSONProtocol(); |
| 964 | }; |
| 965 | |
| 966 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 967 | // Create a JSON request for the clear command. |
| 968 | DebugRequest.prototype.clearCommandToJSONRequest_ = function(args) { |
| 969 | // Build a evaluate request from the text command. |
| 970 | var request = this.createRequest('clearbreakpoint'); |
| 971 | |
| 972 | // Process arguments if any. |
| 973 | if (args && args.length > 0) { |
| 974 | request.arguments = {}; |
| 975 | request.arguments.breakpoint = parseInt(args); |
| 976 | } else { |
| 977 | throw new Error('Invalid break arguments.'); |
| 978 | } |
| 979 | |
| 980 | return request.toJSONProtocol(); |
| 981 | }; |
| 982 | |
| 983 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 984 | // Create a JSON request for the change breakpoint command. |
| 985 | DebugRequest.prototype.changeBreakpointCommandToJSONRequest_ = |
| 986 | function(args, command) { |
| 987 | |
| 988 | var request; |
| 989 | |
| 990 | // Check for exception breaks first: |
| 991 | // en[able] exc[eptions] [all|unc[aught]] |
| 992 | // en[able] [all|unc[aught]] exc[eptions] |
| 993 | // dis[able] exc[eptions] [all|unc[aught]] |
| 994 | // dis[able] [all|unc[aught]] exc[eptions] |
| 995 | if ((command == 'enable' || command == 'disable') && |
| 996 | args && args.length > 1) { |
| 997 | var nextPos = args.indexOf(' '); |
| 998 | var arg1 = (nextPos > 0) ? args.substring(0, nextPos) : args; |
| 999 | var excType = null; |
| 1000 | |
| 1001 | // Check for: |
| 1002 | // en[able] exc[eptions] [all|unc[aught]] |
| 1003 | // dis[able] exc[eptions] [all|unc[aught]] |
| 1004 | if (arg1 == 'exc' || arg1 == 'exception' || arg1 == 'exceptions') { |
| 1005 | |
| 1006 | var arg2 = (nextPos > 0) ? |
| 1007 | args.substring(nextPos + 1, args.length) : 'all'; |
| 1008 | if (!arg2) { |
| 1009 | arg2 = 'all'; // if unspecified, set for all. |
| 1010 | } if (arg2 == 'unc') { // check for short cut. |
| 1011 | arg2 = 'uncaught'; |
| 1012 | } |
| 1013 | excType = arg2; |
| 1014 | |
| 1015 | // Check for: |
| 1016 | // en[able] [all|unc[aught]] exc[eptions] |
| 1017 | // dis[able] [all|unc[aught]] exc[eptions] |
| 1018 | } else if (arg1 == 'all' || arg1 == 'unc' || arg1 == 'uncaught') { |
| 1019 | |
| 1020 | var arg2 = (nextPos > 0) ? |
| 1021 | args.substring(nextPos + 1, args.length) : null; |
| 1022 | if (arg2 == 'exc' || arg1 == 'exception' || arg1 == 'exceptions') { |
| 1023 | excType = arg1; |
| 1024 | if (excType == 'unc') { |
| 1025 | excType = 'uncaught'; |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | // If we matched one of the command formats, then excType will be non-null: |
| 1031 | if (excType) { |
| 1032 | // Build a evaluate request from the text command. |
| 1033 | request = this.createRequest('setexceptionbreak'); |
| 1034 | |
| 1035 | request.arguments = {}; |
| 1036 | request.arguments.type = excType; |
| 1037 | request.arguments.enabled = (command == 'enable'); |
| 1038 | |
| 1039 | return request.toJSONProtocol(); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | // Build a evaluate request from the text command. |
| 1044 | request = this.createRequest('changebreakpoint'); |
| 1045 | |
| 1046 | // Process arguments if any. |
| 1047 | if (args && args.length > 0) { |
| 1048 | request.arguments = {}; |
| 1049 | var pos = args.indexOf(' '); |
| 1050 | var breakpointArg = args; |
| 1051 | var otherArgs; |
| 1052 | if (pos > 0) { |
| 1053 | breakpointArg = args.substring(0, pos); |
| 1054 | otherArgs = args.substring(pos + 1, args.length); |
| 1055 | } |
| 1056 | |
| 1057 | request.arguments.breakpoint = parseInt(breakpointArg); |
| 1058 | |
| 1059 | switch(command) { |
| 1060 | case 'cond': |
| 1061 | request.arguments.condition = otherArgs ? otherArgs : null; |
| 1062 | break; |
| 1063 | case 'enable': |
| 1064 | request.arguments.enabled = true; |
| 1065 | break; |
| 1066 | case 'disable': |
| 1067 | request.arguments.enabled = false; |
| 1068 | break; |
| 1069 | case 'ignore': |
| 1070 | request.arguments.ignoreCount = parseInt(otherArgs); |
| 1071 | break; |
| 1072 | default: |
| 1073 | throw new Error('Invalid arguments.'); |
| 1074 | } |
| 1075 | } else { |
| 1076 | throw new Error('Invalid arguments.'); |
| 1077 | } |
| 1078 | |
| 1079 | return request.toJSONProtocol(); |
| 1080 | }; |
| 1081 | |
| 1082 | |
| 1083 | // Create a JSON request for the disconnect command. |
| 1084 | DebugRequest.prototype.disconnectCommandToJSONRequest_ = function(args) { |
| 1085 | var request; |
| 1086 | request = this.createRequest('disconnect'); |
| 1087 | return request.toJSONProtocol(); |
| 1088 | }; |
| 1089 | |
| 1090 | |
| 1091 | // Create a JSON request for the info command. |
| 1092 | DebugRequest.prototype.infoCommandToJSONRequest_ = function(args) { |
| 1093 | var request; |
| 1094 | if (args && (args == 'break' || args == 'br')) { |
| 1095 | // Build a evaluate request from the text command. |
| 1096 | request = this.createRequest('listbreakpoints'); |
| 1097 | last_cmd = 'info break'; |
| 1098 | } else if (args && (args == 'locals' || args == 'lo')) { |
| 1099 | // Build a evaluate request from the text command. |
| 1100 | request = this.createRequest('frame'); |
| 1101 | last_cmd = 'info locals'; |
| 1102 | } else if (args && (args == 'args' || args == 'ar')) { |
| 1103 | // Build a evaluate request from the text command. |
| 1104 | request = this.createRequest('frame'); |
| 1105 | last_cmd = 'info args'; |
| 1106 | } else { |
| 1107 | throw new Error('Invalid info arguments.'); |
| 1108 | } |
| 1109 | |
| 1110 | return request.toJSONProtocol(); |
| 1111 | }; |
| 1112 | |
| 1113 | |
| 1114 | DebugRequest.prototype.v8FlagsToJSONRequest_ = function(args) { |
| 1115 | var request; |
| 1116 | request = this.createRequest('v8flags'); |
| 1117 | request.arguments = {}; |
| 1118 | request.arguments.flags = args; |
| 1119 | return request.toJSONProtocol(); |
| 1120 | }; |
| 1121 | |
| 1122 | |
| 1123 | DebugRequest.prototype.gcToJSONRequest_ = function(args) { |
| 1124 | var request; |
| 1125 | if (!args) { |
| 1126 | args = 'all'; |
| 1127 | } |
| 1128 | var args = args.split(/\s+/g); |
| 1129 | var cmd = args[0]; |
| 1130 | |
| 1131 | switch(cmd) { |
| 1132 | case 'all': |
| 1133 | case 'quick': |
| 1134 | case 'full': |
| 1135 | case 'young': |
| 1136 | case 'old': |
| 1137 | case 'compact': |
| 1138 | case 'sweep': |
| 1139 | case 'scavenge': { |
| 1140 | if (cmd == 'young') { cmd = 'quick'; } |
| 1141 | else if (cmd == 'old') { cmd = 'full'; } |
| 1142 | |
| 1143 | request = this.createRequest('gc'); |
| 1144 | request.arguments = {}; |
| 1145 | request.arguments.type = cmd; |
| 1146 | break; |
| 1147 | } |
| 1148 | // Else fall thru to the default case below to report the error. |
| 1149 | default: |
| 1150 | throw new Error('Missing arguments after ' + cmd + '.'); |
| 1151 | } |
| 1152 | return request.toJSONProtocol(); |
| 1153 | }; |
| 1154 | |
| 1155 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1156 | // Create a JSON request for the threads command. |
| 1157 | DebugRequest.prototype.threadsCommandToJSONRequest_ = function(args) { |
| 1158 | // Build a threads request from the text command. |
| 1159 | var request = this.createRequest('threads'); |
| 1160 | return request.toJSONProtocol(); |
| 1161 | }; |
| 1162 | |
| 1163 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1164 | // Handle the trace command. |
| 1165 | DebugRequest.prototype.traceCommand_ = function(args) { |
| 1166 | // Process arguments. |
| 1167 | if (args && args.length > 0) { |
| 1168 | if (args == 'compile') { |
| 1169 | trace_compile = !trace_compile; |
| 1170 | print('Tracing of compiled scripts ' + (trace_compile ? 'on' : 'off')); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1171 | } else if (args === 'debug json' || args === 'json' || args === 'packets') { |
| 1172 | trace_debug_json = !trace_debug_json; |
| 1173 | print('Tracing of debug json packets ' + |
| 1174 | (trace_debug_json ? 'on' : 'off')); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1175 | } else { |
| 1176 | throw new Error('Invalid trace arguments.'); |
| 1177 | } |
| 1178 | } else { |
| 1179 | throw new Error('Invalid trace arguments.'); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | // Handle the help command. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1184 | DebugRequest.prototype.helpCommand_ = function(args) { |
| 1185 | // Help os quite simple. |
| 1186 | if (args && args.length > 0) { |
| 1187 | print('warning: arguments to \'help\' are ignored'); |
| 1188 | } |
| 1189 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1190 | print('Note: <> denotes symbollic values to be replaced with real values.'); |
| 1191 | print('Note: [] denotes optional parts of commands, or optional options / arguments.'); |
| 1192 | print(' e.g. d[elete] - you get the same command if you type d or delete.'); |
| 1193 | print(''); |
| 1194 | print('[break] - break as soon as possible'); |
| 1195 | print('b[reak] location [condition]'); |
| 1196 | print(' - break on named function: location is a function name'); |
| 1197 | print(' - break on function: location is #<id>#'); |
| 1198 | print(' - break on script position: location is name:line[:column]'); |
| 1199 | print(''); |
| 1200 | print('clear <breakpoint #> - deletes the specified user defined breakpoint'); |
| 1201 | print('d[elete] <breakpoint #> - deletes the specified user defined breakpoint'); |
| 1202 | print('dis[able] <breakpoint #> - disables the specified user defined breakpoint'); |
| 1203 | print('dis[able] exc[eptions] [[all] | unc[aught]]'); |
| 1204 | print(' - disables breaking on exceptions'); |
| 1205 | print('en[able] <breakpoint #> - enables the specified user defined breakpoint'); |
| 1206 | print('en[able] exc[eptions] [[all] | unc[aught]]'); |
| 1207 | print(' - enables breaking on exceptions'); |
| 1208 | print(''); |
| 1209 | print('b[ack]t[race] [n] | [-n] | [from to]'); |
| 1210 | print(' - prints the stack back trace'); |
| 1211 | print('f[rame] - prints info about the current frame context'); |
| 1212 | print('f[rame] <frame #> - set context to specified frame #'); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1213 | print('scopes'); |
| 1214 | print('scope <scope #>'); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1215 | print(''); |
| 1216 | print('up - set context to caller of current frame'); |
| 1217 | print('do[wn] - set context to callee of current frame'); |
| 1218 | print('inf[o] br[eak] - prints info about breakpoints in use'); |
| 1219 | print('inf[o] ar[gs] - prints info about arguments of the current function'); |
| 1220 | print('inf[o] lo[cals] - prints info about locals in the current function'); |
| 1221 | print('inf[o] liveobjectlist|lol - same as \'lol info\''); |
| 1222 | print(''); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1223 | print('step [in | next | out| min [step count]]'); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1224 | print('c[ontinue] - continue executing after a breakpoint'); |
| 1225 | print('s[tep] [<N>] - step into the next N callees (default N is 1)'); |
| 1226 | print('s[tep]i [<N>] - step into the next N callees (default N is 1)'); |
| 1227 | print('n[ext] [<N>] - step over the next N callees (default N is 1)'); |
| 1228 | print('fin[ish] [<N>] - step out of N frames (default N is 1)'); |
| 1229 | print(''); |
| 1230 | print('p[rint] <expression> - prints the result of the specified expression'); |
| 1231 | print('dir <expression> - prints the object structure of the result'); |
| 1232 | print('set <var> = <expression> - executes the specified statement'); |
| 1233 | print(''); |
| 1234 | print('l[ist] - list the source code around for the current pc'); |
| 1235 | print('l[ist] [- | <start>,<end>] - list the specified range of source code'); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1236 | print('source [from line [num lines]]'); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1237 | print('scr[ipts] [native|extensions|all]'); |
| 1238 | print('scr[ipts] [<filter text>] - list scripts with the specified text in its description'); |
| 1239 | print(''); |
| 1240 | print('gc - runs the garbage collector'); |
| 1241 | print(''); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1242 | print('trace compile'); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1243 | // hidden command: trace debug json - toggles tracing of debug json packets |
| 1244 | print(''); |
| 1245 | print('disconnect|exit|quit - disconnects and quits the debugger'); |
| 1246 | print('help - prints this help information'); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1250 | function formatHandleReference_(value) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1251 | if (value.handle() >= 0) { |
| 1252 | return '#' + value.handle() + '#'; |
| 1253 | } else { |
| 1254 | return '#Transient#'; |
| 1255 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1259 | function formatObject_(value, include_properties) { |
| 1260 | var result = ''; |
| 1261 | result += formatHandleReference_(value); |
| 1262 | result += ', type: object' |
| 1263 | result += ', constructor '; |
| 1264 | var ctor = value.constructorFunctionValue(); |
| 1265 | result += formatHandleReference_(ctor); |
| 1266 | result += ', __proto__ '; |
| 1267 | var proto = value.protoObjectValue(); |
| 1268 | result += formatHandleReference_(proto); |
| 1269 | result += ', '; |
| 1270 | result += value.propertyCount(); |
| 1271 | result += ' properties.'; |
| 1272 | if (include_properties) { |
| 1273 | result += '\n'; |
| 1274 | for (var i = 0; i < value.propertyCount(); i++) { |
| 1275 | result += ' '; |
| 1276 | result += value.propertyName(i); |
| 1277 | result += ': '; |
| 1278 | var property_value = value.propertyValue(i); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1279 | if (property_value instanceof ProtocolReference) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1280 | result += '<no type>'; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1281 | } else { |
| 1282 | if (property_value && property_value.type()) { |
| 1283 | result += property_value.type(); |
| 1284 | } else { |
| 1285 | result += '<no type>'; |
| 1286 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1287 | } |
| 1288 | result += ' '; |
| 1289 | result += formatHandleReference_(property_value); |
| 1290 | result += '\n'; |
| 1291 | } |
| 1292 | } |
| 1293 | return result; |
| 1294 | } |
| 1295 | |
| 1296 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1297 | function formatScope_(scope) { |
| 1298 | var result = ''; |
| 1299 | var index = scope.index; |
| 1300 | result += '#' + (index <= 9 ? '0' : '') + index; |
| 1301 | result += ' '; |
| 1302 | switch (scope.type) { |
| 1303 | case Debug.ScopeType.Global: |
| 1304 | result += 'Global, '; |
| 1305 | result += '#' + scope.object.ref + '#'; |
| 1306 | break; |
| 1307 | case Debug.ScopeType.Local: |
| 1308 | result += 'Local'; |
| 1309 | break; |
| 1310 | case Debug.ScopeType.With: |
| 1311 | result += 'With, '; |
| 1312 | result += '#' + scope.object.ref + '#'; |
| 1313 | break; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1314 | case Debug.ScopeType.Catch: |
| 1315 | result += 'Catch, '; |
| 1316 | result += '#' + scope.object.ref + '#'; |
| 1317 | break; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1318 | case Debug.ScopeType.Closure: |
| 1319 | result += 'Closure'; |
| 1320 | break; |
| 1321 | default: |
| 1322 | result += 'UNKNOWN'; |
| 1323 | } |
| 1324 | return result; |
| 1325 | } |
| 1326 | |
| 1327 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1328 | function refObjectToString_(protocolPackage, handle) { |
| 1329 | var value = protocolPackage.lookup(handle); |
| 1330 | var result = ''; |
| 1331 | if (value.isString()) { |
| 1332 | result = '"' + value.value() + '"'; |
| 1333 | } else if (value.isPrimitive()) { |
| 1334 | result = value.valueString(); |
| 1335 | } else if (value.isObject()) { |
| 1336 | result += formatObject_(value, true); |
| 1337 | } |
| 1338 | return result; |
| 1339 | } |
| 1340 | |
| 1341 | |
| 1342 | // Rounds number 'num' to 'length' decimal places. |
| 1343 | function roundNumber(num, length) { |
| 1344 | var factor = Math.pow(10, length); |
| 1345 | return Math.round(num * factor) / factor; |
| 1346 | } |
| 1347 | |
| 1348 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1349 | // Convert a JSON response to text for display in a text based debugger. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1350 | function DebugResponseDetails(response) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1351 | details = {text:'', running:false} |
| 1352 | |
| 1353 | try { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1354 | if (!response.success()) { |
| 1355 | details.text = response.message(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1356 | return details; |
| 1357 | } |
| 1358 | |
| 1359 | // Get the running state. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1360 | details.running = response.running(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1361 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1362 | var body = response.body(); |
| 1363 | var result = ''; |
| 1364 | switch (response.command()) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1365 | case 'suspend': |
| 1366 | details.text = 'stopped'; |
| 1367 | break; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1368 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1369 | case 'setbreakpoint': |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1370 | result = 'set breakpoint #'; |
| 1371 | result += body.breakpoint; |
| 1372 | details.text = result; |
| 1373 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1374 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1375 | case 'clearbreakpoint': |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1376 | result = 'cleared breakpoint #'; |
| 1377 | result += body.breakpoint; |
| 1378 | details.text = result; |
| 1379 | break; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1380 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1381 | case 'changebreakpoint': |
| 1382 | result = 'successfully changed breakpoint'; |
| 1383 | details.text = result; |
| 1384 | break; |
| 1385 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1386 | case 'listbreakpoints': |
| 1387 | result = 'breakpoints: (' + body.breakpoints.length + ')'; |
| 1388 | for (var i = 0; i < body.breakpoints.length; i++) { |
| 1389 | var breakpoint = body.breakpoints[i]; |
| 1390 | result += '\n id=' + breakpoint.number; |
| 1391 | result += ' type=' + breakpoint.type; |
| 1392 | if (breakpoint.script_id) { |
| 1393 | result += ' script_id=' + breakpoint.script_id; |
| 1394 | } |
| 1395 | if (breakpoint.script_name) { |
| 1396 | result += ' script_name=' + breakpoint.script_name; |
| 1397 | } |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1398 | result += ' line=' + (breakpoint.line + 1); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1399 | if (breakpoint.column != null) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1400 | result += ' column=' + (breakpoint.column + 1); |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1401 | } |
| 1402 | if (breakpoint.groupId) { |
| 1403 | result += ' groupId=' + breakpoint.groupId; |
| 1404 | } |
| 1405 | if (breakpoint.ignoreCount) { |
| 1406 | result += ' ignoreCount=' + breakpoint.ignoreCount; |
| 1407 | } |
| 1408 | if (breakpoint.active === false) { |
| 1409 | result += ' inactive'; |
| 1410 | } |
| 1411 | if (breakpoint.condition) { |
| 1412 | result += ' condition=' + breakpoint.condition; |
| 1413 | } |
| 1414 | result += ' hit_count=' + breakpoint.hit_count; |
| 1415 | } |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1416 | if (body.breakpoints.length === 0) { |
| 1417 | result = "No user defined breakpoints\n"; |
| 1418 | } else { |
| 1419 | result += '\n'; |
| 1420 | } |
| 1421 | if (body.breakOnExceptions) { |
| 1422 | result += '* breaking on ALL exceptions is enabled\n'; |
| 1423 | } else if (body.breakOnUncaughtExceptions) { |
| 1424 | result += '* breaking on UNCAUGHT exceptions is enabled\n'; |
| 1425 | } else { |
| 1426 | result += '* all exception breakpoints are disabled\n'; |
| 1427 | } |
| 1428 | details.text = result; |
| 1429 | break; |
| 1430 | |
| 1431 | case 'setexceptionbreak': |
| 1432 | result = 'Break on ' + body.type + ' exceptions: '; |
| 1433 | result += body.enabled ? 'enabled' : 'disabled'; |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1434 | details.text = result; |
| 1435 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1436 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1437 | case 'backtrace': |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1438 | if (body.totalFrames == 0) { |
| 1439 | result = '(empty stack)'; |
| 1440 | } else { |
| 1441 | var result = 'Frames #' + body.fromFrame + ' to #' + |
| 1442 | (body.toFrame - 1) + ' of ' + body.totalFrames + '\n'; |
| 1443 | for (i = 0; i < body.frames.length; i++) { |
| 1444 | if (i != 0) result += '\n'; |
| 1445 | result += body.frames[i].text; |
| 1446 | } |
| 1447 | } |
| 1448 | details.text = result; |
| 1449 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1450 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1451 | case 'frame': |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1452 | if (last_cmd === 'info locals') { |
| 1453 | var locals = body.locals; |
| 1454 | if (locals.length === 0) { |
| 1455 | result = 'No locals'; |
| 1456 | } else { |
| 1457 | for (var i = 0; i < locals.length; i++) { |
| 1458 | var local = locals[i]; |
| 1459 | result += local.name + ' = '; |
| 1460 | result += refObjectToString_(response, local.value.ref); |
| 1461 | result += '\n'; |
| 1462 | } |
| 1463 | } |
| 1464 | } else if (last_cmd === 'info args') { |
| 1465 | var args = body.arguments; |
| 1466 | if (args.length === 0) { |
| 1467 | result = 'No arguments'; |
| 1468 | } else { |
| 1469 | for (var i = 0; i < args.length; i++) { |
| 1470 | var arg = args[i]; |
| 1471 | result += arg.name + ' = '; |
| 1472 | result += refObjectToString_(response, arg.value.ref); |
| 1473 | result += '\n'; |
| 1474 | } |
| 1475 | } |
| 1476 | } else { |
| 1477 | result = SourceUnderline(body.sourceLineText, |
| 1478 | body.column); |
| 1479 | Debug.State.currentSourceLine = body.line; |
| 1480 | Debug.State.currentFrame = body.index; |
| 1481 | Debug.State.displaySourceStartLine = -1; |
| 1482 | Debug.State.displaySourceEndLine = -1; |
| 1483 | } |
| 1484 | details.text = result; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1485 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1486 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1487 | case 'scopes': |
| 1488 | if (body.totalScopes == 0) { |
| 1489 | result = '(no scopes)'; |
| 1490 | } else { |
| 1491 | result = 'Scopes #' + body.fromScope + ' to #' + |
| 1492 | (body.toScope - 1) + ' of ' + body.totalScopes + '\n'; |
| 1493 | for (i = 0; i < body.scopes.length; i++) { |
| 1494 | if (i != 0) { |
| 1495 | result += '\n'; |
| 1496 | } |
| 1497 | result += formatScope_(body.scopes[i]); |
| 1498 | } |
| 1499 | } |
| 1500 | details.text = result; |
| 1501 | break; |
| 1502 | |
| 1503 | case 'scope': |
| 1504 | result += formatScope_(body); |
| 1505 | result += '\n'; |
| 1506 | var scope_object_value = response.lookup(body.object.ref); |
| 1507 | result += formatObject_(scope_object_value, true); |
| 1508 | details.text = result; |
| 1509 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1510 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1511 | case 'evaluate': |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1512 | case 'lookup': |
| 1513 | if (last_cmd == 'p' || last_cmd == 'print') { |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1514 | result = body.text; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1515 | } else { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1516 | var value; |
| 1517 | if (lookup_handle) { |
| 1518 | value = response.bodyValue(lookup_handle); |
| 1519 | } else { |
| 1520 | value = response.bodyValue(); |
| 1521 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1522 | if (value.isObject()) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1523 | result += formatObject_(value, true); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1524 | } else { |
| 1525 | result += 'type: '; |
| 1526 | result += value.type(); |
| 1527 | if (!value.isUndefined() && !value.isNull()) { |
| 1528 | result += ', '; |
| 1529 | if (value.isString()) { |
| 1530 | result += '"'; |
| 1531 | } |
| 1532 | result += value.value(); |
| 1533 | if (value.isString()) { |
| 1534 | result += '"'; |
| 1535 | } |
| 1536 | } |
| 1537 | result += '\n'; |
| 1538 | } |
| 1539 | } |
| 1540 | details.text = result; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1541 | break; |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1542 | |
| 1543 | case 'references': |
| 1544 | var count = body.length; |
| 1545 | result += 'found ' + count + ' objects'; |
| 1546 | result += '\n'; |
| 1547 | for (var i = 0; i < count; i++) { |
| 1548 | var value = response.bodyValue(i); |
| 1549 | result += formatObject_(value, false); |
| 1550 | result += '\n'; |
| 1551 | } |
| 1552 | details.text = result; |
| 1553 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1554 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1555 | case 'source': |
| 1556 | // Get the source from the response. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1557 | var source = body.source; |
| 1558 | var from_line = body.fromLine + 1; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1559 | var lines = source.split('\n'); |
| 1560 | var maxdigits = 1 + Math.floor(log10(from_line + lines.length)); |
| 1561 | if (maxdigits < 3) { |
| 1562 | maxdigits = 3; |
| 1563 | } |
| 1564 | var result = ''; |
| 1565 | for (var num = 0; num < lines.length; num++) { |
| 1566 | // Check if there's an extra newline at the end. |
| 1567 | if (num == (lines.length - 1) && lines[num].length == 0) { |
| 1568 | break; |
| 1569 | } |
| 1570 | |
| 1571 | var current_line = from_line + num; |
| 1572 | spacer = maxdigits - (1 + Math.floor(log10(current_line))); |
| 1573 | if (current_line == Debug.State.currentSourceLine + 1) { |
| 1574 | for (var i = 0; i < maxdigits; i++) { |
| 1575 | result += '>'; |
| 1576 | } |
| 1577 | result += ' '; |
| 1578 | } else { |
| 1579 | for (var i = 0; i < spacer; i++) { |
| 1580 | result += ' '; |
| 1581 | } |
| 1582 | result += current_line + ': '; |
| 1583 | } |
| 1584 | result += lines[num]; |
| 1585 | result += '\n'; |
| 1586 | } |
| 1587 | details.text = result; |
| 1588 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1589 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1590 | case 'scripts': |
| 1591 | var result = ''; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1592 | for (i = 0; i < body.length; i++) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1593 | if (i != 0) result += '\n'; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1594 | if (body[i].id) { |
| 1595 | result += body[i].id; |
| 1596 | } else { |
| 1597 | result += '[no id]'; |
| 1598 | } |
| 1599 | result += ', '; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1600 | if (body[i].name) { |
| 1601 | result += body[i].name; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1602 | } else { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1603 | if (body[i].compilationType == Debug.ScriptCompilationType.Eval |
| 1604 | && body[i].evalFromScript |
| 1605 | ) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1606 | result += 'eval from '; |
| 1607 | var script_value = response.lookup(body[i].evalFromScript.ref); |
| 1608 | result += ' ' + script_value.field('name'); |
| 1609 | result += ':' + (body[i].evalFromLocation.line + 1); |
| 1610 | result += ':' + body[i].evalFromLocation.column; |
| 1611 | } else if (body[i].compilationType == |
| 1612 | Debug.ScriptCompilationType.JSON) { |
| 1613 | result += 'JSON '; |
| 1614 | } else { // body[i].compilation == Debug.ScriptCompilationType.Host |
| 1615 | result += '[unnamed] '; |
| 1616 | } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1617 | } |
| 1618 | result += ' (lines: '; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1619 | result += body[i].lineCount; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1620 | result += ', length: '; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1621 | result += body[i].sourceLength; |
| 1622 | if (body[i].type == Debug.ScriptType.Native) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1623 | result += ', native'; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1624 | } else if (body[i].type == Debug.ScriptType.Extension) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1625 | result += ', extension'; |
| 1626 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1627 | result += '), ['; |
| 1628 | var sourceStart = body[i].sourceStart; |
| 1629 | if (sourceStart.length > 40) { |
| 1630 | sourceStart = sourceStart.substring(0, 37) + '...'; |
| 1631 | } |
| 1632 | result += sourceStart; |
| 1633 | result += ']'; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1634 | } |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1635 | if (body.length == 0) { |
| 1636 | result = "no matching scripts found"; |
| 1637 | } |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1638 | details.text = result; |
| 1639 | break; |
| 1640 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1641 | case 'threads': |
| 1642 | var result = 'Active V8 threads: ' + body.totalThreads + '\n'; |
| 1643 | body.threads.sort(function(a, b) { return a.id - b.id; }); |
| 1644 | for (i = 0; i < body.threads.length; i++) { |
| 1645 | result += body.threads[i].current ? '*' : ' '; |
| 1646 | result += ' '; |
| 1647 | result += body.threads[i].id; |
| 1648 | result += '\n'; |
| 1649 | } |
| 1650 | details.text = result; |
| 1651 | break; |
| 1652 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1653 | case 'continue': |
| 1654 | details.text = "(running)"; |
| 1655 | break; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1656 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1657 | case 'v8flags': |
| 1658 | details.text = "flags set"; |
| 1659 | break; |
| 1660 | |
| 1661 | case 'gc': |
| 1662 | details.text = "GC " + body.before + " => " + body.after; |
| 1663 | if (body.after > (1024*1024)) { |
| 1664 | details.text += |
| 1665 | " (" + roundNumber(body.before/(1024*1024), 1) + "M => " + |
| 1666 | roundNumber(body.after/(1024*1024), 1) + "M)"; |
| 1667 | } else if (body.after > 1024) { |
| 1668 | details.text += |
| 1669 | " (" + roundNumber(body.before/1024, 1) + "K => " + |
| 1670 | roundNumber(body.after/1024, 1) + "K)"; |
| 1671 | } |
| 1672 | break; |
| 1673 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1674 | default: |
| 1675 | details.text = |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1676 | 'Response for unknown command \'' + response.command() + '\'' + |
| 1677 | ' (' + response.raw_json() + ')'; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1678 | } |
| 1679 | } catch (e) { |
| 1680 | details.text = 'Error: "' + e + '" formatting response'; |
| 1681 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1682 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1683 | return details; |
| 1684 | }; |
| 1685 | |
| 1686 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1687 | /** |
| 1688 | * Protocol packages send from the debugger. |
| 1689 | * @param {string} json - raw protocol packet as JSON string. |
| 1690 | * @constructor |
| 1691 | */ |
| 1692 | function ProtocolPackage(json) { |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1693 | this.raw_json_ = json; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1694 | this.packet_ = JSON.parse(json); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1695 | this.refs_ = []; |
| 1696 | if (this.packet_.refs) { |
| 1697 | for (var i = 0; i < this.packet_.refs.length; i++) { |
| 1698 | this.refs_[this.packet_.refs[i].handle] = this.packet_.refs[i]; |
| 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | |
| 1704 | /** |
| 1705 | * Get the packet type. |
| 1706 | * @return {String} the packet type |
| 1707 | */ |
| 1708 | ProtocolPackage.prototype.type = function() { |
| 1709 | return this.packet_.type; |
| 1710 | } |
| 1711 | |
| 1712 | |
| 1713 | /** |
| 1714 | * Get the packet event. |
| 1715 | * @return {Object} the packet event |
| 1716 | */ |
| 1717 | ProtocolPackage.prototype.event = function() { |
| 1718 | return this.packet_.event; |
| 1719 | } |
| 1720 | |
| 1721 | |
| 1722 | /** |
| 1723 | * Get the packet request sequence. |
| 1724 | * @return {number} the packet request sequence |
| 1725 | */ |
| 1726 | ProtocolPackage.prototype.requestSeq = function() { |
| 1727 | return this.packet_.request_seq; |
| 1728 | } |
| 1729 | |
| 1730 | |
| 1731 | /** |
| 1732 | * Get the packet request sequence. |
| 1733 | * @return {number} the packet request sequence |
| 1734 | */ |
| 1735 | ProtocolPackage.prototype.running = function() { |
| 1736 | return this.packet_.running ? true : false; |
| 1737 | } |
| 1738 | |
| 1739 | |
| 1740 | ProtocolPackage.prototype.success = function() { |
| 1741 | return this.packet_.success ? true : false; |
| 1742 | } |
| 1743 | |
| 1744 | |
| 1745 | ProtocolPackage.prototype.message = function() { |
| 1746 | return this.packet_.message; |
| 1747 | } |
| 1748 | |
| 1749 | |
| 1750 | ProtocolPackage.prototype.command = function() { |
| 1751 | return this.packet_.command; |
| 1752 | } |
| 1753 | |
| 1754 | |
| 1755 | ProtocolPackage.prototype.body = function() { |
| 1756 | return this.packet_.body; |
| 1757 | } |
| 1758 | |
| 1759 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1760 | ProtocolPackage.prototype.bodyValue = function(index) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1761 | if (index != null) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1762 | return new ProtocolValue(this.packet_.body[index], this); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 1763 | } else { |
| 1764 | return new ProtocolValue(this.packet_.body, this); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 1765 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | |
| 1769 | ProtocolPackage.prototype.body = function() { |
| 1770 | return this.packet_.body; |
| 1771 | } |
| 1772 | |
| 1773 | |
| 1774 | ProtocolPackage.prototype.lookup = function(handle) { |
| 1775 | var value = this.refs_[handle]; |
| 1776 | if (value) { |
| 1777 | return new ProtocolValue(value, this); |
| 1778 | } else { |
| 1779 | return new ProtocolReference(handle); |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 1784 | ProtocolPackage.prototype.raw_json = function() { |
| 1785 | return this.raw_json_; |
| 1786 | } |
| 1787 | |
| 1788 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1789 | function ProtocolValue(value, packet) { |
| 1790 | this.value_ = value; |
| 1791 | this.packet_ = packet; |
| 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | /** |
| 1796 | * Get the value type. |
| 1797 | * @return {String} the value type |
| 1798 | */ |
| 1799 | ProtocolValue.prototype.type = function() { |
| 1800 | return this.value_.type; |
| 1801 | } |
| 1802 | |
| 1803 | |
| 1804 | /** |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1805 | * Get a metadata field from a protocol value. |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 1806 | * @return {Object} the metadata field value |
| 1807 | */ |
| 1808 | ProtocolValue.prototype.field = function(name) { |
| 1809 | return this.value_[name]; |
| 1810 | } |
| 1811 | |
| 1812 | |
| 1813 | /** |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1814 | * Check is the value is a primitive value. |
| 1815 | * @return {boolean} true if the value is primitive |
| 1816 | */ |
| 1817 | ProtocolValue.prototype.isPrimitive = function() { |
| 1818 | return this.isUndefined() || this.isNull() || this.isBoolean() || |
| 1819 | this.isNumber() || this.isString(); |
| 1820 | } |
| 1821 | |
| 1822 | |
| 1823 | /** |
| 1824 | * Get the object handle. |
| 1825 | * @return {number} the value handle |
| 1826 | */ |
| 1827 | ProtocolValue.prototype.handle = function() { |
| 1828 | return this.value_.handle; |
| 1829 | } |
| 1830 | |
| 1831 | |
| 1832 | /** |
| 1833 | * Check is the value is undefined. |
| 1834 | * @return {boolean} true if the value is undefined |
| 1835 | */ |
| 1836 | ProtocolValue.prototype.isUndefined = function() { |
| 1837 | return this.value_.type == 'undefined'; |
| 1838 | } |
| 1839 | |
| 1840 | |
| 1841 | /** |
| 1842 | * Check is the value is null. |
| 1843 | * @return {boolean} true if the value is null |
| 1844 | */ |
| 1845 | ProtocolValue.prototype.isNull = function() { |
| 1846 | return this.value_.type == 'null'; |
| 1847 | } |
| 1848 | |
| 1849 | |
| 1850 | /** |
| 1851 | * Check is the value is a boolean. |
| 1852 | * @return {boolean} true if the value is a boolean |
| 1853 | */ |
| 1854 | ProtocolValue.prototype.isBoolean = function() { |
| 1855 | return this.value_.type == 'boolean'; |
| 1856 | } |
| 1857 | |
| 1858 | |
| 1859 | /** |
| 1860 | * Check is the value is a number. |
| 1861 | * @return {boolean} true if the value is a number |
| 1862 | */ |
| 1863 | ProtocolValue.prototype.isNumber = function() { |
| 1864 | return this.value_.type == 'number'; |
| 1865 | } |
| 1866 | |
| 1867 | |
| 1868 | /** |
| 1869 | * Check is the value is a string. |
| 1870 | * @return {boolean} true if the value is a string |
| 1871 | */ |
| 1872 | ProtocolValue.prototype.isString = function() { |
| 1873 | return this.value_.type == 'string'; |
| 1874 | } |
| 1875 | |
| 1876 | |
| 1877 | /** |
| 1878 | * Check is the value is an object. |
| 1879 | * @return {boolean} true if the value is an object |
| 1880 | */ |
| 1881 | ProtocolValue.prototype.isObject = function() { |
| 1882 | return this.value_.type == 'object' || this.value_.type == 'function' || |
| 1883 | this.value_.type == 'error' || this.value_.type == 'regexp'; |
| 1884 | } |
| 1885 | |
| 1886 | |
| 1887 | /** |
| 1888 | * Get the constructor function |
| 1889 | * @return {ProtocolValue} constructor function |
| 1890 | */ |
| 1891 | ProtocolValue.prototype.constructorFunctionValue = function() { |
| 1892 | var ctor = this.value_.constructorFunction; |
| 1893 | return this.packet_.lookup(ctor.ref); |
| 1894 | } |
| 1895 | |
| 1896 | |
| 1897 | /** |
| 1898 | * Get the __proto__ value |
| 1899 | * @return {ProtocolValue} __proto__ value |
| 1900 | */ |
| 1901 | ProtocolValue.prototype.protoObjectValue = function() { |
| 1902 | var proto = this.value_.protoObject; |
| 1903 | return this.packet_.lookup(proto.ref); |
| 1904 | } |
| 1905 | |
| 1906 | |
| 1907 | /** |
| 1908 | * Get the number og properties. |
| 1909 | * @return {number} the number of properties |
| 1910 | */ |
| 1911 | ProtocolValue.prototype.propertyCount = function() { |
| 1912 | return this.value_.properties ? this.value_.properties.length : 0; |
| 1913 | } |
| 1914 | |
| 1915 | |
| 1916 | /** |
| 1917 | * Get the specified property name. |
| 1918 | * @return {string} property name |
| 1919 | */ |
| 1920 | ProtocolValue.prototype.propertyName = function(index) { |
| 1921 | var property = this.value_.properties[index]; |
| 1922 | return property.name; |
| 1923 | } |
| 1924 | |
| 1925 | |
| 1926 | /** |
| 1927 | * Return index for the property name. |
| 1928 | * @param name The property name to look for |
| 1929 | * @return {number} index for the property name |
| 1930 | */ |
| 1931 | ProtocolValue.prototype.propertyIndex = function(name) { |
| 1932 | for (var i = 0; i < this.propertyCount(); i++) { |
| 1933 | if (this.value_.properties[i].name == name) { |
| 1934 | return i; |
| 1935 | } |
| 1936 | } |
| 1937 | return null; |
| 1938 | } |
| 1939 | |
| 1940 | |
| 1941 | /** |
| 1942 | * Get the specified property value. |
| 1943 | * @return {ProtocolValue} property value |
| 1944 | */ |
| 1945 | ProtocolValue.prototype.propertyValue = function(index) { |
| 1946 | var property = this.value_.properties[index]; |
| 1947 | return this.packet_.lookup(property.ref); |
| 1948 | } |
| 1949 | |
| 1950 | |
| 1951 | /** |
| 1952 | * Check is the value is a string. |
| 1953 | * @return {boolean} true if the value is a string |
| 1954 | */ |
| 1955 | ProtocolValue.prototype.value = function() { |
| 1956 | return this.value_.value; |
| 1957 | } |
| 1958 | |
| 1959 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1960 | ProtocolValue.prototype.valueString = function() { |
| 1961 | return this.value_.text; |
| 1962 | } |
| 1963 | |
| 1964 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1965 | function ProtocolReference(handle) { |
| 1966 | this.handle_ = handle; |
| 1967 | } |
| 1968 | |
| 1969 | |
| 1970 | ProtocolReference.prototype.handle = function() { |
| 1971 | return this.handle_; |
| 1972 | } |
| 1973 | |
| 1974 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1975 | function MakeJSONPair_(name, value) { |
| 1976 | return '"' + name + '":' + value; |
| 1977 | } |
| 1978 | |
| 1979 | |
| 1980 | function ArrayToJSONObject_(content) { |
| 1981 | return '{' + content.join(',') + '}'; |
| 1982 | } |
| 1983 | |
| 1984 | |
| 1985 | function ArrayToJSONArray_(content) { |
| 1986 | return '[' + content.join(',') + ']'; |
| 1987 | } |
| 1988 | |
| 1989 | |
| 1990 | function BooleanToJSON_(value) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1991 | return String(value); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | |
| 1995 | function NumberToJSON_(value) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1996 | return String(value); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | |
| 2000 | // Mapping of some control characters to avoid the \uXXXX syntax for most |
| 2001 | // commonly used control cahracters. |
| 2002 | const ctrlCharMap_ = { |
| 2003 | '\b': '\\b', |
| 2004 | '\t': '\\t', |
| 2005 | '\n': '\\n', |
| 2006 | '\f': '\\f', |
| 2007 | '\r': '\\r', |
| 2008 | '"' : '\\"', |
| 2009 | '\\': '\\\\' |
| 2010 | }; |
| 2011 | |
| 2012 | |
| 2013 | // Regular expression testing for ", \ and control characters (0x00 - 0x1F). |
| 2014 | const ctrlCharTest_ = new RegExp('["\\\\\x00-\x1F]'); |
| 2015 | |
| 2016 | |
| 2017 | // Regular expression matching ", \ and control characters (0x00 - 0x1F) |
| 2018 | // globally. |
| 2019 | const ctrlCharMatch_ = new RegExp('["\\\\\x00-\x1F]', 'g'); |
| 2020 | |
| 2021 | |
| 2022 | /** |
| 2023 | * Convert a String to its JSON representation (see http://www.json.org/). To |
| 2024 | * avoid depending on the String object this method calls the functions in |
| 2025 | * string.js directly and not through the value. |
| 2026 | * @param {String} value The String value to format as JSON |
| 2027 | * @return {string} JSON formatted String value |
| 2028 | */ |
| 2029 | function StringToJSON_(value) { |
| 2030 | // Check for" , \ and control characters (0x00 - 0x1F). No need to call |
| 2031 | // RegExpTest as ctrlchar is constructed using RegExp. |
| 2032 | if (ctrlCharTest_.test(value)) { |
| 2033 | // Replace ", \ and control characters (0x00 - 0x1F). |
| 2034 | return '"' + |
| 2035 | value.replace(ctrlCharMatch_, function (char) { |
| 2036 | // Use charmap if possible. |
| 2037 | var mapped = ctrlCharMap_[char]; |
| 2038 | if (mapped) return mapped; |
| 2039 | mapped = char.charCodeAt(); |
| 2040 | // Convert control character to unicode escape sequence. |
| 2041 | return '\\u00' + |
| 2042 | '0' + // TODO %NumberToRadixString(Math.floor(mapped / 16), 16) + |
| 2043 | '0' // TODO %NumberToRadixString(mapped % 16, 16); |
| 2044 | }) |
| 2045 | + '"'; |
| 2046 | } |
| 2047 | |
| 2048 | // Simple string with no special characters. |
| 2049 | return '"' + value + '"'; |
| 2050 | } |
| 2051 | |
| 2052 | |
| 2053 | /** |
| 2054 | * Convert a Date to ISO 8601 format. To avoid depending on the Date object |
| 2055 | * this method calls the functions in date.js directly and not through the |
| 2056 | * value. |
| 2057 | * @param {Date} value The Date value to format as JSON |
| 2058 | * @return {string} JSON formatted Date value |
| 2059 | */ |
| 2060 | function DateToISO8601_(value) { |
| 2061 | function f(n) { |
| 2062 | return n < 10 ? '0' + n : n; |
| 2063 | } |
| 2064 | function g(n) { |
| 2065 | return n < 10 ? '00' + n : n < 100 ? '0' + n : n; |
| 2066 | } |
| 2067 | return builtins.GetUTCFullYearFrom(value) + '-' + |
| 2068 | f(builtins.GetUTCMonthFrom(value) + 1) + '-' + |
| 2069 | f(builtins.GetUTCDateFrom(value)) + 'T' + |
| 2070 | f(builtins.GetUTCHoursFrom(value)) + ':' + |
| 2071 | f(builtins.GetUTCMinutesFrom(value)) + ':' + |
| 2072 | f(builtins.GetUTCSecondsFrom(value)) + '.' + |
| 2073 | g(builtins.GetUTCMillisecondsFrom(value)) + 'Z'; |
| 2074 | } |
| 2075 | |
| 2076 | |
| 2077 | /** |
| 2078 | * Convert a Date to ISO 8601 format. To avoid depending on the Date object |
| 2079 | * this method calls the functions in date.js directly and not through the |
| 2080 | * value. |
| 2081 | * @param {Date} value The Date value to format as JSON |
| 2082 | * @return {string} JSON formatted Date value |
| 2083 | */ |
| 2084 | function DateToJSON_(value) { |
| 2085 | return '"' + DateToISO8601_(value) + '"'; |
| 2086 | } |
| 2087 | |
| 2088 | |
| 2089 | /** |
| 2090 | * Convert an Object to its JSON representation (see http://www.json.org/). |
| 2091 | * This implementation simply runs through all string property names and adds |
| 2092 | * each property to the JSON representation for some predefined types. For type |
| 2093 | * "object" the function calls itself recursively unless the object has the |
| 2094 | * function property "toJSONProtocol" in which case that is used. This is not |
| 2095 | * a general implementation but sufficient for the debugger. Note that circular |
| 2096 | * structures will cause infinite recursion. |
| 2097 | * @param {Object} object The object to format as JSON |
| 2098 | * @return {string} JSON formatted object value |
| 2099 | */ |
| 2100 | function SimpleObjectToJSON_(object) { |
| 2101 | var content = []; |
| 2102 | for (var key in object) { |
| 2103 | // Only consider string keys. |
| 2104 | if (typeof key == 'string') { |
| 2105 | var property_value = object[key]; |
| 2106 | |
| 2107 | // Format the value based on its type. |
| 2108 | var property_value_json; |
| 2109 | switch (typeof property_value) { |
| 2110 | case 'object': |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 2111 | if (property_value === null) { |
| 2112 | property_value_json = 'null'; |
| 2113 | } else if (typeof property_value.toJSONProtocol == 'function') { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2114 | property_value_json = property_value.toJSONProtocol(true) |
| 2115 | } else if (property_value.constructor.name == 'Array'){ |
| 2116 | property_value_json = SimpleArrayToJSON_(property_value); |
| 2117 | } else { |
| 2118 | property_value_json = SimpleObjectToJSON_(property_value); |
| 2119 | } |
| 2120 | break; |
| 2121 | |
| 2122 | case 'boolean': |
| 2123 | property_value_json = BooleanToJSON_(property_value); |
| 2124 | break; |
| 2125 | |
| 2126 | case 'number': |
| 2127 | property_value_json = NumberToJSON_(property_value); |
| 2128 | break; |
| 2129 | |
| 2130 | case 'string': |
| 2131 | property_value_json = StringToJSON_(property_value); |
| 2132 | break; |
| 2133 | |
| 2134 | default: |
| 2135 | property_value_json = null; |
| 2136 | } |
| 2137 | |
| 2138 | // Add the property if relevant. |
| 2139 | if (property_value_json) { |
| 2140 | content.push(StringToJSON_(key) + ':' + property_value_json); |
| 2141 | } |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | // Make JSON object representation. |
| 2146 | return '{' + content.join(',') + '}'; |
| 2147 | } |
| 2148 | |
| 2149 | |
| 2150 | /** |
| 2151 | * Convert an array to its JSON representation. This is a VERY simple |
| 2152 | * implementation just to support what is needed for the debugger. |
| 2153 | * @param {Array} arrya The array to format as JSON |
| 2154 | * @return {string} JSON formatted array value |
| 2155 | */ |
| 2156 | function SimpleArrayToJSON_(array) { |
| 2157 | // Make JSON array representation. |
| 2158 | var json = '['; |
| 2159 | for (var i = 0; i < array.length; i++) { |
| 2160 | if (i != 0) { |
| 2161 | json += ','; |
| 2162 | } |
| 2163 | var elem = array[i]; |
| 2164 | if (elem.toJSONProtocol) { |
| 2165 | json += elem.toJSONProtocol(true) |
| 2166 | } else if (typeof(elem) === 'object') { |
| 2167 | json += SimpleObjectToJSON_(elem); |
| 2168 | } else if (typeof(elem) === 'boolean') { |
| 2169 | json += BooleanToJSON_(elem); |
| 2170 | } else if (typeof(elem) === 'number') { |
| 2171 | json += NumberToJSON_(elem); |
| 2172 | } else if (typeof(elem) === 'string') { |
| 2173 | json += StringToJSON_(elem); |
| 2174 | } else { |
| 2175 | json += elem; |
| 2176 | } |
| 2177 | } |
| 2178 | json += ']'; |
| 2179 | return json; |
| 2180 | } |