194 thread.setThreadPDAccess(access);
195 return thread;
196 } catch (Exception e) {
197 throw new RuntimeException("Unable to deduce type of thread from address " + threadAddr +
198 " (expected type JavaThread, CompilerThread, MonitorDeflationThread, ServiceThread, JvmtiAgentThread or CodeCacheSweeperThread)", e);
199 }
200 }
201
202 /** Memory operations */
203 public void oopsDo(AddressVisitor oopVisitor) {
204 // FIXME: add more of VM functionality
205 Threads threads = VM.getVM().getThreads();
206 for (int i = 0; i < threads.getNumberOfThreads(); i++) {
207 JavaThread thread = threads.getJavaThreadAt(i);
208 thread.oopsDo(oopVisitor);
209 }
210 }
211
212 // refer to Threads::owning_thread_from_monitor_owner
213 public JavaThread owningThreadFromMonitor(Address o) {
214 if (o == null) return null;
215 for (int i = 0; i < getNumberOfThreads(); i++) {
216 JavaThread thread = getJavaThreadAt(i);
217 if (o.equals(thread.threadObjectAddress())) {
218 return thread;
219 }
220 }
221
222 for (int i = 0; i < getNumberOfThreads(); i++) {
223 JavaThread thread = getJavaThreadAt(i);
224 if (thread.isLockOwned(o))
225 return thread;
226 }
227 return null;
228 }
229
230 public JavaThread owningThreadFromMonitor(ObjectMonitor monitor) {
231 return owningThreadFromMonitor(monitor.owner());
232 }
233
234 // refer to Threads::get_pending_threads
235 // Get list of Java threads that are waiting to enter the specified monitor.
236 public List<JavaThread> getPendingThreads(ObjectMonitor monitor) {
237 List<JavaThread> pendingThreads = new ArrayList<>();
238 for (int i = 0; i < getNumberOfThreads(); i++) {
239 JavaThread thread = getJavaThreadAt(i);
240 if (thread.isCompilerThread() || thread.isCodeCacheSweeperThread()) {
241 continue;
242 }
243 ObjectMonitor pending = thread.getCurrentPendingMonitor();
244 if (monitor.equals(pending)) {
245 pendingThreads.add(thread);
246 }
247 }
248 return pendingThreads;
249 }
250
251 // Get list of Java threads that have called Object.wait on the specified monitor.
|
194 thread.setThreadPDAccess(access);
195 return thread;
196 } catch (Exception e) {
197 throw new RuntimeException("Unable to deduce type of thread from address " + threadAddr +
198 " (expected type JavaThread, CompilerThread, MonitorDeflationThread, ServiceThread, JvmtiAgentThread or CodeCacheSweeperThread)", e);
199 }
200 }
201
202 /** Memory operations */
203 public void oopsDo(AddressVisitor oopVisitor) {
204 // FIXME: add more of VM functionality
205 Threads threads = VM.getVM().getThreads();
206 for (int i = 0; i < threads.getNumberOfThreads(); i++) {
207 JavaThread thread = threads.getJavaThreadAt(i);
208 thread.oopsDo(oopVisitor);
209 }
210 }
211
212 // refer to Threads::owning_thread_from_monitor_owner
213 public JavaThread owningThreadFromMonitor(Address o) {
214 assert(VM.getVM().getCommandLineFlag("LockingMode").getInt() != LockingMode.getLightweight());
215 if (o == null) return null;
216 for (int i = 0; i < getNumberOfThreads(); i++) {
217 JavaThread thread = getJavaThreadAt(i);
218 if (o.equals(thread.threadObjectAddress())) {
219 return thread;
220 }
221 }
222
223 for (int i = 0; i < getNumberOfThreads(); i++) {
224 JavaThread thread = getJavaThreadAt(i);
225 if (thread.isLockOwned(o))
226 return thread;
227 }
228 return null;
229 }
230
231 public JavaThread owningThreadFromMonitor(ObjectMonitor monitor) {
232 if (VM.getVM().getCommandLineFlag("LockingMode").getInt() == LockingMode.getLightweight()) {
233 if (monitor.isOwnedAnonymous()) {
234 OopHandle object = monitor.object();
235 for (int i = 0; i < getNumberOfThreads(); i++) {
236 JavaThread thread = getJavaThreadAt(i);
237 if (thread.isLockOwned(object)) {
238 return thread;
239 }
240 }
241 // We should have found the owner, however, as the VM could be in any state, including the middle
242 // of performing GC, it is not always possible to do so. Just return null if we can't locate it.
243 System.out.println("Warning: We failed to find a thread that owns an anonymous lock. This is likely");
244 System.out.println("due to the JVM currently running a GC. Locking information may not be accurate.");
245 return null;
246 }
247 // Owner can only be threads at this point.
248 Address o = monitor.owner();
249 if (o == null) return null;
250 return new JavaThread(o);
251 } else {
252 return owningThreadFromMonitor(monitor.owner());
253 }
254 }
255
256 // refer to Threads::get_pending_threads
257 // Get list of Java threads that are waiting to enter the specified monitor.
258 public List<JavaThread> getPendingThreads(ObjectMonitor monitor) {
259 List<JavaThread> pendingThreads = new ArrayList<>();
260 for (int i = 0; i < getNumberOfThreads(); i++) {
261 JavaThread thread = getJavaThreadAt(i);
262 if (thread.isCompilerThread() || thread.isCodeCacheSweeperThread()) {
263 continue;
264 }
265 ObjectMonitor pending = thread.getCurrentPendingMonitor();
266 if (monitor.equals(pending)) {
267 pendingThreads.add(thread);
268 }
269 }
270 return pendingThreads;
271 }
272
273 // Get list of Java threads that have called Object.wait on the specified monitor.
|