annotate agent/src/share/classes/sun/jvm/hotspot/oops/OopUtilities.java @ 24234:ea6f94ab283b default tip

Added tag jvmci-0.36 for changeset 8128b98d4736
author Gilles Duboscq <gilles.m.duboscq@oracle.com>
date Mon, 18 Sep 2017 18:49:45 +0200
parents efe013052465
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
23802
efe013052465 8059677: Thread.getName() instantiates Strings
shade
parents: 22841
diff changeset
2 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 196
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 196
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 196
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.oops;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.util.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import sun.jvm.hotspot.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import sun.jvm.hotspot.memory.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import sun.jvm.hotspot.types.TypeDataBase;
a61af66fc99e Initial load
duke
parents:
diff changeset
33 import sun.jvm.hotspot.utilities.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
34 import sun.jvm.hotspot.jdi.JVMTIThreadState;
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 /** A utility class encapsulating useful oop operations */
a61af66fc99e Initial load
duke
parents:
diff changeset
37
a61af66fc99e Initial load
duke
parents:
diff changeset
38 public class OopUtilities implements /* imports */ JVMTIThreadState {
a61af66fc99e Initial load
duke
parents:
diff changeset
39
a61af66fc99e Initial load
duke
parents:
diff changeset
40 // FIXME: access should be synchronized and cleared when VM is
a61af66fc99e Initial load
duke
parents:
diff changeset
41 // resumed
a61af66fc99e Initial load
duke
parents:
diff changeset
42 // String fields
a61af66fc99e Initial load
duke
parents:
diff changeset
43 private static IntField offsetField;
a61af66fc99e Initial load
duke
parents:
diff changeset
44 private static IntField countField;
a61af66fc99e Initial load
duke
parents:
diff changeset
45 private static OopField valueField;
a61af66fc99e Initial load
duke
parents:
diff changeset
46 // ThreadGroup fields
a61af66fc99e Initial load
duke
parents:
diff changeset
47 private static OopField threadGroupParentField;
a61af66fc99e Initial load
duke
parents:
diff changeset
48 private static OopField threadGroupNameField;
a61af66fc99e Initial load
duke
parents:
diff changeset
49 private static IntField threadGroupNThreadsField;
a61af66fc99e Initial load
duke
parents:
diff changeset
50 private static OopField threadGroupThreadsField;
a61af66fc99e Initial load
duke
parents:
diff changeset
51 private static IntField threadGroupNGroupsField;
a61af66fc99e Initial load
duke
parents:
diff changeset
52 private static OopField threadGroupGroupsField;
a61af66fc99e Initial load
duke
parents:
diff changeset
53 // Thread fields
a61af66fc99e Initial load
duke
parents:
diff changeset
54 private static OopField threadNameField;
a61af66fc99e Initial load
duke
parents:
diff changeset
55 private static OopField threadGroupField;
a61af66fc99e Initial load
duke
parents:
diff changeset
56 private static LongField threadEETopField;
22841
f9d003ea9023 8046282: SA update
poonam
parents: 6169
diff changeset
57 //tid field is new since 1.5
f9d003ea9023 8046282: SA update
poonam
parents: 6169
diff changeset
58 private static LongField threadTIDField;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
59 // threadStatus field is new since 1.5
a61af66fc99e Initial load
duke
parents:
diff changeset
60 private static IntField threadStatusField;
a61af66fc99e Initial load
duke
parents:
diff changeset
61 // parkBlocker field is new since 1.6
a61af66fc99e Initial load
duke
parents:
diff changeset
62 private static OopField threadParkBlockerField;
a61af66fc99e Initial load
duke
parents:
diff changeset
63
a61af66fc99e Initial load
duke
parents:
diff changeset
64 // possible values of java_lang_Thread::ThreadStatus
a61af66fc99e Initial load
duke
parents:
diff changeset
65 private static int THREAD_STATUS_NEW;
a61af66fc99e Initial load
duke
parents:
diff changeset
66 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
67 Other enum constants are not needed as of now. Uncomment these as and when needed.
a61af66fc99e Initial load
duke
parents:
diff changeset
68
a61af66fc99e Initial load
duke
parents:
diff changeset
69 private static int THREAD_STATUS_RUNNABLE;
a61af66fc99e Initial load
duke
parents:
diff changeset
70 private static int THREAD_STATUS_SLEEPING;
a61af66fc99e Initial load
duke
parents:
diff changeset
71 private static int THREAD_STATUS_IN_OBJECT_WAIT;
a61af66fc99e Initial load
duke
parents:
diff changeset
72 private static int THREAD_STATUS_IN_OBJECT_WAIT_TIMED;
a61af66fc99e Initial load
duke
parents:
diff changeset
73 private static int THREAD_STATUS_PARKED;
a61af66fc99e Initial load
duke
parents:
diff changeset
74 private static int THREAD_STATUS_PARKED_TIMED;
a61af66fc99e Initial load
duke
parents:
diff changeset
75 private static int THREAD_STATUS_BLOCKED_ON_MONITOR_ENTER;
a61af66fc99e Initial load
duke
parents:
diff changeset
76 private static int THREAD_STATUS_TERMINATED;
a61af66fc99e Initial load
duke
parents:
diff changeset
77 */
a61af66fc99e Initial load
duke
parents:
diff changeset
78
a61af66fc99e Initial load
duke
parents:
diff changeset
79 // java.util.concurrent.locks.AbstractOwnableSynchronizer fields
a61af66fc99e Initial load
duke
parents:
diff changeset
80 private static OopField absOwnSyncOwnerThreadField;
a61af66fc99e Initial load
duke
parents:
diff changeset
81
a61af66fc99e Initial load
duke
parents:
diff changeset
82 static {
a61af66fc99e Initial load
duke
parents:
diff changeset
83 VM.registerVMInitializedObserver(new Observer() {
a61af66fc99e Initial load
duke
parents:
diff changeset
84 public void update(Observable o, Object data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
85 initialize(VM.getVM().getTypeDataBase());
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87 });
a61af66fc99e Initial load
duke
parents:
diff changeset
88 }
a61af66fc99e Initial load
duke
parents:
diff changeset
89
a61af66fc99e Initial load
duke
parents:
diff changeset
90 private static synchronized void initialize(TypeDataBase db) {
a61af66fc99e Initial load
duke
parents:
diff changeset
91 // FIXME: don't need this observer; however, do need a VM resumed
a61af66fc99e Initial load
duke
parents:
diff changeset
92 // and suspended observer to refetch fields
a61af66fc99e Initial load
duke
parents:
diff changeset
93 }
a61af66fc99e Initial load
duke
parents:
diff changeset
94
a61af66fc99e Initial load
duke
parents:
diff changeset
95 public static String charArrayToString(TypeArray charArray) {
a61af66fc99e Initial load
duke
parents:
diff changeset
96 if (charArray == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
97 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
98 }
a61af66fc99e Initial load
duke
parents:
diff changeset
99 return charArrayToString(charArray, 0, (int) charArray.getLength());
a61af66fc99e Initial load
duke
parents:
diff changeset
100 }
a61af66fc99e Initial load
duke
parents:
diff changeset
101
a61af66fc99e Initial load
duke
parents:
diff changeset
102 public static String charArrayToString(TypeArray charArray, int offset, int length) {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 if (charArray == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
104 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
105 }
a61af66fc99e Initial load
duke
parents:
diff changeset
106 final int limit = offset + length;
a61af66fc99e Initial load
duke
parents:
diff changeset
107 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
108 Assert.that(offset >= 0 && limit <= charArray.getLength(), "out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110 StringBuffer buf = new StringBuffer(length);
a61af66fc99e Initial load
duke
parents:
diff changeset
111 for (int i = offset; i < limit; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
112 buf.append(charArray.getCharAt(i));
a61af66fc99e Initial load
duke
parents:
diff changeset
113 }
a61af66fc99e Initial load
duke
parents:
diff changeset
114 return buf.toString();
a61af66fc99e Initial load
duke
parents:
diff changeset
115 }
a61af66fc99e Initial load
duke
parents:
diff changeset
116
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
117 public static String escapeString(String s) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
118 StringBuilder sb = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
119 for (int index = 0; index < s.length(); index++) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
120 char value = s.charAt(index);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
121 if (value >= 32 && value < 127 || value == '\'' || value == '\\') {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
122 if (sb != null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
123 sb.append(value);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
124 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
125 } else {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
126 if (sb == null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
127 sb = new StringBuilder(s.length() * 2);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
128 sb.append(s, 0, index);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
129 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
130 sb.append("\\u");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
131 if (value < 0x10) sb.append("000");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
132 else if (value < 0x100) sb.append("00");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
133 else if (value < 0x1000) sb.append("0");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
134 sb.append(Integer.toHexString(value));
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
135 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
136 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
137 if (sb != null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
138 return sb.toString();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
139 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
140 return s;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
141 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
142
0
a61af66fc99e Initial load
duke
parents:
diff changeset
143 public static String stringOopToString(Oop stringOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
144 if (offsetField == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
145 InstanceKlass k = (InstanceKlass) stringOop.getKlass();
6169
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
146 offsetField = (IntField) k.findField("offset", "I"); // optional
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
147 countField = (IntField) k.findField("count", "I"); // optional
0
a61af66fc99e Initial load
duke
parents:
diff changeset
148 valueField = (OopField) k.findField("value", "[C");
a61af66fc99e Initial load
duke
parents:
diff changeset
149 if (Assert.ASSERTS_ENABLED) {
6169
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
150 Assert.that(valueField != null, "Field \'value\' of java.lang.String not found");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
151 }
a61af66fc99e Initial load
duke
parents:
diff changeset
152 }
6169
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
153 if (offsetField != null && countField != null) {
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
154 return charArrayToString((TypeArray) valueField.getValue(stringOop),
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
155 offsetField.getValue(stringOop),
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
156 countField.getValue(stringOop));
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
157 }
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
158 return charArrayToString((TypeArray) valueField.getValue(stringOop));
0
a61af66fc99e Initial load
duke
parents:
diff changeset
159 }
a61af66fc99e Initial load
duke
parents:
diff changeset
160
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
161 public static String stringOopToEscapedString(Oop stringOop) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
162 return escapeString(stringOopToString(stringOop));
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
163 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
164
0
a61af66fc99e Initial load
duke
parents:
diff changeset
165 private static void initThreadGroupFields() {
a61af66fc99e Initial load
duke
parents:
diff changeset
166 if (threadGroupParentField == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
167 SystemDictionary sysDict = VM.getVM().getSystemDictionary();
a61af66fc99e Initial load
duke
parents:
diff changeset
168 InstanceKlass k = sysDict.getThreadGroupKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
169 threadGroupParentField = (OopField) k.findField("parent", "Ljava/lang/ThreadGroup;");
a61af66fc99e Initial load
duke
parents:
diff changeset
170 threadGroupNameField = (OopField) k.findField("name", "Ljava/lang/String;");
a61af66fc99e Initial load
duke
parents:
diff changeset
171 threadGroupNThreadsField = (IntField) k.findField("nthreads", "I");
a61af66fc99e Initial load
duke
parents:
diff changeset
172 threadGroupThreadsField = (OopField) k.findField("threads", "[Ljava/lang/Thread;");
a61af66fc99e Initial load
duke
parents:
diff changeset
173 threadGroupNGroupsField = (IntField) k.findField("ngroups", "I");
a61af66fc99e Initial load
duke
parents:
diff changeset
174 threadGroupGroupsField = (OopField) k.findField("groups", "[Ljava/lang/ThreadGroup;");
a61af66fc99e Initial load
duke
parents:
diff changeset
175 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
176 Assert.that(threadGroupParentField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
177 threadGroupNameField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
178 threadGroupNThreadsField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
179 threadGroupThreadsField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
180 threadGroupNGroupsField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
181 threadGroupGroupsField != null, "must find all java.lang.ThreadGroup fields");
a61af66fc99e Initial load
duke
parents:
diff changeset
182 }
a61af66fc99e Initial load
duke
parents:
diff changeset
183 }
a61af66fc99e Initial load
duke
parents:
diff changeset
184 }
a61af66fc99e Initial load
duke
parents:
diff changeset
185
a61af66fc99e Initial load
duke
parents:
diff changeset
186 public static Oop threadGroupOopGetParent(Oop threadGroupOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
187 initThreadGroupFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
188 return threadGroupParentField.getValue(threadGroupOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
189 }
a61af66fc99e Initial load
duke
parents:
diff changeset
190
a61af66fc99e Initial load
duke
parents:
diff changeset
191 public static String threadGroupOopGetName(Oop threadGroupOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
192 initThreadGroupFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
193 return stringOopToString(threadGroupNameField.getValue(threadGroupOop));
a61af66fc99e Initial load
duke
parents:
diff changeset
194 }
a61af66fc99e Initial load
duke
parents:
diff changeset
195
a61af66fc99e Initial load
duke
parents:
diff changeset
196 public static Oop[] threadGroupOopGetThreads(Oop threadGroupOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
197 initThreadGroupFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
198 int nthreads = threadGroupNThreadsField.getValue(threadGroupOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
199 Oop[] result = new Oop[nthreads];
a61af66fc99e Initial load
duke
parents:
diff changeset
200 ObjArray threads = (ObjArray) threadGroupThreadsField.getValue(threadGroupOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
201 for (int i = 0; i < nthreads; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
202 result[i] = threads.getObjAt(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
203 }
a61af66fc99e Initial load
duke
parents:
diff changeset
204 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
205 }
a61af66fc99e Initial load
duke
parents:
diff changeset
206
a61af66fc99e Initial load
duke
parents:
diff changeset
207 public static Oop[] threadGroupOopGetGroups(Oop threadGroupOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
208 initThreadGroupFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
209 int ngroups = threadGroupNGroupsField.getValue(threadGroupOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
210 Oop[] result = new Oop[ngroups];
a61af66fc99e Initial load
duke
parents:
diff changeset
211 ObjArray groups = (ObjArray) threadGroupGroupsField.getValue(threadGroupOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
212 for (int i = 0; i < ngroups; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
213 result[i] = groups.getObjAt(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
214 }
a61af66fc99e Initial load
duke
parents:
diff changeset
215 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
216 }
a61af66fc99e Initial load
duke
parents:
diff changeset
217
a61af66fc99e Initial load
duke
parents:
diff changeset
218 private static void initThreadFields() {
a61af66fc99e Initial load
duke
parents:
diff changeset
219 if (threadNameField == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
220 SystemDictionary sysDict = VM.getVM().getSystemDictionary();
a61af66fc99e Initial load
duke
parents:
diff changeset
221 InstanceKlass k = sysDict.getThreadKlass();
23802
efe013052465 8059677: Thread.getName() instantiates Strings
shade
parents: 22841
diff changeset
222 threadNameField = (OopField) k.findField("name", "Ljava/lang/String;");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
223 threadGroupField = (OopField) k.findField("group", "Ljava/lang/ThreadGroup;");
a61af66fc99e Initial load
duke
parents:
diff changeset
224 threadEETopField = (LongField) k.findField("eetop", "J");
22841
f9d003ea9023 8046282: SA update
poonam
parents: 6169
diff changeset
225 threadTIDField = (LongField) k.findField("tid", "J");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
226 threadStatusField = (IntField) k.findField("threadStatus", "I");
a61af66fc99e Initial load
duke
parents:
diff changeset
227 threadParkBlockerField = (OopField) k.findField("parkBlocker",
a61af66fc99e Initial load
duke
parents:
diff changeset
228 "Ljava/lang/Object;");
a61af66fc99e Initial load
duke
parents:
diff changeset
229 TypeDataBase db = VM.getVM().getTypeDataBase();
a61af66fc99e Initial load
duke
parents:
diff changeset
230 THREAD_STATUS_NEW = db.lookupIntConstant("java_lang_Thread::NEW").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
231 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
232 Other enum constants are not needed as of now. Uncomment these as and when needed.
a61af66fc99e Initial load
duke
parents:
diff changeset
233
a61af66fc99e Initial load
duke
parents:
diff changeset
234 THREAD_STATUS_RUNNABLE = db.lookupIntConstant("java_lang_Thread::RUNNABLE").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
235 THREAD_STATUS_SLEEPING = db.lookupIntConstant("java_lang_Thread::SLEEPING").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
236 THREAD_STATUS_IN_OBJECT_WAIT = db.lookupIntConstant("java_lang_Thread::IN_OBJECT_WAIT").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
237 THREAD_STATUS_IN_OBJECT_WAIT_TIMED = db.lookupIntConstant("java_lang_Thread::IN_OBJECT_WAIT_TIMED").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
238 THREAD_STATUS_PARKED = db.lookupIntConstant("java_lang_Thread::PARKED").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
239 THREAD_STATUS_PARKED_TIMED = db.lookupIntConstant("java_lang_Thread::PARKED_TIMED").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
240 THREAD_STATUS_BLOCKED_ON_MONITOR_ENTER = db.lookupIntConstant("java_lang_Thread::BLOCKED_ON_MONITOR_ENTER").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
241 THREAD_STATUS_TERMINATED = db.lookupIntConstant("java_lang_Thread::TERMINATED").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
242 */
a61af66fc99e Initial load
duke
parents:
diff changeset
243
a61af66fc99e Initial load
duke
parents:
diff changeset
244 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
245 // it is okay to miss threadStatusField, because this was
a61af66fc99e Initial load
duke
parents:
diff changeset
246 // introduced only in 1.5 JDK.
a61af66fc99e Initial load
duke
parents:
diff changeset
247 Assert.that(threadNameField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
248 threadGroupField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
249 threadEETopField != null, "must find all java.lang.Thread fields");
a61af66fc99e Initial load
duke
parents:
diff changeset
250 }
a61af66fc99e Initial load
duke
parents:
diff changeset
251 }
a61af66fc99e Initial load
duke
parents:
diff changeset
252 }
a61af66fc99e Initial load
duke
parents:
diff changeset
253
a61af66fc99e Initial load
duke
parents:
diff changeset
254 public static Oop threadOopGetThreadGroup(Oop threadOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
255 initThreadFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
256 return threadGroupField.getValue(threadOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
257 }
a61af66fc99e Initial load
duke
parents:
diff changeset
258
a61af66fc99e Initial load
duke
parents:
diff changeset
259 public static String threadOopGetName(Oop threadOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
260 initThreadFields();
23802
efe013052465 8059677: Thread.getName() instantiates Strings
shade
parents: 22841
diff changeset
261 return stringOopToString(threadNameField.getValue(threadOop));
0
a61af66fc99e Initial load
duke
parents:
diff changeset
262 }
a61af66fc99e Initial load
duke
parents:
diff changeset
263
a61af66fc99e Initial load
duke
parents:
diff changeset
264 /** May return null if, e.g., thread was not started */
a61af66fc99e Initial load
duke
parents:
diff changeset
265 public static JavaThread threadOopGetJavaThread(Oop threadOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
266 initThreadFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
267 Address addr = threadOop.getHandle().getAddressAt(threadEETopField.getOffset());
a61af66fc99e Initial load
duke
parents:
diff changeset
268 if (addr == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
269 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
270 }
a61af66fc99e Initial load
duke
parents:
diff changeset
271 return VM.getVM().getThreads().createJavaThreadWrapper(addr);
a61af66fc99e Initial load
duke
parents:
diff changeset
272 }
a61af66fc99e Initial load
duke
parents:
diff changeset
273
22841
f9d003ea9023 8046282: SA update
poonam
parents: 6169
diff changeset
274 public static long threadOopGetTID(Oop threadOop) {
f9d003ea9023 8046282: SA update
poonam
parents: 6169
diff changeset
275 initThreadFields();
f9d003ea9023 8046282: SA update
poonam
parents: 6169
diff changeset
276 if (threadTIDField != null) {
f9d003ea9023 8046282: SA update
poonam
parents: 6169
diff changeset
277 return threadTIDField.getValue(threadOop);
f9d003ea9023 8046282: SA update
poonam
parents: 6169
diff changeset
278 } else {
f9d003ea9023 8046282: SA update
poonam
parents: 6169
diff changeset
279 return 0;
f9d003ea9023 8046282: SA update
poonam
parents: 6169
diff changeset
280 }
f9d003ea9023 8046282: SA update
poonam
parents: 6169
diff changeset
281 }
f9d003ea9023 8046282: SA update
poonam
parents: 6169
diff changeset
282
0
a61af66fc99e Initial load
duke
parents:
diff changeset
283 /** returns value of java.lang.Thread.threadStatus field */
a61af66fc99e Initial load
duke
parents:
diff changeset
284 public static int threadOopGetThreadStatus(Oop threadOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
285 initThreadFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
286 // The threadStatus is only present starting in 1.5
a61af66fc99e Initial load
duke
parents:
diff changeset
287 if (threadStatusField != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
288 return (int) threadStatusField.getValue(threadOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
289 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
290 // All we can easily figure out is if it is alive, but that is
a61af66fc99e Initial load
duke
parents:
diff changeset
291 // enough info for a valid unknown status.
a61af66fc99e Initial load
duke
parents:
diff changeset
292 JavaThread thr = threadOopGetJavaThread(threadOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
293 if (thr == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
294 // the thread hasn't run yet or is in the process of exiting
a61af66fc99e Initial load
duke
parents:
diff changeset
295 return THREAD_STATUS_NEW;
a61af66fc99e Initial load
duke
parents:
diff changeset
296 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
297 return JVMTI_THREAD_STATE_ALIVE;
a61af66fc99e Initial load
duke
parents:
diff changeset
298 }
a61af66fc99e Initial load
duke
parents:
diff changeset
299 }
a61af66fc99e Initial load
duke
parents:
diff changeset
300 }
a61af66fc99e Initial load
duke
parents:
diff changeset
301
a61af66fc99e Initial load
duke
parents:
diff changeset
302 /** returns value of java.lang.Thread.parkBlocker field */
a61af66fc99e Initial load
duke
parents:
diff changeset
303 public static Oop threadOopGetParkBlocker(Oop threadOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
304 initThreadFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
305 if (threadParkBlockerField != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
306 return threadParkBlockerField.getValue(threadOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
307 }
a61af66fc99e Initial load
duke
parents:
diff changeset
308 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
309 }
a61af66fc99e Initial load
duke
parents:
diff changeset
310
a61af66fc99e Initial load
duke
parents:
diff changeset
311 // initialize fields for j.u.c.l AbstractOwnableSynchornizer class
a61af66fc99e Initial load
duke
parents:
diff changeset
312 private static void initAbsOwnSyncFields() {
a61af66fc99e Initial load
duke
parents:
diff changeset
313 if (absOwnSyncOwnerThreadField == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
314 SystemDictionary sysDict = VM.getVM().getSystemDictionary();
a61af66fc99e Initial load
duke
parents:
diff changeset
315 InstanceKlass k = sysDict.getAbstractOwnableSynchronizerKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
316 absOwnSyncOwnerThreadField =
a61af66fc99e Initial load
duke
parents:
diff changeset
317 (OopField) k.findField("exclusiveOwnerThread",
a61af66fc99e Initial load
duke
parents:
diff changeset
318 "Ljava/lang/Thread;");
a61af66fc99e Initial load
duke
parents:
diff changeset
319 }
a61af66fc99e Initial load
duke
parents:
diff changeset
320 }
a61af66fc99e Initial load
duke
parents:
diff changeset
321
a61af66fc99e Initial load
duke
parents:
diff changeset
322 // return exclusiveOwnerThread field of AbstractOwnableSynchronizer class
a61af66fc99e Initial load
duke
parents:
diff changeset
323 public static Oop abstractOwnableSynchronizerGetOwnerThread(Oop oop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
324 initAbsOwnSyncFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
325 if (absOwnSyncOwnerThreadField == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
326 return null; // pre-1.6 VM?
a61af66fc99e Initial load
duke
parents:
diff changeset
327 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
328 return absOwnSyncOwnerThreadField.getValue(oop);
a61af66fc99e Initial load
duke
parents:
diff changeset
329 }
a61af66fc99e Initial load
duke
parents:
diff changeset
330 }
a61af66fc99e Initial load
duke
parents:
diff changeset
331 }