blob: 033455e9df94e29349f3e229c9ef16ab47b7ca30 [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 = '';
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000120//var lol_is_enabled; // Set to true in d8.cc if LIVE_OBJECT_LIST is defined.
121var lol_next_dump_index = 0;
122const kDefaultLolLinesToPrintAtATime = 10;
123const kMaxLolLinesToPrintAtATime = 1000;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000124var repeat_cmd_line = '';
125var is_running = true;
126
127// Copied from debug-delay.js. This is needed below:
128function ScriptTypeFlag(type) {
129 return (1 << type);
130}
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000131
132
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000133// Process a debugger JSON message into a display text and a running status.
134// This function returns an object with properties "text" and "running" holding
135// this information.
136function DebugMessageDetails(message) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000137 if (trace_debug_json) {
138 print("received: '" + message + "'");
139 }
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000140 // Convert the JSON string to an object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000141 var response = new ProtocolPackage(message);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000142 is_running = response.running();
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000143
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000144 if (response.type() == 'event') {
145 return DebugEventDetails(response);
146 } else {
147 return DebugResponseDetails(response);
148 }
149}
150
151function DebugEventDetails(response) {
152 details = {text:'', running:false}
153
154 // Get the running state.
155 details.running = response.running();
156
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000157 var body = response.body();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000158 var result = '';
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000159 switch (response.event()) {
160 case 'break':
161 if (body.breakpoints) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000162 result += 'breakpoint';
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000163 if (body.breakpoints.length > 1) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000164 result += 's';
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000165 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000166 result += ' #';
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000167 for (var i = 0; i < body.breakpoints.length; i++) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000168 if (i > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000169 result += ', #';
iposva@chromium.org245aa852009-02-10 00:49:54 +0000170 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000171 result += body.breakpoints[i];
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000172 }
iposva@chromium.org245aa852009-02-10 00:49:54 +0000173 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000174 result += 'break';
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000175 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000176 result += ' in ';
177 result += body.invocationText;
178 result += ', ';
179 result += SourceInfo(body);
180 result += '\n';
181 result += SourceUnderline(body.sourceLineText, body.sourceColumn);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000182 Debug.State.currentSourceLine = body.sourceLine;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000183 Debug.State.displaySourceStartLine = -1;
184 Debug.State.displaySourceEndLine = -1;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000185 Debug.State.currentFrame = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000186 details.text = result;
187 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000188
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000189 case 'exception':
190 if (body.uncaught) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000191 result += 'Uncaught: ';
iposva@chromium.org245aa852009-02-10 00:49:54 +0000192 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000193 result += 'Exception: ';
iposva@chromium.org245aa852009-02-10 00:49:54 +0000194 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000195 result += '"';
196 result += body.exception.text;
197 result += '"';
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000198 if (body.sourceLine >= 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000199 result += ', ';
200 result += SourceInfo(body);
201 result += '\n';
202 result += SourceUnderline(body.sourceLineText, body.sourceColumn);
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000203 Debug.State.currentSourceLine = body.sourceLine;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000204 Debug.State.displaySourceStartLine = -1;
205 Debug.State.displaySourceEndLine = -1;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000206 Debug.State.currentFrame = 0;
207 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000208 result += ' (empty stack)';
iposva@chromium.org245aa852009-02-10 00:49:54 +0000209 Debug.State.currentSourceLine = -1;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000210 Debug.State.displaySourceStartLine = -1;
211 Debug.State.displaySourceEndLine = -1;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000212 Debug.State.currentFrame = kNoFrame;
213 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000214 details.text = result;
215 break;
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000216
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000217 case 'afterCompile':
iposva@chromium.org245aa852009-02-10 00:49:54 +0000218 if (trace_compile) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000219 result = 'Source ' + body.script.name + ' compiled:\n'
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000220 var source = body.script.source;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000221 if (!(source[source.length - 1] == '\n')) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000222 result += source;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000223 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000224 result += source.substring(0, source.length - 1);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000225 }
iposva@chromium.org245aa852009-02-10 00:49:54 +0000226 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000227 details.text = result;
228 break;
229
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000230 case 'scriptCollected':
231 details.text = result;
232 break;
233
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000234 default:
235 details.text = 'Unknown debug event ' + response.event();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000236 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000237
238 return details;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000239};
240
241
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000242function SourceInfo(body) {
243 var result = '';
lrn@chromium.org25156de2010-04-06 13:10:27 +0000244
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000245 if (body.script) {
246 if (body.script.name) {
247 result += body.script.name;
248 } else {
249 result += '[unnamed]';
250 }
251 }
252 result += ' line ';
253 result += body.sourceLine + 1;
254 result += ' column ';
255 result += body.sourceColumn + 1;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000256
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000257 return result;
258}
259
260
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000261function SourceUnderline(source_text, position) {
262 if (!source_text) {
263 return;
264 }
265
266 // Create an underline with a caret pointing to the source position. If the
267 // source contains a tab character the underline will have a tab character in
268 // the same place otherwise the underline will have a space character.
269 var underline = '';
270 for (var i = 0; i < position; i++) {
271 if (source_text[i] == '\t') {
272 underline += '\t';
273 } else {
274 underline += ' ';
275 }
276 }
277 underline += '^';
278
279 // Return the source line text with the underline beneath.
280 return source_text + '\n' + underline;
281};
282
283
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000284// Converts a text command to a JSON request.
285function DebugCommandToJSONRequest(cmd_line) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000286 var result = new DebugRequest(cmd_line).JSONRequest();
287 if (trace_debug_json && result) {
288 print("sending: '" + result + "'");
289 }
290 return result;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000291};
292
293
294function DebugRequest(cmd_line) {
295 // If the very first character is a { assume that a JSON request have been
296 // entered as a command. Converting that to a JSON request is trivial.
297 if (cmd_line && cmd_line.length > 0 && cmd_line.charAt(0) == '{') {
298 this.request_ = cmd_line;
299 return;
300 }
301
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000302 // Check for a simple carriage return to repeat the last command:
303 var is_repeating = false;
304 if (cmd_line == '\n') {
305 if (is_running) {
306 cmd_line = 'break'; // Not in debugger mode, break with a frame request.
307 } else {
308 cmd_line = repeat_cmd_line; // use command to repeat.
309 is_repeating = true;
310 }
311 }
312 if (!is_running) { // Only save the command if in debugger mode.
313 repeat_cmd_line = cmd_line; // save last command.
314 }
315
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000316 // Trim string for leading and trailing whitespace.
317 cmd_line = cmd_line.replace(/^\s+|\s+$/g, '');
318
319 // Find the command.
320 var pos = cmd_line.indexOf(' ');
321 var cmd;
322 var args;
323 if (pos == -1) {
324 cmd = cmd_line;
325 args = '';
326 } else {
327 cmd = cmd_line.slice(0, pos);
328 args = cmd_line.slice(pos).replace(/^\s+|\s+$/g, '');
329 }
330
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000331 if ((cmd === undefined) || !cmd) {
332 this.request_ = void 0;
333 return;
334 }
335
336 last_cmd = cmd;
337
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000338 // Switch on command.
339 switch (cmd) {
340 case 'continue':
341 case 'c':
342 this.request_ = this.continueCommandToJSONRequest_(args);
343 break;
344
345 case 'step':
346 case 's':
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000347 this.request_ = this.stepCommandToJSONRequest_(args, 'in');
348 break;
349
350 case 'stepi':
351 case 'si':
352 this.request_ = this.stepCommandToJSONRequest_(args, 'min');
353 break;
354
355 case 'next':
356 case 'n':
357 this.request_ = this.stepCommandToJSONRequest_(args, 'next');
358 break;
359
360 case 'finish':
361 case 'fin':
362 this.request_ = this.stepCommandToJSONRequest_(args, 'out');
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000363 break;
364
365 case 'backtrace':
366 case 'bt':
367 this.request_ = this.backtraceCommandToJSONRequest_(args);
368 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000369
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000370 case 'frame':
371 case 'f':
372 this.request_ = this.frameCommandToJSONRequest_(args);
373 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000374
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000375 case 'scopes':
376 this.request_ = this.scopesCommandToJSONRequest_(args);
377 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000378
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000379 case 'scope':
380 this.request_ = this.scopeCommandToJSONRequest_(args);
381 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000382
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000383 case 'disconnect':
384 case 'exit':
385 case 'quit':
386 this.request_ = this.disconnectCommandToJSONRequest_(args);
387 break;
388
389 case 'up':
390 this.request_ =
391 this.frameCommandToJSONRequest_('' +
392 (Debug.State.currentFrame + 1));
393 break;
394
395 case 'down':
396 case 'do':
397 this.request_ =
398 this.frameCommandToJSONRequest_('' +
399 (Debug.State.currentFrame - 1));
400 break;
401
402 case 'set':
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000403 case 'print':
404 case 'p':
405 this.request_ = this.printCommandToJSONRequest_(args);
406 break;
407
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000408 case 'dir':
409 this.request_ = this.dirCommandToJSONRequest_(args);
410 break;
411
iposva@chromium.org245aa852009-02-10 00:49:54 +0000412 case 'references':
413 this.request_ = this.referencesCommandToJSONRequest_(args);
414 break;
415
416 case 'instances':
417 this.request_ = this.instancesCommandToJSONRequest_(args);
418 break;
419
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000420 case 'list':
421 case 'l':
422 this.request_ = this.listCommandToJSONRequest_(args);
423 break;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000424 case 'source':
425 this.request_ = this.sourceCommandToJSONRequest_(args);
426 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000427
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000428 case 'scripts':
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000429 case 'script':
430 case 'scr':
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000431 this.request_ = this.scriptsCommandToJSONRequest_(args);
432 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000433
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000434 case 'break':
435 case 'b':
436 this.request_ = this.breakCommandToJSONRequest_(args);
437 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000438
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000439 case 'breakpoints':
440 case 'bb':
441 this.request_ = this.breakpointsCommandToJSONRequest_(args);
442 break;
443
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000444 case 'clear':
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000445 case 'delete':
446 case 'd':
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000447 this.request_ = this.clearCommandToJSONRequest_(args);
448 break;
449
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000450 case 'threads':
451 this.request_ = this.threadsCommandToJSONRequest_(args);
452 break;
453
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000454 case 'cond':
455 this.request_ = this.changeBreakpointCommandToJSONRequest_(args, 'cond');
456 break;
457
458 case 'enable':
459 case 'en':
460 this.request_ =
461 this.changeBreakpointCommandToJSONRequest_(args, 'enable');
462 break;
463
464 case 'disable':
465 case 'dis':
466 this.request_ =
467 this.changeBreakpointCommandToJSONRequest_(args, 'disable');
468 break;
469
470 case 'ignore':
471 this.request_ =
472 this.changeBreakpointCommandToJSONRequest_(args, 'ignore');
473 break;
474
475 case 'info':
476 case 'inf':
477 this.request_ = this.infoCommandToJSONRequest_(args);
478 break;
479
480 case 'flags':
481 this.request_ = this.v8FlagsToJSONRequest_(args);
482 break;
483
484 case 'gc':
485 this.request_ = this.gcToJSONRequest_(args);
486 break;
487
iposva@chromium.org245aa852009-02-10 00:49:54 +0000488 case 'trace':
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000489 case 'tr':
iposva@chromium.org245aa852009-02-10 00:49:54 +0000490 // Return undefined to indicate command handled internally (no JSON).
491 this.request_ = void 0;
492 this.traceCommand_(args);
493 break;
494
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000495 case 'help':
496 case '?':
497 this.helpCommand_(args);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000498 // Return undefined to indicate command handled internally (no JSON).
499 this.request_ = void 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000500 break;
501
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000502 case 'liveobjectlist':
503 case 'lol':
504 if (lol_is_enabled) {
505 this.request_ = this.lolToJSONRequest_(args, is_repeating);
506 break;
507 }
508
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000509 default:
510 throw new Error('Unknown command "' + cmd + '"');
511 }
512}
513
514DebugRequest.prototype.JSONRequest = function() {
515 return this.request_;
516}
517
518
519function RequestPacket(command) {
520 this.seq = 0;
521 this.type = 'request';
522 this.command = command;
523}
524
525
526RequestPacket.prototype.toJSONProtocol = function() {
527 // Encode the protocol header.
528 var json = '{';
529 json += '"seq":' + this.seq;
530 json += ',"type":"' + this.type + '"';
531 if (this.command) {
532 json += ',"command":' + StringToJSON_(this.command);
533 }
534 if (this.arguments) {
535 json += ',"arguments":';
536 // Encode the arguments part.
537 if (this.arguments.toJSONProtocol) {
538 json += this.arguments.toJSONProtocol()
539 } else {
540 json += SimpleObjectToJSON_(this.arguments);
541 }
542 }
543 json += '}';
544 return json;
545}
546
547
548DebugRequest.prototype.createRequest = function(command) {
549 return new RequestPacket(command);
550};
551
552
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000553// Note: we use detected command repetition as a signal for continuation here.
554DebugRequest.prototype.createLOLRequest = function(command,
555 start_index,
556 lines_to_dump,
557 is_continuation) {
558 if (is_continuation) {
559 start_index = lol_next_dump_index;
560 }
561
562 if (lines_to_dump) {
563 lines_to_dump = parseInt(lines_to_dump);
564 } else {
565 lines_to_dump = kDefaultLolLinesToPrintAtATime;
566 }
567 if (lines_to_dump > kMaxLolLinesToPrintAtATime) {
568 lines_to_dump = kMaxLolLinesToPrintAtATime;
569 }
570
571 // Save the next start_index to dump from:
572 lol_next_dump_index = start_index + lines_to_dump;
573
574 var request = this.createRequest(command);
575 request.arguments = {};
576 request.arguments.start = start_index;
577 request.arguments.count = lines_to_dump;
578
579 return request;
580};
581
582
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000583// Create a JSON request for the evaluation command.
584DebugRequest.prototype.makeEvaluateJSONRequest_ = function(expression) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000585 // Global varaible used to store whether a handle was requested.
586 lookup_handle = null;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000587
588 if (lol_is_enabled) {
589 // Check if the expression is a obj id in the form @<obj id>.
590 var obj_id_match = expression.match(/^@([0-9]+)$/);
591 if (obj_id_match) {
592 var obj_id = parseInt(obj_id_match[1]);
593 // Build a dump request.
594 var request = this.createRequest('getobj');
595 request.arguments = {};
596 request.arguments.obj_id = obj_id;
597 return request.toJSONProtocol();
598 }
599 }
600
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000601 // Check if the expression is a handle id in the form #<handle>#.
602 var handle_match = expression.match(/^#([0-9]*)#$/);
603 if (handle_match) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000604 // Remember the handle requested in a global variable.
605 lookup_handle = parseInt(handle_match[1]);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000606 // Build a lookup request.
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000607 var request = this.createRequest('lookup');
608 request.arguments = {};
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000609 request.arguments.handles = [ lookup_handle ];
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000610 return request.toJSONProtocol();
611 } else {
612 // Build an evaluate request.
613 var request = this.createRequest('evaluate');
614 request.arguments = {};
615 request.arguments.expression = expression;
ager@chromium.org381abbb2009-02-25 13:23:22 +0000616 // Request a global evaluation if there is no current frame.
617 if (Debug.State.currentFrame == kNoFrame) {
618 request.arguments.global = true;
619 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000620 return request.toJSONProtocol();
621 }
622};
623
624
iposva@chromium.org245aa852009-02-10 00:49:54 +0000625// Create a JSON request for the references/instances command.
626DebugRequest.prototype.makeReferencesJSONRequest_ = function(handle, type) {
627 // Build a references request.
628 var handle_match = handle.match(/^#([0-9]*)#$/);
629 if (handle_match) {
630 var request = this.createRequest('references');
631 request.arguments = {};
632 request.arguments.type = type;
633 request.arguments.handle = parseInt(handle_match[1]);
634 return request.toJSONProtocol();
635 } else {
636 throw new Error('Invalid object id.');
637 }
638};
639
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000640
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000641// Create a JSON request for the continue command.
642DebugRequest.prototype.continueCommandToJSONRequest_ = function(args) {
643 var request = this.createRequest('continue');
644 return request.toJSONProtocol();
645};
646
647
648// Create a JSON request for the step command.
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000649DebugRequest.prototype.stepCommandToJSONRequest_ = function(args, type) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000650 // Requesting a step is through the continue command with additional
651 // arguments.
652 var request = this.createRequest('continue');
653 request.arguments = {};
654
655 // Process arguments if any.
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000656
657 // Only process args if the command is 'step' which is indicated by type being
658 // set to 'in'. For all other commands, ignore the args.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000659 if (args && args.length > 0) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000660 args = args.split(/\s+/g);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000661
662 if (args.length > 2) {
663 throw new Error('Invalid step arguments.');
664 }
665
666 if (args.length > 0) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000667 // Check if we have a gdb stype step command. If so, the 1st arg would
668 // be the step count. If it's not a number, then assume that we're
669 // parsing for the legacy v8 step command.
670 var stepcount = Number(args[0]);
671 if (stepcount == Number.NaN) {
672 // No step count at arg 1. Process as legacy d8 step command:
673 if (args.length == 2) {
674 var stepcount = parseInt(args[1]);
675 if (isNaN(stepcount) || stepcount <= 0) {
676 throw new Error('Invalid step count argument "' + args[0] + '".');
677 }
678 request.arguments.stepcount = stepcount;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000679 }
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000680
681 // Get the step action.
682 switch (args[0]) {
683 case 'in':
684 case 'i':
685 request.arguments.stepaction = 'in';
686 break;
687
688 case 'min':
689 case 'm':
690 request.arguments.stepaction = 'min';
691 break;
692
693 case 'next':
694 case 'n':
695 request.arguments.stepaction = 'next';
696 break;
697
698 case 'out':
699 case 'o':
700 request.arguments.stepaction = 'out';
701 break;
702
703 default:
704 throw new Error('Invalid step argument "' + args[0] + '".');
705 }
706
707 } else {
708 // gdb style step commands:
709 request.arguments.stepaction = type;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000710 request.arguments.stepcount = stepcount;
711 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000712 }
713 } else {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000714 // Default is step of the specified type.
715 request.arguments.stepaction = type;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000716 }
717
718 return request.toJSONProtocol();
719};
720
721
722// Create a JSON request for the backtrace command.
723DebugRequest.prototype.backtraceCommandToJSONRequest_ = function(args) {
724 // Build a backtrace request from the text command.
725 var request = this.createRequest('backtrace');
lrn@chromium.org25156de2010-04-06 13:10:27 +0000726
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000727 // Default is to show top 10 frames.
728 request.arguments = {};
729 request.arguments.fromFrame = 0;
730 request.arguments.toFrame = 10;
731
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000732 args = args.split(/\s*[ ]+\s*/g);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000733 if (args.length == 1 && args[0].length > 0) {
734 var frameCount = parseInt(args[0]);
735 if (frameCount > 0) {
736 // Show top frames.
737 request.arguments.fromFrame = 0;
738 request.arguments.toFrame = frameCount;
739 } else {
740 // Show bottom frames.
741 request.arguments.fromFrame = 0;
742 request.arguments.toFrame = -frameCount;
743 request.arguments.bottom = true;
744 }
745 } else if (args.length == 2) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000746 var fromFrame = parseInt(args[0]);
747 var toFrame = parseInt(args[1]);
748 if (isNaN(fromFrame) || fromFrame < 0) {
749 throw new Error('Invalid start frame argument "' + args[0] + '".');
750 }
751 if (isNaN(toFrame) || toFrame < 0) {
752 throw new Error('Invalid end frame argument "' + args[1] + '".');
753 }
754 if (fromFrame > toFrame) {
755 throw new Error('Invalid arguments start frame cannot be larger ' +
756 'than end frame.');
757 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000758 // Show frame range.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000759 request.arguments.fromFrame = fromFrame;
760 request.arguments.toFrame = toFrame + 1;
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000761 } else if (args.length > 2) {
762 throw new Error('Invalid backtrace arguments.');
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000763 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000764
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000765 return request.toJSONProtocol();
766};
767
768
769// Create a JSON request for the frame command.
770DebugRequest.prototype.frameCommandToJSONRequest_ = function(args) {
771 // Build a frame request from the text command.
772 var request = this.createRequest('frame');
773 args = args.split(/\s*[ ]+\s*/g);
774 if (args.length > 0 && args[0].length > 0) {
775 request.arguments = {};
776 request.arguments.number = args[0];
777 }
778 return request.toJSONProtocol();
779};
780
781
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000782// Create a JSON request for the scopes command.
783DebugRequest.prototype.scopesCommandToJSONRequest_ = function(args) {
784 // Build a scopes request from the text command.
785 var request = this.createRequest('scopes');
786 return request.toJSONProtocol();
787};
788
789
790// Create a JSON request for the scope command.
791DebugRequest.prototype.scopeCommandToJSONRequest_ = function(args) {
792 // Build a scope request from the text command.
793 var request = this.createRequest('scope');
794 args = args.split(/\s*[ ]+\s*/g);
795 if (args.length > 0 && args[0].length > 0) {
796 request.arguments = {};
797 request.arguments.number = args[0];
798 }
799 return request.toJSONProtocol();
800};
801
802
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000803// Create a JSON request for the print command.
804DebugRequest.prototype.printCommandToJSONRequest_ = function(args) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000805 // Build an evaluate request from the text command.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000806 if (args.length == 0) {
807 throw new Error('Missing expression.');
808 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000809 return this.makeEvaluateJSONRequest_(args);
810};
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000811
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000812
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000813// Create a JSON request for the dir command.
814DebugRequest.prototype.dirCommandToJSONRequest_ = function(args) {
815 // Build an evaluate request from the text command.
816 if (args.length == 0) {
817 throw new Error('Missing expression.');
818 }
819 return this.makeEvaluateJSONRequest_(args);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000820};
821
822
iposva@chromium.org245aa852009-02-10 00:49:54 +0000823// Create a JSON request for the references command.
824DebugRequest.prototype.referencesCommandToJSONRequest_ = function(args) {
825 // Build an evaluate request from the text command.
826 if (args.length == 0) {
827 throw new Error('Missing object id.');
828 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000829
iposva@chromium.org245aa852009-02-10 00:49:54 +0000830 return this.makeReferencesJSONRequest_(args, 'referencedBy');
831};
832
833
834// Create a JSON request for the instances command.
835DebugRequest.prototype.instancesCommandToJSONRequest_ = function(args) {
836 // Build an evaluate request from the text command.
837 if (args.length == 0) {
838 throw new Error('Missing object id.');
839 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000840
iposva@chromium.org245aa852009-02-10 00:49:54 +0000841 // Build a references request.
842 return this.makeReferencesJSONRequest_(args, 'constructedBy');
843};
844
845
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000846// Create a JSON request for the list command.
847DebugRequest.prototype.listCommandToJSONRequest_ = function(args) {
848
849 // Default is ten lines starting five lines before the current location.
850 if (Debug.State.displaySourceEndLine == -1) {
851 // If we list forwards, we will start listing after the last source end
852 // line. Set it to start from 5 lines before the current location.
853 Debug.State.displaySourceEndLine = Debug.State.currentSourceLine - 5;
854 // If we list backwards, we will start listing backwards from the last
855 // source start line. Set it to start from 1 lines before the current
856 // location.
857 Debug.State.displaySourceStartLine = Debug.State.currentSourceLine + 1;
858 }
859
860 var from = Debug.State.displaySourceEndLine + 1;
861 var lines = 10;
862
863 // Parse the arguments.
864 args = args.split(/\s*,\s*/g);
865 if (args == '') {
866 } else if ((args.length == 1) && (args[0] == '-')) {
867 from = Debug.State.displaySourceStartLine - lines;
868 } else if (args.length == 2) {
869 from = parseInt(args[0]);
870 lines = parseInt(args[1]) - from + 1; // inclusive of the ending line.
871 } else {
872 throw new Error('Invalid list arguments.');
873 }
874 Debug.State.displaySourceStartLine = from;
875 Debug.State.displaySourceEndLine = from + lines - 1;
876 var sourceArgs = '' + from + ' ' + lines;
877 return this.sourceCommandToJSONRequest_(sourceArgs);
878};
879
880
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000881// Create a JSON request for the source command.
882DebugRequest.prototype.sourceCommandToJSONRequest_ = function(args) {
883 // Build a evaluate request from the text command.
884 var request = this.createRequest('source');
885
886 // Default is ten lines starting five lines before the current location.
887 var from = Debug.State.currentSourceLine - 5;
888 var lines = 10;
889
890 // Parse the arguments.
891 args = args.split(/\s*[ ]+\s*/g);
892 if (args.length > 1 && args[0].length > 0 && args[1].length > 0) {
893 from = parseInt(args[0]) - 1;
894 lines = parseInt(args[1]);
895 } else if (args.length > 0 && args[0].length > 0) {
896 from = parseInt(args[0]) - 1;
897 }
898
899 if (from < 0) from = 0;
900 if (lines < 0) lines = 10;
901
902 // Request source arround current source location.
903 request.arguments = {};
904 request.arguments.fromLine = from;
905 request.arguments.toLine = from + lines;
906
907 return request.toJSONProtocol();
908};
909
910
911// Create a JSON request for the scripts command.
912DebugRequest.prototype.scriptsCommandToJSONRequest_ = function(args) {
913 // Build a evaluate request from the text command.
914 var request = this.createRequest('scripts');
915
916 // Process arguments if any.
917 if (args && args.length > 0) {
918 args = args.split(/\s*[ ]+\s*/g);
919
920 if (args.length > 1) {
921 throw new Error('Invalid scripts arguments.');
922 }
923
924 request.arguments = {};
925 switch (args[0]) {
926 case 'natives':
927 request.arguments.types = ScriptTypeFlag(Debug.ScriptType.Native);
928 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000929
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000930 case 'extensions':
931 request.arguments.types = ScriptTypeFlag(Debug.ScriptType.Extension);
932 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000933
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000934 case 'all':
935 request.arguments.types =
936 ScriptTypeFlag(Debug.ScriptType.Normal) |
937 ScriptTypeFlag(Debug.ScriptType.Native) |
938 ScriptTypeFlag(Debug.ScriptType.Extension);
939 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000940
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000941 default:
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000942 // If the arg is not one of the know one aboves, then it must be a
943 // filter used for filtering the results:
944 request.arguments.filter = args[0];
945 break;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000946 }
947 }
948
949 return request.toJSONProtocol();
950};
951
952
953// Create a JSON request for the break command.
954DebugRequest.prototype.breakCommandToJSONRequest_ = function(args) {
955 // Build a evaluate request from the text command.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000956 // Process arguments if any.
957 if (args && args.length > 0) {
958 var target = args;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000959 var type = 'function';
960 var line;
961 var column;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000962 var condition;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000963 var pos;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000964
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000965 var request = this.createRequest('setbreakpoint');
966
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000967 // Break the args into target spec and condition if appropriate.
968
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000969 // Check for breakpoint condition.
970 pos = args.indexOf(' ');
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000971 if (pos > 0) {
972 target = args.substring(0, pos);
973 condition = args.substring(pos + 1, args.length);
974 }
975
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000976 // Check for script breakpoint (name:line[:column]). If no ':' in break
977 // specification it is considered a function break point.
978 pos = target.indexOf(':');
979 if (pos > 0) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000980 var tmp = target.substring(pos + 1, target.length);
981 target = target.substring(0, pos);
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000982 if (target[0] == '/' && target[target.length - 1] == '/') {
983 type = 'scriptRegExp';
984 target = target.substring(1, target.length - 1);
985 } else {
986 type = 'script';
987 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000988
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000989 // Check for both line and column.
990 pos = tmp.indexOf(':');
991 if (pos > 0) {
992 column = parseInt(tmp.substring(pos + 1, tmp.length)) - 1;
993 line = parseInt(tmp.substring(0, pos)) - 1;
994 } else {
995 line = parseInt(tmp) - 1;
996 }
997 } else if (target[0] == '#' && target[target.length - 1] == '#') {
998 type = 'handle';
999 target = target.substring(1, target.length - 1);
1000 } else {
1001 type = 'function';
1002 }
lrn@chromium.org25156de2010-04-06 13:10:27 +00001003
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001004 request.arguments = {};
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001005 request.arguments.type = type;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001006 request.arguments.target = target;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001007 request.arguments.line = line;
1008 request.arguments.column = column;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001009 request.arguments.condition = condition;
1010 } else {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001011 var request = this.createRequest('suspend');
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001012 }
1013
1014 return request.toJSONProtocol();
1015};
1016
1017
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001018DebugRequest.prototype.breakpointsCommandToJSONRequest_ = function(args) {
1019 if (args && args.length > 0) {
1020 throw new Error('Unexpected arguments.');
1021 }
1022 var request = this.createRequest('listbreakpoints');
1023 return request.toJSONProtocol();
1024};
1025
1026
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001027// Create a JSON request for the clear command.
1028DebugRequest.prototype.clearCommandToJSONRequest_ = function(args) {
1029 // Build a evaluate request from the text command.
1030 var request = this.createRequest('clearbreakpoint');
1031
1032 // Process arguments if any.
1033 if (args && args.length > 0) {
1034 request.arguments = {};
1035 request.arguments.breakpoint = parseInt(args);
1036 } else {
1037 throw new Error('Invalid break arguments.');
1038 }
1039
1040 return request.toJSONProtocol();
1041};
1042
1043
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001044// Create a JSON request for the change breakpoint command.
1045DebugRequest.prototype.changeBreakpointCommandToJSONRequest_ =
1046 function(args, command) {
1047
1048 var request;
1049
1050 // Check for exception breaks first:
1051 // en[able] exc[eptions] [all|unc[aught]]
1052 // en[able] [all|unc[aught]] exc[eptions]
1053 // dis[able] exc[eptions] [all|unc[aught]]
1054 // dis[able] [all|unc[aught]] exc[eptions]
1055 if ((command == 'enable' || command == 'disable') &&
1056 args && args.length > 1) {
1057 var nextPos = args.indexOf(' ');
1058 var arg1 = (nextPos > 0) ? args.substring(0, nextPos) : args;
1059 var excType = null;
1060
1061 // Check for:
1062 // en[able] exc[eptions] [all|unc[aught]]
1063 // dis[able] exc[eptions] [all|unc[aught]]
1064 if (arg1 == 'exc' || arg1 == 'exception' || arg1 == 'exceptions') {
1065
1066 var arg2 = (nextPos > 0) ?
1067 args.substring(nextPos + 1, args.length) : 'all';
1068 if (!arg2) {
1069 arg2 = 'all'; // if unspecified, set for all.
1070 } if (arg2 == 'unc') { // check for short cut.
1071 arg2 = 'uncaught';
1072 }
1073 excType = arg2;
1074
1075 // Check for:
1076 // en[able] [all|unc[aught]] exc[eptions]
1077 // dis[able] [all|unc[aught]] exc[eptions]
1078 } else if (arg1 == 'all' || arg1 == 'unc' || arg1 == 'uncaught') {
1079
1080 var arg2 = (nextPos > 0) ?
1081 args.substring(nextPos + 1, args.length) : null;
1082 if (arg2 == 'exc' || arg1 == 'exception' || arg1 == 'exceptions') {
1083 excType = arg1;
1084 if (excType == 'unc') {
1085 excType = 'uncaught';
1086 }
1087 }
1088 }
1089
1090 // If we matched one of the command formats, then excType will be non-null:
1091 if (excType) {
1092 // Build a evaluate request from the text command.
1093 request = this.createRequest('setexceptionbreak');
1094
1095 request.arguments = {};
1096 request.arguments.type = excType;
1097 request.arguments.enabled = (command == 'enable');
1098
1099 return request.toJSONProtocol();
1100 }
1101 }
1102
1103 // Build a evaluate request from the text command.
1104 request = this.createRequest('changebreakpoint');
1105
1106 // Process arguments if any.
1107 if (args && args.length > 0) {
1108 request.arguments = {};
1109 var pos = args.indexOf(' ');
1110 var breakpointArg = args;
1111 var otherArgs;
1112 if (pos > 0) {
1113 breakpointArg = args.substring(0, pos);
1114 otherArgs = args.substring(pos + 1, args.length);
1115 }
1116
1117 request.arguments.breakpoint = parseInt(breakpointArg);
1118
1119 switch(command) {
1120 case 'cond':
1121 request.arguments.condition = otherArgs ? otherArgs : null;
1122 break;
1123 case 'enable':
1124 request.arguments.enabled = true;
1125 break;
1126 case 'disable':
1127 request.arguments.enabled = false;
1128 break;
1129 case 'ignore':
1130 request.arguments.ignoreCount = parseInt(otherArgs);
1131 break;
1132 default:
1133 throw new Error('Invalid arguments.');
1134 }
1135 } else {
1136 throw new Error('Invalid arguments.');
1137 }
1138
1139 return request.toJSONProtocol();
1140};
1141
1142
1143// Create a JSON request for the disconnect command.
1144DebugRequest.prototype.disconnectCommandToJSONRequest_ = function(args) {
1145 var request;
1146 request = this.createRequest('disconnect');
1147 return request.toJSONProtocol();
1148};
1149
1150
1151// Create a JSON request for the info command.
1152DebugRequest.prototype.infoCommandToJSONRequest_ = function(args) {
1153 var request;
1154 if (args && (args == 'break' || args == 'br')) {
1155 // Build a evaluate request from the text command.
1156 request = this.createRequest('listbreakpoints');
1157 last_cmd = 'info break';
1158 } else if (args && (args == 'locals' || args == 'lo')) {
1159 // Build a evaluate request from the text command.
1160 request = this.createRequest('frame');
1161 last_cmd = 'info locals';
1162 } else if (args && (args == 'args' || args == 'ar')) {
1163 // Build a evaluate request from the text command.
1164 request = this.createRequest('frame');
1165 last_cmd = 'info args';
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001166 } else if (lol_is_enabled &&
1167 args && (args == 'liveobjectlist' || args == 'lol')) {
1168 // Build a evaluate request from the text command.
1169 return this.liveObjectListToJSONRequest_(null);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001170 } else {
1171 throw new Error('Invalid info arguments.');
1172 }
1173
1174 return request.toJSONProtocol();
1175};
1176
1177
1178DebugRequest.prototype.v8FlagsToJSONRequest_ = function(args) {
1179 var request;
1180 request = this.createRequest('v8flags');
1181 request.arguments = {};
1182 request.arguments.flags = args;
1183 return request.toJSONProtocol();
1184};
1185
1186
1187DebugRequest.prototype.gcToJSONRequest_ = function(args) {
1188 var request;
1189 if (!args) {
1190 args = 'all';
1191 }
1192 var args = args.split(/\s+/g);
1193 var cmd = args[0];
1194
1195 switch(cmd) {
1196 case 'all':
1197 case 'quick':
1198 case 'full':
1199 case 'young':
1200 case 'old':
1201 case 'compact':
1202 case 'sweep':
1203 case 'scavenge': {
1204 if (cmd == 'young') { cmd = 'quick'; }
1205 else if (cmd == 'old') { cmd = 'full'; }
1206
1207 request = this.createRequest('gc');
1208 request.arguments = {};
1209 request.arguments.type = cmd;
1210 break;
1211 }
1212 // Else fall thru to the default case below to report the error.
1213 default:
1214 throw new Error('Missing arguments after ' + cmd + '.');
1215 }
1216 return request.toJSONProtocol();
1217};
1218
1219
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001220// Args: [v[erbose]] [<N>] [i[ndex] <i>] [t[ype] <type>] [sp[ace] <space>]
1221DebugRequest.prototype.lolMakeListRequest =
1222 function(cmd, args, first_arg_index, is_repeating) {
1223
1224 var request;
1225 var start_index = 0;
1226 var dump_limit = void 0;
1227 var type_filter = void 0;
1228 var space_filter = void 0;
1229 var prop_filter = void 0;
1230 var is_verbose = false;
1231 var i;
1232
1233 for (i = first_arg_index; i < args.length; i++) {
1234 var arg = args[i];
1235 // Check for [v[erbose]]:
1236 if (arg === 'verbose' || arg === 'v') {
1237 // Nothing to do. This is already implied by args.length > 3.
1238 is_verbose = true;
1239
1240 // Check for [<N>]:
1241 } else if (arg.match(/^[0-9]+$/)) {
1242 dump_limit = arg;
1243 is_verbose = true;
1244
1245 // Check for i[ndex] <i>:
1246 } else if (arg === 'index' || arg === 'i') {
1247 i++;
1248 if (args.length < i) {
1249 throw new Error('Missing index after ' + arg + '.');
1250 }
1251 start_index = parseInt(args[i]);
1252 // The user input start index starts at 1:
1253 if (start_index <= 0) {
1254 throw new Error('Invalid index ' + args[i] + '.');
1255 }
1256 start_index -= 1;
1257 is_verbose = true;
1258
1259 // Check for t[ype] <type>:
1260 } else if (arg === 'type' || arg === 't') {
1261 i++;
1262 if (args.length < i) {
1263 throw new Error('Missing type after ' + arg + '.');
1264 }
1265 type_filter = args[i];
1266
1267 // Check for space <heap space name>:
1268 } else if (arg === 'space' || arg === 'sp') {
1269 i++;
1270 if (args.length < i) {
1271 throw new Error('Missing space name after ' + arg + '.');
1272 }
1273 space_filter = args[i];
1274
1275 // Check for property <prop name>:
1276 } else if (arg === 'property' || arg === 'prop') {
1277 i++;
1278 if (args.length < i) {
1279 throw new Error('Missing property name after ' + arg + '.');
1280 }
1281 prop_filter = args[i];
1282
1283 } else {
1284 throw new Error('Unknown args at ' + arg + '.');
1285 }
1286 }
1287
1288 // Build the verbose request:
1289 if (is_verbose) {
1290 request = this.createLOLRequest('lol-'+cmd,
1291 start_index,
1292 dump_limit,
1293 is_repeating);
1294 request.arguments.verbose = true;
1295 } else {
1296 request = this.createRequest('lol-'+cmd);
1297 request.arguments = {};
1298 }
1299
1300 request.arguments.filter = {};
1301 if (type_filter) {
1302 request.arguments.filter.type = type_filter;
1303 }
1304 if (space_filter) {
1305 request.arguments.filter.space = space_filter;
1306 }
1307 if (prop_filter) {
1308 request.arguments.filter.prop = prop_filter;
1309 }
1310
1311 return request;
1312}
1313
1314
1315function extractObjId(args) {
1316 var id = args;
1317 id = id.match(/^@([0-9]+)$/);
1318 if (id) {
1319 id = id[1];
1320 } else {
1321 throw new Error('Invalid obj id ' + args + '.');
1322 }
1323 return parseInt(id);
1324}
1325
1326
1327DebugRequest.prototype.lolToJSONRequest_ = function(args, is_repeating) {
1328 var request;
1329 // Use default command if one is not specified:
1330 if (!args) {
1331 args = 'info';
1332 }
1333
1334 var orig_args = args;
1335 var first_arg_index;
1336
1337 var arg, i;
1338 var args = args.split(/\s+/g);
1339 var cmd = args[0];
1340 var id;
1341
1342 // Command: <id> [v[erbose]] ...
1343 if (cmd.match(/^[0-9]+$/)) {
1344 // Convert to the padded list command:
1345 // Command: l[ist] <dummy> <id> [v[erbose]] ...
1346
1347 // Insert the implicit 'list' in front and process as normal:
1348 cmd = 'list';
1349 args.unshift(cmd);
1350 }
1351
1352 switch(cmd) {
1353 // Command: c[apture]
1354 case 'capture':
1355 case 'c':
1356 request = this.createRequest('lol-capture');
1357 break;
1358
1359 // Command: clear|d[elete] <id>|all
1360 case 'clear':
1361 case 'delete':
1362 case 'del': {
1363 if (args.length < 2) {
1364 throw new Error('Missing argument after ' + cmd + '.');
1365 } else if (args.length > 2) {
1366 throw new Error('Too many arguments after ' + cmd + '.');
1367 }
1368 id = args[1];
1369 if (id.match(/^[0-9]+$/)) {
1370 // Delete a specific lol record:
1371 request = this.createRequest('lol-delete');
1372 request.arguments = {};
1373 request.arguments.id = parseInt(id);
1374 } else if (id === 'all') {
1375 // Delete all:
1376 request = this.createRequest('lol-reset');
1377 } else {
1378 throw new Error('Invalid argument after ' + cmd + '.');
1379 }
1380 break;
1381 }
1382
1383 // Command: diff <id1> <id2> [<dump options>]
1384 case 'diff':
1385 first_arg_index = 3;
1386
1387 // Command: list <dummy> <id> [<dump options>]
1388 case 'list':
1389
1390 // Command: ret[ainers] <obj id> [<dump options>]
1391 case 'retainers':
1392 case 'ret':
1393 case 'retaining-paths':
1394 case 'rp': {
1395 if (cmd === 'ret') cmd = 'retainers';
1396 else if (cmd === 'rp') cmd = 'retaining-paths';
1397
1398 if (!first_arg_index) first_arg_index = 2;
1399
1400 if (args.length < first_arg_index) {
1401 throw new Error('Too few arguments after ' + cmd + '.');
1402 }
1403
1404 var request_cmd = (cmd === 'list') ? 'diff':cmd;
1405 request = this.lolMakeListRequest(request_cmd,
1406 args,
1407 first_arg_index,
1408 is_repeating);
1409
1410 if (cmd === 'diff') {
1411 request.arguments.id1 = parseInt(args[1]);
1412 request.arguments.id2 = parseInt(args[2]);
1413 } else if (cmd == 'list') {
1414 request.arguments.id1 = 0;
1415 request.arguments.id2 = parseInt(args[1]);
1416 } else {
1417 request.arguments.id = extractObjId(args[1]);
1418 }
1419 break;
1420 }
1421
1422 // Command: getid
1423 case 'getid': {
1424 request = this.createRequest('lol-getid');
1425 request.arguments = {};
1426 request.arguments.address = args[1];
1427 break;
1428 }
1429
1430 // Command: inf[o] [<N>]
1431 case 'info':
1432 case 'inf': {
1433 if (args.length > 2) {
1434 throw new Error('Too many arguments after ' + cmd + '.');
1435 }
1436 // Built the info request:
1437 request = this.createLOLRequest('lol-info', 0, args[1], is_repeating);
1438 break;
1439 }
1440
1441 // Command: path <obj id 1> <obj id 2>
1442 case 'path': {
1443 request = this.createRequest('lol-path');
1444 request.arguments = {};
1445 if (args.length > 2) {
1446 request.arguments.id1 = extractObjId(args[1]);
1447 request.arguments.id2 = extractObjId(args[2]);
1448 } else {
1449 request.arguments.id1 = 0;
1450 request.arguments.id2 = extractObjId(args[1]);
1451 }
1452 break;
1453 }
1454
1455 // Command: print
1456 case 'print': {
1457 request = this.createRequest('lol-print');
1458 request.arguments = {};
1459 request.arguments.id = extractObjId(args[1]);
1460 break;
1461 }
1462
1463 // Command: reset
1464 case 'reset': {
1465 request = this.createRequest('lol-reset');
1466 break;
1467 }
1468
1469 default:
1470 throw new Error('Invalid arguments.');
1471 }
1472 return request.toJSONProtocol();
1473};
1474
1475
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001476// Create a JSON request for the threads command.
1477DebugRequest.prototype.threadsCommandToJSONRequest_ = function(args) {
1478 // Build a threads request from the text command.
1479 var request = this.createRequest('threads');
1480 return request.toJSONProtocol();
1481};
1482
1483
iposva@chromium.org245aa852009-02-10 00:49:54 +00001484// Handle the trace command.
1485DebugRequest.prototype.traceCommand_ = function(args) {
1486 // Process arguments.
1487 if (args && args.length > 0) {
1488 if (args == 'compile') {
1489 trace_compile = !trace_compile;
1490 print('Tracing of compiled scripts ' + (trace_compile ? 'on' : 'off'));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001491 } else if (args === 'debug json' || args === 'json' || args === 'packets') {
1492 trace_debug_json = !trace_debug_json;
1493 print('Tracing of debug json packets ' +
1494 (trace_debug_json ? 'on' : 'off'));
iposva@chromium.org245aa852009-02-10 00:49:54 +00001495 } else {
1496 throw new Error('Invalid trace arguments.');
1497 }
1498 } else {
1499 throw new Error('Invalid trace arguments.');
1500 }
1501}
1502
1503// Handle the help command.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001504DebugRequest.prototype.helpCommand_ = function(args) {
1505 // Help os quite simple.
1506 if (args && args.length > 0) {
1507 print('warning: arguments to \'help\' are ignored');
1508 }
1509
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001510 print('Note: <> denotes symbollic values to be replaced with real values.');
1511 print('Note: [] denotes optional parts of commands, or optional options / arguments.');
1512 print(' e.g. d[elete] - you get the same command if you type d or delete.');
1513 print('');
1514 print('[break] - break as soon as possible');
1515 print('b[reak] location [condition]');
1516 print(' - break on named function: location is a function name');
1517 print(' - break on function: location is #<id>#');
1518 print(' - break on script position: location is name:line[:column]');
1519 print('');
1520 print('clear <breakpoint #> - deletes the specified user defined breakpoint');
1521 print('d[elete] <breakpoint #> - deletes the specified user defined breakpoint');
1522 print('dis[able] <breakpoint #> - disables the specified user defined breakpoint');
1523 print('dis[able] exc[eptions] [[all] | unc[aught]]');
1524 print(' - disables breaking on exceptions');
1525 print('en[able] <breakpoint #> - enables the specified user defined breakpoint');
1526 print('en[able] exc[eptions] [[all] | unc[aught]]');
1527 print(' - enables breaking on exceptions');
1528 print('');
1529 print('b[ack]t[race] [n] | [-n] | [from to]');
1530 print(' - prints the stack back trace');
1531 print('f[rame] - prints info about the current frame context');
1532 print('f[rame] <frame #> - set context to specified frame #');
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001533 print('scopes');
1534 print('scope <scope #>');
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001535 print('');
1536 print('up - set context to caller of current frame');
1537 print('do[wn] - set context to callee of current frame');
1538 print('inf[o] br[eak] - prints info about breakpoints in use');
1539 print('inf[o] ar[gs] - prints info about arguments of the current function');
1540 print('inf[o] lo[cals] - prints info about locals in the current function');
1541 print('inf[o] liveobjectlist|lol - same as \'lol info\'');
1542 print('');
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001543 print('step [in | next | out| min [step count]]');
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001544 print('c[ontinue] - continue executing after a breakpoint');
1545 print('s[tep] [<N>] - step into the next N callees (default N is 1)');
1546 print('s[tep]i [<N>] - step into the next N callees (default N is 1)');
1547 print('n[ext] [<N>] - step over the next N callees (default N is 1)');
1548 print('fin[ish] [<N>] - step out of N frames (default N is 1)');
1549 print('');
1550 print('p[rint] <expression> - prints the result of the specified expression');
1551 print('dir <expression> - prints the object structure of the result');
1552 print('set <var> = <expression> - executes the specified statement');
1553 print('');
1554 print('l[ist] - list the source code around for the current pc');
1555 print('l[ist] [- | <start>,<end>] - list the specified range of source code');
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001556 print('source [from line [num lines]]');
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001557 print('scr[ipts] [native|extensions|all]');
1558 print('scr[ipts] [<filter text>] - list scripts with the specified text in its description');
1559 print('');
1560 print('gc - runs the garbage collector');
1561 print('');
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001562
1563 if (lol_is_enabled) {
1564 print('liveobjectlist|lol <command> - live object list tracking.');
1565 print(' where <command> can be:');
1566 print(' c[apture] - captures a LOL list.');
1567 print(' clear|del[ete] <id>|all - clears LOL of id <id>.');
1568 print(' If \'all\' is unspecified instead, will clear all.');
1569 print(' diff <id1> <id2> [<dump options>]');
1570 print(' - prints the diff between LOLs id1 and id2.');
1571 print(' - also see <dump options> below.');
1572 print(' getid <address> - gets the obj id for the specified address if available.');
1573 print(' The address must be in hex form prefixed with 0x.');
1574 print(' inf[o] [<N>] - lists summary info of all LOL lists.');
1575 print(' If N is specified, will print N items at a time.');
1576 print(' [l[ist]] <id> [<dump options>]');
1577 print(' - prints the listing of objects in LOL id.');
1578 print(' - also see <dump options> below.');
1579 print(' reset - clears all LOL lists.');
1580 print(' ret[ainers] <id> [<dump options>]');
1581 print(' - prints the list of retainers of obj id.');
1582 print(' - also see <dump options> below.');
1583 print(' path <id1> <id2> - prints the retaining path from obj id1 to id2.');
1584 print(' If only one id is specified, will print the path from');
1585 print(' roots to the specified object if available.');
1586 print(' print <id> - prints the obj for the specified obj id if available.');
1587 print('');
1588 print(' <dump options> includes:');
1589 print(' [v[erbose]] - do verbose dump.');
1590 print(' [<N>] - dump N items at a time. Implies verbose dump.');
1591 print(' If unspecified, N will default to '+
1592 kDefaultLolLinesToPrintAtATime+'. Max N is '+
1593 kMaxLolLinesToPrintAtATime+'.');
1594 print(' [i[ndex] <i>] - start dump from index i. Implies verbose dump.');
1595 print(' [t[ype] <type>] - filter by type.');
1596 print(' [sp[ace] <space name>] - filter by heap space where <space name> is one of');
1597 print(' { cell, code, lo, map, new, old-data, old-pointer }.');
1598 print('');
1599 print(' If the verbose option, or an option that implies a verbose dump');
1600 print(' is specified, then a verbose dump will requested. Else, a summary dump');
1601 print(' will be requested.');
1602 print('');
1603 }
1604
iposva@chromium.org245aa852009-02-10 00:49:54 +00001605 print('trace compile');
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001606 // hidden command: trace debug json - toggles tracing of debug json packets
1607 print('');
1608 print('disconnect|exit|quit - disconnects and quits the debugger');
1609 print('help - prints this help information');
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001610}
1611
1612
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001613function formatHandleReference_(value) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001614 if (value.handle() >= 0) {
1615 return '#' + value.handle() + '#';
1616 } else {
1617 return '#Transient#';
1618 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001619}
1620
1621
iposva@chromium.org245aa852009-02-10 00:49:54 +00001622function formatObject_(value, include_properties) {
1623 var result = '';
1624 result += formatHandleReference_(value);
1625 result += ', type: object'
1626 result += ', constructor ';
1627 var ctor = value.constructorFunctionValue();
1628 result += formatHandleReference_(ctor);
1629 result += ', __proto__ ';
1630 var proto = value.protoObjectValue();
1631 result += formatHandleReference_(proto);
1632 result += ', ';
1633 result += value.propertyCount();
1634 result += ' properties.';
1635 if (include_properties) {
1636 result += '\n';
1637 for (var i = 0; i < value.propertyCount(); i++) {
1638 result += ' ';
1639 result += value.propertyName(i);
1640 result += ': ';
1641 var property_value = value.propertyValue(i);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001642 if (property_value instanceof ProtocolReference) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001643 result += '<no type>';
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001644 } else {
1645 if (property_value && property_value.type()) {
1646 result += property_value.type();
1647 } else {
1648 result += '<no type>';
1649 }
iposva@chromium.org245aa852009-02-10 00:49:54 +00001650 }
1651 result += ' ';
1652 result += formatHandleReference_(property_value);
1653 result += '\n';
1654 }
1655 }
1656 return result;
1657}
1658
1659
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001660function formatScope_(scope) {
1661 var result = '';
1662 var index = scope.index;
1663 result += '#' + (index <= 9 ? '0' : '') + index;
1664 result += ' ';
1665 switch (scope.type) {
1666 case Debug.ScopeType.Global:
1667 result += 'Global, ';
1668 result += '#' + scope.object.ref + '#';
1669 break;
1670 case Debug.ScopeType.Local:
1671 result += 'Local';
1672 break;
1673 case Debug.ScopeType.With:
1674 result += 'With, ';
1675 result += '#' + scope.object.ref + '#';
1676 break;
ager@chromium.orga1645e22009-09-09 19:27:10 +00001677 case Debug.ScopeType.Catch:
1678 result += 'Catch, ';
1679 result += '#' + scope.object.ref + '#';
1680 break;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001681 case Debug.ScopeType.Closure:
1682 result += 'Closure';
1683 break;
1684 default:
1685 result += 'UNKNOWN';
1686 }
1687 return result;
1688}
1689
1690
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001691function refObjectToString_(protocolPackage, handle) {
1692 var value = protocolPackage.lookup(handle);
1693 var result = '';
1694 if (value.isString()) {
1695 result = '"' + value.value() + '"';
1696 } else if (value.isPrimitive()) {
1697 result = value.valueString();
1698 } else if (value.isObject()) {
1699 result += formatObject_(value, true);
1700 }
1701 return result;
1702}
1703
1704
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001705function decodeLolCaptureResponse(body) {
1706 var result;
1707 result = 'Captured live object list '+ body.id +
1708 ': count '+ body.count + ' size ' + body.size;
1709 return result;
1710}
1711
1712
1713function decodeLolDeleteResponse(body) {
1714 var result;
1715 result = 'Deleted live object list '+ body.id;
1716 return result;
1717}
1718
1719
1720function digitsIn(value) {
1721 var digits = 0;
1722 if (value === 0) value = 1;
1723 while (value >= 1) {
1724 digits++;
1725 value /= 10;
1726 }
1727 return digits;
1728}
1729
1730
1731function padding(value, max_digits) {
1732 var padding_digits = max_digits - digitsIn(value);
1733 var padding = '';
1734 while (padding_digits > 0) {
1735 padding += ' ';
1736 padding_digits--;
1737 }
1738 return padding;
1739}
1740
1741
1742function decodeLolInfoResponse(body) {
1743 var result;
1744 var lists = body.lists;
1745 var length = lists.length;
1746 var first_index = body.first_index + 1;
1747 var has_more = ((first_index + length) <= body.count);
1748 result = 'captured live object lists';
1749 if (has_more || (first_index != 1)) {
1750 result += ' ['+ length +' of '+ body.count +
1751 ': starting from '+ first_index +']';
1752 }
1753 result += ':\n';
1754 var max_digits = digitsIn(body.count);
1755 var last_count = 0;
1756 var last_size = 0;
1757 for (var i = 0; i < length; i++) {
1758 var entry = lists[i];
1759 var count = entry.count;
1760 var size = entry.size;
1761 var index = first_index + i;
1762 result += ' [' + padding(index, max_digits) + index + '] id '+ entry.id +
1763 ': count '+ count;
1764 if (last_count > 0) {
1765 result += '(+' + (count - last_count) + ')';
1766 }
1767 result += ' size '+ size;
1768 if (last_size > 0) {
1769 result += '(+' + (size - last_size) + ')';
1770 }
1771 result += '\n';
1772 last_count = count;
1773 last_size = size;
1774 }
1775 result += ' total: '+length+' lists\n';
1776 if (has_more) {
1777 result += ' -- press <enter> for more --\n';
1778 } else {
1779 repeat_cmd_line = '';
1780 }
1781 if (length === 0) result += ' none\n';
1782
1783 return result;
1784}
1785
1786
1787function decodeLolListResponse(body, title) {
1788
1789 var result;
1790 var total_count = body.count;
1791 var total_size = body.size;
1792 var length;
1793 var max_digits;
1794 var i;
1795 var entry;
1796 var index;
1797
1798 var max_count_digits = digitsIn(total_count);
1799 var max_size_digits;
1800
1801 var summary = body.summary;
1802 if (summary) {
1803
1804 var roots_count = 0;
1805 var found_root = body.found_root || 0;
1806 var found_weak_root = body.found_weak_root || 0;
1807
1808 // Print the summary result:
1809 result = 'summary of objects:\n';
1810 length = summary.length;
1811 if (found_root !== 0) {
1812 roots_count++;
1813 }
1814 if (found_weak_root !== 0) {
1815 roots_count++;
1816 }
1817 max_digits = digitsIn(length + roots_count);
1818 max_size_digits = digitsIn(total_size);
1819
1820 index = 1;
1821 if (found_root !== 0) {
1822 result += ' [' + padding(index, max_digits) + index + '] ' +
1823 ' count '+ 1 + padding(0, max_count_digits) +
1824 ' '+ padding(0, max_size_digits+1) +
1825 ' : <root>\n';
1826 index++;
1827 }
1828 if (found_weak_root !== 0) {
1829 result += ' [' + padding(index, max_digits) + index + '] ' +
1830 ' count '+ 1 + padding(0, max_count_digits) +
1831 ' '+ padding(0, max_size_digits+1) +
1832 ' : <weak root>\n';
1833 index++;
1834 }
1835
1836 for (i = 0; i < length; i++) {
1837 entry = summary[i];
1838 var count = entry.count;
1839 var size = entry.size;
1840 result += ' [' + padding(index, max_digits) + index + '] ' +
1841 ' count '+ count + padding(count, max_count_digits) +
1842 ' size '+ size + padding(size, max_size_digits) +
1843 ' : <' + entry.desc + '>\n';
1844 index++;
1845 }
1846 result += '\n total count: '+(total_count+roots_count)+'\n';
1847 if (body.size) {
1848 result += ' total size: '+body.size+'\n';
1849 }
1850
1851 } else {
1852 // Print the full dump result:
1853 var first_index = body.first_index + 1;
1854 var elements = body.elements;
1855 length = elements.length;
1856 var has_more = ((first_index + length) <= total_count);
1857 result = title;
1858 if (has_more || (first_index != 1)) {
1859 result += ' ['+ length +' of '+ total_count +
1860 ': starting from '+ first_index +']';
1861 }
1862 result += ':\n';
1863 if (length === 0) result += ' none\n';
1864 max_digits = digitsIn(length);
1865
1866 var max_id = 0;
1867 var max_size = 0;
1868 for (i = 0; i < length; i++) {
1869 entry = elements[i];
1870 if (entry.id > max_id) max_id = entry.id;
1871 if (entry.size > max_size) max_size = entry.size;
1872 }
1873 var max_id_digits = digitsIn(max_id);
1874 max_size_digits = digitsIn(max_size);
1875
1876 for (i = 0; i < length; i++) {
1877 entry = elements[i];
1878 index = first_index + i;
1879 result += ' ['+ padding(index, max_digits) + index +']';
1880 if (entry.id !== 0) {
1881 result += ' @' + entry.id + padding(entry.id, max_id_digits) +
1882 ': size ' + entry.size + ', ' +
1883 padding(entry.size, max_size_digits) + entry.desc + '\n';
1884 } else {
1885 // Must be a root or weak root:
1886 result += ' ' + entry.desc + '\n';
1887 }
1888 }
1889 if (has_more) {
1890 result += ' -- press <enter> for more --\n';
1891 } else {
1892 repeat_cmd_line = '';
1893 }
1894 if (length === 0) result += ' none\n';
1895 }
1896
1897 return result;
1898}
1899
1900
1901function decodeLolDiffResponse(body) {
1902 var title = 'objects';
1903 return decodeLolListResponse(body, title);
1904}
1905
1906
1907function decodeLolRetainersResponse(body) {
1908 var title = 'retainers for @' + body.id;
1909 return decodeLolListResponse(body, title);
1910}
1911
1912
1913function decodeLolPathResponse(body) {
1914 return body.path;
1915}
1916
1917
1918function decodeLolResetResponse(body) {
1919 return 'Reset all live object lists.';
1920}
1921
1922
1923function decodeLolGetIdResponse(body) {
1924 if (body.id == 0) {
1925 return 'Address is invalid, or object has been moved or collected';
1926 }
1927 return 'obj id is @' + body.id;
1928}
1929
1930
1931function decodeLolPrintResponse(body) {
1932 return body.dump;
1933}
1934
1935
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001936// Rounds number 'num' to 'length' decimal places.
1937function roundNumber(num, length) {
1938 var factor = Math.pow(10, length);
1939 return Math.round(num * factor) / factor;
1940}
1941
1942
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001943// Convert a JSON response to text for display in a text based debugger.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001944function DebugResponseDetails(response) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001945 details = {text:'', running:false}
1946
1947 try {
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001948 if (!response.success()) {
1949 details.text = response.message();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001950 return details;
1951 }
1952
1953 // Get the running state.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001954 details.running = response.running();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001955
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001956 var body = response.body();
1957 var result = '';
1958 switch (response.command()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001959 case 'suspend':
1960 details.text = 'stopped';
1961 break;
vegorov@chromium.org42841962010-10-18 11:18:59 +00001962
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001963 case 'setbreakpoint':
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001964 result = 'set breakpoint #';
1965 result += body.breakpoint;
1966 details.text = result;
1967 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +00001968
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001969 case 'clearbreakpoint':
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001970 result = 'cleared breakpoint #';
1971 result += body.breakpoint;
1972 details.text = result;
1973 break;
vegorov@chromium.org42841962010-10-18 11:18:59 +00001974
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001975 case 'changebreakpoint':
1976 result = 'successfully changed breakpoint';
1977 details.text = result;
1978 break;
1979
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001980 case 'listbreakpoints':
1981 result = 'breakpoints: (' + body.breakpoints.length + ')';
1982 for (var i = 0; i < body.breakpoints.length; i++) {
1983 var breakpoint = body.breakpoints[i];
1984 result += '\n id=' + breakpoint.number;
1985 result += ' type=' + breakpoint.type;
1986 if (breakpoint.script_id) {
1987 result += ' script_id=' + breakpoint.script_id;
1988 }
1989 if (breakpoint.script_name) {
1990 result += ' script_name=' + breakpoint.script_name;
1991 }
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00001992 if (breakpoint.script_regexp) {
1993 result += ' script_regexp=' + breakpoint.script_regexp;
1994 }
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001995 result += ' line=' + (breakpoint.line + 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001996 if (breakpoint.column != null) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001997 result += ' column=' + (breakpoint.column + 1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001998 }
1999 if (breakpoint.groupId) {
2000 result += ' groupId=' + breakpoint.groupId;
2001 }
2002 if (breakpoint.ignoreCount) {
2003 result += ' ignoreCount=' + breakpoint.ignoreCount;
2004 }
2005 if (breakpoint.active === false) {
2006 result += ' inactive';
2007 }
2008 if (breakpoint.condition) {
2009 result += ' condition=' + breakpoint.condition;
2010 }
2011 result += ' hit_count=' + breakpoint.hit_count;
2012 }
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002013 if (body.breakpoints.length === 0) {
2014 result = "No user defined breakpoints\n";
2015 } else {
2016 result += '\n';
2017 }
2018 if (body.breakOnExceptions) {
2019 result += '* breaking on ALL exceptions is enabled\n';
2020 } else if (body.breakOnUncaughtExceptions) {
2021 result += '* breaking on UNCAUGHT exceptions is enabled\n';
2022 } else {
2023 result += '* all exception breakpoints are disabled\n';
2024 }
2025 details.text = result;
2026 break;
2027
2028 case 'setexceptionbreak':
2029 result = 'Break on ' + body.type + ' exceptions: ';
2030 result += body.enabled ? 'enabled' : 'disabled';
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002031 details.text = result;
2032 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +00002033
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002034 case 'backtrace':
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002035 if (body.totalFrames == 0) {
2036 result = '(empty stack)';
2037 } else {
2038 var result = 'Frames #' + body.fromFrame + ' to #' +
2039 (body.toFrame - 1) + ' of ' + body.totalFrames + '\n';
2040 for (i = 0; i < body.frames.length; i++) {
2041 if (i != 0) result += '\n';
2042 result += body.frames[i].text;
2043 }
2044 }
2045 details.text = result;
2046 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +00002047
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002048 case 'frame':
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002049 if (last_cmd === 'info locals') {
2050 var locals = body.locals;
2051 if (locals.length === 0) {
2052 result = 'No locals';
2053 } else {
2054 for (var i = 0; i < locals.length; i++) {
2055 var local = locals[i];
2056 result += local.name + ' = ';
2057 result += refObjectToString_(response, local.value.ref);
2058 result += '\n';
2059 }
2060 }
2061 } else if (last_cmd === 'info args') {
2062 var args = body.arguments;
2063 if (args.length === 0) {
2064 result = 'No arguments';
2065 } else {
2066 for (var i = 0; i < args.length; i++) {
2067 var arg = args[i];
2068 result += arg.name + ' = ';
2069 result += refObjectToString_(response, arg.value.ref);
2070 result += '\n';
2071 }
2072 }
2073 } else {
2074 result = SourceUnderline(body.sourceLineText,
2075 body.column);
2076 Debug.State.currentSourceLine = body.line;
2077 Debug.State.currentFrame = body.index;
2078 Debug.State.displaySourceStartLine = -1;
2079 Debug.State.displaySourceEndLine = -1;
2080 }
2081 details.text = result;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002082 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +00002083
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002084 case 'scopes':
2085 if (body.totalScopes == 0) {
2086 result = '(no scopes)';
2087 } else {
2088 result = 'Scopes #' + body.fromScope + ' to #' +
2089 (body.toScope - 1) + ' of ' + body.totalScopes + '\n';
2090 for (i = 0; i < body.scopes.length; i++) {
2091 if (i != 0) {
2092 result += '\n';
2093 }
2094 result += formatScope_(body.scopes[i]);
2095 }
2096 }
2097 details.text = result;
2098 break;
2099
2100 case 'scope':
2101 result += formatScope_(body);
2102 result += '\n';
2103 var scope_object_value = response.lookup(body.object.ref);
2104 result += formatObject_(scope_object_value, true);
2105 details.text = result;
2106 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +00002107
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002108 case 'evaluate':
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002109 case 'lookup':
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002110 case 'getobj':
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002111 if (last_cmd == 'p' || last_cmd == 'print') {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002112 result = body.text;
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002113 } else {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002114 var value;
2115 if (lookup_handle) {
2116 value = response.bodyValue(lookup_handle);
2117 } else {
2118 value = response.bodyValue();
2119 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002120 if (value.isObject()) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00002121 result += formatObject_(value, true);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002122 } else {
2123 result += 'type: ';
2124 result += value.type();
2125 if (!value.isUndefined() && !value.isNull()) {
2126 result += ', ';
2127 if (value.isString()) {
2128 result += '"';
2129 }
2130 result += value.value();
2131 if (value.isString()) {
2132 result += '"';
2133 }
2134 }
2135 result += '\n';
2136 }
2137 }
2138 details.text = result;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002139 break;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002140
2141 case 'references':
2142 var count = body.length;
2143 result += 'found ' + count + ' objects';
2144 result += '\n';
2145 for (var i = 0; i < count; i++) {
2146 var value = response.bodyValue(i);
2147 result += formatObject_(value, false);
2148 result += '\n';
2149 }
2150 details.text = result;
2151 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +00002152
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002153 case 'source':
2154 // Get the source from the response.
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002155 var source = body.source;
2156 var from_line = body.fromLine + 1;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002157 var lines = source.split('\n');
2158 var maxdigits = 1 + Math.floor(log10(from_line + lines.length));
2159 if (maxdigits < 3) {
2160 maxdigits = 3;
2161 }
2162 var result = '';
2163 for (var num = 0; num < lines.length; num++) {
2164 // Check if there's an extra newline at the end.
2165 if (num == (lines.length - 1) && lines[num].length == 0) {
2166 break;
2167 }
2168
2169 var current_line = from_line + num;
2170 spacer = maxdigits - (1 + Math.floor(log10(current_line)));
2171 if (current_line == Debug.State.currentSourceLine + 1) {
2172 for (var i = 0; i < maxdigits; i++) {
2173 result += '>';
2174 }
2175 result += ' ';
2176 } else {
2177 for (var i = 0; i < spacer; i++) {
2178 result += ' ';
2179 }
2180 result += current_line + ': ';
2181 }
2182 result += lines[num];
2183 result += '\n';
2184 }
2185 details.text = result;
2186 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +00002187
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002188 case 'scripts':
2189 var result = '';
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002190 for (i = 0; i < body.length; i++) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002191 if (i != 0) result += '\n';
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002192 if (body[i].id) {
2193 result += body[i].id;
2194 } else {
2195 result += '[no id]';
2196 }
2197 result += ', ';
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002198 if (body[i].name) {
2199 result += body[i].name;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002200 } else {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002201 if (body[i].compilationType == Debug.ScriptCompilationType.Eval
2202 && body[i].evalFromScript
2203 ) {
ager@chromium.orge2902be2009-06-08 12:21:35 +00002204 result += 'eval from ';
2205 var script_value = response.lookup(body[i].evalFromScript.ref);
2206 result += ' ' + script_value.field('name');
2207 result += ':' + (body[i].evalFromLocation.line + 1);
2208 result += ':' + body[i].evalFromLocation.column;
2209 } else if (body[i].compilationType ==
2210 Debug.ScriptCompilationType.JSON) {
2211 result += 'JSON ';
2212 } else { // body[i].compilation == Debug.ScriptCompilationType.Host
2213 result += '[unnamed] ';
2214 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002215 }
2216 result += ' (lines: ';
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002217 result += body[i].lineCount;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002218 result += ', length: ';
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002219 result += body[i].sourceLength;
2220 if (body[i].type == Debug.ScriptType.Native) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002221 result += ', native';
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002222 } else if (body[i].type == Debug.ScriptType.Extension) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002223 result += ', extension';
2224 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002225 result += '), [';
2226 var sourceStart = body[i].sourceStart;
2227 if (sourceStart.length > 40) {
2228 sourceStart = sourceStart.substring(0, 37) + '...';
2229 }
2230 result += sourceStart;
2231 result += ']';
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002232 }
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002233 if (body.length == 0) {
2234 result = "no matching scripts found";
2235 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002236 details.text = result;
2237 break;
2238
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002239 case 'threads':
2240 var result = 'Active V8 threads: ' + body.totalThreads + '\n';
2241 body.threads.sort(function(a, b) { return a.id - b.id; });
2242 for (i = 0; i < body.threads.length; i++) {
2243 result += body.threads[i].current ? '*' : ' ';
2244 result += ' ';
2245 result += body.threads[i].id;
2246 result += '\n';
2247 }
2248 details.text = result;
2249 break;
2250
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002251 case 'continue':
2252 details.text = "(running)";
2253 break;
lrn@chromium.org25156de2010-04-06 13:10:27 +00002254
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002255 case 'v8flags':
2256 details.text = "flags set";
2257 break;
2258
2259 case 'gc':
2260 details.text = "GC " + body.before + " => " + body.after;
2261 if (body.after > (1024*1024)) {
2262 details.text +=
2263 " (" + roundNumber(body.before/(1024*1024), 1) + "M => " +
2264 roundNumber(body.after/(1024*1024), 1) + "M)";
2265 } else if (body.after > 1024) {
2266 details.text +=
2267 " (" + roundNumber(body.before/1024, 1) + "K => " +
2268 roundNumber(body.after/1024, 1) + "K)";
2269 }
2270 break;
2271
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002272 case 'lol-capture':
2273 details.text = decodeLolCaptureResponse(body);
2274 break;
2275 case 'lol-delete':
2276 details.text = decodeLolDeleteResponse(body);
2277 break;
2278 case 'lol-diff':
2279 details.text = decodeLolDiffResponse(body);
2280 break;
2281 case 'lol-getid':
2282 details.text = decodeLolGetIdResponse(body);
2283 break;
2284 case 'lol-info':
2285 details.text = decodeLolInfoResponse(body);
2286 break;
2287 case 'lol-print':
2288 details.text = decodeLolPrintResponse(body);
2289 break;
2290 case 'lol-reset':
2291 details.text = decodeLolResetResponse(body);
2292 break;
2293 case 'lol-retainers':
2294 details.text = decodeLolRetainersResponse(body);
2295 break;
2296 case 'lol-path':
2297 details.text = decodeLolPathResponse(body);
2298 break;
2299
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002300 default:
2301 details.text =
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002302 'Response for unknown command \'' + response.command() + '\'' +
2303 ' (' + response.raw_json() + ')';
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002304 }
2305 } catch (e) {
2306 details.text = 'Error: "' + e + '" formatting response';
2307 }
lrn@chromium.org25156de2010-04-06 13:10:27 +00002308
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002309 return details;
2310};
2311
2312
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002313/**
2314 * Protocol packages send from the debugger.
2315 * @param {string} json - raw protocol packet as JSON string.
2316 * @constructor
2317 */
2318function ProtocolPackage(json) {
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002319 this.raw_json_ = json;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002320 this.packet_ = JSON.parse(json);
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002321 this.refs_ = [];
2322 if (this.packet_.refs) {
2323 for (var i = 0; i < this.packet_.refs.length; i++) {
2324 this.refs_[this.packet_.refs[i].handle] = this.packet_.refs[i];
2325 }
2326 }
2327}
2328
2329
2330/**
2331 * Get the packet type.
2332 * @return {String} the packet type
2333 */
2334ProtocolPackage.prototype.type = function() {
2335 return this.packet_.type;
2336}
2337
2338
2339/**
2340 * Get the packet event.
2341 * @return {Object} the packet event
2342 */
2343ProtocolPackage.prototype.event = function() {
2344 return this.packet_.event;
2345}
2346
2347
2348/**
2349 * Get the packet request sequence.
2350 * @return {number} the packet request sequence
2351 */
2352ProtocolPackage.prototype.requestSeq = function() {
2353 return this.packet_.request_seq;
2354}
2355
2356
2357/**
2358 * Get the packet request sequence.
2359 * @return {number} the packet request sequence
2360 */
2361ProtocolPackage.prototype.running = function() {
2362 return this.packet_.running ? true : false;
2363}
2364
2365
2366ProtocolPackage.prototype.success = function() {
2367 return this.packet_.success ? true : false;
2368}
2369
2370
2371ProtocolPackage.prototype.message = function() {
2372 return this.packet_.message;
2373}
2374
2375
2376ProtocolPackage.prototype.command = function() {
2377 return this.packet_.command;
2378}
2379
2380
2381ProtocolPackage.prototype.body = function() {
2382 return this.packet_.body;
2383}
2384
2385
iposva@chromium.org245aa852009-02-10 00:49:54 +00002386ProtocolPackage.prototype.bodyValue = function(index) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002387 if (index != null) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00002388 return new ProtocolValue(this.packet_.body[index], this);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002389 } else {
2390 return new ProtocolValue(this.packet_.body, this);
iposva@chromium.org245aa852009-02-10 00:49:54 +00002391 }
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002392}
2393
2394
2395ProtocolPackage.prototype.body = function() {
2396 return this.packet_.body;
2397}
2398
2399
2400ProtocolPackage.prototype.lookup = function(handle) {
2401 var value = this.refs_[handle];
2402 if (value) {
2403 return new ProtocolValue(value, this);
2404 } else {
2405 return new ProtocolReference(handle);
2406 }
2407}
2408
2409
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00002410ProtocolPackage.prototype.raw_json = function() {
2411 return this.raw_json_;
2412}
2413
2414
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002415function ProtocolValue(value, packet) {
2416 this.value_ = value;
2417 this.packet_ = packet;
2418}
2419
2420
2421/**
2422 * Get the value type.
2423 * @return {String} the value type
2424 */
2425ProtocolValue.prototype.type = function() {
2426 return this.value_.type;
2427}
2428
2429
2430/**
lrn@chromium.org25156de2010-04-06 13:10:27 +00002431 * Get a metadata field from a protocol value.
ager@chromium.orge2902be2009-06-08 12:21:35 +00002432 * @return {Object} the metadata field value
2433 */
2434ProtocolValue.prototype.field = function(name) {
2435 return this.value_[name];
2436}
2437
2438
2439/**
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002440 * Check is the value is a primitive value.
2441 * @return {boolean} true if the value is primitive
2442 */
2443ProtocolValue.prototype.isPrimitive = function() {
2444 return this.isUndefined() || this.isNull() || this.isBoolean() ||
2445 this.isNumber() || this.isString();
2446}
2447
2448
2449/**
2450 * Get the object handle.
2451 * @return {number} the value handle
2452 */
2453ProtocolValue.prototype.handle = function() {
2454 return this.value_.handle;
2455}
2456
2457
2458/**
2459 * Check is the value is undefined.
2460 * @return {boolean} true if the value is undefined
2461 */
2462ProtocolValue.prototype.isUndefined = function() {
2463 return this.value_.type == 'undefined';
2464}
2465
2466
2467/**
2468 * Check is the value is null.
2469 * @return {boolean} true if the value is null
2470 */
2471ProtocolValue.prototype.isNull = function() {
2472 return this.value_.type == 'null';
2473}
2474
2475
2476/**
2477 * Check is the value is a boolean.
2478 * @return {boolean} true if the value is a boolean
2479 */
2480ProtocolValue.prototype.isBoolean = function() {
2481 return this.value_.type == 'boolean';
2482}
2483
2484
2485/**
2486 * Check is the value is a number.
2487 * @return {boolean} true if the value is a number
2488 */
2489ProtocolValue.prototype.isNumber = function() {
2490 return this.value_.type == 'number';
2491}
2492
2493
2494/**
2495 * Check is the value is a string.
2496 * @return {boolean} true if the value is a string
2497 */
2498ProtocolValue.prototype.isString = function() {
2499 return this.value_.type == 'string';
2500}
2501
2502
2503/**
2504 * Check is the value is an object.
2505 * @return {boolean} true if the value is an object
2506 */
2507ProtocolValue.prototype.isObject = function() {
2508 return this.value_.type == 'object' || this.value_.type == 'function' ||
2509 this.value_.type == 'error' || this.value_.type == 'regexp';
2510}
2511
2512
2513/**
2514 * Get the constructor function
2515 * @return {ProtocolValue} constructor function
2516 */
2517ProtocolValue.prototype.constructorFunctionValue = function() {
2518 var ctor = this.value_.constructorFunction;
2519 return this.packet_.lookup(ctor.ref);
2520}
2521
2522
2523/**
2524 * Get the __proto__ value
2525 * @return {ProtocolValue} __proto__ value
2526 */
2527ProtocolValue.prototype.protoObjectValue = function() {
2528 var proto = this.value_.protoObject;
2529 return this.packet_.lookup(proto.ref);
2530}
2531
2532
2533/**
2534 * Get the number og properties.
2535 * @return {number} the number of properties
2536 */
2537ProtocolValue.prototype.propertyCount = function() {
2538 return this.value_.properties ? this.value_.properties.length : 0;
2539}
2540
2541
2542/**
2543 * Get the specified property name.
2544 * @return {string} property name
2545 */
2546ProtocolValue.prototype.propertyName = function(index) {
2547 var property = this.value_.properties[index];
2548 return property.name;
2549}
2550
2551
2552/**
2553 * Return index for the property name.
2554 * @param name The property name to look for
2555 * @return {number} index for the property name
2556 */
2557ProtocolValue.prototype.propertyIndex = function(name) {
2558 for (var i = 0; i < this.propertyCount(); i++) {
2559 if (this.value_.properties[i].name == name) {
2560 return i;
2561 }
2562 }
2563 return null;
2564}
2565
2566
2567/**
2568 * Get the specified property value.
2569 * @return {ProtocolValue} property value
2570 */
2571ProtocolValue.prototype.propertyValue = function(index) {
2572 var property = this.value_.properties[index];
2573 return this.packet_.lookup(property.ref);
2574}
2575
2576
2577/**
2578 * Check is the value is a string.
2579 * @return {boolean} true if the value is a string
2580 */
2581ProtocolValue.prototype.value = function() {
2582 return this.value_.value;
2583}
2584
2585
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002586ProtocolValue.prototype.valueString = function() {
2587 return this.value_.text;
2588}
2589
2590
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002591function ProtocolReference(handle) {
2592 this.handle_ = handle;
2593}
2594
2595
2596ProtocolReference.prototype.handle = function() {
2597 return this.handle_;
2598}
2599
2600
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002601function MakeJSONPair_(name, value) {
2602 return '"' + name + '":' + value;
2603}
2604
2605
2606function ArrayToJSONObject_(content) {
2607 return '{' + content.join(',') + '}';
2608}
2609
2610
2611function ArrayToJSONArray_(content) {
2612 return '[' + content.join(',') + ']';
2613}
2614
2615
2616function BooleanToJSON_(value) {
lrn@chromium.org25156de2010-04-06 13:10:27 +00002617 return String(value);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002618}
2619
2620
2621function NumberToJSON_(value) {
lrn@chromium.org25156de2010-04-06 13:10:27 +00002622 return String(value);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002623}
2624
2625
2626// Mapping of some control characters to avoid the \uXXXX syntax for most
2627// commonly used control cahracters.
2628const ctrlCharMap_ = {
2629 '\b': '\\b',
2630 '\t': '\\t',
2631 '\n': '\\n',
2632 '\f': '\\f',
2633 '\r': '\\r',
2634 '"' : '\\"',
2635 '\\': '\\\\'
2636};
2637
2638
2639// Regular expression testing for ", \ and control characters (0x00 - 0x1F).
2640const ctrlCharTest_ = new RegExp('["\\\\\x00-\x1F]');
2641
2642
2643// Regular expression matching ", \ and control characters (0x00 - 0x1F)
2644// globally.
2645const ctrlCharMatch_ = new RegExp('["\\\\\x00-\x1F]', 'g');
2646
2647
2648/**
2649 * Convert a String to its JSON representation (see http://www.json.org/). To
2650 * avoid depending on the String object this method calls the functions in
2651 * string.js directly and not through the value.
2652 * @param {String} value The String value to format as JSON
2653 * @return {string} JSON formatted String value
2654 */
2655function StringToJSON_(value) {
2656 // Check for" , \ and control characters (0x00 - 0x1F). No need to call
2657 // RegExpTest as ctrlchar is constructed using RegExp.
2658 if (ctrlCharTest_.test(value)) {
2659 // Replace ", \ and control characters (0x00 - 0x1F).
2660 return '"' +
2661 value.replace(ctrlCharMatch_, function (char) {
2662 // Use charmap if possible.
2663 var mapped = ctrlCharMap_[char];
2664 if (mapped) return mapped;
2665 mapped = char.charCodeAt();
2666 // Convert control character to unicode escape sequence.
2667 return '\\u00' +
2668 '0' + // TODO %NumberToRadixString(Math.floor(mapped / 16), 16) +
2669 '0' // TODO %NumberToRadixString(mapped % 16, 16);
2670 })
2671 + '"';
2672 }
2673
2674 // Simple string with no special characters.
2675 return '"' + value + '"';
2676}
2677
2678
2679/**
2680 * Convert a Date to ISO 8601 format. To avoid depending on the Date object
2681 * this method calls the functions in date.js directly and not through the
2682 * value.
2683 * @param {Date} value The Date value to format as JSON
2684 * @return {string} JSON formatted Date value
2685 */
2686function DateToISO8601_(value) {
2687 function f(n) {
2688 return n < 10 ? '0' + n : n;
2689 }
2690 function g(n) {
2691 return n < 10 ? '00' + n : n < 100 ? '0' + n : n;
2692 }
2693 return builtins.GetUTCFullYearFrom(value) + '-' +
2694 f(builtins.GetUTCMonthFrom(value) + 1) + '-' +
2695 f(builtins.GetUTCDateFrom(value)) + 'T' +
2696 f(builtins.GetUTCHoursFrom(value)) + ':' +
2697 f(builtins.GetUTCMinutesFrom(value)) + ':' +
2698 f(builtins.GetUTCSecondsFrom(value)) + '.' +
2699 g(builtins.GetUTCMillisecondsFrom(value)) + 'Z';
2700}
2701
2702
2703/**
2704 * Convert a Date to ISO 8601 format. To avoid depending on the Date object
2705 * this method calls the functions in date.js directly and not through the
2706 * value.
2707 * @param {Date} value The Date value to format as JSON
2708 * @return {string} JSON formatted Date value
2709 */
2710function DateToJSON_(value) {
2711 return '"' + DateToISO8601_(value) + '"';
2712}
2713
2714
2715/**
2716 * Convert an Object to its JSON representation (see http://www.json.org/).
2717 * This implementation simply runs through all string property names and adds
2718 * each property to the JSON representation for some predefined types. For type
2719 * "object" the function calls itself recursively unless the object has the
2720 * function property "toJSONProtocol" in which case that is used. This is not
2721 * a general implementation but sufficient for the debugger. Note that circular
2722 * structures will cause infinite recursion.
2723 * @param {Object} object The object to format as JSON
2724 * @return {string} JSON formatted object value
2725 */
2726function SimpleObjectToJSON_(object) {
2727 var content = [];
2728 for (var key in object) {
2729 // Only consider string keys.
2730 if (typeof key == 'string') {
2731 var property_value = object[key];
2732
2733 // Format the value based on its type.
2734 var property_value_json;
2735 switch (typeof property_value) {
2736 case 'object':
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00002737 if (property_value === null) {
2738 property_value_json = 'null';
2739 } else if (typeof property_value.toJSONProtocol == 'function') {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002740 property_value_json = property_value.toJSONProtocol(true)
2741 } else if (property_value.constructor.name == 'Array'){
2742 property_value_json = SimpleArrayToJSON_(property_value);
2743 } else {
2744 property_value_json = SimpleObjectToJSON_(property_value);
2745 }
2746 break;
2747
2748 case 'boolean':
2749 property_value_json = BooleanToJSON_(property_value);
2750 break;
2751
2752 case 'number':
2753 property_value_json = NumberToJSON_(property_value);
2754 break;
2755
2756 case 'string':
2757 property_value_json = StringToJSON_(property_value);
2758 break;
2759
2760 default:
2761 property_value_json = null;
2762 }
2763
2764 // Add the property if relevant.
2765 if (property_value_json) {
2766 content.push(StringToJSON_(key) + ':' + property_value_json);
2767 }
2768 }
2769 }
2770
2771 // Make JSON object representation.
2772 return '{' + content.join(',') + '}';
2773}
2774
2775
2776/**
2777 * Convert an array to its JSON representation. This is a VERY simple
2778 * implementation just to support what is needed for the debugger.
2779 * @param {Array} arrya The array to format as JSON
2780 * @return {string} JSON formatted array value
2781 */
2782function SimpleArrayToJSON_(array) {
2783 // Make JSON array representation.
2784 var json = '[';
2785 for (var i = 0; i < array.length; i++) {
2786 if (i != 0) {
2787 json += ',';
2788 }
2789 var elem = array[i];
2790 if (elem.toJSONProtocol) {
2791 json += elem.toJSONProtocol(true)
2792 } else if (typeof(elem) === 'object') {
2793 json += SimpleObjectToJSON_(elem);
2794 } else if (typeof(elem) === 'boolean') {
2795 json += BooleanToJSON_(elem);
2796 } else if (typeof(elem) === 'number') {
2797 json += NumberToJSON_(elem);
2798 } else if (typeof(elem) === 'string') {
2799 json += StringToJSON_(elem);
2800 } else {
2801 json += elem;
2802 }
2803 }
2804 json += ']';
2805 return json;
2806}