1 /* 2 * Copyright (c) 2000, 2012, 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.oops; 26 27 import java.util.*; 28 29 import sun.jvm.hotspot.debugger.*; 30 import sun.jvm.hotspot.memory.*; 31 import sun.jvm.hotspot.runtime.*; 32 import sun.jvm.hotspot.types.TypeDataBase; 33 import sun.jvm.hotspot.utilities.*; 34 import sun.jvm.hotspot.jdi.JVMTIThreadState; 35 36 /** A utility class encapsulating useful oop operations */ 37 38 public class OopUtilities implements /* imports */ JVMTIThreadState { 39 40 // FIXME: access should be synchronized and cleared when VM is 41 // resumed 42 // String fields 43 private static ByteField coderField; 44 private static OopField valueField; 45 // ThreadGroup fields 46 private static OopField threadGroupParentField; 47 private static OopField threadGroupNameField; 48 private static IntField threadGroupNThreadsField; 49 private static OopField threadGroupThreadsField; 50 private static IntField threadGroupNGroupsField; 51 private static OopField threadGroupGroupsField; 52 // Thread fields 53 private static OopField threadNameField; 54 private static OopField threadGroupField; 55 private static LongField threadEETopField; 56 //tid field is new since 1.5 57 private static LongField threadTIDField; 58 // threadStatus field is new since 1.5 59 private static IntField threadStatusField; 60 // parkBlocker field is new since 1.6 61 private static OopField threadParkBlockerField; 62 63 // possible values of java_lang_Thread::ThreadStatus 64 private static int THREAD_STATUS_NEW; 65 /* 66 Other enum constants are not needed as of now. Uncomment these as and when needed. 67 68 private static int THREAD_STATUS_RUNNABLE; 69 private static int THREAD_STATUS_SLEEPING; 70 private static int THREAD_STATUS_IN_OBJECT_WAIT; 71 private static int THREAD_STATUS_IN_OBJECT_WAIT_TIMED; 72 private static int THREAD_STATUS_PARKED; 73 private static int THREAD_STATUS_PARKED_TIMED; 74 private static int THREAD_STATUS_BLOCKED_ON_MONITOR_ENTER; 75 private static int THREAD_STATUS_TERMINATED; 76 */ 77 78 // java.util.concurrent.locks.AbstractOwnableSynchronizer fields 79 private static OopField absOwnSyncOwnerThreadField; 80 81 static { 82 VM.registerVMInitializedObserver(new Observer() { 83 public void update(Observable o, Object data) { 84 initialize(VM.getVM().getTypeDataBase()); 85 } 86 }); 87 } 88 89 private static synchronized void initialize(TypeDataBase db) { 90 // FIXME: don't need this observer; however, do need a VM resumed 91 // and suspended observer to refetch fields 92 } 93 94 public static String charArrayToString(TypeArray charArray) { 95 if (charArray == null) { 96 return null; 97 } 98 int length = (int)charArray.getLength(); 99 StringBuffer buf = new StringBuffer(length); 100 for (int i = 0; i < length; i++) { 101 buf.append(charArray.getCharAt(i)); 102 } 103 return buf.toString(); 104 } 105 106 public static String byteArrayToString(TypeArray byteArray, byte coder) { 107 if (byteArray == null) { 108 return null; 109 } 110 int length = (int)byteArray.getLength() >> coder; 111 StringBuffer buf = new StringBuffer(length); 112 if (coder == 0) { 113 // Latin1 encoded 114 for (int i = 0; i < length; i++) { 115 buf.append((char)(byteArray.getByteAt(i) & 0xff)); 116 } 117 } else { 118 // UTF16 encoded 119 for (int i = 0; i < length; i++) { 120 buf.append(byteArray.getCharAt(i)); 121 } 122 } 123 return buf.toString(); 124 } 125 126 public static String escapeString(String s) { 127 StringBuilder sb = null; 128 for (int index = 0; index < s.length(); index++) { 129 char value = s.charAt(index); 130 if (value >= 32 && value < 127 || value == '\'' || value == '\\') { 131 if (sb != null) { 132 sb.append(value); 133 } 134 } else { 135 if (sb == null) { 136 sb = new StringBuilder(s.length() * 2); 137 sb.append(s, 0, index); 138 } 139 sb.append("\\u"); 140 if (value < 0x10) sb.append("000"); 141 else if (value < 0x100) sb.append("00"); 142 else if (value < 0x1000) sb.append("0"); 143 sb.append(Integer.toHexString(value)); 144 } 145 } 146 if (sb != null) { 147 return sb.toString(); 148 } 149 return s; 150 } 151 152 public static String stringOopToString(Oop stringOop) { 153 InstanceKlass k = (InstanceKlass) stringOop.getKlass(); 154 coderField = (ByteField) k.findField("coder", "B"); 155 valueField = (OopField) k.findField("value", "[B"); 156 if (Assert.ASSERTS_ENABLED) { 157 Assert.that(coderField != null, "Field \'coder\' of java.lang.String not found"); 158 Assert.that(valueField != null, "Field \'value\' of java.lang.String not found"); 159 } 160 return byteArrayToString((TypeArray) valueField.getValue(stringOop), coderField.getValue(stringOop)); 161 } 162 163 public static String stringOopToEscapedString(Oop stringOop) { 164 return escapeString(stringOopToString(stringOop)); 165 } 166 167 private static void initThreadGroupFields() { 168 if (threadGroupParentField == null) { 169 SystemDictionary sysDict = VM.getVM().getSystemDictionary(); 170 InstanceKlass k = sysDict.getThreadGroupKlass(); 171 threadGroupParentField = (OopField) k.findField("parent", "Ljava/lang/ThreadGroup;"); 172 threadGroupNameField = (OopField) k.findField("name", "Ljava/lang/String;"); 173 threadGroupNThreadsField = (IntField) k.findField("nthreads", "I"); 174 threadGroupThreadsField = (OopField) k.findField("threads", "[Ljava/lang/Thread;"); 175 threadGroupNGroupsField = (IntField) k.findField("ngroups", "I"); 176 threadGroupGroupsField = (OopField) k.findField("groups", "[Ljava/lang/ThreadGroup;"); 177 if (Assert.ASSERTS_ENABLED) { 178 Assert.that(threadGroupParentField != null && 179 threadGroupNameField != null && 180 threadGroupNThreadsField != null && 181 threadGroupThreadsField != null && 182 threadGroupNGroupsField != null && 183 threadGroupGroupsField != null, "must find all java.lang.ThreadGroup fields"); 184 } 185 } 186 } 187 188 public static Oop threadGroupOopGetParent(Oop threadGroupOop) { 189 initThreadGroupFields(); 190 return threadGroupParentField.getValue(threadGroupOop); 191 } 192 193 public static String threadGroupOopGetName(Oop threadGroupOop) { 194 initThreadGroupFields(); 195 return stringOopToString(threadGroupNameField.getValue(threadGroupOop)); 196 } 197 198 public static Oop[] threadGroupOopGetThreads(Oop threadGroupOop) { 199 initThreadGroupFields(); 200 int nthreads = threadGroupNThreadsField.getValue(threadGroupOop); 201 Oop[] result = new Oop[nthreads]; 202 ObjArray threads = (ObjArray) threadGroupThreadsField.getValue(threadGroupOop); 203 for (int i = 0; i < nthreads; i++) { 204 result[i] = threads.getObjAt(i); 205 } 206 return result; 207 } 208 209 public static Oop[] threadGroupOopGetGroups(Oop threadGroupOop) { 210 initThreadGroupFields(); 211 int ngroups = threadGroupNGroupsField.getValue(threadGroupOop); 212 Oop[] result = new Oop[ngroups]; 213 ObjArray groups = (ObjArray) threadGroupGroupsField.getValue(threadGroupOop); 214 for (int i = 0; i < ngroups; i++) { 215 result[i] = groups.getObjAt(i); 216 } 217 return result; 218 } 219 220 private static void initThreadFields() { 221 if (threadNameField == null) { 222 SystemDictionary sysDict = VM.getVM().getSystemDictionary(); 223 InstanceKlass k = sysDict.getThreadKlass(); 224 threadNameField = (OopField) k.findField("name", "Ljava/lang/String;"); 225 threadGroupField = (OopField) k.findField("group", "Ljava/lang/ThreadGroup;"); 226 threadEETopField = (LongField) k.findField("eetop", "J"); 227 threadTIDField = (LongField) k.findField("tid", "J"); 228 threadStatusField = (IntField) k.findField("threadStatus", "I"); 229 threadParkBlockerField = (OopField) k.findField("parkBlocker", 230 "Ljava/lang/Object;"); 231 TypeDataBase db = VM.getVM().getTypeDataBase(); 232 THREAD_STATUS_NEW = db.lookupIntConstant("java_lang_Thread::NEW").intValue(); 233 /* 234 Other enum constants are not needed as of now. Uncomment these as and when needed. 235 236 THREAD_STATUS_RUNNABLE = db.lookupIntConstant("java_lang_Thread::RUNNABLE").intValue(); 237 THREAD_STATUS_SLEEPING = db.lookupIntConstant("java_lang_Thread::SLEEPING").intValue(); 238 THREAD_STATUS_IN_OBJECT_WAIT = db.lookupIntConstant("java_lang_Thread::IN_OBJECT_WAIT").intValue(); 239 THREAD_STATUS_IN_OBJECT_WAIT_TIMED = db.lookupIntConstant("java_lang_Thread::IN_OBJECT_WAIT_TIMED").intValue(); 240 THREAD_STATUS_PARKED = db.lookupIntConstant("java_lang_Thread::PARKED").intValue(); 241 THREAD_STATUS_PARKED_TIMED = db.lookupIntConstant("java_lang_Thread::PARKED_TIMED").intValue(); 242 THREAD_STATUS_BLOCKED_ON_MONITOR_ENTER = db.lookupIntConstant("java_lang_Thread::BLOCKED_ON_MONITOR_ENTER").intValue(); 243 THREAD_STATUS_TERMINATED = db.lookupIntConstant("java_lang_Thread::TERMINATED").intValue(); 244 */ 245 246 if (Assert.ASSERTS_ENABLED) { 247 // it is okay to miss threadStatusField, because this was 248 // introduced only in 1.5 JDK. 249 Assert.that(threadNameField != null && 250 threadGroupField != null && 251 threadEETopField != null, "must find all java.lang.Thread fields"); 252 } 253 } 254 } 255 256 public static Oop threadOopGetThreadGroup(Oop threadOop) { 257 initThreadFields(); 258 return threadGroupField.getValue(threadOop); 259 } 260 261 public static String threadOopGetName(Oop threadOop) { 262 initThreadFields(); 263 return stringOopToString(threadNameField.getValue(threadOop)); 264 } 265 266 /** May return null if, e.g., thread was not started */ 267 public static JavaThread threadOopGetJavaThread(Oop threadOop) { 268 initThreadFields(); 269 Address addr = threadOop.getHandle().getAddressAt(threadEETopField.getOffset()); 270 if (addr == null) { 271 return null; 272 } 273 return VM.getVM().getThreads().createJavaThreadWrapper(addr); 274 } 275 276 public static long threadOopGetTID(Oop threadOop) { 277 initThreadFields(); 278 if (threadTIDField != null) { 279 return threadTIDField.getValue(threadOop); 280 } else { 281 return 0; 282 } 283 } 284 285 /** returns value of java.lang.Thread.threadStatus field */ 286 public static int threadOopGetThreadStatus(Oop threadOop) { 287 initThreadFields(); 288 // The threadStatus is only present starting in 1.5 289 if (threadStatusField != null) { 290 return (int) threadStatusField.getValue(threadOop); 291 } else { 292 // All we can easily figure out is if it is alive, but that is 293 // enough info for a valid unknown status. 294 JavaThread thr = threadOopGetJavaThread(threadOop); 295 if (thr == null) { 296 // the thread hasn't run yet or is in the process of exiting 297 return THREAD_STATUS_NEW; 298 } else { 299 return JVMTI_THREAD_STATE_ALIVE; 300 } 301 } 302 } 303 304 /** returns value of java.lang.Thread.parkBlocker field */ 305 public static Oop threadOopGetParkBlocker(Oop threadOop) { 306 initThreadFields(); 307 if (threadParkBlockerField != null) { 308 return threadParkBlockerField.getValue(threadOop); 309 } 310 return null; 311 } 312 313 // initialize fields for j.u.c.l AbstractOwnableSynchornizer class 314 private static void initAbsOwnSyncFields() { 315 if (absOwnSyncOwnerThreadField == null) { 316 SystemDictionary sysDict = VM.getVM().getSystemDictionary(); 317 InstanceKlass k = sysDict.getAbstractOwnableSynchronizerKlass(); 318 absOwnSyncOwnerThreadField = 319 (OopField) k.findField("exclusiveOwnerThread", 320 "Ljava/lang/Thread;"); 321 } 322 } 323 324 // return exclusiveOwnerThread field of AbstractOwnableSynchronizer class 325 public static Oop abstractOwnableSynchronizerGetOwnerThread(Oop oop) { 326 initAbsOwnSyncFields(); 327 if (absOwnSyncOwnerThreadField == null) { 328 return null; // pre-1.6 VM? 329 } else { 330 return absOwnSyncOwnerThreadField.getValue(oop); 331 } 332 } 333 }