| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Simple interactive debugger shell that connects to the Dart VM's debugger | 5 // Simple interactive debugger shell that connects to the Dart VM's debugger |
| 6 // connection port. | 6 // connection port. |
| 7 | 7 |
| 8 #import("dart:io"); | 8 #import("dart:io"); |
| 9 #import("dart:json"); | 9 #import("dart:json"); |
| 10 | 10 |
| 11 Map<int, Completer> outstandingCommands; | 11 Map<int, Completer> outstandingCommands; |
| 12 | 12 |
| 13 Socket vmSock; | 13 Socket vmSock; |
| 14 String vmData; |
| 14 OutputStream vmStream; | 15 OutputStream vmStream; |
| 15 int seqNum = 0; | 16 int seqNum = 0; |
| 16 | 17 |
| 17 bool verbose = false; | 18 bool verbose = false; |
| 18 | 19 |
| 19 // The current stack trace, while the VM is paused. It's a list | 20 // The current stack trace, while the VM is paused. It's a list |
| 20 // of activation frames. | 21 // of activation frames. |
| 21 List stackTrace; | 22 List stackTrace; |
| 22 | 23 |
| 23 // The current activation frame, while the VM is paused. | 24 // The current activation frame, while the VM is paused. |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 } else { | 310 } else { |
| 310 var completer = outstandingCommands[id]; | 311 var completer = outstandingCommands[id]; |
| 311 completer.complete(msg); | 312 completer.complete(msg); |
| 312 } | 313 } |
| 313 outstandingCommands.remove(id); | 314 outstandingCommands.remove(id); |
| 314 } | 315 } |
| 315 } | 316 } |
| 316 } | 317 } |
| 317 | 318 |
| 318 | 319 |
| 319 // TODO(hausner): Need to handle the case where we receive only a partial | 320 void processVmData(String data) { |
| 320 // message from the debugger, e.g. when the message is too big to fit in | |
| 321 // one network packet. | |
| 322 void processVmData(String s) { | |
| 323 final printMessages = false; | 321 final printMessages = false; |
| 324 int msg_len = JSON.length(s); | 322 if (vmData == null || vmData.length == 0) { |
| 323 vmData = data; |
| 324 } else { |
| 325 vmData = vmData.concat(data); |
| 326 } |
| 327 int msg_len = JSON.length(vmData); |
| 325 if (printMessages && msg_len == 0) { | 328 if (printMessages && msg_len == 0) { |
| 326 print("vm sent illegal or partial json message '$s'"); | 329 print("have partial or illegal json message" |
| 327 quitShell(); | 330 " of ${vmData.length} chars:\n'$vmData'"); |
| 328 return; | 331 return; |
| 329 } | 332 } |
| 330 while (msg_len > 0 && msg_len <= s.length) { | 333 while (msg_len > 0 && msg_len <= vmData.length) { |
| 331 if (msg_len == s.length) { | 334 if (msg_len == vmData.length) { |
| 332 if (printMessages) { print("message: $s"); } | 335 if (printMessages) { print("have one full message:\n$vmData"); } |
| 333 processVmMessage(s); | 336 processVmMessage(vmData); |
| 337 vmData = null; |
| 334 return; | 338 return; |
| 335 } | 339 } |
| 336 if (printMessages) { print("at least one message: '$s'"); } | 340 if (printMessages) { print("at least one message: '$vmData'"); } |
| 337 var msg = s.substring(0, msg_len); | 341 var msg = vmData.substring(0, msg_len); |
| 338 if (printMessages) { print("first message: $msg"); } | 342 if (printMessages) { print("first message: $msg"); } |
| 339 processVmMessage(msg); | 343 processVmMessage(msg); |
| 340 s = s.substring(msg_len); | 344 vmData = vmData.substring(msg_len); |
| 341 msg_len = JSON.length(s); | 345 msg_len = JSON.length(vmData); |
| 342 } | 346 } |
| 343 if (printMessages) { print("leftover vm data '$s'"); } | 347 if (printMessages) { print("leftover vm data '$vmData'"); } |
| 344 } | 348 } |
| 345 | 349 |
| 346 | 350 |
| 347 void main() { | 351 void main() { |
| 348 outstandingCommands = new Map<int, Completer>(); | 352 outstandingCommands = new Map<int, Completer>(); |
| 349 vmSock = new Socket("127.0.0.1", 5858); | 353 vmSock = new Socket("127.0.0.1", 5858); |
| 350 vmStream = new SocketOutputStream(vmSock); | 354 vmStream = new SocketOutputStream(vmSock); |
| 351 var stdinStream = new StringInputStream(stdin); | 355 var stdinStream = new StringInputStream(stdin); |
| 352 stdinStream.onLine = () { | 356 stdinStream.onLine = () { |
| 353 processCommand(stdinStream.readLine()); | 357 processCommand(stdinStream.readLine()); |
| 354 }; | 358 }; |
| 355 var vmInStream = new SocketInputStream(vmSock); | 359 var vmInStream = new SocketInputStream(vmSock); |
| 356 vmInStream.onData = () { | 360 vmInStream.onData = () { |
| 357 var s = new String.fromCharCodes(vmInStream.read()); | 361 var s = new String.fromCharCodes(vmInStream.read()); |
| 358 processVmData(s); | 362 processVmData(s); |
| 359 }; | 363 }; |
| 360 vmInStream.onError = (err) { | 364 vmInStream.onError = (err) { |
| 361 print("Error in debug connection: $err"); | 365 print("Error in debug connection: $err"); |
| 362 quitShell(); | 366 quitShell(); |
| 363 }; | 367 }; |
| 364 vmInStream.onClosed = () { | 368 vmInStream.onClosed = () { |
| 365 print("VM debugger connection closed"); | 369 print("VM debugger connection closed"); |
| 366 quitShell(); | 370 quitShell(); |
| 367 }; | 371 }; |
| 368 } | 372 } |
| OLD | NEW |