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