1 /* 2 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 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 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 package sun.jvm.hotspot.runtime; 26 27 import java.util.*; 28 29 import sun.jvm.hotspot.debugger.*; 30 import sun.jvm.hotspot.types.*; 31 import sun.jvm.hotspot.runtime.solaris_sparc.SolarisSPARCJavaThreadPDAccess; 32 import sun.jvm.hotspot.runtime.solaris_x86.SolarisX86JavaThreadPDAccess; 33 import sun.jvm.hotspot.runtime.solaris_amd64.SolarisAMD64JavaThreadPDAccess; 34 import sun.jvm.hotspot.runtime.win32_amd64.Win32AMD64JavaThreadPDAccess; 35 import sun.jvm.hotspot.runtime.win32_x86.Win32X86JavaThreadPDAccess; 36 import sun.jvm.hotspot.runtime.linux_x86.LinuxX86JavaThreadPDAccess; 37 import sun.jvm.hotspot.runtime.linux_amd64.LinuxAMD64JavaThreadPDAccess; 38 import sun.jvm.hotspot.runtime.linux_aarch64.LinuxAARCH64JavaThreadPDAccess; 39 import sun.jvm.hotspot.runtime.linux_ppc64.LinuxPPC64JavaThreadPDAccess; 40 import sun.jvm.hotspot.runtime.linux_sparc.LinuxSPARCJavaThreadPDAccess; 41 import sun.jvm.hotspot.runtime.bsd_x86.BsdX86JavaThreadPDAccess; 42 import sun.jvm.hotspot.runtime.bsd_amd64.BsdAMD64JavaThreadPDAccess; 43 import sun.jvm.hotspot.utilities.*; 44 45 class ThreadsList extends VMObject { 46 private static AddressField threadsField; 47 private static CIntegerField lengthField; 48 49 static { 50 VM.registerVMInitializedObserver((o, d) -> initialize(VM.getVM().getTypeDataBase())); 51 } 52 53 private static synchronized void initialize(TypeDataBase db) { 54 Type type = db.lookupType("ThreadsList"); 55 lengthField = type.getCIntegerField("_length"); 56 threadsField = type.getAddressField("_threads"); 57 } 58 59 public Address getJavaThreadAddressAt(int i) { 60 Address threadAddr = threadsField.getValue(addr); 61 Address at = threadAddr.getAddressAt(VM.getVM().getAddressSize() * i); 62 return at; 63 } 64 65 public long length() { 66 return lengthField.getValue(addr); 67 } 68 69 public ThreadsList(Address addr) { 70 super(addr); 71 } 72 } 73 74 public class Threads { 75 private static JavaThreadFactory threadFactory; 76 private static AddressField threadListField; 77 private static VirtualConstructor virtualConstructor; 78 private static JavaThreadPDAccess access; 79 private static ThreadsList _list; 80 81 static { 82 VM.registerVMInitializedObserver(new Observer() { 83 public void update(Observable o, Object data) { 84 initialize(VM.getVM().getTypeDataBase()); 85 } 86 }); 87 } 88 89 private static synchronized void initialize(TypeDataBase db) { 90 Type type = db.lookupType("ThreadsSMRSupport"); 91 threadListField = type.getAddressField("_java_thread_list"); 92 93 // Instantiate appropriate platform-specific JavaThreadFactory 94 String os = VM.getVM().getOS(); 95 String cpu = VM.getVM().getCPU(); 96 97 access = null; 98 // FIXME: find the platform specific PD class by reflection? 99 if (os.equals("solaris")) { 100 if (cpu.equals("sparc")) { 101 access = new SolarisSPARCJavaThreadPDAccess(); 102 } else if (cpu.equals("x86")) { 103 access = new SolarisX86JavaThreadPDAccess(); 104 } else if (cpu.equals("amd64")) { 105 access = new SolarisAMD64JavaThreadPDAccess(); 106 } 107 } else if (os.equals("win32")) { 108 if (cpu.equals("x86")) { 109 access = new Win32X86JavaThreadPDAccess(); 110 } else if (cpu.equals("amd64")) { 111 access = new Win32AMD64JavaThreadPDAccess(); 112 } 113 } else if (os.equals("linux")) { 114 if (cpu.equals("x86")) { 115 access = new LinuxX86JavaThreadPDAccess(); 116 } else if (cpu.equals("amd64")) { 117 access = new LinuxAMD64JavaThreadPDAccess(); 118 } else if (cpu.equals("sparc")) { 119 access = new LinuxSPARCJavaThreadPDAccess(); 120 } else if (cpu.equals("ppc64")) { 121 access = new LinuxPPC64JavaThreadPDAccess(); 122 } else if (cpu.equals("aarch64")) { 123 access = new LinuxAARCH64JavaThreadPDAccess(); 124 } else { 125 try { 126 access = (JavaThreadPDAccess) 127 Class.forName("sun.jvm.hotspot.runtime.linux_" + 128 cpu.toLowerCase() + ".Linux" + cpu.toUpperCase() + 129 "JavaThreadPDAccess").newInstance(); 130 } catch (Exception e) { 131 throw new RuntimeException("OS/CPU combination " + os + "/" + cpu + 132 " not yet supported"); 133 } 134 } 135 } else if (os.equals("bsd")) { 136 if (cpu.equals("x86")) { 137 access = new BsdX86JavaThreadPDAccess(); 138 } else if (cpu.equals("amd64") || cpu.equals("x86_64")) { 139 access = new BsdAMD64JavaThreadPDAccess(); 140 } 141 } else if (os.equals("darwin")) { 142 if (cpu.equals("amd64") || cpu.equals("x86_64")) { 143 access = new BsdAMD64JavaThreadPDAccess(); 144 } 145 } 146 147 if (access == null) { 148 throw new RuntimeException("OS/CPU combination " + os + "/" + cpu + 149 " not yet supported"); 150 } 151 152 virtualConstructor = new VirtualConstructor(db); 153 // Add mappings for all known thread types 154 virtualConstructor.addMapping("JavaThread", JavaThread.class); 155 if (!VM.getVM().isCore()) { 156 virtualConstructor.addMapping("CompilerThread", CompilerThread.class); 157 virtualConstructor.addMapping("CodeCacheSweeperThread", CodeCacheSweeperThread.class); 158 } 159 virtualConstructor.addMapping("JvmtiAgentThread", JvmtiAgentThread.class); 160 virtualConstructor.addMapping("ServiceThread", ServiceThread.class); 161 } 162 163 public Threads() { 164 _list = VMObjectFactory.newObject(ThreadsList.class, threadListField.getValue()); 165 } 166 167 /** NOTE: this returns objects of type JavaThread, CompilerThread, 168 JvmtiAgentThread, and ServiceThread. 169 The latter four are subclasses of the former. Most operations 170 (fetching the top frame, etc.) are only allowed to be performed on 171 a "pure" JavaThread. For this reason, {@link 172 sun.jvm.hotspot.runtime.JavaThread#isJavaThread} has been 173 changed from the definition in the VM (which returns true for 174 all of these thread types) to return true for JavaThreads and 175 false for the three subclasses. FIXME: should reconsider the 176 inheritance hierarchy; see {@link 177 sun.jvm.hotspot.runtime.JavaThread#isJavaThread}. */ 178 public JavaThread getJavaThreadAt(int i) { 179 if (i < _list.length()) { 180 return createJavaThreadWrapper(_list.getJavaThreadAddressAt(i)); 181 } 182 return null; 183 } 184 185 public int getNumberOfThreads() { 186 return (int) _list.length(); 187 } 188 189 /** Routine for instantiating appropriately-typed wrapper for a 190 JavaThread. Currently needs to be public for OopUtilities to 191 access it. */ 192 public JavaThread createJavaThreadWrapper(Address threadAddr) { 193 try { 194 JavaThread thread = (JavaThread)virtualConstructor.instantiateWrapperFor(threadAddr); 195 thread.setThreadPDAccess(access); 196 return thread; 197 } catch (Exception e) { 198 throw new RuntimeException("Unable to deduce type of thread from address " + threadAddr + 199 " (expected type JavaThread, CompilerThread, ServiceThread, JvmtiAgentThread or CodeCacheSweeperThread)", e); 200 } 201 } 202 203 /** Memory operations */ 204 public void oopsDo(AddressVisitor oopVisitor) { 205 // FIXME: add more of VM functionality 206 Threads threads = VM.getVM().getThreads(); 207 for (int i = 0; i < threads.getNumberOfThreads(); i++) { 208 JavaThread thread = threads.getJavaThreadAt(i); 209 thread.oopsDo(oopVisitor); 210 } 211 } 212 213 // refer to Threads::owning_thread_from_monitor_owner 214 public JavaThread owningThreadFromMonitor(Address o) { 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 return owningThreadFromMonitor(monitor.owner()); 233 } 234 235 // refer to Threads::get_pending_threads 236 // Get list of Java threads that are waiting to enter the specified monitor. 237 public List getPendingThreads(ObjectMonitor monitor) { 238 List pendingThreads = new ArrayList(); 239 for (int i = 0; i < getNumberOfThreads(); i++) { 240 JavaThread thread = getJavaThreadAt(i); 241 if (thread.isCompilerThread() || thread.isCodeCacheSweeperThread()) { 242 continue; 243 } 244 ObjectMonitor pending = thread.getCurrentPendingMonitor(); 245 if (monitor.equals(pending)) { 246 pendingThreads.add(thread); 247 } 248 } 249 return pendingThreads; 250 } 251 252 // Get list of Java threads that have called Object.wait on the specified monitor. 253 public List getWaitingThreads(ObjectMonitor monitor) { 254 List pendingThreads = new ArrayList(); 255 for (int i = 0; i < getNumberOfThreads(); i++) { 256 JavaThread thread = getJavaThreadAt(i); 257 ObjectMonitor waiting = thread.getCurrentWaitingMonitor(); 258 if (monitor.equals(waiting)) { 259 pendingThreads.add(thread); 260 } 261 } 262 return pendingThreads; 263 } 264 265 // FIXME: add other accessors 266 }