1 /*
  2  * Copyright (c) 2000, 2022, 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 
 31 import sun.jvm.hotspot.code.NMethod;
 32 import sun.jvm.hotspot.debugger.Address;
 33 import sun.jvm.hotspot.interpreter.OopMapCacheEntry;
 34 import sun.jvm.hotspot.runtime.SignatureConverter;
 35 import sun.jvm.hotspot.runtime.VM;
 36 import sun.jvm.hotspot.runtime.VMObjectFactory;
 37 import sun.jvm.hotspot.types.AddressField;
 38 import sun.jvm.hotspot.types.Type;
 39 import sun.jvm.hotspot.types.TypeDataBase;
 40 import sun.jvm.hotspot.types.WrongTypeException;
 41 import sun.jvm.hotspot.utilities.Assert;
 42 
 43 // A Method represents a Java method
 44 
 45 public class Method extends Metadata {
 46   static {
 47     VM.registerVMInitializedObserver(new Observer() {
 48         public void update(Observable o, Object data) {
 49           initialize(VM.getVM().getTypeDataBase());
 50         }
 51       });
 52   }
 53 
 54   private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {
 55     type                       = db.lookupType("Method");
 56     constMethod                = type.getAddressField("_constMethod");
 57     methodData                 = type.getAddressField("_method_data");
 58     methodCounters             = type.getAddressField("_method_counters");
 59     accessFlags                = new CIntField(type.getCIntegerField("_access_flags"), 0);
 60     code                       = type.getAddressField("_code");
 61     vtableIndex                = new CIntField(type.getCIntegerField("_vtable_index"), 0);
 62 
 63     /*
 64     fromCompiledCodeEntryPoint = type.getAddressField("_from_compiled_code_entry_point");
 65     interpreterEntry           = type.getAddressField("_from_interpreted_entry");
 66     */
 67 
 68     objectInitializerName = null;
 69     valueFactoryName = null;
 70     classInitializerName = null;
 71   }
 72 
 73   public Method(Address addr) {
 74     super(addr);
 75   }
 76 
 77   public boolean isMethod()            { return true; }
 78 
 79   // Not a Method field, used to keep type.
 80   private static Type type;
 81 
 82   // Fields
 83   private static AddressField  constMethod;
 84   private static AddressField  methodData;
 85   private static AddressField  methodCounters;
 86   private static CIntField accessFlags;
 87   private static CIntField vtableIndex;
 88 
 89   private static AddressField       code;
 90   /*
 91   private static AddressCField      fromCompiledCodeEntryPoint;
 92   private static AddressField       interpreterEntry;
 93   */
 94 
 95 
 96   // constant method names - <init>, <clinit>
 97   // Initialized lazily to avoid initialization ordering dependencies between ArrayKlass and String
 98   private static String objectInitializerName;
 99   private static String valueFactoryName;
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 valueFactoryName() {
108     if (valueFactoryName == null) {
109       valueFactoryName = "<vnew>";
110     }
111     return classInitializerName;
112   }
113   private static String classInitializerName() {
114     if (classInitializerName == null) {
115       classInitializerName = "<clinit>";
116     }
117     return classInitializerName;
118   }
119 
120 
121   // Accessors for declared fields
122   public ConstMethod  getConstMethod()                {
123     Address addr = constMethod.getValue(getAddress());
124     return VMObjectFactory.newObject(ConstMethod.class, addr);
125   }
126   public ConstantPool getConstants()                  {
127     return getConstMethod().getConstants();
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()) || getName().equals(valueFactoryName()));
257   }
258 
259   public boolean isStaticInitializer() {
260      return isStatic() && getName().equals(classInitializerName());
261   }
262 
263   public boolean isObsolete() {
264      return getAccessFlagsObj().isObsolete();
265   }
266 
267   public OopMapCacheEntry getMaskFor(int bci) {
268     OopMapCacheEntry entry = new OopMapCacheEntry();
269     entry.fill(this, bci);
270     return entry;
271   }
272 
273   public long getSize() {
274     return type.getSize() + (isNative() ? 2: 0);
275   }
276 
277   public void printValueOn(PrintStream tty) {
278       tty.print("Method " + getMethodHolder().getName().asString() + "." +
279                 getName().asString() + getSignature().asString() + "@" + getAddress());
280   }
281 
282   public void iterateFields(MetadataVisitor visitor) {
283       visitor.doCInt(accessFlags, true);
284     }
285 
286   public boolean hasLineNumberTable() {
287     return getConstMethod().hasLineNumberTable();
288   }
289 
290   public int getLineNumberFromBCI(int bci) {
291     return getConstMethod().getLineNumberFromBCI(bci);
292   }
293 
294   public LineNumberTableElement[] getLineNumberTable() {
295     return getConstMethod().getLineNumberTable();
296   }
297 
298   public boolean hasLocalVariableTable() {
299     return getConstMethod().hasLocalVariableTable();
300   }
301 
302   /** Should only be called if table is present */
303   public LocalVariableTableElement[] getLocalVariableTable() {
304     return getConstMethod().getLocalVariableTable();
305   }
306 
307   public Symbol getLocalVariableName(int bci, int slot) {
308     if (! hasLocalVariableTable()) {
309        return null;
310     }
311 
312     LocalVariableTableElement[] locals = getLocalVariableTable();
313     for (int l = 0; l < locals.length; l++) {
314        LocalVariableTableElement local = locals[l];
315        if ((bci >= local.getStartBCI()) &&
316           (bci < (local.getStartBCI() + local.getLength())) &&
317           slot == local.getSlot()) {
318           return getConstants().getSymbolAt(local.getNameCPIndex());
319        }
320     }
321 
322     return null;
323   }
324 
325   public boolean hasExceptionTable() {
326     return getConstMethod().hasExceptionTable();
327   }
328 
329   public ExceptionTableElement[] getExceptionTable() {
330     return getConstMethod().getExceptionTable();
331   }
332 
333   public boolean hasCheckedExceptions() {
334     return getConstMethod().hasCheckedExceptions();
335   }
336 
337   /** Should only be called if table is present */
338   public CheckedExceptionElement[] getCheckedExceptions() {
339     return getConstMethod().getCheckedExceptions();
340   }
341 
342   /** Returns name and signature in external form for debugging
343       purposes */
344   public String externalNameAndSignature() {
345     final StringBuffer buf = new StringBuffer();
346     buf.append(getMethodHolder().getName().asString());
347     buf.append(".");
348     buf.append(getName().asString());
349     buf.append("(");
350     new SignatureConverter(getSignature(), buf).iterateParameters();
351     buf.append(")");
352     return buf.toString().replace('/', '.');
353   }
354 
355   public void dumpReplayData(PrintStream out) {
356       NMethod nm = getNativeMethod();
357       int code_size = 0;
358       if (nm != null) {
359         code_size = (int)nm.codeEnd().minus(nm.getVerifiedEntryPoint());
360       }
361       Klass holder = getMethodHolder();
362       out.println("ciMethod " +
363                   nameAsAscii() + " " +
364                   getInvocationCount() + " " +
365                   getBackedgeCount() + " " +
366                   interpreterInvocationCount() + " " +
367                   interpreterThrowoutCount() + " " +
368                   code_size);
369   }
370 
371   public int interpreterThrowoutCount() {
372     return getMethodCounters().interpreterThrowoutCount();
373   }
374 
375   public long interpreterInvocationCount() {
376     return getInvocationCount();
377   }
378 
379   public String nameAsAscii() {
380     return getMethodHolder().getName().asString() + " " +
381       OopUtilities.escapeString(getName().asString()) + " " +
382       getSignature().asString();
383   }
384 }
--- EOF ---