author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 45714 | jdk/src/jdk.jdi/share/classes/com/sun/tools/jdi/VMState.java@1820d351198d |
child 52068 | 218b5b64f102 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
45714 | 2 |
* Copyright (c) 1999, 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.lang.ref.WeakReference; |
29 |
import java.util.ArrayList; |
|
30 |
import java.util.Arrays; |
|
31 |
import java.util.Iterator; |
|
32 |
import java.util.List; |
|
2 | 33 |
|
45714 | 34 |
import com.sun.jdi.ThreadGroupReference; |
35 |
import com.sun.jdi.ThreadReference; |
|
36 |
import com.sun.jdi.VirtualMachine; |
|
2 | 37 |
|
38 |
class VMState { |
|
39 |
private final VirtualMachineImpl vm; |
|
40 |
||
41 |
// Listeners |
|
45714 | 42 |
private final List<WeakReference<VMListener>> listeners = new ArrayList<>(); // synchronized (this) |
2 | 43 |
private boolean notifyingListeners = false; // synchronized (this) |
44 |
||
45 |
/* |
|
46 |
* Certain information can be cached only when the entire VM is |
|
47 |
* suspended and there are no pending resumes. The fields below |
|
48 |
* are used to track whether there are pending resumes. (There |
|
49 |
* is an assumption that JDWP command ids are increasing over time.) |
|
50 |
*/ |
|
51 |
private int lastCompletedCommandId = 0; // synchronized (this) |
|
52 |
private int lastResumeCommandId = 0; // synchronized (this) |
|
53 |
||
54 |
// This is cached only while the VM is suspended |
|
55 |
private static class Cache { |
|
56 |
List<ThreadGroupReference> groups = null; // cached Top Level ThreadGroups |
|
57 |
List<ThreadReference> threads = null; // cached Threads |
|
58 |
} |
|
59 |
||
60 |
private Cache cache = null; // synchronized (this) |
|
61 |
private static final Cache markerCache = new Cache(); |
|
62 |
||
63 |
private void disableCache() { |
|
64 |
synchronized (this) { |
|
65 |
cache = null; |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
private void enableCache() { |
|
70 |
synchronized (this) { |
|
71 |
cache = markerCache; |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
private Cache getCache() { |
|
76 |
synchronized (this) { |
|
77 |
if (cache == markerCache) { |
|
78 |
cache = new Cache(); |
|
79 |
} |
|
80 |
return cache; |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
VMState(VirtualMachineImpl vm) { |
|
85 |
this.vm = vm; |
|
86 |
} |
|
87 |
||
88 |
/** |
|
89 |
* Is the VM currently suspended, for the purpose of caching? |
|
90 |
* Must be called synchronized on vm.state() |
|
91 |
*/ |
|
92 |
boolean isSuspended() { |
|
93 |
return cache != null; |
|
94 |
} |
|
95 |
||
96 |
/* |
|
97 |
* A JDWP command has been completed (reply has been received). |
|
98 |
* Update data that tracks pending resume commands. |
|
99 |
*/ |
|
100 |
synchronized void notifyCommandComplete(int id) { |
|
101 |
lastCompletedCommandId = id; |
|
102 |
} |
|
103 |
||
104 |
synchronized void freeze() { |
|
105 |
if (cache == null && (lastCompletedCommandId >= lastResumeCommandId)) { |
|
106 |
/* |
|
107 |
* No pending resumes to worry about. The VM is suspended |
|
108 |
* and additional state can be cached. Notify all |
|
109 |
* interested listeners. |
|
110 |
*/ |
|
111 |
processVMAction(new VMAction(vm, VMAction.VM_SUSPENDED)); |
|
112 |
enableCache(); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
synchronized PacketStream thawCommand(CommandSender sender) { |
|
117 |
PacketStream stream = sender.send(); |
|
118 |
lastResumeCommandId = stream.id(); |
|
119 |
thaw(); |
|
120 |
return stream; |
|
121 |
} |
|
122 |
||
123 |
/** |
|
836
d81b1f62fb82
6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents:
2
diff
changeset
|
124 |
* All threads are resuming |
2 | 125 |
*/ |
836
d81b1f62fb82
6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents:
2
diff
changeset
|
126 |
void thaw() { |
d81b1f62fb82
6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents:
2
diff
changeset
|
127 |
thaw(null); |
d81b1f62fb82
6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents:
2
diff
changeset
|
128 |
} |
d81b1f62fb82
6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents:
2
diff
changeset
|
129 |
|
d81b1f62fb82
6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents:
2
diff
changeset
|
130 |
/** |
d81b1f62fb82
6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents:
2
diff
changeset
|
131 |
* Tell listeners to invalidate suspend-sensitive caches. |
d81b1f62fb82
6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents:
2
diff
changeset
|
132 |
* If resumingThread != null, then only that thread is being |
d81b1f62fb82
6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents:
2
diff
changeset
|
133 |
* resumed. |
d81b1f62fb82
6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents:
2
diff
changeset
|
134 |
*/ |
d81b1f62fb82
6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents:
2
diff
changeset
|
135 |
synchronized void thaw(ThreadReference resumingThread) { |
2 | 136 |
if (cache != null) { |
11277 | 137 |
if ((vm.traceFlags & VirtualMachine.TRACE_OBJREFS) != 0) { |
2 | 138 |
vm.printTrace("Clearing VM suspended cache"); |
139 |
} |
|
140 |
disableCache(); |
|
141 |
} |
|
836
d81b1f62fb82
6700889: Thread resume invalidates all stack frames, even from other threads
jjh
parents:
2
diff
changeset
|
142 |
processVMAction(new VMAction(vm, resumingThread, VMAction.VM_NOT_SUSPENDED)); |
2 | 143 |
} |
144 |
||
145 |
private synchronized void processVMAction(VMAction action) { |
|
146 |
if (!notifyingListeners) { |
|
147 |
// Prevent recursion |
|
148 |
notifyingListeners = true; |
|
149 |
||
11277 | 150 |
Iterator<WeakReference<VMListener>> iter = listeners.iterator(); |
2 | 151 |
while (iter.hasNext()) { |
11277 | 152 |
WeakReference<VMListener> ref = iter.next(); |
153 |
VMListener listener = ref.get(); |
|
2 | 154 |
if (listener != null) { |
155 |
boolean keep = true; |
|
156 |
switch (action.id()) { |
|
157 |
case VMAction.VM_SUSPENDED: |
|
158 |
keep = listener.vmSuspended(action); |
|
159 |
break; |
|
160 |
case VMAction.VM_NOT_SUSPENDED: |
|
161 |
keep = listener.vmNotSuspended(action); |
|
162 |
break; |
|
163 |
} |
|
164 |
if (!keep) { |
|
165 |
iter.remove(); |
|
166 |
} |
|
167 |
} else { |
|
168 |
// Listener is unreachable; clean up |
|
169 |
iter.remove(); |
|
170 |
} |
|
171 |
} |
|
172 |
||
173 |
notifyingListeners = false; |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
synchronized void addListener(VMListener listener) { |
|
178 |
listeners.add(new WeakReference<VMListener>(listener)); |
|
179 |
} |
|
180 |
||
181 |
synchronized boolean hasListener(VMListener listener) { |
|
182 |
return listeners.contains(listener); |
|
183 |
} |
|
184 |
||
185 |
synchronized void removeListener(VMListener listener) { |
|
11277 | 186 |
Iterator<WeakReference<VMListener>> iter = listeners.iterator(); |
2 | 187 |
while (iter.hasNext()) { |
11277 | 188 |
WeakReference<VMListener> ref = iter.next(); |
2 | 189 |
if (listener.equals(ref.get())) { |
190 |
iter.remove(); |
|
191 |
break; |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
List<ThreadReference> allThreads() { |
|
197 |
List<ThreadReference> threads = null; |
|
198 |
try { |
|
199 |
Cache local = getCache(); |
|
200 |
||
201 |
if (local != null) { |
|
202 |
// may be stale when returned, but not provably so |
|
203 |
threads = local.threads; |
|
204 |
} |
|
205 |
if (threads == null) { |
|
206 |
threads = Arrays.asList((ThreadReference[])JDWP.VirtualMachine.AllThreads. |
|
207 |
process(vm).threads); |
|
208 |
if (local != null) { |
|
209 |
local.threads = threads; |
|
11277 | 210 |
if ((vm.traceFlags & VirtualMachine.TRACE_OBJREFS) != 0) { |
2 | 211 |
vm.printTrace("Caching all threads (count = " + |
212 |
threads.size() + ") while VM suspended"); |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
} catch (JDWPException exc) { |
|
217 |
throw exc.toJDIException(); |
|
218 |
} |
|
219 |
return threads; |
|
220 |
} |
|
221 |
||
222 |
||
223 |
List<ThreadGroupReference> topLevelThreadGroups() { |
|
224 |
List<ThreadGroupReference> groups = null; |
|
225 |
try { |
|
226 |
Cache local = getCache(); |
|
227 |
||
228 |
if (local != null) { |
|
229 |
groups = local.groups; |
|
230 |
} |
|
231 |
if (groups == null) { |
|
232 |
groups = Arrays.asList( |
|
233 |
(ThreadGroupReference[])JDWP.VirtualMachine.TopLevelThreadGroups. |
|
234 |
process(vm).groups); |
|
235 |
if (local != null) { |
|
236 |
local.groups = groups; |
|
11277 | 237 |
if ((vm.traceFlags & VirtualMachine.TRACE_OBJREFS) != 0) { |
2 | 238 |
vm.printTrace( |
239 |
"Caching top level thread groups (count = " + |
|
240 |
groups.size() + ") while VM suspended"); |
|
241 |
} |
|
242 |
} |
|
243 |
} |
|
244 |
} catch (JDWPException exc) { |
|
245 |
throw exc.toJDIException(); |
|
246 |
} |
|
247 |
return groups; |
|
248 |
} |
|
249 |
} |