OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Simple interactive debugger shell that connects to the Dart VM's debugger |
| 6 // connection port. |
| 7 |
| 8 #import("dart:io"); |
| 9 #import("dart:json"); |
| 10 |
| 11 Map<int, Completer> outstandingCommands; |
| 12 |
| 13 Socket vmSock; |
| 14 OutputStream vmStream; |
| 15 int seqNum = 0; |
| 16 |
| 17 bool verbose = false; |
| 18 |
| 19 |
| 20 void printHelp() { |
| 21 print(""" |
| 22 q Quit debugger shell |
| 23 r Resume execution |
| 24 s Single step |
| 25 so Step over |
| 26 si Step into |
| 27 ll List loaded libraries |
| 28 ls <libname> List loaded scripts in library |
| 29 """); |
| 30 } |
| 31 |
| 32 |
| 33 void quitShell() { |
| 34 vmStream.close(); |
| 35 vmSock.close(); |
| 36 stdin.close(); |
| 37 } |
| 38 |
| 39 |
| 40 Future sendCmd(Map<String, Dynamic> cmd) { |
| 41 var completer = new Completer(); |
| 42 assert(cmd["id"] != null); |
| 43 var id = cmd["id"]; |
| 44 outstandingCommands[id] = completer; |
| 45 if (verbose) { |
| 46 print("sending: '${JSON.stringify(cmd)}'"); |
| 47 } |
| 48 vmStream.writeString(JSON.stringify(cmd)); |
| 49 return completer.future; |
| 50 } |
| 51 |
| 52 |
| 53 void processCommand(String cmdLine) { |
| 54 seqNum++; |
| 55 var args = cmdLine.split(' '); |
| 56 if (args.length == 0) { |
| 57 return; |
| 58 } |
| 59 var cmd = args[0]; |
| 60 if (cmd == "r") { |
| 61 var cmd = { "id": seqNum, "command": "resume" }; |
| 62 sendCmd(cmd).then((result) => handleGenericResponse(result)); |
| 63 } else if (cmd == "s") { |
| 64 var cmd = { "id": seqNum, "command": "stepOver" }; |
| 65 sendCmd(cmd).then((result) => handleGenericResponse(result)); |
| 66 } else if (cmd == "si") { |
| 67 var cmd = { "id": seqNum, "command": "stepInto" }; |
| 68 sendCmd(cmd).then((result) => handleGenericResponse(result)); |
| 69 } else if (cmd == "so") { |
| 70 var cmd = { "id": seqNum, "command": "stepOut" }; |
| 71 sendCmd(cmd).then((result) => handleGenericResponse(result)); |
| 72 } else if (cmd == "ll") { |
| 73 var cmd = { "id": seqNum, "command": "getLibraryURLs" }; |
| 74 sendCmd(cmd).then((result) => handleGetLibraryResponse(result)); |
| 75 } else if (cmd == "ls") { |
| 76 if (args.length < 2) { |
| 77 return; |
| 78 } |
| 79 var cmd = { "id": seqNum, "command": "getScriptURLs", |
| 80 "param": { "library": args[1] }}; |
| 81 sendCmd(cmd).then((result) => handleGetScriptsResponse(result)); |
| 82 } else if (cmd == "q") { |
| 83 quitShell(); |
| 84 } else if (cmd == "h") { |
| 85 printHelp(); |
| 86 } else { |
| 87 print("Error: $cmd not understood"); |
| 88 } |
| 89 } |
| 90 |
| 91 |
| 92 void handleGetLibraryResponse(response) { |
| 93 var result = response["result"]; |
| 94 assert(result != null); |
| 95 var urls = result["urls"]; |
| 96 assert(urls != null); |
| 97 assert(urls is List); |
| 98 print("Loaded isolates:"); |
| 99 for (int i = 0; i < urls.length; i++) { |
| 100 print(" $i ${urls[i]}"); |
| 101 } |
| 102 } |
| 103 |
| 104 |
| 105 void handleGetScriptsResponse(response) { |
| 106 var result = response["result"]; |
| 107 assert(result != null); |
| 108 var urls = result["urls"]; |
| 109 assert(urls != null); |
| 110 assert(urls is List); |
| 111 print("Loaded scripts:"); |
| 112 for (int i = 0; i < urls.length; i++) { |
| 113 print(" $i ${urls[i]}"); |
| 114 } |
| 115 } |
| 116 |
| 117 |
| 118 void handleGenericResponse(response) { |
| 119 if (response["error"] != null) { |
| 120 print("Error: ${response["error"]}"); |
| 121 } |
| 122 } |
| 123 |
| 124 |
| 125 void printStackTrace(var trace) { |
| 126 if (trace == null) { |
| 127 return; |
| 128 } |
| 129 var frames = trace["callFrames"]; |
| 130 if (frames is !List) { |
| 131 print("unexpected type for frames parameter $frames"); |
| 132 return; |
| 133 } |
| 134 for (int i = 0; i < frames.length; i++) { |
| 135 var frame = frames[i]; |
| 136 var fname = frame["functionName"]; |
| 137 var url = frame["location"]["url"]; |
| 138 var line = frame["location"]["lineNumber"]; |
| 139 print("$i $fname ($url:$line)"); |
| 140 } |
| 141 } |
| 142 |
| 143 |
| 144 // TODO(hausner): Need to handle the case where we receive only a partial |
| 145 // message from the debugger, e.g. when the message is too big to fit in |
| 146 // one network packet. |
| 147 void processVmData(String s) { |
| 148 if (verbose) print("vm: $s"); |
| 149 var msg = JSON.parse(s); |
| 150 if (msg == null) { |
| 151 return; |
| 152 } |
| 153 var event = msg["event"]; |
| 154 if (event == "paused") { |
| 155 print("VM paused, stack trace:"); |
| 156 printStackTrace(msg["params"]); |
| 157 return; |
| 158 } |
| 159 if (msg["id"] != null) { |
| 160 var id = msg["id"]; |
| 161 if (outstandingCommands.containsKey(id)) { |
| 162 if (msg["error"] != null) { |
| 163 print("VM says: ${msg["error"]}"); |
| 164 } else { |
| 165 var completer = outstandingCommands[id]; |
| 166 completer.complete(msg); |
| 167 } |
| 168 outstandingCommands.remove(id); |
| 169 } |
| 170 } |
| 171 } |
| 172 |
| 173 |
| 174 void main() { |
| 175 outstandingCommands = new Map<int, Completer>(); |
| 176 vmSock = new Socket("127.0.0.1", 5858); |
| 177 vmStream = new SocketOutputStream(vmSock); |
| 178 var stdinStream = new StringInputStream(stdin); |
| 179 stdinStream.onLine = () { |
| 180 processCommand(stdinStream.readLine()); |
| 181 }; |
| 182 var vmInStream = new SocketInputStream(vmSock); |
| 183 vmInStream.onData = () { |
| 184 var s = new String.fromCharCodes(vmInStream.read()); |
| 185 processVmData(s); |
| 186 }; |
| 187 vmInStream.onError = (err) { |
| 188 print("Error in debug connection: $err"); |
| 189 quitShell(); |
| 190 }; |
| 191 vmInStream.onClosed = () { |
| 192 print("VM debugger connection closed"); |
| 193 quitShell(); |
| 194 }; |
| 195 } |
OLD | NEW |