1 /* 2 * Copyright (c) 2000, 2020, 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.win32_x86.Win32X86JavaThreadPDAccess; 32 import sun.jvm.hotspot.runtime.win32_amd64.Win32AMD64JavaThreadPDAccess; 33 import sun.jvm.hotspot.runtime.win32_aarch64.Win32AARCH64JavaThreadPDAccess; 34 import sun.jvm.hotspot.runtime.linux_x86.LinuxX86JavaThreadPDAccess; 35 import sun.jvm.hotspot.runtime.linux_amd64.LinuxAMD64JavaThreadPDAccess; 36 import sun.jvm.hotspot.runtime.linux_aarch64.LinuxAARCH64JavaThreadPDAccess; 37 import sun.jvm.hotspot.runtime.linux_ppc64.LinuxPPC64JavaThreadPDAccess; 38 import sun.jvm.hotspot.runtime.bsd_x86.BsdX86JavaThreadPDAccess; 39 import sun.jvm.hotspot.runtime.bsd_amd64.BsdAMD64JavaThreadPDAccess; 40 import sun.jvm.hotspot.utilities.*; 41 import sun.jvm.hotspot.utilities.Observable; 42 import sun.jvm.hotspot.utilities.Observer; 43 44 class ThreadsList extends VMObject { 45 private static AddressField threadsField; 46 private static CIntegerField lengthField; 47 48 static { 49 VM.registerVMInitializedObserver((o, d) -> initialize(VM.getVM().getTypeDataBase())); 50 } 51 52 private static synchronized void initialize(TypeDataBase db) { 53 Type type = db.lookupType("ThreadsList"); 54 lengthField = type.getCIntegerField("_length"); 55 threadsField = type.getAddressField("_threads"); 56 } 57 58 public Address getJavaThreadAddressAt(int i) { 59 Address threadAddr = threadsField.getValue(addr); 60 Address at = threadAddr.getAddressAt(VM.getVM().getAddressSize() * i); 61 return at; 62 } 63 64 public long length() { 65 return lengthField.getValue(addr); 66 } 67 68 public ThreadsList(Address addr) { 69 super(addr); 70 } 71 } 72 73 public class Threads { 74 private static JavaThreadFactory threadFactory; 75 private static AddressField threadListField; 76 private static VirtualConstructor virtualConstructor; 77 private static JavaThreadPDAccess access; 78 private static ThreadsList _list; 79 80 static { 81 VM.registerVMInitializedObserver(new Observer() { 82 public void update(Observable o, Object data) { 83 initialize(VM.getVM().getTypeDataBase()); 84 } 85 }); 86 } 87 88 private static synchronized void initialize(TypeDataBase db) { 89 Type type = db.lookupType("ThreadsSMRSupport"); 90 threadListField = type.getAddressField("_java_thread_list"); 91 92 // Instantiate appropriate platform-specific JavaThreadFactory 93 String os = VM.getVM().getOS(); 94 String cpu = VM.getVM().getCPU(); 95 96 access = null; 97 // FIXME: find the platform specific PD class by reflection? 98 if (os.equals("win32")) { 99 if (cpu.equals("x86")) { 100 access = new Win32X86JavaThreadPDAccess(); 101 } else if (cpu.equals("amd64")) { 102 access = new Win32AMD64JavaThreadPDAccess(); 103 } else if (cpu.equals("aarch64")) { 104 access = new Win32AARCH64JavaThreadPDAccess(); 105 } 106 } else if (os.equals("linux")) { 107 if (cpu.equals("x86")) { 108 access = new LinuxX86JavaThreadPDAccess(); 109 } else if (cpu.equals("amd64")) { 110 access = new LinuxAMD64JavaThreadPDAccess(); 111 } else if (cpu.equals("ppc64")) { 112 access = new LinuxPPC64JavaThreadPDAccess(); 113 } else if (cpu.equals("aarch64")) { 114 access = new LinuxAARCH64JavaThreadPDAccess(); 115 } else { 116 try { 117 access = (JavaThreadPDAccess) 118 Class.forName("sun.jvm.hotspot.runtime.linux_" + 119 cpu.toLowerCase() + ".Linux" + cpu.toUpperCase() + 120 "JavaThreadPDAccess").getDeclaredConstructor().newInstance(); 121 } catch (Exception e) { 122 throw new RuntimeException("OS/CPU combination " + os + "/" + cpu + 123 " not yet supported"); 124 } 125 } 126 } else if (os.equals("bsd")) { 127 if (cpu.equals("x86")) { 128 access = new BsdX86JavaThreadPDAccess(); 129 } else if (cpu.equals("amd64") || cpu.equals("x86_64")) { 130 access = new BsdAMD64JavaThreadPDAccess(); 131 } 132 } else if (os.equals("darwin")) { 133 if (cpu.equals("amd64") || cpu.equals("x86_64")) { 134 access = new BsdAMD64JavaThreadPDAccess(); 135 } 136 } 137 138 if (access == null) { 139 throw new RuntimeException("OS/CPU combination " + os + "/" + cpu + 140 " not yet supported"); 141 } 142 143 virtualConstructor = new VirtualConstructor(db); 144 // Add mappings for all known thread types 145 virtualConstructor.addMapping("JavaThread", JavaThread.class); 146 if (!VM.getVM().isCore()) { 147 virtualConstructor.addMapping("CompilerThread", CompilerThread.class); 148 virtualConstructor.addMapping("CodeCacheSweeperThread", CodeCacheSweeperThread.class); 149 } 150 virtualConstructor.addMapping("JvmtiAgentThread", JvmtiAgentThread.class); 151 virtualConstructor.addMapping("ServiceThread", ServiceThread.class); 152 virtualConstructor.addMapping("NotificationThread", NotificationThread.class); 153 } 154 155 public Threads() { 156 _list = VMObjectFactory.newObject(ThreadsList.class, threadListField.getValue()); 157 } 158 159 /** NOTE: this returns objects of type JavaThread, CompilerThread, 160 JvmtiAgentThread, NotificationThread, and ServiceThread. 161 The latter four are subclasses of the former. Most operations 162 (fetching the top frame, etc.) are only allowed to be performed on 163 a "pure" JavaThread. For this reason, {@link 164 sun.jvm.hotspot.runtime.JavaThread#isJavaThread} has been 165 changed from the definition in the VM (which returns true for 166 all of these thread types) to return true for JavaThreads and 167 false for the four subclasses. FIXME: should reconsider the 168 inheritance hierarchy; see {@link 169 sun.jvm.hotspot.runtime.JavaThread#isJavaThread}. */ 170 public JavaThread getJavaThreadAt(int i) { 171 if (i < _list.length()) { 172 return createJavaThreadWrapper(_list.getJavaThreadAddressAt(i)); 173 } 174 return null; 175 } 176 177 public int getNumberOfThreads() { 178 return (int) _list.length(); 179 } 180 181 /** Routine for instantiating appropriately-typed wrapper for a 182 JavaThread. Currently needs to be public for OopUtilities to 183 access it. */ 184 public JavaThread createJavaThreadWrapper(Address threadAddr) { 185 try { 186 JavaThread thread = (JavaThread)virtualConstructor.instantiateWrapperFor(threadAddr); 187 thread.setThreadPDAccess(access); 188 return thread; 189 } catch (Exception e) { 190 throw new RuntimeException("Unable to deduce type of thread from address " + threadAddr + 191 " (expected type JavaThread, CompilerThread, ServiceThread, JvmtiAgentThread or CodeCacheSweeperThread)", e); 192 } 193 } 194 195 /** Memory operations */ 196 public void oopsDo(AddressVisitor oopVisitor) { 197 // FIXME: add more of VM functionality 198 Threads threads = VM.getVM().getThreads(); 199 for (int i = 0; i < threads.getNumberOfThreads(); i++) { 200 JavaThread thread = threads.getJavaThreadAt(i); 201 thread.oopsDo(oopVisitor); 202 } 203 } 204 205 // refer to Threads::owning_thread_from_monitor_owner 206 public JavaThread owningThreadFromMonitor(Address o) { 207 if (o == null) return null; 208 for (int i = 0; i < getNumberOfThreads(); i++) { 209 JavaThread thread = getJavaThreadAt(i); 210 if (o.equals(thread.threadObjectAddress())) { 211 return thread; 212 } 213 } 214 215 for (int i = 0; i < getNumberOfThreads(); i++) { 216 JavaThread thread = getJavaThreadAt(i); 217 if (thread.isLockOwned(o)) 218 return thread; 219 } 220 return null; 221 } 222 223 public JavaThread owningThreadFromMonitor(ObjectMonitor monitor) { 224 return owningThreadFromMonitor(monitor.owner()); 225 } 226 227 // refer to Threads::get_pending_threads 228 // Get list of Java threads that are waiting to enter the specified monitor. 229 public List<JavaThread> getPendingThreads(ObjectMonitor monitor) { 230 List<JavaThread> pendingThreads = new ArrayList<>(); 231 for (int i = 0; i < getNumberOfThreads(); i++) { 232 JavaThread thread = getJavaThreadAt(i); 233 if (thread.isCompilerThread() || thread.isCodeCacheSweeperThread()) { 234 continue; 235 } 236 ObjectMonitor pending = thread.getCurrentPendingMonitor(); 237 if (monitor.equals(pending)) { 238 pendingThreads.add(thread); 239 } 240 } 241 return pendingThreads; 242 } 243 244 // Get list of Java threads that have called Object.wait on the specified monitor. 245 public List<JavaThread> getWaitingThreads(ObjectMonitor monitor) { 246 List<JavaThread> pendingThreads = new ArrayList<>(); 247 for (int i = 0; i < getNumberOfThreads(); i++) { 248 JavaThread thread = getJavaThreadAt(i); 249 ObjectMonitor waiting = thread.getCurrentWaitingMonitor(); 250 if (monitor.equals(waiting)) { 251 pendingThreads.add(thread); 252 } 253 } 254 return pendingThreads; 255 } 256 257 // FIXME: add other accessors 258 }