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