1 /*
2  * Copyright (c) 2004, 2017, 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 JIntField interruptedField;
35     private static Field threadIdField;
36     private static CIntegerField threadStateField;
37 
38     // ThreadStates read from underlying process
39     private static int ALLOCATED;
40     private static int INITIALIZED;
41     private static int RUNNABLE;
42     private static int MONITOR_WAIT;
43     private static int CONDVAR_WAIT;
44     private static int OBJECT_WAIT;
45     private static int BREAKPOINTED;
46     private static int SLEEPING;
47     private static int ZOMBIE;
48 
49     static {
VM.registerVMInitializedObserver(new Observer() { public void update(Observable o, Object data) { initialize(VM.getVM().getTypeDataBase()); } })50         VM.registerVMInitializedObserver(new Observer() {
51             public void update(Observable o, Object data) {
52                 initialize(VM.getVM().getTypeDataBase());
53             }
54         });
55     }
56 
initialize(TypeDataBase db)57     private static synchronized void initialize(TypeDataBase db) {
58         Type type = db.lookupType("OSThread");
59         interruptedField = type.getJIntField("_interrupted");
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 
interrupted()78     public boolean interrupted() {
79         return ((int)interruptedField.getValue(addr)) != 0;
80     }
81 
threadId()82     public int threadId() {
83         return threadIdField.getJInt(addr);
84     }
85 
getThreadState()86     public ThreadState getThreadState() {
87         int val = (int)threadStateField.getValue(addr);
88         if (val ==  ALLOCATED) {
89             return ThreadState.ALLOCATED;
90         } else if (val ==  INITIALIZED) {
91             return ThreadState.INITIALIZED;
92         } else if (val ==  RUNNABLE) {
93             return ThreadState.RUNNABLE;
94         } else if (val ==  MONITOR_WAIT) {
95             return ThreadState.MONITOR_WAIT;
96         } else if (val ==  CONDVAR_WAIT) {
97             return ThreadState.CONDVAR_WAIT;
98         } else if (val ==  OBJECT_WAIT) {
99             return ThreadState.OBJECT_WAIT;
100         } else if (val ==  BREAKPOINTED) {
101             return ThreadState.BREAKPOINTED;
102         } else if (val ==  SLEEPING) {
103             return ThreadState.SLEEPING;
104         } else if (val ==  ZOMBIE) {
105             return ThreadState.ZOMBIE;
106         } else {
107             throw new RuntimeException("Illegal thread state " + val);
108         }
109     }
110 }
111