202 result[i] = threads.getObjAt(i); 203 } 204 return result; 205 } 206 207 public static Oop[] threadGroupOopGetGroups(Oop threadGroupOop) { 208 initThreadGroupFields(); 209 int ngroups = threadGroupNGroupsField.getValue(threadGroupOop); 210 Oop[] result = new Oop[ngroups]; 211 ObjArray groups = (ObjArray) threadGroupGroupsField.getValue(threadGroupOop); 212 for (int i = 0; i < ngroups; i++) { 213 result[i] = groups.getObjAt(i); 214 } 215 return result; 216 } 217 218 private static void initThreadFields() { 219 if (threadNameField == null) { 220 SystemDictionary sysDict = VM.getVM().getSystemDictionary(); 221 InstanceKlass k = sysDict.getThreadKlass(); 222 threadNameField = (OopField) k.findField("name", "[C"); 223 threadGroupField = (OopField) k.findField("group", "Ljava/lang/ThreadGroup;"); 224 threadEETopField = (LongField) k.findField("eetop", "J"); 225 threadTIDField = (LongField) k.findField("tid", "J"); 226 threadStatusField = (IntField) k.findField("threadStatus", "I"); 227 threadParkBlockerField = (OopField) k.findField("parkBlocker", 228 "Ljava/lang/Object;"); 229 TypeDataBase db = VM.getVM().getTypeDataBase(); 230 THREAD_STATUS_NEW = db.lookupIntConstant("java_lang_Thread::NEW").intValue(); 231 /* 232 Other enum constants are not needed as of now. Uncomment these as and when needed. 233 234 THREAD_STATUS_RUNNABLE = db.lookupIntConstant("java_lang_Thread::RUNNABLE").intValue(); 235 THREAD_STATUS_SLEEPING = db.lookupIntConstant("java_lang_Thread::SLEEPING").intValue(); 236 THREAD_STATUS_IN_OBJECT_WAIT = db.lookupIntConstant("java_lang_Thread::IN_OBJECT_WAIT").intValue(); 237 THREAD_STATUS_IN_OBJECT_WAIT_TIMED = db.lookupIntConstant("java_lang_Thread::IN_OBJECT_WAIT_TIMED").intValue(); 238 THREAD_STATUS_PARKED = db.lookupIntConstant("java_lang_Thread::PARKED").intValue(); 239 THREAD_STATUS_PARKED_TIMED = db.lookupIntConstant("java_lang_Thread::PARKED_TIMED").intValue(); 240 THREAD_STATUS_BLOCKED_ON_MONITOR_ENTER = db.lookupIntConstant("java_lang_Thread::BLOCKED_ON_MONITOR_ENTER").intValue(); 241 THREAD_STATUS_TERMINATED = db.lookupIntConstant("java_lang_Thread::TERMINATED").intValue(); 242 */ 243 244 if (Assert.ASSERTS_ENABLED) { 245 // it is okay to miss threadStatusField, because this was 246 // introduced only in 1.5 JDK. 247 Assert.that(threadNameField != null && 248 threadGroupField != null && 249 threadEETopField != null, "must find all java.lang.Thread fields"); 250 } 251 } 252 } 253 254 public static Oop threadOopGetThreadGroup(Oop threadOop) { 255 initThreadFields(); 256 return threadGroupField.getValue(threadOop); 257 } 258 259 public static String threadOopGetName(Oop threadOop) { 260 initThreadFields(); 261 return charArrayToString((TypeArray) threadNameField.getValue(threadOop)); 262 } 263 264 /** May return null if, e.g., thread was not started */ 265 public static JavaThread threadOopGetJavaThread(Oop threadOop) { 266 initThreadFields(); 267 Address addr = threadOop.getHandle().getAddressAt(threadEETopField.getOffset()); 268 if (addr == null) { 269 return null; 270 } 271 return VM.getVM().getThreads().createJavaThreadWrapper(addr); 272 } 273 274 public static long threadOopGetTID(Oop threadOop) { 275 initThreadFields(); 276 if (threadTIDField != null) { 277 return threadTIDField.getValue(threadOop); 278 } else { 279 return 0; 280 } 281 } | 202 result[i] = threads.getObjAt(i); 203 } 204 return result; 205 } 206 207 public static Oop[] threadGroupOopGetGroups(Oop threadGroupOop) { 208 initThreadGroupFields(); 209 int ngroups = threadGroupNGroupsField.getValue(threadGroupOop); 210 Oop[] result = new Oop[ngroups]; 211 ObjArray groups = (ObjArray) threadGroupGroupsField.getValue(threadGroupOop); 212 for (int i = 0; i < ngroups; i++) { 213 result[i] = groups.getObjAt(i); 214 } 215 return result; 216 } 217 218 private static void initThreadFields() { 219 if (threadNameField == null) { 220 SystemDictionary sysDict = VM.getVM().getSystemDictionary(); 221 InstanceKlass k = sysDict.getThreadKlass(); 222 threadNameField = (OopField) k.findField("name", "Ljava/lang/String;"); 223 threadGroupField = (OopField) k.findField("group", "Ljava/lang/ThreadGroup;"); 224 threadEETopField = (LongField) k.findField("eetop", "J"); 225 threadTIDField = (LongField) k.findField("tid", "J"); 226 threadStatusField = (IntField) k.findField("threadStatus", "I"); 227 threadParkBlockerField = (OopField) k.findField("parkBlocker", 228 "Ljava/lang/Object;"); 229 TypeDataBase db = VM.getVM().getTypeDataBase(); 230 THREAD_STATUS_NEW = db.lookupIntConstant("java_lang_Thread::NEW").intValue(); 231 /* 232 Other enum constants are not needed as of now. Uncomment these as and when needed. 233 234 THREAD_STATUS_RUNNABLE = db.lookupIntConstant("java_lang_Thread::RUNNABLE").intValue(); 235 THREAD_STATUS_SLEEPING = db.lookupIntConstant("java_lang_Thread::SLEEPING").intValue(); 236 THREAD_STATUS_IN_OBJECT_WAIT = db.lookupIntConstant("java_lang_Thread::IN_OBJECT_WAIT").intValue(); 237 THREAD_STATUS_IN_OBJECT_WAIT_TIMED = db.lookupIntConstant("java_lang_Thread::IN_OBJECT_WAIT_TIMED").intValue(); 238 THREAD_STATUS_PARKED = db.lookupIntConstant("java_lang_Thread::PARKED").intValue(); 239 THREAD_STATUS_PARKED_TIMED = db.lookupIntConstant("java_lang_Thread::PARKED_TIMED").intValue(); 240 THREAD_STATUS_BLOCKED_ON_MONITOR_ENTER = db.lookupIntConstant("java_lang_Thread::BLOCKED_ON_MONITOR_ENTER").intValue(); 241 THREAD_STATUS_TERMINATED = db.lookupIntConstant("java_lang_Thread::TERMINATED").intValue(); 242 */ 243 244 if (Assert.ASSERTS_ENABLED) { 245 // it is okay to miss threadStatusField, because this was 246 // introduced only in 1.5 JDK. 247 Assert.that(threadNameField != null && 248 threadGroupField != null && 249 threadEETopField != null, "must find all java.lang.Thread fields"); 250 } 251 } 252 } 253 254 public static Oop threadOopGetThreadGroup(Oop threadOop) { 255 initThreadFields(); 256 return threadGroupField.getValue(threadOop); 257 } 258 259 public static String threadOopGetName(Oop threadOop) { 260 initThreadFields(); 261 return stringOopToString(threadNameField.getValue(threadOop)); 262 } 263 264 /** May return null if, e.g., thread was not started */ 265 public static JavaThread threadOopGetJavaThread(Oop threadOop) { 266 initThreadFields(); 267 Address addr = threadOop.getHandle().getAddressAt(threadEETopField.getOffset()); 268 if (addr == null) { 269 return null; 270 } 271 return VM.getVM().getThreads().createJavaThreadWrapper(addr); 272 } 273 274 public static long threadOopGetTID(Oop threadOop) { 275 initThreadFields(); 276 if (threadTIDField != null) { 277 return threadTIDField.getValue(threadOop); 278 } else { 279 return 0; 280 } 281 } |