1 /* 2 * Copyright 2000-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25 package sun.jvm.hotspot.code; 26 27 import java.io.*; 28 import java.util.*; 29 import sun.jvm.hotspot.debugger.*; 30 import sun.jvm.hotspot.memory.*; 31 import sun.jvm.hotspot.oops.*; 32 import sun.jvm.hotspot.runtime.*; 33 import sun.jvm.hotspot.types.*; 34 import sun.jvm.hotspot.utilities.*; 35 36 public class NMethod extends CodeBlob { 37 private static long pcDescSize; 38 private static CIntegerField zombieInstructionSizeField; 39 private static sun.jvm.hotspot.types.OopField methodField; 40 /** != InvocationEntryBci if this nmethod is an on-stack replacement method */ 41 private static CIntegerField entryBCIField; 42 /** To support simple linked-list chaining of nmethods */ 43 private static AddressField osrLinkField; 44 private static AddressField scavengeRootLinkField; 45 private static JByteField scavengeRootStateField; 46 47 /** Offsets for different nmethod parts */ 48 private static CIntegerField exceptionOffsetField; 49 private static CIntegerField deoptOffsetField; 50 private static CIntegerField origPCOffsetField; 51 private static CIntegerField stubOffsetField; 52 private static CIntegerField scopesDataOffsetField; 53 private static CIntegerField scopesPCsOffsetField; 54 private static CIntegerField dependenciesOffsetField; 55 private static CIntegerField handlerTableOffsetField; 56 private static CIntegerField nulChkTableOffsetField; 57 private static CIntegerField nmethodEndOffsetField; 58 59 /** Offsets for entry points */ 60 /** Entry point with class check */ 61 private static AddressField entryPointField; 62 /** Entry point without class check */ 63 private static AddressField verifiedEntryPointField; 64 /** Entry point for on stack replacement */ 65 private static AddressField osrEntryPointField; 66 67 // FIXME: add access to flags (how?) 68 69 /** NMethod Flushing lock (if non-zero, then the nmethod is not removed) */ 70 private static JIntField lockCountField; 71 72 /** not_entrant method removal. Each mark_sweep pass will update 73 this mark to current sweep invocation count if it is seen on the 74 stack. An not_entrant method can be removed when there is no 75 more activations, i.e., when the _stack_traversal_mark is less than 76 current sweep traversal index. */ 77 private static CIntegerField stackTraversalMarkField; 78 79 static { 80 VM.registerVMInitializedObserver(new Observer() { 81 public void update(Observable o, Object data) { 82 initialize(VM.getVM().getTypeDataBase()); 83 } 84 }); 85 } 86 87 private static void initialize(TypeDataBase db) { 88 Type type = db.lookupType("nmethod"); 89 90 zombieInstructionSizeField = type.getCIntegerField("_zombie_instruction_size"); 91 methodField = type.getOopField("_method"); 92 entryBCIField = type.getCIntegerField("_entry_bci"); 93 osrLinkField = type.getAddressField("_osr_link"); 94 scavengeRootLinkField = type.getAddressField("_scavenge_root_link"); 95 scavengeRootStateField = type.getJByteField("_scavenge_root_state"); 96 97 exceptionOffsetField = type.getCIntegerField("_exception_offset"); 98 deoptOffsetField = type.getCIntegerField("_deoptimize_offset"); 99 origPCOffsetField = type.getCIntegerField("_orig_pc_offset"); 100 stubOffsetField = type.getCIntegerField("_stub_offset"); 101 scopesDataOffsetField = type.getCIntegerField("_scopes_data_offset"); 102 scopesPCsOffsetField = type.getCIntegerField("_scopes_pcs_offset"); 103 dependenciesOffsetField = type.getCIntegerField("_dependencies_offset"); 104 handlerTableOffsetField = type.getCIntegerField("_handler_table_offset"); 105 nulChkTableOffsetField = type.getCIntegerField("_nul_chk_table_offset"); 106 nmethodEndOffsetField = type.getCIntegerField("_nmethod_end_offset"); 107 entryPointField = type.getAddressField("_entry_point"); 108 verifiedEntryPointField = type.getAddressField("_verified_entry_point"); 109 osrEntryPointField = type.getAddressField("_osr_entry_point"); 110 lockCountField = type.getJIntField("_lock_count"); 111 stackTraversalMarkField = type.getCIntegerField("_stack_traversal_mark"); 112 113 pcDescSize = db.lookupType("PcDesc").getSize(); 114 } 115 116 public NMethod(Address addr) { 117 super(addr); 118 } 119 120 121 // Accessors 122 public Address getAddress() { 123 return addr; 124 } 125 126 public Method getMethod() { 127 return (Method) VM.getVM().getObjectHeap().newOop(methodField.getValue(addr)); 128 } 129 130 // Type info 131 public boolean isNMethod() { return true; } 132 public boolean isJavaMethod() { return !getMethod().isNative(); } 133 public boolean isNativeMethod() { return getMethod().isNative(); } 134 public boolean isOSRMethod() { return getEntryBCI() != VM.getVM().getInvocationEntryBCI(); } 135 136 /** Boundaries for different parts */ 137 public Address constantsBegin() { return instructionsBegin(); } 138 public Address constantsEnd() { return getEntryPoint(); } 139 public Address codeBegin() { return getEntryPoint(); } 140 public Address codeEnd() { return headerBegin().addOffsetTo(getStubOffset()); } 141 public Address exceptionBegin() { return headerBegin().addOffsetTo(getExceptionOffset()); } 142 public Address deoptBegin() { return headerBegin().addOffsetTo(getDeoptOffset()); } 143 public Address stubBegin() { return headerBegin().addOffsetTo(getStubOffset()); } 144 public Address stubEnd() { return headerBegin().addOffsetTo(getScopesDataOffset()); } 145 public Address scopesDataBegin() { return headerBegin().addOffsetTo(getScopesDataOffset()); } 146 public Address scopesDataEnd() { return headerBegin().addOffsetTo(getScopesPCsOffset()); } 147 public Address scopesPCsBegin() { return headerBegin().addOffsetTo(getScopesPCsOffset()); } 148 public Address scopesPCsEnd() { return headerBegin().addOffsetTo(getDependenciesOffset()); } 149 public Address dependenciesBegin() { return headerBegin().addOffsetTo(getDependenciesOffset()); } 150 public Address dependenciesEnd() { return headerBegin().addOffsetTo(getHandlerTableOffset()); } 151 public Address handlerTableBegin() { return headerBegin().addOffsetTo(getHandlerTableOffset()); } 152 public Address handlerTableEnd() { return headerBegin().addOffsetTo(getNulChkTableOffset()); } 153 public Address nulChkTableBegin() { return headerBegin().addOffsetTo(getNulChkTableOffset()); } 154 public Address nulChkTableEnd() { return headerBegin().addOffsetTo(getNMethodEndOffset()); } 155 156 public int constantsSize() { return (int) constantsEnd() .minus(constantsBegin()); } 157 public int codeSize() { return (int) codeEnd() .minus(codeBegin()); } 158 public int stubSize() { return (int) stubEnd() .minus(stubBegin()); } 159 public int scopesDataSize() { return (int) scopesDataEnd() .minus(scopesDataBegin()); } 160 public int scopesPCsSize() { return (int) scopesPCsEnd() .minus(scopesPCsBegin()); } 161 public int dependenciesSize() { return (int) dependenciesEnd().minus(dependenciesBegin()); } 162 public int handlerTableSize() { return (int) handlerTableEnd().minus(handlerTableBegin()); } 163 public int nulChkTableSize() { return (int) nulChkTableEnd() .minus(nulChkTableBegin()); } 164 public int origPCOffset() { return (int) origPCOffsetField.getValue(addr); } 165 166 public int totalSize() { 167 return 168 constantsSize() + 169 codeSize() + 170 stubSize() + 171 scopesDataSize() + 172 scopesPCsSize() + 173 dependenciesSize() + 174 handlerTableSize() + 175 nulChkTableSize(); 176 } 177 178 public boolean constantsContains (Address addr) { return constantsBegin() .lessThanOrEqual(addr) && constantsEnd() .greaterThan(addr); } 179 public boolean codeContains (Address addr) { return codeBegin() .lessThanOrEqual(addr) && codeEnd() .greaterThan(addr); } 180 public boolean stubContains (Address addr) { return stubBegin() .lessThanOrEqual(addr) && stubEnd() .greaterThan(addr); } 181 public boolean scopesDataContains (Address addr) { return scopesDataBegin() .lessThanOrEqual(addr) && scopesDataEnd() .greaterThan(addr); } 182 public boolean scopesPCsContains (Address addr) { return scopesPCsBegin() .lessThanOrEqual(addr) && scopesPCsEnd() .greaterThan(addr); } 183 public boolean handlerTableContains(Address addr) { return handlerTableBegin().lessThanOrEqual(addr) && handlerTableEnd().greaterThan(addr); } 184 public boolean nulChkTableContains (Address addr) { return nulChkTableBegin() .lessThanOrEqual(addr) && nulChkTableEnd() .greaterThan(addr); } 185 186 /** Entry points */ 187 public Address getEntryPoint() { return entryPointField.getValue(addr); } 188 public Address getVerifiedEntryPoint() { return verifiedEntryPointField.getValue(addr); } 189 190 // FIXME: add interpreter_entry_point() 191 // FIXME: add lazy_interpreter_entry_point() for C2 192 193 // ********** 194 // * FIXME: * ADD ACCESS TO FLAGS!!!! 195 // ********** 196 // public boolean isInUse(); 197 // public boolean isAlive(); 198 // public boolean isNotEntrant(); 199 // public boolean isZombie(); 200 201 // ******************************** 202 // * MAJOR FIXME: MAJOR HACK HERE * 203 // ******************************** 204 public boolean isZombie() { return false; } 205 206 // public boolean isUnloaded(); 207 // public boolean isYoung(); 208 // public boolean isOld(); 209 // public int age(); 210 // public boolean isMarkedForDeoptimization(); 211 // public boolean isMarkedForUnloading(); 212 // public boolean isMarkedForReclamation(); 213 // public int level(); 214 // public int version(); 215 216 // FIXME: add mutators for above 217 // FIXME: add exception cache access? 218 219 /** On-stack replacement support */ 220 // FIXME: add mutators 221 public int getOSREntryBCI() { 222 if (Assert.ASSERTS_ENABLED) { 223 Assert.that(getEntryBCI() != VM.getVM().getInvocationEntryBCI(), "wrong kind of nmethod"); 224 } 225 return getEntryBCI(); 226 } 227 228 public NMethod getOSRLink() { 229 return (NMethod) VMObjectFactory.newObject(NMethod.class, osrLinkField.getValue(addr)); 230 } 231 232 public NMethod getScavengeRootLink() { 233 return (NMethod) VMObjectFactory.newObject(NMethod.class, scavengeRootLinkField.getValue(addr)); 234 } 235 236 public int getScavengeRootState() { 237 return (int) scavengeRootStateField.getValue(addr); 238 } 239 240 241 /** Tells whether frames described by this nmethod can be 242 deoptimized. Note: native wrappers cannot be deoptimized. */ 243 public boolean canBeDeoptimized() { return isJavaMethod(); } 244 245 // FIXME: add inline cache support 246 // FIXME: add flush() 247 248 public boolean isLockedByVM() { return lockCountField.getValue(addr) > 0; } 249 250 // FIXME: add mark_as_seen_on_stack 251 // FIXME: add can_not_entrant_be_converted 252 253 // FIXME: add GC support 254 // void follow_roots_or_mark_for_unloading(bool unloading_occurred, bool& marked_for_unloading); 255 // void follow_root_or_mark_for_unloading(oop* root, bool unloading_occurred, bool& marked_for_unloading); 256 // void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, void f(oop*)); 257 // void adjust_pointers(); 258 259 /** Finds a PCDesc with real-pc equal to "pc" */ 260 public PCDesc getPCDescAt(Address pc) { 261 // FIXME: consider adding cache like the one down in the VM 262 for (Address p = scopesPCsBegin(); p.lessThan(scopesPCsEnd()); p = p.addOffsetTo(pcDescSize)) { 263 PCDesc pcDesc = new PCDesc(p); 264 if (pcDesc.getRealPC(this).equals(pc)) { 265 return pcDesc; 266 } 267 } 268 return null; 269 } 270 271 /** ScopeDesc for an instruction */ 272 public ScopeDesc getScopeDescAt(Address pc) { 273 PCDesc pd = getPCDescAt(pc); 274 if (Assert.ASSERTS_ENABLED) { 275 Assert.that(pd != null, "scope must be present"); 276 } 277 return new ScopeDesc(this, pd.getScopeDecodeOffset(), pd.getObjDecodeOffset(), pd.getReexecute()); 278 } 279 280 /** This is only for use by the debugging system, and is only 281 intended for use in the topmost frame, where we are not 282 guaranteed to be at a PC for which we have a PCDesc. It finds 283 the PCDesc with realPC closest to the current PC. */ 284 public PCDesc getPCDescNearDbg(Address pc) { 285 PCDesc bestGuessPCDesc = null; 286 long bestDistance = 0; 287 for (Address p = scopesPCsBegin(); p.lessThan(scopesPCsEnd()); p = p.addOffsetTo(pcDescSize)) { 288 PCDesc pcDesc = new PCDesc(p); 289 // In case pc is null 290 long distance = -pcDesc.getRealPC(this).minus(pc); 291 if ((bestGuessPCDesc == null) || 292 ((distance >= 0) && (distance < bestDistance))) { 293 bestGuessPCDesc = pcDesc; 294 bestDistance = distance; 295 } 296 } 297 return bestGuessPCDesc; 298 } 299 300 /** This is only for use by the debugging system, and is only 301 intended for use in the topmost frame, where we are not 302 guaranteed to be at a PC for which we have a PCDesc. It finds 303 the ScopeDesc closest to the current PC. NOTE that this may 304 return NULL for compiled methods which don't have any 305 ScopeDescs! */ 306 public ScopeDesc getScopeDescNearDbg(Address pc) { 307 PCDesc pd = getPCDescNearDbg(pc); 308 if (pd == null) return null; 309 return new ScopeDesc(this, pd.getScopeDecodeOffset(), pd.getObjDecodeOffset(), pd.getReexecute()); 310 } 311 312 public Map/*<Address, PCDesc>*/ getSafepoints() { 313 Map safepoints = new HashMap(); // Map<Address, PCDesc> 314 sun.jvm.hotspot.debugger.Address p = null; 315 for (p = scopesPCsBegin(); p.lessThan(scopesPCsEnd()); 316 p = p.addOffsetTo(pcDescSize)) { 317 PCDesc pcDesc = new PCDesc(p); 318 sun.jvm.hotspot.debugger.Address pc = pcDesc.getRealPC(this); 319 safepoints.put(pc, pcDesc); 320 } 321 return safepoints; 322 } 323 324 // FIXME: add getPCOffsetForBCI() 325 // FIXME: add embeddedOopAt() 326 // FIXME: add isDependentOn() 327 // FIXME: add isPatchableAt() 328 329 /** Support for code generation. Only here for proof-of-concept. */ 330 public static int getEntryPointOffset() { return (int) entryPointField.getOffset(); } 331 public static int getVerifiedEntryPointOffset() { return (int) verifiedEntryPointField.getOffset(); } 332 public static int getOSREntryPointOffset() { return (int) osrEntryPointField.getOffset(); } 333 public static int getEntryBCIOffset() { return (int) entryBCIField.getOffset(); } 334 /** NOTE: renamed from "method_offset_in_bytes" */ 335 public static int getMethodOffset() { return (int) methodField.getOffset(); } 336 337 public void print() { 338 printOn(System.out); 339 } 340 341 public String toString() { 342 Method method = getMethod(); 343 return "NMethod for " + 344 method.getMethodHolder().getName().asString() + "." + 345 method.getName().asString() + method.getSignature().asString() + "==>n" + 346 super.toString(); 347 } 348 349 public String flagsToString() { 350 // FIXME need access to flags... 351 return ""; 352 } 353 354 public String getName() { 355 Method method = getMethod(); 356 return "NMethod for " + 357 method.getMethodHolder().getName().asString() + "." + 358 method.getName().asString() + 359 method.getSignature().asString(); 360 } 361 362 //-------------------------------------------------------------------------------- 363 // Internals only below this point 364 // 365 366 private int getEntryBCI() { return (int) entryBCIField .getValue(addr); } 367 private int getExceptionOffset() { return (int) exceptionOffsetField .getValue(addr); } 368 private int getDeoptOffset() { return (int) deoptOffsetField .getValue(addr); } 369 private int getStubOffset() { return (int) stubOffsetField .getValue(addr); } 370 private int getScopesDataOffset() { return (int) scopesDataOffsetField .getValue(addr); } 371 private int getScopesPCsOffset() { return (int) scopesPCsOffsetField .getValue(addr); } 372 private int getDependenciesOffset() { return (int) dependenciesOffsetField.getValue(addr); } 373 private int getHandlerTableOffset() { return (int) handlerTableOffsetField.getValue(addr); } 374 private int getNulChkTableOffset() { return (int) nulChkTableOffsetField .getValue(addr); } 375 private int getNMethodEndOffset() { return (int) nmethodEndOffsetField .getValue(addr); } 376 }