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