blob: b0edb706ad7c00051318ef70d57f61d8e2300e4e [file] [log] [blame]
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001// 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.org5a8ca6c2008-10-23 13:57:19 +000028String.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.com37abdec2009-01-06 14:43:28 +000032}
33
34function log10(num) {
35 return Math.log(num)/Math.log(10);
36}
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000037
38function ToInspectableObject(obj) {
39 if (!obj && typeof obj === 'object') {
40 return void 0;
41 } else {
42 return Object(obj);
43 }
44}
45
46function 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.com37abdec2009-01-06 14:43:28 +000073
74
75// Global object holding debugger related constants and state.
76const Debug = {};
77
78
79// Debug events which can occour in the V8 JavaScript engine. These originate
iposva@chromium.org245aa852009-02-10 00:49:54 +000080// from the API include file v8-debug.h.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +000081Debug.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.
89Debug.ScriptType = { Native: 0,
90 Extension: 1,
91 Normal: 2 };
92
93
ager@chromium.orge2902be2009-06-08 12:21:35 +000094// The different types of script compilations matching enum
95// Script::CompilationType in objects.h.
96Debug.ScriptCompilationType = { Host: 0,
97 Eval: 1,
98 JSON: 2 };
99
100
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000101// The different types of scopes matching constants runtime.cc.
102Debug.ScopeType = { Global: 0,
103 Local: 1,
104 With: 2,
ager@chromium.orga1645e22009-09-09 19:27:10 +0000105 Closure: 3,
106 Catch: 4 };
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000107
108
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000109// Current debug state.
110const kNoFrame = -1;
111Debug.State = {
112 currentFrame: kNoFrame,
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000113 displaySourceStartLine: -1,
114 displaySourceEndLine: -1,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000115 currentSourceLine: -1
116}
iposva@chromium.org245aa852009-02-10 00:49:54 +0000117var trace_compile = false; // Tracing all compile events?
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000118var trace_debug_json = false; // Tracing all debug json packets?
119var last_cmd_line = '';
120var repeat_cmd_line = '';
121var is_running = true;
122
123// Copied from debug-delay.js. This is needed below:
124function ScriptTypeFlag(type) {
125 return (1 << type);
126}
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000127
128
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000129// 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.
132function DebugMessageDetails(message) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000133 if (trace_debug_json) {
134 print("received: '" + message + "'");
135 }
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000136 // Convert the JSON string to an object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000137 var response = new ProtocolPackage(message);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000138 is_running = response.running();
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000139
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000140 if (response.type() == 'event') {
141 return DebugEventDetails(response);
142 } else {
143 return DebugResponseDetails(response);
144 }
145}
146
147function DebugEventDetails(response) {
148 details = {text:'', running:false}
149
150 // Get the running state.
151 details.running = response.running();
152
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000153 var body = response.body();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000154 var result = '';
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000155 switch (response.event()) {
156 case 'break':
157 if (body.breakpoints) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000158 result += 'breakpoint';
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000159 if (body.breakpoints.length > 1) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000160 result += 's';
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000161 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000162 result += ' #';
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000163 for (var i = 0; i < body.breakpoints.length; i++) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000164 if (i > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000165 result += ', #';
iposva@chromium.org245aa852009-02-10 00:49:54 +0000166 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000167 result += body.breakpoints[i];
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000168 }
iposva@chromium.org245aa852009-02-10 00:49:54 +0000169 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000170 result += 'break';
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000171 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000172 result += ' in ';
173 result += body.invocationText;
174 result += ', ';
175 result += SourceInfo(body);
176 result += '\n';
177 result += SourceUnderline(body.sourceLineText, body.sourceColumn);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000178 Debug.State.currentSourceLine = body.sourceLine;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000179 Debug.State.displaySourceStartLine = -1;
180 Debug.State.displaySourceEndLine = -1;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000181 Debug.State.currentFrame = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000182 details.text = result;
183 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000184
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000185 case 'exception':
186 if (body.uncaught) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000187 result += 'Uncaught: ';
iposva@chromium.org245aa852009-02-10 00:49:54 +0000188 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000189 result += 'Exception: ';
iposva@chromium.org245aa852009-02-10 00:49:54 +0000190 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000191 result += '"';
192 result += body.exception.text;
193 result += '"';
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000194 if (body.sourceLine >= 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000195 result += ', ';
196 result += SourceInfo(body);
197 result += '\n';
198 result += SourceUnderline(body.sourceLineText, body.sourceColumn);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000199 Debug.State.currentSourceLine = body.sourceLine;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000200 Debug.State.displaySourceStartLine = -1;
201 Debug.State.displaySourceEndLine = -1;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000202 Debug.State.currentFrame = 0;
203 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000204 result += ' (empty stack)';
iposva@chromium.org245aa852009-02-10 00:49:54 +0000205 Debug.State.currentSourceLine = -1;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000206 Debug.State.displaySourceStartLine = -1;
207 Debug.State.displaySourceEndLine = -1;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000208 Debug.State.currentFrame = kNoFrame;
209 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000210 details.text = result;
211 break;
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000212
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000213 case 'afterCompile':
iposva@chromium.org245aa852009-02-10 00:49:54 +0000214 if (trace_compile) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000215 result = 'Source ' + body.script.name + ' compiled:\n'
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000216 var source = body.script.source;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000217 if (!(source[source.length - 1] == '\n')) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000218 result += source;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000219 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000220 result += source.substring(0, source.length - 1);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000221 }
iposva@chromium.org245aa852009-02-10 00:49:54 +0000222 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000223 details.text = result;
224 break;
225
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000226 case 'scriptCollected':
227 details.text = result;
228 break;
229
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000230 default:
231 details.text = 'Unknown debug event ' + response.event();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000232 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000233
234 return details;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000235};
236
237
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000238function SourceInfo(body) {
239 var result = '';
lrn@chromium.org25156de2010-04-06 13:10:27 +0000240
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000241 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.org25156de2010-04-06 13:10:27 +0000252
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000253 return result;
254}
255
256
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000257function 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.com37abdec2009-01-06 14:43:28 +0000280// Converts a text command to a JSON request.
281function DebugCommandToJSONRequest(cmd_line) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000282 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.com37abdec2009-01-06 14:43:28 +0000287};
288
289
290function 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.orgd2c22f02011-01-10 08:15:37 +0000298 // 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.com37abdec2009-01-06 14:43:28 +0000312 // 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.orgd2c22f02011-01-10 08:15:37 +0000327 if ((cmd === undefined) || !cmd) {
328 this.request_ = void 0;
329 return;
330 }
331
332 last_cmd = cmd;
333
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000334 // 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.orgd2c22f02011-01-10 08:15:37 +0000343 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.com37abdec2009-01-06 14:43:28 +0000359 break;
360
361 case 'backtrace':
362 case 'bt':
363 this.request_ = this.backtraceCommandToJSONRequest_(args);
364 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000365
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000366 case 'frame':
367 case 'f':
368 this.request_ = this.frameCommandToJSONRequest_(args);
369 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000370
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000371 case 'scopes':
372 this.request_ = this.scopesCommandToJSONRequest_(args);
373 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000374
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000375 case 'scope':
376 this.request_ = this.scopeCommandToJSONRequest_(args);
377 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000378
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000379 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.com37abdec2009-01-06 14:43:28 +0000399 case 'print':
400 case 'p':
401 this.request_ = this.printCommandToJSONRequest_(args);
402 break;
403
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000404 case 'dir':
405 this.request_ = this.dirCommandToJSONRequest_(args);
406 break;
407
iposva@chromium.org245aa852009-02-10 00:49:54 +0000408 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.orgd2c22f02011-01-10 08:15:37 +0000416 case 'list':
417 case 'l':
418 this.request_ = this.listCommandToJSONRequest_(args);
419 break;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000420 case 'source':
421 this.request_ = this.sourceCommandToJSONRequest_(args);
422 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000423
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000424 case 'scripts':
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000425 case 'script':
426 case 'scr':
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000427 this.request_ = this.scriptsCommandToJSONRequest_(args);
428 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000429
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000430 case 'break':
431 case 'b':
432 this.request_ = this.breakCommandToJSONRequest_(args);
433 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000434
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000435 case 'breakpoints':
436 case 'bb':
437 this.request_ = this.breakpointsCommandToJSONRequest_(args);
438 break;
439
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000440 case 'clear':
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000441 case 'delete':
442 case 'd':
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000443 this.request_ = this.clearCommandToJSONRequest_(args);
444 break;
445
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000446 case 'threads':
447 this.request_ = this.threadsCommandToJSONRequest_(args);
448 break;
449
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000450 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.org245aa852009-02-10 00:49:54 +0000484 case 'trace':
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000485 case 'tr':
iposva@chromium.org245aa852009-02-10 00:49:54 +0000486 // 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.com37abdec2009-01-06 14:43:28 +0000491 case 'help':
492 case '?':
493 this.helpCommand_(args);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000494 // Return undefined to indicate command handled internally (no JSON).
495 this.request_ = void 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000496 break;
497
498 default:
499 throw new Error('Unknown command "' + cmd + '"');
500 }
501}
502
503DebugRequest.prototype.JSONRequest = function() {
504 return this.request_;
505}
506
507
508function RequestPacket(command) {
509 this.seq = 0;
510 this.type = 'request';
511 this.command = command;
512}
513
514
515RequestPacket.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
537DebugRequest.prototype.createRequest = function(command) {
538 return new RequestPacket(command);
539};
540
541
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000542// Create a JSON request for the evaluation command.
543DebugRequest.prototype.makeEvaluateJSONRequest_ = function(expression) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000544 // Global varaible used to store whether a handle was requested.
545 lookup_handle = null;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000546 // 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.orgeadaf222009-06-16 09:43:10 +0000549 // Remember the handle requested in a global variable.
550 lookup_handle = parseInt(handle_match[1]);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000551 // Build a lookup request.
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000552 var request = this.createRequest('lookup');
553 request.arguments = {};
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000554 request.arguments.handles = [ lookup_handle ];
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000555 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.org381abbb2009-02-25 13:23:22 +0000561 // Request a global evaluation if there is no current frame.
562 if (Debug.State.currentFrame == kNoFrame) {
563 request.arguments.global = true;
564 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000565 return request.toJSONProtocol();
566 }
567};
568
569
iposva@chromium.org245aa852009-02-10 00:49:54 +0000570// Create a JSON request for the references/instances command.
571DebugRequest.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.orgddb913d2009-01-27 10:01:48 +0000585
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000586// Create a JSON request for the continue command.
587DebugRequest.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.orgd2c22f02011-01-10 08:15:37 +0000594DebugRequest.prototype.stepCommandToJSONRequest_ = function(args, type) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000595 // 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.orgd2c22f02011-01-10 08:15:37 +0000601
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.com37abdec2009-01-06 14:43:28 +0000604 if (args && args.length > 0) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000605 args = args.split(/\s+/g);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000606
607 if (args.length > 2) {
608 throw new Error('Invalid step arguments.');
609 }
610
611 if (args.length > 0) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000612 // 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.com37abdec2009-01-06 14:43:28 +0000624 }
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000625
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.com37abdec2009-01-06 14:43:28 +0000655 request.arguments.stepcount = stepcount;
656 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000657 }
658 } else {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000659 // Default is step of the specified type.
660 request.arguments.stepaction = type;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000661 }
662
663 return request.toJSONProtocol();
664};
665
666
667// Create a JSON request for the backtrace command.
668DebugRequest.prototype.backtraceCommandToJSONRequest_ = function(args) {
669 // Build a backtrace request from the text command.
670 var request = this.createRequest('backtrace');
lrn@chromium.org25156de2010-04-06 13:10:27 +0000671
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000672 // 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.com37abdec2009-01-06 14:43:28 +0000677 args = args.split(/\s*[ ]+\s*/g);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000678 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.com37abdec2009-01-06 14:43:28 +0000691 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.orgb3284ad2009-05-18 06:12:45 +0000703 // Show frame range.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000704 request.arguments.fromFrame = fromFrame;
705 request.arguments.toFrame = toFrame + 1;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000706 } else if (args.length > 2) {
707 throw new Error('Invalid backtrace arguments.');
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000708 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000709
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000710 return request.toJSONProtocol();
711};
712
713
714// Create a JSON request for the frame command.
715DebugRequest.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.orgeadaf222009-06-16 09:43:10 +0000727// Create a JSON request for the scopes command.
728DebugRequest.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.
736DebugRequest.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.com37abdec2009-01-06 14:43:28 +0000748// Create a JSON request for the print command.
749DebugRequest.prototype.printCommandToJSONRequest_ = function(args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000750 // Build an evaluate request from the text command.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000751 if (args.length == 0) {
752 throw new Error('Missing expression.');
753 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000754 return this.makeEvaluateJSONRequest_(args);
755};
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000756
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000757
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000758// Create a JSON request for the dir command.
759DebugRequest.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.com37abdec2009-01-06 14:43:28 +0000765};
766
767
iposva@chromium.org245aa852009-02-10 00:49:54 +0000768// Create a JSON request for the references command.
769DebugRequest.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.org25156de2010-04-06 13:10:27 +0000774
iposva@chromium.org245aa852009-02-10 00:49:54 +0000775 return this.makeReferencesJSONRequest_(args, 'referencedBy');
776};
777
778
779// Create a JSON request for the instances command.
780DebugRequest.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.org25156de2010-04-06 13:10:27 +0000785
iposva@chromium.org245aa852009-02-10 00:49:54 +0000786 // Build a references request.
787 return this.makeReferencesJSONRequest_(args, 'constructedBy');
788};
789
790
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000791// Create a JSON request for the list command.
792DebugRequest.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.com37abdec2009-01-06 14:43:28 +0000826// Create a JSON request for the source command.
827DebugRequest.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.
857DebugRequest.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.org25156de2010-04-06 13:10:27 +0000874
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000875 case 'extensions':
876 request.arguments.types = ScriptTypeFlag(Debug.ScriptType.Extension);
877 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000878
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000879 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.org25156de2010-04-06 13:10:27 +0000885
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000886 default:
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000887 // 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.com37abdec2009-01-06 14:43:28 +0000891 }
892 }
893
894 return request.toJSONProtocol();
895};
896
897
898// Create a JSON request for the break command.
899DebugRequest.prototype.breakCommandToJSONRequest_ = function(args) {
900 // Build a evaluate request from the text command.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000901 // Process arguments if any.
902 if (args && args.length > 0) {
903 var target = args;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000904 var type = 'function';
905 var line;
906 var column;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000907 var condition;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000908 var pos;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000909
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000910 var request = this.createRequest('setbreakpoint');
911
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000912 // Break the args into target spec and condition if appropriate.
913
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000914 // Check for breakpoint condition.
915 pos = args.indexOf(' ');
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000916 if (pos > 0) {
917 target = args.substring(0, pos);
918 condition = args.substring(pos + 1, args.length);
919 }
920
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000921 // 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.org25156de2010-04-06 13:10:27 +0000928
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000929 // 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.org25156de2010-04-06 13:10:27 +0000943
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000944 request.arguments = {};
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000945 request.arguments.type = type;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000946 request.arguments.target = target;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000947 request.arguments.line = line;
948 request.arguments.column = column;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000949 request.arguments.condition = condition;
950 } else {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000951 var request = this.createRequest('suspend');
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000952 }
953
954 return request.toJSONProtocol();
955};
956
957
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000958DebugRequest.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.com37abdec2009-01-06 14:43:28 +0000967// Create a JSON request for the clear command.
968DebugRequest.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.orgd2c22f02011-01-10 08:15:37 +0000984// Create a JSON request for the change breakpoint command.
985DebugRequest.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.
1084DebugRequest.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.
1092DebugRequest.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
1114DebugRequest.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
1123DebugRequest.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.orgbb29dc92009-03-24 13:25:23 +00001156// Create a JSON request for the threads command.
1157DebugRequest.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.org245aa852009-02-10 00:49:54 +00001164// Handle the trace command.
1165DebugRequest.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.orgd2c22f02011-01-10 08:15:37 +00001171 } 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.org245aa852009-02-10 00:49:54 +00001175 } 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.com37abdec2009-01-06 14:43:28 +00001184DebugRequest.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.orgd2c22f02011-01-10 08:15:37 +00001190 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.orgeadaf222009-06-16 09:43:10 +00001213 print('scopes');
1214 print('scope <scope #>');
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001215 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.com37abdec2009-01-06 14:43:28 +00001223 print('step [in | next | out| min [step count]]');
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001224 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.com37abdec2009-01-06 14:43:28 +00001236 print('source [from line [num lines]]');
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001237 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.org245aa852009-02-10 00:49:54 +00001242 print('trace compile');
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001243 // 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.com37abdec2009-01-06 14:43:28 +00001247}
1248
1249
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001250function formatHandleReference_(value) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001251 if (value.handle() >= 0) {
1252 return '#' + value.handle() + '#';
1253 } else {
1254 return '#Transient#';
1255 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001256}
1257
1258
iposva@chromium.org245aa852009-02-10 00:49:54 +00001259function 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.orgeadaf222009-06-16 09:43:10 +00001279 if (property_value instanceof ProtocolReference) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001280 result += '<no type>';
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001281 } else {
1282 if (property_value && property_value.type()) {
1283 result += property_value.type();
1284 } else {
1285 result += '<no type>';
1286 }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001287 }
1288 result += ' ';
1289 result += formatHandleReference_(property_value);
1290 result += '\n';
1291 }
1292 }
1293 return result;
1294}
1295
1296
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001297function 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.orga1645e22009-09-09 19:27:10 +00001314 case Debug.ScopeType.Catch:
1315 result += 'Catch, ';
1316 result += '#' + scope.object.ref + '#';
1317 break;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001318 case Debug.ScopeType.Closure:
1319 result += 'Closure';
1320 break;
1321 default:
1322 result += 'UNKNOWN';
1323 }
1324 return result;
1325}
1326
1327
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001328function 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.
1343function roundNumber(num, length) {
1344 var factor = Math.pow(10, length);
1345 return Math.round(num * factor) / factor;
1346}
1347
1348
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001349// Convert a JSON response to text for display in a text based debugger.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001350function DebugResponseDetails(response) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001351 details = {text:'', running:false}
1352
1353 try {
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001354 if (!response.success()) {
1355 details.text = response.message();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001356 return details;
1357 }
1358
1359 // Get the running state.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001360 details.running = response.running();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001361
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001362 var body = response.body();
1363 var result = '';
1364 switch (response.command()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001365 case 'suspend':
1366 details.text = 'stopped';
1367 break;
vegorov@chromium.org42841962010-10-18 11:18:59 +00001368
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001369 case 'setbreakpoint':
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001370 result = 'set breakpoint #';
1371 result += body.breakpoint;
1372 details.text = result;
1373 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +00001374
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001375 case 'clearbreakpoint':
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001376 result = 'cleared breakpoint #';
1377 result += body.breakpoint;
1378 details.text = result;
1379 break;
vegorov@chromium.org42841962010-10-18 11:18:59 +00001380
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001381 case 'changebreakpoint':
1382 result = 'successfully changed breakpoint';
1383 details.text = result;
1384 break;
1385
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001386 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.orgd2c22f02011-01-10 08:15:37 +00001398 result += ' line=' + (breakpoint.line + 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001399 if (breakpoint.column != null) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001400 result += ' column=' + (breakpoint.column + 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001401 }
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.orgd2c22f02011-01-10 08:15:37 +00001416 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.orgdff694e2010-05-17 09:10:26 +00001434 details.text = result;
1435 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +00001436
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001437 case 'backtrace':
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001438 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.org25156de2010-04-06 13:10:27 +00001450
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001451 case 'frame':
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001452 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.com37abdec2009-01-06 14:43:28 +00001485 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +00001486
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001487 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.org25156de2010-04-06 13:10:27 +00001510
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001511 case 'evaluate':
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001512 case 'lookup':
1513 if (last_cmd == 'p' || last_cmd == 'print') {
ager@chromium.org381abbb2009-02-25 13:23:22 +00001514 result = body.text;
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001515 } else {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001516 var value;
1517 if (lookup_handle) {
1518 value = response.bodyValue(lookup_handle);
1519 } else {
1520 value = response.bodyValue();
1521 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001522 if (value.isObject()) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001523 result += formatObject_(value, true);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001524 } 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.com37abdec2009-01-06 14:43:28 +00001541 break;
iposva@chromium.org245aa852009-02-10 00:49:54 +00001542
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.org25156de2010-04-06 13:10:27 +00001554
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001555 case 'source':
1556 // Get the source from the response.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001557 var source = body.source;
1558 var from_line = body.fromLine + 1;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001559 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.org25156de2010-04-06 13:10:27 +00001589
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001590 case 'scripts':
1591 var result = '';
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001592 for (i = 0; i < body.length; i++) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001593 if (i != 0) result += '\n';
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001594 if (body[i].id) {
1595 result += body[i].id;
1596 } else {
1597 result += '[no id]';
1598 }
1599 result += ', ';
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001600 if (body[i].name) {
1601 result += body[i].name;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001602 } else {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001603 if (body[i].compilationType == Debug.ScriptCompilationType.Eval
1604 && body[i].evalFromScript
1605 ) {
ager@chromium.orge2902be2009-06-08 12:21:35 +00001606 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.com37abdec2009-01-06 14:43:28 +00001617 }
1618 result += ' (lines: ';
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001619 result += body[i].lineCount;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001620 result += ', length: ';
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001621 result += body[i].sourceLength;
1622 if (body[i].type == Debug.ScriptType.Native) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001623 result += ', native';
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001624 } else if (body[i].type == Debug.ScriptType.Extension) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001625 result += ', extension';
1626 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001627 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.com37abdec2009-01-06 14:43:28 +00001634 }
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001635 if (body.length == 0) {
1636 result = "no matching scripts found";
1637 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001638 details.text = result;
1639 break;
1640
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001641 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.com37abdec2009-01-06 14:43:28 +00001653 case 'continue':
1654 details.text = "(running)";
1655 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +00001656
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001657 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.com37abdec2009-01-06 14:43:28 +00001674 default:
1675 details.text =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001676 'Response for unknown command \'' + response.command() + '\'' +
1677 ' (' + response.raw_json() + ')';
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001678 }
1679 } catch (e) {
1680 details.text = 'Error: "' + e + '" formatting response';
1681 }
lrn@chromium.org25156de2010-04-06 13:10:27 +00001682
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001683 return details;
1684};
1685
1686
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001687/**
1688 * Protocol packages send from the debugger.
1689 * @param {string} json - raw protocol packet as JSON string.
1690 * @constructor
1691 */
1692function ProtocolPackage(json) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001693 this.raw_json_ = json;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001694 this.packet_ = JSON.parse(json);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001695 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 */
1708ProtocolPackage.prototype.type = function() {
1709 return this.packet_.type;
1710}
1711
1712
1713/**
1714 * Get the packet event.
1715 * @return {Object} the packet event
1716 */
1717ProtocolPackage.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 */
1726ProtocolPackage.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 */
1735ProtocolPackage.prototype.running = function() {
1736 return this.packet_.running ? true : false;
1737}
1738
1739
1740ProtocolPackage.prototype.success = function() {
1741 return this.packet_.success ? true : false;
1742}
1743
1744
1745ProtocolPackage.prototype.message = function() {
1746 return this.packet_.message;
1747}
1748
1749
1750ProtocolPackage.prototype.command = function() {
1751 return this.packet_.command;
1752}
1753
1754
1755ProtocolPackage.prototype.body = function() {
1756 return this.packet_.body;
1757}
1758
1759
iposva@chromium.org245aa852009-02-10 00:49:54 +00001760ProtocolPackage.prototype.bodyValue = function(index) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001761 if (index != null) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001762 return new ProtocolValue(this.packet_.body[index], this);
ager@chromium.org381abbb2009-02-25 13:23:22 +00001763 } else {
1764 return new ProtocolValue(this.packet_.body, this);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001765 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001766}
1767
1768
1769ProtocolPackage.prototype.body = function() {
1770 return this.packet_.body;
1771}
1772
1773
1774ProtocolPackage.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.orgdff694e2010-05-17 09:10:26 +00001784ProtocolPackage.prototype.raw_json = function() {
1785 return this.raw_json_;
1786}
1787
1788
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001789function 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 */
1799ProtocolValue.prototype.type = function() {
1800 return this.value_.type;
1801}
1802
1803
1804/**
lrn@chromium.org25156de2010-04-06 13:10:27 +00001805 * Get a metadata field from a protocol value.
ager@chromium.orge2902be2009-06-08 12:21:35 +00001806 * @return {Object} the metadata field value
1807 */
1808ProtocolValue.prototype.field = function(name) {
1809 return this.value_[name];
1810}
1811
1812
1813/**
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001814 * Check is the value is a primitive value.
1815 * @return {boolean} true if the value is primitive
1816 */
1817ProtocolValue.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 */
1827ProtocolValue.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 */
1836ProtocolValue.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 */
1845ProtocolValue.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 */
1854ProtocolValue.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 */
1863ProtocolValue.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 */
1872ProtocolValue.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 */
1881ProtocolValue.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 */
1891ProtocolValue.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 */
1901ProtocolValue.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 */
1911ProtocolValue.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 */
1920ProtocolValue.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 */
1931ProtocolValue.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 */
1945ProtocolValue.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 */
1955ProtocolValue.prototype.value = function() {
1956 return this.value_.value;
1957}
1958
1959
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001960ProtocolValue.prototype.valueString = function() {
1961 return this.value_.text;
1962}
1963
1964
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001965function ProtocolReference(handle) {
1966 this.handle_ = handle;
1967}
1968
1969
1970ProtocolReference.prototype.handle = function() {
1971 return this.handle_;
1972}
1973
1974
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001975function MakeJSONPair_(name, value) {
1976 return '"' + name + '":' + value;
1977}
1978
1979
1980function ArrayToJSONObject_(content) {
1981 return '{' + content.join(',') + '}';
1982}
1983
1984
1985function ArrayToJSONArray_(content) {
1986 return '[' + content.join(',') + ']';
1987}
1988
1989
1990function BooleanToJSON_(value) {
lrn@chromium.org25156de2010-04-06 13:10:27 +00001991 return String(value);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001992}
1993
1994
1995function NumberToJSON_(value) {
lrn@chromium.org25156de2010-04-06 13:10:27 +00001996 return String(value);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001997}
1998
1999
2000// Mapping of some control characters to avoid the \uXXXX syntax for most
2001// commonly used control cahracters.
2002const 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).
2014const ctrlCharTest_ = new RegExp('["\\\\\x00-\x1F]');
2015
2016
2017// Regular expression matching ", \ and control characters (0x00 - 0x1F)
2018// globally.
2019const 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 */
2029function 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 */
2060function 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 */
2084function 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 */
2100function 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.orgd2c22f02011-01-10 08:15:37 +00002111 if (property_value === null) {
2112 property_value_json = 'null';
2113 } else if (typeof property_value.toJSONProtocol == 'function') {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002114 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 */
2156function 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}