1 /* 2 * Copyright (c) 2000, 2023, 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.io.PrintStream; 28 import sun.jvm.hotspot.utilities.Observable; 29 import sun.jvm.hotspot.utilities.Observer; 30 import sun.jvm.hotspot.utilities.U1Array; 31 32 import sun.jvm.hotspot.code.NMethod; 33 import sun.jvm.hotspot.debugger.Address; 34 import sun.jvm.hotspot.interpreter.OopMapCacheEntry; 35 import sun.jvm.hotspot.runtime.SignatureConverter; 36 import sun.jvm.hotspot.runtime.VM; 37 import sun.jvm.hotspot.runtime.VMObjectFactory; 38 import sun.jvm.hotspot.types.AddressField; 39 import sun.jvm.hotspot.types.Type; 40 import sun.jvm.hotspot.types.TypeDataBase; 41 import sun.jvm.hotspot.types.WrongTypeException; 42 import sun.jvm.hotspot.utilities.Assert; 43 import sun.jvm.hotspot.utilities.U1Array; 44 45 // A Method represents a Java method 46 47 public class Method extends Metadata { 48 static { 49 VM.registerVMInitializedObserver(new Observer() { 50 public void update(Observable o, Object data) { 51 initialize(VM.getVM().getTypeDataBase()); 52 } 53 }); 54 } 55 56 private static synchronized void initialize(TypeDataBase db) throws WrongTypeException { 57 type = db.lookupType("Method"); 58 constMethod = type.getAddressField("_constMethod"); 59 methodData = type.getAddressField("_method_data"); 60 methodCounters = type.getAddressField("_method_counters"); 61 accessFlags = new CIntField(type.getCIntegerField("_access_flags"), 0); 62 code = type.getAddressField("_code"); 63 vtableIndex = new CIntField(type.getCIntegerField("_vtable_index"), 0); 64 65 /* 66 fromCompiledCodeEntryPoint = type.getAddressField("_from_compiled_code_entry_point"); 67 interpreterEntry = type.getAddressField("_from_interpreted_entry"); 68 */ 69 70 objectInitializerName = null; 71 classInitializerName = null; 72 } 73 74 public Method(Address addr) { 75 super(addr); 76 } 77 78 public boolean isMethod() { return true; } 79 80 // Not a Method field, used to keep type. 81 private static Type type; 82 83 // Fields 84 private static AddressField constMethod; 85 private static AddressField methodData; 86 private static AddressField methodCounters; 87 private static CIntField accessFlags; 88 private static CIntField vtableIndex; 89 90 private static AddressField code; 91 /* 92 private static AddressCField fromCompiledCodeEntryPoint; 93 private static AddressField interpreterEntry; 94 */ 95 96 97 // constant method names - <init>, <clinit> 98 // Initialized lazily to avoid initialization ordering dependencies between ArrayKlass and String 99 private static String objectInitializerName; 100 private static String classInitializerName; 101 private static String objectInitializerName() { 102 if (objectInitializerName == null) { 103 objectInitializerName = "<init>"; 104 } 105 return objectInitializerName; 106 } 107 private static String classInitializerName() { 108 if (classInitializerName == null) { 109 classInitializerName = "<clinit>"; 110 } 111 return classInitializerName; 112 } 113 114 115 // Accessors for declared fields 116 public ConstMethod getConstMethod() { 117 Address addr = constMethod.getValue(getAddress()); 118 return VMObjectFactory.newObject(ConstMethod.class, addr); 119 } 120 public ConstantPool getConstants() { 121 return getConstMethod().getConstants(); 122 } 123 public boolean hasStackMapTable() { 124 return getConstMethod().hasStackMapTable(); 125 } 126 public U1Array getStackMapData() { 127 return getConstMethod().getStackMapData(); 128 } 129 public MethodData getMethodData() { 130 Address addr = methodData.getValue(getAddress()); 131 return VMObjectFactory.newObject(MethodData.class, addr); 132 } 133 public MethodCounters getMethodCounters() { 134 Address addr = methodCounters.getValue(getAddress()); 135 return VMObjectFactory.newObject(MethodCounters.class, addr); 136 } 137 /** WARNING: this is in words, not useful in this system; use getObjectSize() instead */ 138 public long getMaxStack() { return getConstMethod().getMaxStack(); } 139 public long getMaxLocals() { return getConstMethod().getMaxLocals(); } 140 public long getSizeOfParameters() { return getConstMethod().getSizeOfParameters(); } 141 public long getNameIndex() { return getConstMethod().getNameIndex(); } 142 public long getSignatureIndex() { return getConstMethod().getSignatureIndex(); } 143 public long getGenericSignatureIndex() { return getConstMethod().getGenericSignatureIndex(); } 144 public long getAccessFlags() { return accessFlags.getValue(this); } 145 public long getCodeSize() { return getConstMethod().getCodeSize(); } 146 public long getVtableIndex() { return vtableIndex.getValue(this); } 147 public long getInvocationCount() { 148 MethodCounters mc = getMethodCounters(); 149 return mc == null ? 0 : mc.getInvocationCounter(); 150 } 151 public long getBackedgeCount() { 152 MethodCounters mc = getMethodCounters(); 153 return mc == null ? 0 : mc.getBackedgeCounter(); 154 } 155 156 // get associated compiled native method, if available, else return null. 157 public NMethod getNativeMethod() { 158 Address addr = code.getValue(getAddress()); 159 return VMObjectFactory.newObject(NMethod.class, addr); 160 } 161 162 // Convenience routine 163 public AccessFlags getAccessFlagsObj() { 164 return new AccessFlags(getAccessFlags()); 165 } 166 167 /** Get a bytecode or breakpoint at the given bci */ 168 public int getBytecodeOrBPAt(int bci) { 169 return getConstMethod().getBytecodeOrBPAt(bci); 170 } 171 172 /** Fetch the original non-breakpoint bytecode at the specified 173 bci. It is required that there is currently a bytecode at this 174 bci. */ 175 public int getOrigBytecodeAt(int bci) { 176 BreakpointInfo bp = getMethodHolder().getBreakpoints(); 177 for (; bp != null; bp = bp.getNext()) { 178 if (bp.match(this, bci)) { 179 return bp.getOrigBytecode(); 180 } 181 } 182 System.err.println("Requested bci " + bci); 183 for (; bp != null; bp = bp.getNext()) { 184 System.err.println("Breakpoint at bci " + bp.getBCI() + ", bytecode " + 185 bp.getOrigBytecode()); 186 } 187 Assert.that(false, "Should not reach here"); 188 return -1; // not reached 189 } 190 191 public byte getBytecodeByteArg(int bci) { 192 return getConstMethod().getBytecodeByteArg(bci); 193 } 194 195 /** Fetches a 16-bit big-endian ("Java ordered") value from the 196 bytecode stream */ 197 public short getBytecodeShortArg(int bci) { 198 return getConstMethod().getBytecodeShortArg(bci); 199 } 200 201 /** Fetches a 16-bit native ordered value from the 202 bytecode stream */ 203 public short getNativeShortArg(int bci) { 204 return getConstMethod().getNativeShortArg(bci); 205 } 206 207 /** Fetches a 32-bit big-endian ("Java ordered") value from the 208 bytecode stream */ 209 public int getBytecodeIntArg(int bci) { 210 return getConstMethod().getBytecodeIntArg(bci); 211 } 212 213 /** Fetches a 32-bit native ordered value from the 214 bytecode stream */ 215 public int getNativeIntArg(int bci) { 216 return getConstMethod().getNativeIntArg(bci); 217 } 218 219 public byte[] getByteCode() { 220 return getConstMethod().getByteCode(); 221 } 222 223 /* 224 public Address getCode() { return codeField.getValue(this); } 225 public Address getInterpreterEntry() { return interpreterEntryField.getValue(this); } 226 public Address getFromCompiledCodeEntryPoint() { return fromCompiledCodeEntryPointField.getValue(this); } 227 */ 228 // Accessors 229 public Symbol getName() { return getConstants().getSymbolAt(getNameIndex()); } 230 public Symbol getSignature() { return getConstants().getSymbolAt(getSignatureIndex()); } 231 public Symbol getGenericSignature() { 232 long index = getGenericSignatureIndex(); 233 return (index != 0L) ? getConstants().getSymbolAt(index) : null; 234 } 235 236 // Method holder (the Klass holding this method) 237 public InstanceKlass getMethodHolder() { return getConstants().getPoolHolder(); } 238 239 // Access flags 240 public boolean isPublic() { return getAccessFlagsObj().isPublic(); } 241 public boolean isPrivate() { return getAccessFlagsObj().isPrivate(); } 242 public boolean isProtected() { return getAccessFlagsObj().isProtected(); } 243 public boolean isPackagePrivate() { AccessFlags af = getAccessFlagsObj(); 244 return (!af.isPublic() && !af.isPrivate() && !af.isProtected()); } 245 public boolean isStatic() { return getAccessFlagsObj().isStatic(); } 246 public boolean isFinal() { return getAccessFlagsObj().isFinal(); } 247 public boolean isSynchronized() { return getAccessFlagsObj().isSynchronized(); } 248 public boolean isBridge() { return getAccessFlagsObj().isBridge(); } 249 public boolean isVarArgs() { return getAccessFlagsObj().isVarArgs(); } 250 public boolean isNative() { return getAccessFlagsObj().isNative(); } 251 public boolean isAbstract() { return getAccessFlagsObj().isAbstract(); } 252 public boolean isStrict() { return getAccessFlagsObj().isStrict(); } 253 public boolean isSynthetic() { return getAccessFlagsObj().isSynthetic(); } 254 255 public boolean isConstructor() { 256 return (!isStatic()) && getName().equals(objectInitializerName()); 257 } 258 259 public boolean isStaticInitializer() { 260 return isStatic() && getName().equals(classInitializerName()); 261 } 262 263 public OopMapCacheEntry getMaskFor(int bci) { 264 OopMapCacheEntry entry = new OopMapCacheEntry(); 265 entry.fill(this, bci); 266 return entry; 267 } 268 269 public long getSize() { 270 return type.getSize() + (isNative() ? 2: 0); 271 } 272 273 public void printValueOn(PrintStream tty) { 274 tty.print("Method " + getMethodHolder().getName().asString() + "." + 275 getName().asString() + getSignature().asString() + "@" + getAddress()); 276 } 277 278 public void iterateFields(MetadataVisitor visitor) { 279 visitor.doCInt(accessFlags, true); 280 } 281 282 public boolean hasLineNumberTable() { 283 return getConstMethod().hasLineNumberTable(); 284 } 285 286 public int getLineNumberFromBCI(int bci) { 287 return getConstMethod().getLineNumberFromBCI(bci); 288 } 289 290 public LineNumberTableElement[] getLineNumberTable() { 291 return getConstMethod().getLineNumberTable(); 292 } 293 294 public boolean hasLocalVariableTable() { 295 return getConstMethod().hasLocalVariableTable(); 296 } 297 298 /** Should only be called if table is present */ 299 public LocalVariableTableElement[] getLocalVariableTable() { 300 return getConstMethod().getLocalVariableTable(); 301 } 302 303 public Symbol getLocalVariableName(int bci, int slot) { 304 if (! hasLocalVariableTable()) { 305 return null; 306 } 307 308 LocalVariableTableElement[] locals = getLocalVariableTable(); 309 for (int l = 0; l < locals.length; l++) { 310 LocalVariableTableElement local = locals[l]; 311 if ((bci >= local.getStartBCI()) && 312 (bci < (local.getStartBCI() + local.getLength())) && 313 slot == local.getSlot()) { 314 return getConstants().getSymbolAt(local.getNameCPIndex()); 315 } 316 } 317 318 return null; 319 } 320 321 public boolean hasExceptionTable() { 322 return getConstMethod().hasExceptionTable(); 323 } 324 325 public ExceptionTableElement[] getExceptionTable() { 326 return getConstMethod().getExceptionTable(); 327 } 328 329 public boolean hasCheckedExceptions() { 330 return getConstMethod().hasCheckedExceptions(); 331 } 332 333 /** Should only be called if table is present */ 334 public CheckedExceptionElement[] getCheckedExceptions() { 335 return getConstMethod().getCheckedExceptions(); 336 } 337 338 /** Returns name and signature in external form for debugging 339 purposes */ 340 public String externalNameAndSignature() { 341 final StringBuffer buf = new StringBuffer(); 342 buf.append(getMethodHolder().getName().asString()); 343 buf.append("."); 344 buf.append(getName().asString()); 345 buf.append("("); 346 new SignatureConverter(getSignature(), buf).iterateParameters(); 347 buf.append(")"); 348 return buf.toString().replace('/', '.'); 349 } 350 351 public void dumpReplayData(PrintStream out) { 352 NMethod nm = getNativeMethod(); 353 int code_size = 0; 354 if (nm != null) { 355 code_size = (int)nm.codeEnd().minus(nm.getVerifiedEntryPoint()); 356 } 357 Klass holder = getMethodHolder(); 358 out.println("ciMethod " + 359 nameAsAscii() + " " + 360 getInvocationCount() + " " + 361 getBackedgeCount() + " " + 362 interpreterInvocationCount() + " " + 363 interpreterThrowoutCount() + " " + 364 code_size); 365 } 366 367 public int interpreterThrowoutCount() { 368 return getMethodCounters().interpreterThrowoutCount(); 369 } 370 371 public long interpreterInvocationCount() { 372 return getInvocationCount(); 373 } 374 375 public String nameAsAscii() { 376 return getMethodHolder().getName().asString() + " " + 377 OopUtilities.escapeString(getName().asString()) + " " + 378 getSignature().asString(); 379 } 380 381 public U1Array getAnnotations() { 382 return getConstMethod().getMethodAnnotations(); 383 } 384 385 public U1Array getParameterAnnotations() { 386 return getConstMethod().getParameterAnnotations(); 387 } 388 389 public U1Array getTypeAnnotations() { 390 return getConstMethod().getTypeAnnotations(); 391 } 392 393 public U1Array getAnnotationDefault() { 394 return getConstMethod().getDefaultAnnotations(); 395 } 396 }