1 /* 2 * Copyright (c) 2004, 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 import sun.jvm.hotspot.debugger.*; 29 import sun.jvm.hotspot.types.*; 30 import sun.jvm.hotspot.utilities.Observable; 31 import sun.jvm.hotspot.utilities.Observer; 32 33 // The OSThread class holds OS-specific thread information. It is equivalent 34 // to the sys_thread_t structure of the classic JVM implementation. 35 public class OSThread extends VMObject { 36 private static Field threadIdField; 37 private static CIntegerField threadStateField; 38 39 // ThreadStates read from underlying process 40 private static int ALLOCATED; 41 private static int INITIALIZED; 42 private static int RUNNABLE; 43 private static int MONITOR_WAIT; 44 private static int CONDVAR_WAIT; 45 private static int OBJECT_WAIT; 46 private static int BREAKPOINTED; 47 private static int SLEEPING; 48 private static int ZOMBIE; 49 50 static { VM.registerVMInitializedObserver(new Observer() { public void update(Observable o, Object data) { initialize(VM.getVM().getTypeDataBase()); } })51 VM.registerVMInitializedObserver(new Observer() { 52 public void update(Observable o, Object data) { 53 initialize(VM.getVM().getTypeDataBase()); 54 } 55 }); 56 } 57 initialize(TypeDataBase db)58 private static synchronized void initialize(TypeDataBase db) { 59 Type type = db.lookupType("OSThread"); 60 threadIdField = type.getField("_thread_id"); 61 threadStateField = type.getCIntegerField("_state"); 62 63 ALLOCATED = db.lookupIntConstant("ALLOCATED").intValue(); 64 INITIALIZED = db.lookupIntConstant("INITIALIZED").intValue(); 65 RUNNABLE = db.lookupIntConstant("RUNNABLE").intValue(); 66 MONITOR_WAIT = db.lookupIntConstant("MONITOR_WAIT").intValue(); 67 CONDVAR_WAIT = db.lookupIntConstant("CONDVAR_WAIT").intValue(); 68 OBJECT_WAIT = db.lookupIntConstant("OBJECT_WAIT").intValue(); 69 BREAKPOINTED = db.lookupIntConstant("BREAKPOINTED").intValue(); 70 SLEEPING = db.lookupIntConstant("SLEEPING").intValue(); 71 ZOMBIE = db.lookupIntConstant("ZOMBIE").intValue(); 72 } 73 OSThread(Address addr)74 public OSThread(Address addr) { 75 super(addr); 76 } 77 threadId()78 public int threadId() { 79 return threadIdField.getJInt(addr); 80 } 81 getThreadState()82 public ThreadState getThreadState() { 83 int val = (int)threadStateField.getValue(addr); 84 if (val == ALLOCATED) { 85 return ThreadState.ALLOCATED; 86 } else if (val == INITIALIZED) { 87 return ThreadState.INITIALIZED; 88 } else if (val == RUNNABLE) { 89 return ThreadState.RUNNABLE; 90 } else if (val == MONITOR_WAIT) { 91 return ThreadState.MONITOR_WAIT; 92 } else if (val == CONDVAR_WAIT) { 93 return ThreadState.CONDVAR_WAIT; 94 } else if (val == OBJECT_WAIT) { 95 return ThreadState.OBJECT_WAIT; 96 } else if (val == BREAKPOINTED) { 97 return ThreadState.BREAKPOINTED; 98 } else if (val == SLEEPING) { 99 return ThreadState.SLEEPING; 100 } else if (val == ZOMBIE) { 101 return ThreadState.ZOMBIE; 102 } else { 103 throw new RuntimeException("Illegal thread state " + val); 104 } 105 } 106 } 107