author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 46889 | jdk/src/jdk.jdi/share/classes/com/sun/tools/jdi/TargetVM.java@f0fd7a278d72 |
child 50436 | 48ec3c25fe99 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
45714 | 2 |
* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.jdi; |
|
27 |
||
45714 | 28 |
import java.io.IOException; |
29 |
import java.util.ArrayList; |
|
30 |
import java.util.Collections; |
|
31 |
import java.util.HashMap; |
|
32 |
import java.util.Iterator; |
|
33 |
import java.util.List; |
|
34 |
import java.util.Map; |
|
35 |
||
36 |
import com.sun.jdi.VMDisconnectedException; |
|
37 |
import com.sun.jdi.VirtualMachine; |
|
2 | 38 |
import com.sun.jdi.connect.spi.Connection; |
45714 | 39 |
import com.sun.jdi.event.EventQueue; |
2 | 40 |
import com.sun.jdi.event.EventSet; |
41 |
||
42 |
public class TargetVM implements Runnable { |
|
45714 | 43 |
private Map<String, Packet> waitingQueue = new HashMap<>(32,0.75f); |
46889 | 44 |
private volatile boolean shouldListen = true; |
45714 | 45 |
private List<EventQueue> eventQueues = Collections.synchronizedList(new ArrayList<>(2)); |
2 | 46 |
private VirtualMachineImpl vm; |
47 |
private Connection connection; |
|
48 |
private Thread readerThread; |
|
49 |
private EventController eventController = null; |
|
50 |
private boolean eventsHeld = false; |
|
51 |
||
52 |
/* |
|
53 |
* TO DO: The limit numbers below are somewhat arbitrary and should |
|
54 |
* be configurable in the future. |
|
55 |
*/ |
|
56 |
static private final int OVERLOADED_QUEUE = 2000; |
|
57 |
static private final int UNDERLOADED_QUEUE = 100; |
|
58 |
||
59 |
TargetVM(VirtualMachineImpl vm, Connection connection) { |
|
60 |
this.vm = vm; |
|
61 |
this.connection = connection; |
|
62 |
this.readerThread = new Thread(vm.threadGroupForJDI(), |
|
63 |
this, "JDI Target VM Interface"); |
|
64 |
this.readerThread.setDaemon(true); |
|
65 |
} |
|
66 |
||
67 |
void start() { |
|
68 |
readerThread.start(); |
|
69 |
} |
|
70 |
||
71 |
private void dumpPacket(Packet packet, boolean sending) { |
|
72 |
String direction = sending ? "Sending" : "Receiving"; |
|
73 |
if (sending) { |
|
74 |
vm.printTrace(direction + " Command. id=" + packet.id + |
|
75 |
", length=" + packet.data.length + |
|
76 |
", commandSet=" + packet.cmdSet + |
|
77 |
", command=" + packet.cmd + |
|
78 |
", flags=" + packet.flags); |
|
79 |
} else { |
|
80 |
String type = (packet.flags & Packet.Reply) != 0 ? |
|
81 |
"Reply" : "Event"; |
|
82 |
vm.printTrace(direction + " " + type + ". id=" + packet.id + |
|
83 |
", length=" + packet.data.length + |
|
84 |
", errorCode=" + packet.errorCode + |
|
85 |
", flags=" + packet.flags); |
|
86 |
} |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
14342
diff
changeset
|
87 |
StringBuilder line = new StringBuilder(80); |
2 | 88 |
line.append("0000: "); |
89 |
for (int i = 0; i < packet.data.length; i++) { |
|
90 |
if ((i > 0) && (i % 16 == 0)) { |
|
91 |
vm.printTrace(line.toString()); |
|
92 |
line.setLength(0); |
|
93 |
line.append(String.valueOf(i)); |
|
94 |
line.append(": "); |
|
95 |
int len = line.length(); |
|
96 |
for (int j = 0; j < 6 - len; j++) { |
|
97 |
line.insert(0, '0'); |
|
98 |
} |
|
99 |
} |
|
100 |
int val = 0xff & packet.data[i]; |
|
101 |
String str = Integer.toHexString(val); |
|
102 |
if (str.length() == 1) { |
|
103 |
line.append('0'); |
|
104 |
} |
|
105 |
line.append(str); |
|
106 |
line.append(' '); |
|
107 |
} |
|
108 |
if (line.length() > 6) { |
|
109 |
vm.printTrace(line.toString()); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
public void run() { |
|
114 |
if ((vm.traceFlags & VirtualMachine.TRACE_SENDS) != 0) { |
|
115 |
vm.printTrace("Target VM interface thread running"); |
|
116 |
} |
|
117 |
Packet p=null,p2; |
|
118 |
String idString; |
|
119 |
||
45714 | 120 |
while (shouldListen) { |
2 | 121 |
|
122 |
boolean done = false; |
|
123 |
try { |
|
124 |
byte b[] = connection.readPacket(); |
|
125 |
if (b.length == 0) { |
|
126 |
done = true; |
|
127 |
} |
|
128 |
p = Packet.fromByteArray(b); |
|
129 |
} catch (IOException e) { |
|
130 |
done = true; |
|
131 |
} |
|
132 |
||
133 |
if (done) { |
|
134 |
shouldListen = false; |
|
135 |
try { |
|
136 |
connection.close(); |
|
137 |
} catch (IOException ioe) { } |
|
138 |
break; |
|
139 |
} |
|
140 |
||
141 |
if ((vm.traceFlags & VirtualMachineImpl.TRACE_RAW_RECEIVES) != 0) { |
|
142 |
dumpPacket(p, false); |
|
143 |
} |
|
144 |
||
45714 | 145 |
if ((p.flags & Packet.Reply) == 0) { |
2 | 146 |
// It's a command |
147 |
handleVMCommand(p); |
|
148 |
} else { |
|
149 |
/*if(p.errorCode != Packet.ReplyNoError) { |
|
150 |
System.err.println("Packet " + p.id + " returned failure = " + p.errorCode); |
|
151 |
}*/ |
|
152 |
||
153 |
vm.state().notifyCommandComplete(p.id); |
|
154 |
idString = String.valueOf(p.id); |
|
155 |
||
156 |
synchronized(waitingQueue) { |
|
51 | 157 |
p2 = waitingQueue.get(idString); |
2 | 158 |
|
159 |
if (p2 != null) |
|
160 |
waitingQueue.remove(idString); |
|
161 |
} |
|
162 |
||
45714 | 163 |
if (p2 == null) { |
2 | 164 |
// Whoa! a reply without a sender. Problem. |
165 |
// FIX ME! Need to post an error. |
|
166 |
||
167 |
System.err.println("Recieved reply with no sender!"); |
|
168 |
continue; |
|
169 |
} |
|
170 |
p2.errorCode = p.errorCode; |
|
171 |
p2.data = p.data; |
|
172 |
p2.replied = true; |
|
173 |
||
174 |
synchronized(p2) { |
|
175 |
p2.notify(); |
|
176 |
} |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
// inform the VM mamager that this VM is history |
|
181 |
vm.vmManager.disposeVirtualMachine(vm); |
|
46889 | 182 |
if (eventController != null) { |
183 |
eventController.release(); |
|
184 |
} |
|
2 | 185 |
|
186 |
// close down all the event queues |
|
187 |
// Closing a queue causes a VMDisconnectEvent to |
|
188 |
// be put onto the queue. |
|
189 |
synchronized(eventQueues) { |
|
11277 | 190 |
Iterator<EventQueue> iter = eventQueues.iterator(); |
2 | 191 |
while (iter.hasNext()) { |
192 |
((EventQueueImpl)iter.next()).close(); |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
// indirectly throw VMDisconnectedException to |
|
197 |
// command requesters. |
|
198 |
synchronized(waitingQueue) { |
|
11277 | 199 |
Iterator<Packet> iter = waitingQueue.values().iterator(); |
2 | 200 |
while (iter.hasNext()) { |
11277 | 201 |
Packet packet = iter.next(); |
2 | 202 |
synchronized(packet) { |
203 |
packet.notify(); |
|
204 |
} |
|
205 |
} |
|
206 |
waitingQueue.clear(); |
|
207 |
} |
|
208 |
||
209 |
if ((vm.traceFlags & VirtualMachine.TRACE_SENDS) != 0) { |
|
210 |
vm.printTrace("Target VM interface thread exiting"); |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
protected void handleVMCommand(Packet p) { |
|
215 |
switch (p.cmdSet) { |
|
216 |
case JDWP.Event.COMMAND_SET: |
|
217 |
handleEventCmdSet(p); |
|
218 |
break; |
|
219 |
||
220 |
default: |
|
221 |
System.err.println("Ignoring cmd " + p.id + "/" + |
|
222 |
p.cmdSet + "/" + p.cmd + " from the VM"); |
|
223 |
return; |
|
224 |
} |
|
225 |
} |
|
226 |
||
227 |
/* Events should not be constructed on this thread (the thread |
|
228 |
* which reads all data from the transport). This means that the |
|
229 |
* packet cannot be converted to real JDI objects as that may |
|
230 |
* involve further communications with the back end which would |
|
231 |
* deadlock. |
|
232 |
* |
|
233 |
* Instead the whole packet is passed for lazy eval by a queue |
|
234 |
* reading thread. |
|
235 |
*/ |
|
236 |
protected void handleEventCmdSet(Packet p) { |
|
237 |
EventSet eventSet = new EventSetImpl(vm, p); |
|
45714 | 238 |
queueEventSet(eventSet); |
2 | 239 |
} |
240 |
||
241 |
private EventController eventController() { |
|
242 |
if (eventController == null) { |
|
46889 | 243 |
eventController = new EventController(); |
2 | 244 |
} |
245 |
return eventController; |
|
246 |
} |
|
247 |
||
248 |
private synchronized void controlEventFlow(int maxQueueSize) { |
|
249 |
if (!eventsHeld && (maxQueueSize > OVERLOADED_QUEUE)) { |
|
250 |
eventController().hold(); |
|
251 |
eventsHeld = true; |
|
252 |
} else if (eventsHeld && (maxQueueSize < UNDERLOADED_QUEUE)) { |
|
253 |
eventController().release(); |
|
254 |
eventsHeld = false; |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
void notifyDequeueEventSet() { |
|
259 |
int maxQueueSize = 0; |
|
260 |
synchronized(eventQueues) { |
|
11277 | 261 |
Iterator<EventQueue> iter = eventQueues.iterator(); |
2 | 262 |
while (iter.hasNext()) { |
263 |
EventQueueImpl queue = (EventQueueImpl)iter.next(); |
|
264 |
maxQueueSize = Math.max(maxQueueSize, queue.size()); |
|
265 |
} |
|
266 |
} |
|
267 |
controlEventFlow(maxQueueSize); |
|
268 |
} |
|
269 |
||
270 |
private void queueEventSet(EventSet eventSet) { |
|
271 |
int maxQueueSize = 0; |
|
272 |
||
273 |
synchronized(eventQueues) { |
|
11277 | 274 |
Iterator<EventQueue> iter = eventQueues.iterator(); |
2 | 275 |
while (iter.hasNext()) { |
276 |
EventQueueImpl queue = (EventQueueImpl)iter.next(); |
|
277 |
queue.enqueue(eventSet); |
|
278 |
maxQueueSize = Math.max(maxQueueSize, queue.size()); |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
controlEventFlow(maxQueueSize); |
|
283 |
} |
|
284 |
||
285 |
void send(Packet packet) { |
|
286 |
String id = String.valueOf(packet.id); |
|
287 |
||
288 |
synchronized(waitingQueue) { |
|
289 |
waitingQueue.put(id, packet); |
|
290 |
} |
|
291 |
||
292 |
if ((vm.traceFlags & VirtualMachineImpl.TRACE_RAW_SENDS) != 0) { |
|
293 |
dumpPacket(packet, true); |
|
294 |
} |
|
295 |
||
296 |
try { |
|
297 |
connection.writePacket(packet.toByteArray()); |
|
298 |
} catch (IOException e) { |
|
299 |
throw new VMDisconnectedException(e.getMessage()); |
|
300 |
} |
|
301 |
} |
|
302 |
||
303 |
void waitForReply(Packet packet) { |
|
304 |
synchronized(packet) { |
|
305 |
while ((!packet.replied) && shouldListen) { |
|
306 |
try { packet.wait(); } catch (InterruptedException e) {;} |
|
307 |
} |
|
308 |
||
309 |
if (!packet.replied) { |
|
310 |
throw new VMDisconnectedException(); |
|
311 |
} |
|
312 |
} |
|
313 |
} |
|
314 |
||
315 |
void addEventQueue(EventQueueImpl queue) { |
|
316 |
if ((vm.traceFlags & VirtualMachine.TRACE_EVENTS) != 0) { |
|
317 |
vm.printTrace("New event queue added"); |
|
318 |
} |
|
319 |
eventQueues.add(queue); |
|
320 |
} |
|
321 |
||
322 |
void stopListening() { |
|
323 |
if ((vm.traceFlags & VirtualMachine.TRACE_EVENTS) != 0) { |
|
324 |
vm.printTrace("Target VM i/f closing event queues"); |
|
325 |
} |
|
326 |
shouldListen = false; |
|
327 |
try { |
|
328 |
connection.close(); |
|
329 |
} catch (IOException ioe) { } |
|
330 |
} |
|
331 |
||
46889 | 332 |
private class EventController extends Thread { |
2 | 333 |
int controlRequest = 0; |
334 |
||
46889 | 335 |
EventController() { |
2 | 336 |
super(vm.threadGroupForJDI(), "JDI Event Control Thread"); |
337 |
setDaemon(true); |
|
338 |
setPriority((MAX_PRIORITY + NORM_PRIORITY)/2); |
|
339 |
super.start(); |
|
340 |
} |
|
341 |
||
342 |
synchronized void hold() { |
|
343 |
controlRequest++; |
|
344 |
notifyAll(); |
|
345 |
} |
|
346 |
||
347 |
synchronized void release() { |
|
348 |
controlRequest--; |
|
349 |
notifyAll(); |
|
350 |
} |
|
351 |
||
352 |
public void run() { |
|
353 |
while(true) { |
|
354 |
int currentRequest; |
|
355 |
synchronized(this) { |
|
356 |
while (controlRequest == 0) { |
|
357 |
try {wait();} catch (InterruptedException e) {} |
|
46889 | 358 |
if (!shouldListen) { |
359 |
return; |
|
360 |
} |
|
2 | 361 |
} |
362 |
currentRequest = controlRequest; |
|
363 |
controlRequest = 0; |
|
364 |
} |
|
365 |
try { |
|
366 |
if (currentRequest > 0) { |
|
367 |
JDWP.VirtualMachine.HoldEvents.process(vm); |
|
368 |
} else { |
|
369 |
JDWP.VirtualMachine.ReleaseEvents.process(vm); |
|
370 |
} |
|
371 |
} catch (JDWPException e) { |
|
372 |
/* |
|
373 |
* Don't want to terminate the thread, so the |
|
374 |
* stack trace is printed and we continue. |
|
375 |
*/ |
|
376 |
e.toJDIException().printStackTrace(System.err); |
|
377 |
} |
|
378 |
} |
|
379 |
} |
|
380 |
} |
|
381 |
} |