annotate agent/src/share/classes/sun/jvm/hotspot/oops/OopUtilities.java @ 6972:bd7a7ce2e264

6830717: replay of compilations would help with debugging Summary: When java process crashed in compiler thread, repeat the compilation process will help finding root cause. This is done with using SA dump application class data and replay data from core dump, then use debug version of jvm to recompile the problematic java method. Reviewed-by: kvn, twisti, sspitsyn Contributed-by: yumin.qi@oracle.com
author minqi
date Mon, 12 Nov 2012 14:03:53 -0800
parents cfa2c82f4c04
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
6169
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
2 * Copyright (c) 2000, 2012, 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;
a61af66fc99e Initial load
duke
parents:
diff changeset
57 // threadStatus field is new since 1.5
a61af66fc99e Initial load
duke
parents:
diff changeset
58 private static IntField threadStatusField;
a61af66fc99e Initial load
duke
parents:
diff changeset
59 // parkBlocker field is new since 1.6
a61af66fc99e Initial load
duke
parents:
diff changeset
60 private static OopField threadParkBlockerField;
a61af66fc99e Initial load
duke
parents:
diff changeset
61
a61af66fc99e Initial load
duke
parents:
diff changeset
62 // possible values of java_lang_Thread::ThreadStatus
a61af66fc99e Initial load
duke
parents:
diff changeset
63 private static int THREAD_STATUS_NEW;
a61af66fc99e Initial load
duke
parents:
diff changeset
64 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
65 Other enum constants are not needed as of now. Uncomment these as and when needed.
a61af66fc99e Initial load
duke
parents:
diff changeset
66
a61af66fc99e Initial load
duke
parents:
diff changeset
67 private static int THREAD_STATUS_RUNNABLE;
a61af66fc99e Initial load
duke
parents:
diff changeset
68 private static int THREAD_STATUS_SLEEPING;
a61af66fc99e Initial load
duke
parents:
diff changeset
69 private static int THREAD_STATUS_IN_OBJECT_WAIT;
a61af66fc99e Initial load
duke
parents:
diff changeset
70 private static int THREAD_STATUS_IN_OBJECT_WAIT_TIMED;
a61af66fc99e Initial load
duke
parents:
diff changeset
71 private static int THREAD_STATUS_PARKED;
a61af66fc99e Initial load
duke
parents:
diff changeset
72 private static int THREAD_STATUS_PARKED_TIMED;
a61af66fc99e Initial load
duke
parents:
diff changeset
73 private static int THREAD_STATUS_BLOCKED_ON_MONITOR_ENTER;
a61af66fc99e Initial load
duke
parents:
diff changeset
74 private static int THREAD_STATUS_TERMINATED;
a61af66fc99e Initial load
duke
parents:
diff changeset
75 */
a61af66fc99e Initial load
duke
parents:
diff changeset
76
a61af66fc99e Initial load
duke
parents:
diff changeset
77 // java.util.concurrent.locks.AbstractOwnableSynchronizer fields
a61af66fc99e Initial load
duke
parents:
diff changeset
78 private static OopField absOwnSyncOwnerThreadField;
a61af66fc99e Initial load
duke
parents:
diff changeset
79
a61af66fc99e Initial load
duke
parents:
diff changeset
80 static {
a61af66fc99e Initial load
duke
parents:
diff changeset
81 VM.registerVMInitializedObserver(new Observer() {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 public void update(Observable o, Object data) {
a61af66fc99e Initial load
duke
parents:
diff changeset
83 initialize(VM.getVM().getTypeDataBase());
a61af66fc99e Initial load
duke
parents:
diff changeset
84 }
a61af66fc99e Initial load
duke
parents:
diff changeset
85 });
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 private static synchronized void initialize(TypeDataBase db) {
a61af66fc99e Initial load
duke
parents:
diff changeset
89 // FIXME: don't need this observer; however, do need a VM resumed
a61af66fc99e Initial load
duke
parents:
diff changeset
90 // and suspended observer to refetch fields
a61af66fc99e Initial load
duke
parents:
diff changeset
91 }
a61af66fc99e Initial load
duke
parents:
diff changeset
92
a61af66fc99e Initial load
duke
parents:
diff changeset
93 public static String charArrayToString(TypeArray charArray) {
a61af66fc99e Initial load
duke
parents:
diff changeset
94 if (charArray == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
96 }
a61af66fc99e Initial load
duke
parents:
diff changeset
97 return charArrayToString(charArray, 0, (int) charArray.getLength());
a61af66fc99e Initial load
duke
parents:
diff changeset
98 }
a61af66fc99e Initial load
duke
parents:
diff changeset
99
a61af66fc99e Initial load
duke
parents:
diff changeset
100 public static String charArrayToString(TypeArray charArray, int offset, int length) {
a61af66fc99e Initial load
duke
parents:
diff changeset
101 if (charArray == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
102 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
103 }
a61af66fc99e Initial load
duke
parents:
diff changeset
104 final int limit = offset + length;
a61af66fc99e Initial load
duke
parents:
diff changeset
105 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
106 Assert.that(offset >= 0 && limit <= charArray.getLength(), "out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
107 }
a61af66fc99e Initial load
duke
parents:
diff changeset
108 StringBuffer buf = new StringBuffer(length);
a61af66fc99e Initial load
duke
parents:
diff changeset
109 for (int i = offset; i < limit; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
110 buf.append(charArray.getCharAt(i));
a61af66fc99e Initial load
duke
parents:
diff changeset
111 }
a61af66fc99e Initial load
duke
parents:
diff changeset
112 return buf.toString();
a61af66fc99e Initial load
duke
parents:
diff changeset
113 }
a61af66fc99e Initial load
duke
parents:
diff changeset
114
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
115 public static String escapeString(String s) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
116 StringBuilder sb = null;
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
117 for (int index = 0; index < s.length(); index++) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
118 char value = s.charAt(index);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
119 if (value >= 32 && value < 127 || value == '\'' || value == '\\') {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
120 if (sb != null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
121 sb.append(value);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
122 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
123 } else {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
124 if (sb == null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
125 sb = new StringBuilder(s.length() * 2);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
126 sb.append(s, 0, index);
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
127 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
128 sb.append("\\u");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
129 if (value < 0x10) sb.append("000");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
130 else if (value < 0x100) sb.append("00");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
131 else if (value < 0x1000) sb.append("0");
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
132 sb.append(Integer.toHexString(value));
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
133 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
134 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
135 if (sb != null) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
136 return sb.toString();
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
137 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
138 return s;
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
0
a61af66fc99e Initial load
duke
parents:
diff changeset
141 public static String stringOopToString(Oop stringOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
142 if (offsetField == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
143 InstanceKlass k = (InstanceKlass) stringOop.getKlass();
6169
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
144 offsetField = (IntField) k.findField("offset", "I"); // optional
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
145 countField = (IntField) k.findField("count", "I"); // optional
0
a61af66fc99e Initial load
duke
parents:
diff changeset
146 valueField = (OopField) k.findField("value", "[C");
a61af66fc99e Initial load
duke
parents:
diff changeset
147 if (Assert.ASSERTS_ENABLED) {
6169
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
148 Assert.that(valueField != null, "Field \'value\' of java.lang.String not found");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
149 }
a61af66fc99e Initial load
duke
parents:
diff changeset
150 }
6169
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
151 if (offsetField != null && countField != null) {
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
152 return charArrayToString((TypeArray) valueField.getValue(stringOop),
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
153 offsetField.getValue(stringOop),
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
154 countField.getValue(stringOop));
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
155 }
cfa2c82f4c04 7175133: jinfo failed to get system properties after 6924259
minqi
parents: 3939
diff changeset
156 return charArrayToString((TypeArray) valueField.getValue(stringOop));
0
a61af66fc99e Initial load
duke
parents:
diff changeset
157 }
a61af66fc99e Initial load
duke
parents:
diff changeset
158
3939
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
159 public static String stringOopToEscapedString(Oop stringOop) {
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
160 return escapeString(stringOopToString(stringOop));
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
161 }
f6f3bb0ee072 7088955: add C2 IR support to the SA
never
parents: 2411
diff changeset
162
0
a61af66fc99e Initial load
duke
parents:
diff changeset
163 private static void initThreadGroupFields() {
a61af66fc99e Initial load
duke
parents:
diff changeset
164 if (threadGroupParentField == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
165 SystemDictionary sysDict = VM.getVM().getSystemDictionary();
a61af66fc99e Initial load
duke
parents:
diff changeset
166 InstanceKlass k = sysDict.getThreadGroupKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
167 threadGroupParentField = (OopField) k.findField("parent", "Ljava/lang/ThreadGroup;");
a61af66fc99e Initial load
duke
parents:
diff changeset
168 threadGroupNameField = (OopField) k.findField("name", "Ljava/lang/String;");
a61af66fc99e Initial load
duke
parents:
diff changeset
169 threadGroupNThreadsField = (IntField) k.findField("nthreads", "I");
a61af66fc99e Initial load
duke
parents:
diff changeset
170 threadGroupThreadsField = (OopField) k.findField("threads", "[Ljava/lang/Thread;");
a61af66fc99e Initial load
duke
parents:
diff changeset
171 threadGroupNGroupsField = (IntField) k.findField("ngroups", "I");
a61af66fc99e Initial load
duke
parents:
diff changeset
172 threadGroupGroupsField = (OopField) k.findField("groups", "[Ljava/lang/ThreadGroup;");
a61af66fc99e Initial load
duke
parents:
diff changeset
173 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
174 Assert.that(threadGroupParentField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
175 threadGroupNameField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
176 threadGroupNThreadsField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
177 threadGroupThreadsField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
178 threadGroupNGroupsField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
179 threadGroupGroupsField != null, "must find all java.lang.ThreadGroup fields");
a61af66fc99e Initial load
duke
parents:
diff changeset
180 }
a61af66fc99e Initial load
duke
parents:
diff changeset
181 }
a61af66fc99e Initial load
duke
parents:
diff changeset
182 }
a61af66fc99e Initial load
duke
parents:
diff changeset
183
a61af66fc99e Initial load
duke
parents:
diff changeset
184 public static Oop threadGroupOopGetParent(Oop threadGroupOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
185 initThreadGroupFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
186 return threadGroupParentField.getValue(threadGroupOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188
a61af66fc99e Initial load
duke
parents:
diff changeset
189 public static String threadGroupOopGetName(Oop threadGroupOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
190 initThreadGroupFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
191 return stringOopToString(threadGroupNameField.getValue(threadGroupOop));
a61af66fc99e Initial load
duke
parents:
diff changeset
192 }
a61af66fc99e Initial load
duke
parents:
diff changeset
193
a61af66fc99e Initial load
duke
parents:
diff changeset
194 public static Oop[] threadGroupOopGetThreads(Oop threadGroupOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
195 initThreadGroupFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
196 int nthreads = threadGroupNThreadsField.getValue(threadGroupOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
197 Oop[] result = new Oop[nthreads];
a61af66fc99e Initial load
duke
parents:
diff changeset
198 ObjArray threads = (ObjArray) threadGroupThreadsField.getValue(threadGroupOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
199 for (int i = 0; i < nthreads; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
200 result[i] = threads.getObjAt(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
201 }
a61af66fc99e Initial load
duke
parents:
diff changeset
202 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
203 }
a61af66fc99e Initial load
duke
parents:
diff changeset
204
a61af66fc99e Initial load
duke
parents:
diff changeset
205 public static Oop[] threadGroupOopGetGroups(Oop threadGroupOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
206 initThreadGroupFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
207 int ngroups = threadGroupNGroupsField.getValue(threadGroupOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
208 Oop[] result = new Oop[ngroups];
a61af66fc99e Initial load
duke
parents:
diff changeset
209 ObjArray groups = (ObjArray) threadGroupGroupsField.getValue(threadGroupOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
210 for (int i = 0; i < ngroups; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
211 result[i] = groups.getObjAt(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
212 }
a61af66fc99e Initial load
duke
parents:
diff changeset
213 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
214 }
a61af66fc99e Initial load
duke
parents:
diff changeset
215
a61af66fc99e Initial load
duke
parents:
diff changeset
216 private static void initThreadFields() {
a61af66fc99e Initial load
duke
parents:
diff changeset
217 if (threadNameField == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
218 SystemDictionary sysDict = VM.getVM().getSystemDictionary();
a61af66fc99e Initial load
duke
parents:
diff changeset
219 InstanceKlass k = sysDict.getThreadKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
220 threadNameField = (OopField) k.findField("name", "[C");
a61af66fc99e Initial load
duke
parents:
diff changeset
221 threadGroupField = (OopField) k.findField("group", "Ljava/lang/ThreadGroup;");
a61af66fc99e Initial load
duke
parents:
diff changeset
222 threadEETopField = (LongField) k.findField("eetop", "J");
a61af66fc99e Initial load
duke
parents:
diff changeset
223 threadStatusField = (IntField) k.findField("threadStatus", "I");
a61af66fc99e Initial load
duke
parents:
diff changeset
224 threadParkBlockerField = (OopField) k.findField("parkBlocker",
a61af66fc99e Initial load
duke
parents:
diff changeset
225 "Ljava/lang/Object;");
a61af66fc99e Initial load
duke
parents:
diff changeset
226 TypeDataBase db = VM.getVM().getTypeDataBase();
a61af66fc99e Initial load
duke
parents:
diff changeset
227 THREAD_STATUS_NEW = db.lookupIntConstant("java_lang_Thread::NEW").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
228 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
229 Other enum constants are not needed as of now. Uncomment these as and when needed.
a61af66fc99e Initial load
duke
parents:
diff changeset
230
a61af66fc99e Initial load
duke
parents:
diff changeset
231 THREAD_STATUS_RUNNABLE = db.lookupIntConstant("java_lang_Thread::RUNNABLE").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
232 THREAD_STATUS_SLEEPING = db.lookupIntConstant("java_lang_Thread::SLEEPING").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
233 THREAD_STATUS_IN_OBJECT_WAIT = db.lookupIntConstant("java_lang_Thread::IN_OBJECT_WAIT").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
234 THREAD_STATUS_IN_OBJECT_WAIT_TIMED = db.lookupIntConstant("java_lang_Thread::IN_OBJECT_WAIT_TIMED").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
235 THREAD_STATUS_PARKED = db.lookupIntConstant("java_lang_Thread::PARKED").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
236 THREAD_STATUS_PARKED_TIMED = db.lookupIntConstant("java_lang_Thread::PARKED_TIMED").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
237 THREAD_STATUS_BLOCKED_ON_MONITOR_ENTER = db.lookupIntConstant("java_lang_Thread::BLOCKED_ON_MONITOR_ENTER").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
238 THREAD_STATUS_TERMINATED = db.lookupIntConstant("java_lang_Thread::TERMINATED").intValue();
a61af66fc99e Initial load
duke
parents:
diff changeset
239 */
a61af66fc99e Initial load
duke
parents:
diff changeset
240
a61af66fc99e Initial load
duke
parents:
diff changeset
241 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
242 // it is okay to miss threadStatusField, because this was
a61af66fc99e Initial load
duke
parents:
diff changeset
243 // introduced only in 1.5 JDK.
a61af66fc99e Initial load
duke
parents:
diff changeset
244 Assert.that(threadNameField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
245 threadGroupField != null &&
a61af66fc99e Initial load
duke
parents:
diff changeset
246 threadEETopField != null, "must find all java.lang.Thread fields");
a61af66fc99e Initial load
duke
parents:
diff changeset
247 }
a61af66fc99e Initial load
duke
parents:
diff changeset
248 }
a61af66fc99e Initial load
duke
parents:
diff changeset
249 }
a61af66fc99e Initial load
duke
parents:
diff changeset
250
a61af66fc99e Initial load
duke
parents:
diff changeset
251 public static Oop threadOopGetThreadGroup(Oop threadOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
252 initThreadFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
253 return threadGroupField.getValue(threadOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
254 }
a61af66fc99e Initial load
duke
parents:
diff changeset
255
a61af66fc99e Initial load
duke
parents:
diff changeset
256 public static String threadOopGetName(Oop threadOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
257 initThreadFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
258 return charArrayToString((TypeArray) threadNameField.getValue(threadOop));
a61af66fc99e Initial load
duke
parents:
diff changeset
259 }
a61af66fc99e Initial load
duke
parents:
diff changeset
260
a61af66fc99e Initial load
duke
parents:
diff changeset
261 /** May return null if, e.g., thread was not started */
a61af66fc99e Initial load
duke
parents:
diff changeset
262 public static JavaThread threadOopGetJavaThread(Oop threadOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
263 initThreadFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
264 Address addr = threadOop.getHandle().getAddressAt(threadEETopField.getOffset());
a61af66fc99e Initial load
duke
parents:
diff changeset
265 if (addr == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
266 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
267 }
a61af66fc99e Initial load
duke
parents:
diff changeset
268 return VM.getVM().getThreads().createJavaThreadWrapper(addr);
a61af66fc99e Initial load
duke
parents:
diff changeset
269 }
a61af66fc99e Initial load
duke
parents:
diff changeset
270
a61af66fc99e Initial load
duke
parents:
diff changeset
271 /** returns value of java.lang.Thread.threadStatus field */
a61af66fc99e Initial load
duke
parents:
diff changeset
272 public static int threadOopGetThreadStatus(Oop threadOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
273 initThreadFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
274 // The threadStatus is only present starting in 1.5
a61af66fc99e Initial load
duke
parents:
diff changeset
275 if (threadStatusField != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
276 return (int) threadStatusField.getValue(threadOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
277 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
278 // All we can easily figure out is if it is alive, but that is
a61af66fc99e Initial load
duke
parents:
diff changeset
279 // enough info for a valid unknown status.
a61af66fc99e Initial load
duke
parents:
diff changeset
280 JavaThread thr = threadOopGetJavaThread(threadOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
281 if (thr == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
282 // the thread hasn't run yet or is in the process of exiting
a61af66fc99e Initial load
duke
parents:
diff changeset
283 return THREAD_STATUS_NEW;
a61af66fc99e Initial load
duke
parents:
diff changeset
284 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
285 return JVMTI_THREAD_STATE_ALIVE;
a61af66fc99e Initial load
duke
parents:
diff changeset
286 }
a61af66fc99e Initial load
duke
parents:
diff changeset
287 }
a61af66fc99e Initial load
duke
parents:
diff changeset
288 }
a61af66fc99e Initial load
duke
parents:
diff changeset
289
a61af66fc99e Initial load
duke
parents:
diff changeset
290 /** returns value of java.lang.Thread.parkBlocker field */
a61af66fc99e Initial load
duke
parents:
diff changeset
291 public static Oop threadOopGetParkBlocker(Oop threadOop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
292 initThreadFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
293 if (threadParkBlockerField != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
294 return threadParkBlockerField.getValue(threadOop);
a61af66fc99e Initial load
duke
parents:
diff changeset
295 }
a61af66fc99e Initial load
duke
parents:
diff changeset
296 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
297 }
a61af66fc99e Initial load
duke
parents:
diff changeset
298
a61af66fc99e Initial load
duke
parents:
diff changeset
299 // initialize fields for j.u.c.l AbstractOwnableSynchornizer class
a61af66fc99e Initial load
duke
parents:
diff changeset
300 private static void initAbsOwnSyncFields() {
a61af66fc99e Initial load
duke
parents:
diff changeset
301 if (absOwnSyncOwnerThreadField == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
302 SystemDictionary sysDict = VM.getVM().getSystemDictionary();
a61af66fc99e Initial load
duke
parents:
diff changeset
303 InstanceKlass k = sysDict.getAbstractOwnableSynchronizerKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
304 absOwnSyncOwnerThreadField =
a61af66fc99e Initial load
duke
parents:
diff changeset
305 (OopField) k.findField("exclusiveOwnerThread",
a61af66fc99e Initial load
duke
parents:
diff changeset
306 "Ljava/lang/Thread;");
a61af66fc99e Initial load
duke
parents:
diff changeset
307 }
a61af66fc99e Initial load
duke
parents:
diff changeset
308 }
a61af66fc99e Initial load
duke
parents:
diff changeset
309
a61af66fc99e Initial load
duke
parents:
diff changeset
310 // return exclusiveOwnerThread field of AbstractOwnableSynchronizer class
a61af66fc99e Initial load
duke
parents:
diff changeset
311 public static Oop abstractOwnableSynchronizerGetOwnerThread(Oop oop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
312 initAbsOwnSyncFields();
a61af66fc99e Initial load
duke
parents:
diff changeset
313 if (absOwnSyncOwnerThreadField == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
314 return null; // pre-1.6 VM?
a61af66fc99e Initial load
duke
parents:
diff changeset
315 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
316 return absOwnSyncOwnerThreadField.getValue(oop);
a61af66fc99e Initial load
duke
parents:
diff changeset
317 }
a61af66fc99e Initial load
duke
parents:
diff changeset
318 }
a61af66fc99e Initial load
duke
parents:
diff changeset
319 }