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 linkField; 44 /** Offsets for different nmethod parts */ 45 private static CIntegerField exceptionOffsetField; 46 private static CIntegerField deoptOffsetField; 47 private static CIntegerField origPCOffsetField; 48 private static CIntegerField stubOffsetField; 49 private static CIntegerField scopesDataOffsetField; 50 private static CIntegerField scopesPCsOffsetField; 51 private static CIntegerField dependenciesOffsetField; 52 private static CIntegerField handlerTableOffsetField; 53 private static CIntegerField nulChkTableOffsetField; 54 private static CIntegerField nmethodEndOffsetField; 55 56 /** Offsets for entry points */ 57 /** Entry point with class check */ 58 private static AddressField entryPointField; 59 /** Entry point without class check */ 60 private static AddressField verifiedEntryPointField; 61 /** Entry point for on stack replacement */ 62 private static AddressField osrEntryPointField; 63 64 // FIXME: add access to flags (how?) 65 66 /** NMethod Flushing lock (if non-zero, then the nmethod is not removed) */ 67 private static JIntField lockCountField; 68 69 /** not_entrant method removal. Each mark_sweep pass will update 70 this mark to current sweep invocation count if it is seen on the 71 stack. An not_entrant method can be removed when there is no 72 more activations, i.e., when the _stack_traversal_mark is less than 73 current sweep traversal index. */ 74 private static CIntegerField stackTraversalMarkField; 75 76 static { 77 VM.registerVMInitializedObserver(new Observer() { 78 public void update(Observable o, Object data) { 79 initialize(VM.getVM().getTypeDataBase()); 80 } 81 }); 82 } 83 84 private static void initialize(TypeDataBase db) { 85 Type type = db.lookupType("nmethod"); 86 87 zombieInstructionSizeField = type.getCIntegerField("_zombie_instruction_size"); 88 methodField = type.getOopField("_method"); 89 entryBCIField = type.getCIntegerField("_entry_bci"); 90 linkField = type.getAddressField("_link"); 91 exceptionOffsetField = type.getCIntegerField("_exception_offset"); 92 deoptOffsetField = type.getCIntegerField("_deoptimize_offset"); 93 origPCOffsetField = type.getCIntegerField("_orig_pc_offset"); 94 stubOffsetField = type.getCIntegerField("_stub_offset"); 95 scopesDataOffsetField = type.getCIntegerField("_scopes_data_offset"); 96 scopesPCsOffsetField = type.getCIntegerField("_scopes_pcs_offset"); 97 dependenciesOffsetField = type.getCIntegerField("_dependencies_offset"); 98 handlerTableOffsetField = type.getCIntegerField("_handler_table_offset"); 99 nulChkTableOffsetField = type.getCIntegerField("_nul_chk_table_offset"); 100 nmethodEndOffsetField = type.getCIntegerField("_nmethod_end_offset"); 101 entryPointField = type.getAddressField("_entry_point"); 102 verifiedEntryPointField = type.getAddressField("_verified_entry_point"); 103 osrEntryPointField = type.getAddressField("_osr_entry_point"); 104 lockCountField = type.getJIntField("_lock_count"); 105 stackTraversalMarkField = type.getCIntegerField("_stack_traversal_mark"); 106 107 pcDescSize = db.lookupType("PcDesc").getSize(); 108 } 109 110 public NMethod(Address addr) { 111 super(addr); 112 } 113 114 115 // Accessors 116 public Address getAddress() { 117 return addr; 118 } 119 120 public Method getMethod() { 121 return (Method) VM.getVM().getObjectHeap().newOop(methodField.getValue(addr)); 122 } 123 124 // Type info 125 public boolean isNMethod() { return true; } 126 public boolean isJavaMethod() { return !getMethod().isNative(); } 127 public boolean isNativeMethod() { return getMethod().isNative(); } 128 public boolean isOSRMethod() { return getEntryBCI() != VM.getVM().getInvocationEntryBCI(); } 129 130 /** Boundaries for different parts */ 131 public Address constantsBegin() { return instructionsBegin(); } 132 public Address constantsEnd() { return getEntryPoint(); } 133 public Address codeBegin() { return getEntryPoint(); } 134 public Address codeEnd() { return headerBegin().addOffsetTo(getStubOffset()); } 135 public Address exceptionBegin() { return headerBegin().addOffsetTo(getExceptionOffset()); } 136 public Address deoptBegin() { return headerBegin().addOffsetTo(getDeoptOffset()); } 137 public Address stubBegin() { return headerBegin().addOffsetTo(getStubOffset()); } 138 public Address stubEnd() { return headerBegin().addOffsetTo(getScopesDataOffset()); } 139 public Address scopesDataBegin() { return headerBegin().addOffsetTo(getScopesDataOffset()); } 140 public Address scopesDataEnd() { return headerBegin().addOffsetTo(getScopesPCsOffset()); } 141 public Address scopesPCsBegin() { return headerBegin().addOffsetTo(getScopesPCsOffset()); } 142 public Address scopesPCsEnd() { return headerBegin().addOffsetTo(getDependenciesOffset()); } 143 public Address dependenciesBegin() { return headerBegin().addOffsetTo(getDependenciesOffset()); } 144 public Address dependenciesEnd() { return headerBegin().addOffsetTo(getHandlerTableOffset()); } 145 public Address handlerTableBegin() { return headerBegin().addOffsetTo(getHandlerTableOffset()); } 146 public Address handlerTableEnd() { return headerBegin().addOffsetTo(getNulChkTableOffset()); } 147 public Address nulChkTableBegin() { return headerBegin().addOffsetTo(getNulChkTableOffset()); } 148 public Address nulChkTableEnd() { return headerBegin().addOffsetTo(getNMethodEndOffset()); } 149 150 public int constantsSize() { return (int) constantsEnd() .minus(constantsBegin()); } 151 public int codeSize() { return (int) codeEnd() .minus(codeBegin()); } 152 public int stubSize() { return (int) stubEnd() .minus(stubBegin()); } 153 public int scopesDataSize() { return (int) scopesDataEnd() .minus(scopesDataBegin()); } 154 public int scopesPCsSize() { return (int) scopesPCsEnd() .minus(scopesPCsBegin()); } 155 public int dependenciesSize() { return (int) dependenciesEnd().minus(dependenciesBegin()); } 156 public int handlerTableSize() { return (int) handlerTableEnd().minus(handlerTableBegin()); } 157 public int nulChkTableSize() { return (int) nulChkTableEnd() .minus(nulChkTableBegin()); } 158 public int origPCOffset() { return (int) origPCOffsetField.getValue(addr); } 159 160 public int totalSize() { 161 return 162 constantsSize() + 163 codeSize() + 164 stubSize() + 165 scopesDataSize() + 166 scopesPCsSize() + 167 dependenciesSize() + 168 handlerTableSize() + 169 nulChkTableSize(); 170 } 171 172 public boolean constantsContains (Address addr) { return constantsBegin() .lessThanOrEqual(addr) && constantsEnd() .greaterThan(addr); } 173 public boolean codeContains (Address addr) { return codeBegin() .lessThanOrEqual(addr) && codeEnd() .greaterThan(addr); } 174 public boolean stubContains (Address addr) { return stubBegin() .lessThanOrEqual(addr) && stubEnd() .greaterThan(addr); } 175 public boolean scopesDataContains (Address addr) { return scopesDataBegin() .lessThanOrEqual(addr) && scopesDataEnd() .greaterThan(addr); } 176 public boolean scopesPCsContains (Address addr) { return scopesPCsBegin() .lessThanOrEqual(addr) && scopesPCsEnd() .greaterThan(addr); } 177 public boolean handlerTableContains(Address addr) { return handlerTableBegin().lessThanOrEqual(addr) && handlerTableEnd().greaterThan(addr); } 178 public boolean nulChkTableContains (Address addr) { return nulChkTableBegin() .lessThanOrEqual(addr) && nulChkTableEnd() .greaterThan(addr); } 179 180 /** Entry points */ 181 public Address getEntryPoint() { return entryPointField.getValue(addr); } 182 public Address getVerifiedEntryPoint() { return verifiedEntryPointField.getValue(addr); } 183 184 // FIXME: add interpreter_entry_point() 185 // FIXME: add lazy_interpreter_entry_point() for C2 186 187 // ********** 188 // * FIXME: * ADD ACCESS TO FLAGS!!!! 189 // ********** 190 // public boolean isInUse(); 191 // public boolean isAlive(); 192 // public boolean isNotEntrant(); 193 // public boolean isZombie(); 194 195 // ******************************** 196 // * MAJOR FIXME: MAJOR HACK HERE * 197 // ******************************** 198 public boolean isZombie() { return false; } 199 200 // public boolean isUnloaded(); 201 // public boolean isYoung(); 202 // public boolean isOld(); 203 // public int age(); 204 // public boolean isMarkedForDeoptimization(); 205 // public boolean isMarkedForUnloading(); 206 // public boolean isMarkedForReclamation(); 207 // public int level(); 208 // public int version(); 209 210 // FIXME: add mutators for above 211 // FIXME: add exception cache access? 212 213 /** On-stack replacement support */ 214 // FIXME: add mutators 215 public int getOSREntryBCI() { 216 if (Assert.ASSERTS_ENABLED) { 217 Assert.that(getEntryBCI() != VM.getVM().getInvocationEntryBCI(), "wrong kind of nmethod"); 218 } 219 return getEntryBCI(); 220 } 221 222 public NMethod getLink() { 223 return (NMethod) VMObjectFactory.newObject(NMethod.class, linkField.getValue(addr)); 224 } 225 226 /** Tells whether frames described by this nmethod can be 227 deoptimized. Note: native wrappers cannot be deoptimized. */ 228 public boolean canBeDeoptimized() { return isJavaMethod(); } 229 230 // FIXME: add inline cache support 231 // FIXME: add flush() 232 233 public boolean isLockedByVM() { return lockCountField.getValue(addr) > 0; } 234 235 // FIXME: add mark_as_seen_on_stack 236 // FIXME: add can_not_entrant_be_converted 237 238 // FIXME: add GC support 239 // void follow_roots_or_mark_for_unloading(bool unloading_occurred, bool& marked_for_unloading); 240 // void follow_root_or_mark_for_unloading(oop* root, bool unloading_occurred, bool& marked_for_unloading); 241 // void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, void f(oop*)); 242 // void adjust_pointers(); 243 244 /** Finds a PCDesc with real-pc equal to "pc" */ 245 public PCDesc getPCDescAt(Address pc) { 246 // FIXME: consider adding cache like the one down in the VM 247 for (Address p = scopesPCsBegin(); p.lessThan(scopesPCsEnd()); p = p.addOffsetTo(pcDescSize)) { 248 PCDesc pcDesc = new PCDesc(p); 249 if (pcDesc.getRealPC(this).equals(pc)) { 250 return pcDesc; 251 } 252 } 253 return null; 254 } 255 256 /** ScopeDesc for an instruction */ 257 public ScopeDesc getScopeDescAt(Address pc) { 258 PCDesc pd = getPCDescAt(pc); 259 if (Assert.ASSERTS_ENABLED) { 260 Assert.that(pd != null, "scope must be present"); 261 } 262 return new ScopeDesc(this, pd.getScopeDecodeOffset(), pd.getReexecute()); 263 } 264 265 /** This is only for use by the debugging system, and is only 266 intended for use in the topmost frame, where we are not 267 guaranteed to be at a PC for which we have a PCDesc. It finds 268 the PCDesc with realPC closest to the current PC. */ 269 public PCDesc getPCDescNearDbg(Address pc) { 270 PCDesc bestGuessPCDesc = null; 271 long bestDistance = 0; 272 for (Address p = scopesPCsBegin(); p.lessThan(scopesPCsEnd()); p = p.addOffsetTo(pcDescSize)) { 273 PCDesc pcDesc = new PCDesc(p); 274 // In case pc is null 275 long distance = -pcDesc.getRealPC(this).minus(pc); 276 if ((bestGuessPCDesc == null) || 277 ((distance >= 0) && (distance < bestDistance))) { 278 bestGuessPCDesc = pcDesc; 279 bestDistance = distance; 280 } 281 } 282 return bestGuessPCDesc; 283 } 284 285 PCDesc find_pc_desc(long pc, boolean approximate) { 286 return find_pc_desc_internal(pc, approximate); 287 } 288 289 static long addressToLong(sun.jvm.hotspot.debugger.Address addr) { 290 return VM.getVM().getDebugger().getAddressValue(addr); 291 } 292 293 // Finds a PcDesc with real-pc equal to "pc" 294 PCDesc find_pc_desc_internal(long pc, boolean approximate) { 295 long base_address = addressToLong(instructionsBegin()); 296 int pc_offset = (int) (pc - base_address); 297 298 // Fallback algorithm: quasi-linear search for the PcDesc 299 // Find the last pc_offset less than the given offset. 300 // The successor must be the required match, if there is a match at all. 301 // (Use a fixed radix to avoid expensive affine pointer arithmetic.) 302 Address lower = scopesPCsBegin(); 303 Address upper = scopesPCsEnd(); 304 upper = upper.addOffsetTo(-pcDescSize); // exclude final sentinel 305 if (lower.greaterThan(upper)) return null; // native method; no PcDescs at all 306 307 // Take giant steps at first (4096, then 256, then 16, then 1) 308 int LOG2_RADIX = 4; 309 int RADIX = (1 << LOG2_RADIX); 310 Address mid; 311 for (int step = (1 << (LOG2_RADIX*3)); step > 1; step >>= LOG2_RADIX) { 312 while ((mid = lower.addOffsetTo(step * pcDescSize)).lessThan(upper)) { 313 PCDesc m = new PCDesc(mid); 314 if (m.getPCOffset() < pc_offset) { 315 lower = mid; 316 } else { 317 upper = mid; 318 break; 319 } 320 } 321 } 322 323 // Sneak up on the value with a linear search of length ~16. 324 while (true) { 325 mid = lower.addOffsetTo(pcDescSize); 326 PCDesc m = new PCDesc(mid); 327 if (m.getPCOffset() < pc_offset) { 328 lower = mid; 329 } else { 330 upper = mid; 331 break; 332 } 333 } 334 335 PCDesc u = new PCDesc(upper); 336 if (match_desc(u, pc_offset, approximate)) { 337 return u; 338 } else { 339 return null; 340 } 341 } 342 343 344 // ScopeDesc retrieval operation 345 PCDesc pc_desc_at(long pc) { return find_pc_desc(pc, false); } 346 // pc_desc_near returns the first PCDesc at or after the givne pc. 347 PCDesc pc_desc_near(long pc) { return find_pc_desc(pc, true); } 348 349 // Return a the last scope in (begin..end] 350 public ScopeDesc scope_desc_in(long begin, long end) { 351 PCDesc p = pc_desc_near(begin+1); 352 if (p != null && addressToLong(p.getRealPC(this)) <= end) { 353 return new ScopeDesc(this, p.getScopeDecodeOffset(), p.getReexecute()); 354 } 355 return null; 356 } 357 358 static boolean match_desc(PCDesc pc, int pc_offset, boolean approximate) { 359 if (!approximate) 360 return pc.getPCOffset() == pc_offset; 361 else { 362 PCDesc prev = new PCDesc(pc.getAddress().addOffsetTo(-pcDescSize)); 363 return prev.getPCOffset() < pc_offset && pc_offset <= pc.getPCOffset(); 364 } 365 } 366 367 /** This is only for use by the debugging system, and is only 368 intended for use in the topmost frame, where we are not 369 guaranteed to be at a PC for which we have a PCDesc. It finds 370 the ScopeDesc closest to the current PC. NOTE that this may 371 return NULL for compiled methods which don't have any 372 ScopeDescs! */ 373 public ScopeDesc getScopeDescNearDbg(Address pc) { 374 PCDesc pd = getPCDescNearDbg(pc); 375 if (pd == null) return null; 376 return new ScopeDesc(this, pd.getScopeDecodeOffset(), pd.getReexecute()); 377 } 378 379 public Map/*<Address, PCDesc>*/ getSafepoints() { 380 Map safepoints = new HashMap(); // Map<Address, PCDesc> 381 sun.jvm.hotspot.debugger.Address p = null; 382 for (p = scopesPCsBegin(); p.lessThan(scopesPCsEnd()); 383 p = p.addOffsetTo(pcDescSize)) { 384 PCDesc pcDesc = new PCDesc(p); 385 sun.jvm.hotspot.debugger.Address pc = pcDesc.getRealPC(this); 386 safepoints.put(pc, pcDesc); 387 } 388 return safepoints; 389 } 390 391 // FIXME: add getPCOffsetForBCI() 392 // FIXME: add embeddedOopAt() 393 // FIXME: add isDependentOn() 394 // FIXME: add isPatchableAt() 395 396 /** Support for code generation. Only here for proof-of-concept. */ 397 public static int getEntryPointOffset() { return (int) entryPointField.getOffset(); } 398 public static int getVerifiedEntryPointOffset() { return (int) verifiedEntryPointField.getOffset(); } 399 public static int getOSREntryPointOffset() { return (int) osrEntryPointField.getOffset(); } 400 public static int getEntryBCIOffset() { return (int) entryBCIField.getOffset(); } 401 /** NOTE: renamed from "method_offset_in_bytes" */ 402 public static int getMethodOffset() { return (int) methodField.getOffset(); } 403 404 public void print() { 405 printOn(System.out); 406 } 407 408 public String toString() { 409 Method method = getMethod(); 410 return "NMethod for " + 411 method.getMethodHolder().getName().asString() + "." + 412 method.getName().asString() + method.getSignature().asString() + "==>n" + 413 super.toString(); 414 } 415 416 public String flagsToString() { 417 // FIXME need access to flags... 418 return ""; 419 } 420 421 public String getName() { 422 Method method = getMethod(); 423 return "NMethod for " + 424 method.getMethodHolder().getName().asString() + "." + 425 method.getName().asString() + 426 method.getSignature().asString(); 427 } 428 429 //-------------------------------------------------------------------------------- 430 // Internals only below this point 431 // 432 433 private int getEntryBCI() { return (int) entryBCIField .getValue(addr); } 434 private int getExceptionOffset() { return (int) exceptionOffsetField .getValue(addr); } 435 private int getDeoptOffset() { return (int) deoptOffsetField .getValue(addr); } 436 private int getStubOffset() { return (int) stubOffsetField .getValue(addr); } 437 private int getScopesDataOffset() { return (int) scopesDataOffsetField .getValue(addr); } 438 private int getScopesPCsOffset() { return (int) scopesPCsOffsetField .getValue(addr); } 439 private int getDependenciesOffset() { return (int) dependenciesOffsetField.getValue(addr); } 440 private int getHandlerTableOffset() { return (int) handlerTableOffsetField.getValue(addr); } 441 private int getNulChkTableOffset() { return (int) nulChkTableOffsetField .getValue(addr); } 442 private int getNMethodEndOffset() { return (int) nmethodEndOffsetField .getValue(addr); } 443 }