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) print("sending: '${JSON.stringify(cmd)}'"); | |
siva
2012/05/10 20:08:36
not sure what the Dart style guide says but we usu
hausner
2012/05/10 22:36:28
Done.
| |
46 vmStream.writeString(JSON.stringify(cmd)); | |
47 return completer.future; | |
48 } | |
49 | |
50 | |
51 void processCommand(String cmdLine) { | |
52 seqNum++; | |
53 var args = cmdLine.split(' '); | |
54 if (args.length == 0) { | |
55 return; | |
56 } | |
57 var cmd = args[0]; | |
58 if (cmd == "r") { | |
59 var cmd = { "id": seqNum, "command": "resume" }; | |
60 sendCmd(cmd).then((result) => handleGenericResponse(result)); | |
61 } else if (cmd == "s") { | |
62 var cmd = { "id": seqNum, "command": "stepOver" }; | |
63 sendCmd(cmd).then((result) => handleGenericResponse(result)); | |
64 } else if (cmd == "si") { | |
65 var cmd = { "id": seqNum, "command": "stepInto" }; | |
66 sendCmd(cmd).then((result) => handleGenericResponse(result)); | |
67 } else if (cmd == "so") { | |
68 var cmd = { "id": seqNum, "command": "stepOut" }; | |
69 sendCmd(cmd).then((result) => handleGenericResponse(result)); | |
70 } else if (cmd == "ll") { | |
71 var cmd = { "id": seqNum, "command": "getLibraryURLs" }; | |
72 sendCmd(cmd).then((result) => handleGetLibraryResponse(result)); | |
73 } else if (cmd == "ls") { | |
74 if (args.length < 2) { | |
75 return; | |
76 } | |
77 var cmd = { "id": seqNum, "command": "getScriptURLs", | |
78 "param": { "library": args[1] }}; | |
79 sendCmd(cmd).then((result) => handleGetScriptsResponse(result)); | |
80 } else if (cmd == "q") { | |
81 quitShell(); | |
82 } else if (cmd == "h") { | |
83 printHelp(); | |
84 } else { | |
85 print("Error: $cmd not understood"); | |
86 } | |
87 } | |
88 | |
89 | |
90 void handleGetLibraryResponse(response) { | |
91 var result = response["result"]; | |
92 assert(result != null); | |
93 var urls = result["urls"]; | |
94 assert(urls != null); | |
95 assert(urls is List); | |
96 print("Loaded isolates:"); | |
97 for (int i = 0; i < urls.length; i++) { | |
98 print(" $i ${urls[i]}"); | |
99 } | |
100 } | |
101 | |
102 | |
103 void handleGetScriptsResponse(response) { | |
104 var result = response["result"]; | |
105 assert(result != null); | |
106 var urls = result["urls"]; | |
107 assert(urls != null); | |
108 assert(urls is List); | |
109 print("Loaded scripts:"); | |
110 for (int i = 0; i < urls.length; i++) { | |
111 print(" $i ${urls[i]}"); | |
112 } | |
113 } | |
114 | |
115 | |
116 void handleGenericResponse(response) { | |
117 if (response["error"] != null) { | |
118 print("Error: ${response["error"]}"); | |
119 return; | |
siva
2012/05/10 20:08:36
Is this return necessary?
hausner
2012/05/10 22:36:28
Um, no. It may have been at some time.
| |
120 } | |
121 } | |
122 | |
123 | |
124 void printStackTrace(var trace) { | |
125 if (trace == null) { | |
126 return; | |
127 } | |
128 var frames = trace["callFrames"]; | |
129 if (frames is !List) { | |
130 print("unexpected type for frames parameter $frames"); | |
131 return; | |
132 } | |
133 for (int i = 0; i < frames.length; i++) { | |
134 var frame = frames[i]; | |
135 var fname = frame["functionName"]; | |
136 var url = frame["location"]["url"]; | |
137 var line = frame["location"]["lineNumber"]; | |
138 print("$i $fname ($url:$line)"); | |
139 } | |
140 } | |
141 | |
142 | |
143 // TODO(hausner): Need to handle the case where we receive only a partial | |
144 // message from the debugger, e.g. when the message is too big to fit in | |
145 // one network packet. | |
146 void processVmData(String s) { | |
147 if (verbose) print("vm: $s"); | |
148 var msg = JSON.parse(s); | |
149 if (msg == null) { | |
150 return; | |
151 } | |
152 var event = msg["event"]; | |
153 if (event == "paused") { | |
154 print("VM paused, stack trace:"); | |
155 printStackTrace(msg["params"]); | |
156 return; | |
157 } | |
158 if (msg["id"] != null) { | |
159 var id = msg["id"]; | |
160 if (outstandingCommands.containsKey(id)) { | |
161 if (msg["error"] != null) { | |
162 print("VM says: ${msg["error"]}"); | |
163 } else { | |
164 var completer = outstandingCommands[id]; | |
165 completer.complete(msg); | |
166 } | |
167 outstandingCommands.remove(id); | |
168 } | |
169 } | |
170 } | |
171 | |
172 | |
173 void main() { | |
174 outstandingCommands = new Map<int, Completer>(); | |
175 vmSock = new Socket("127.0.0.1", 5858); | |
176 vmStream = new SocketOutputStream(vmSock); | |
177 StringInputStream stdinStream = new StringInputStream(stdin); | |
siva
2012/05/10 20:08:36
var stdinStream = new ....
hausner
2012/05/10 22:36:28
Slowly weaning off of using types :)
| |
178 stdinStream.onLine = () { | |
179 processCommand(stdinStream.readLine()); | |
180 }; | |
181 var vmInStream = new SocketInputStream(vmSock); | |
182 vmInStream.onData = () { | |
183 var s = new String.fromCharCodes(vmInStream.read()); | |
184 processVmData(s); | |
185 }; | |
186 vmInStream.onError = (err) { | |
187 print("Error in debug connection: $err"); | |
188 quitShell(); | |
189 }; | |
190 vmInStream.onClosed = () { | |
191 print("VM debugger connection closed"); | |
192 quitShell(); | |
193 }; | |
194 } | |
OLD | NEW |