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