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.runtime;
 26 
 27 import java.io.*;
 28 import java.util.*;
 29 import sun.jvm.hotspot.oops.*;
 30 import sun.jvm.hotspot.utilities.*;
 31 import sun.jvm.hotspot.debugger.*;
 32 
 33 public abstract class JavaVFrame extends VFrame {
 34 
 35   private static final String ADDRESS_FORMAT = VM.getVM().isLP64() ? "0x%016x"
 36                                                                    : "0x%08x";
 37 
 38   /** JVM state */
 39   public abstract Method getMethod();
 40   public abstract int    getBCI();
 41   public abstract StackValueCollection getLocals();
 42   public abstract StackValueCollection getExpressions();
 43   public abstract List<MonitorInfo> getMonitors();
 44 
 45   /** Test operation */
 46   public boolean isJavaFrame() { return true; }
 47 
 48   /** Package-internal constructor */
 49   JavaVFrame(Frame fr, RegisterMap regMap, JavaThread thread) {
 50     super(fr, regMap, thread);
 51   }
 52 
 53   /** Get monitor (if any) that this JavaVFrame is trying to enter */
 54   // FIXME: not yet implemented
 55   //  public Address getPendingMonitor(int frameCount);
 56 
 57   public void printLockedObjectClassName(PrintStream tty,
 58                                          OopHandle hobj, String lockState) {
 59     if (hobj.asLongValue() != 0L) {
 60       tty.format("\t- %s <" + ADDRESS_FORMAT + "> ",
 61                  lockState, hobj.asLongValue());
 62 
 63       Klass klass = Oop.getKlassForOopHandle(hobj);
 64       String klassName;
 65       if (klass != null) {
 66         klassName = klass.getName().asString();
 67       } else {
 68         klassName = "<unknown class>";
 69       }
 70       tty.print("(a ");
 71       if (klassName.equals("java/lang/Class")) {
 72         Oop obj = VM.getVM().getObjectHeap().newOop(hobj);
 73         klassName = java_lang_Class.asExternalName(obj);
 74         tty.print("java.lang.Class for ");
 75       }
 76       tty.println(klassName.replace('/', '.') + ")");
 77     }
 78   }
 79 
 80   private String identifyLockState(MonitorInfo monitor, String waitingState) {
 81     Mark mark = new Mark(monitor.owner());
 82     if (mark.hasMonitor() &&
 83         ( // we have marked ourself as pending on this monitor
 84           mark.monitor().equals(thread.getCurrentPendingMonitor()) ||
 85           mark.monitor().isOwnedAnonymous() ||
 86           // we are not the owner of this monitor
 87           !mark.monitor().isEntered(thread)
 88         )) {
 89       return waitingState;
 90     }
 91     return "locked";
 92   }
 93 
 94   /** Printing used during stack dumps */
 95   public void printLockInfo(PrintStream tty, int frameCount) {
 96     // If this is the first frame and it is java.lang.Object.wait(...)
 97     // then print out the receiver. Locals are not always available,
 98     // e.g., compiled native frames have no scope so there are no locals.
 99     if (frameCount == 0) {
100       if (getMethod().getName().asString().equals("wait") &&
101           getMethod().getMethodHolder().getName().asString().equals("java/lang/Object")) {
102         String waitState = "waiting on"; // assume we are waiting
103         // If earlier in the output we reported java.lang.Thread.State ==
104         // "WAITING (on object monitor)" and now we report "waiting on", then
105         // we are still waiting for notification or timeout. Otherwise if
106         // we earlier reported java.lang.Thread.State == "BLOCKED (on object
107         // monitor)", then we are actually waiting to re-lock the monitor.
108         StackValueCollection locs = getLocals();
109         if (!locs.isEmpty()) {
110           StackValue sv = locs.get(0);
111           if (sv.getType() == BasicType.getTObject()) {
112             OopHandle o = sv.getObject();
113             if (OopUtilities.threadOopGetThreadStatus(thread.getThreadObj()) == OopUtilities.THREAD_STATUS_BLOCKED_ON_MONITOR_ENTER) {
114               waitState = "waiting to re-lock in wait()";
115             }
116             printLockedObjectClassName(tty, o, waitState);
117           }
118         } else {
119           tty.println("\t- " + waitState + " <no object reference available>");
120         }
121       } else if (thread.getCurrentParkBlocker() != null) {
122         Oop obj = thread.getCurrentParkBlocker();
123         Klass k = obj.getKlass();
124         tty.format("\t- parking to wait for <" + ADDRESS_FORMAT + "> (a %s)",
125                    obj.getHandle().asLongValue(), k.getName().asString());
126         tty.println();
127       }
128     }
129 
130     // Print out all monitors that we have locked, or are trying to lock,
131     // including re-locking after being notified or timing out in a wait().
132     List<MonitorInfo> mons = getMonitors();
133     if (!mons.isEmpty()) {
134       boolean foundFirstMonitor = false;
135       for (int index = mons.size() - 1; index >= 0; index--) {
136         MonitorInfo monitor = mons.get(index);
137         if (monitor.eliminated() && isCompiledFrame()) { // Eliminated in compiled code
138           if (monitor.ownerIsScalarReplaced()) {
139             Klass k = Oop.getKlassForOopHandle(monitor.ownerKlass());
140             tty.println("\t- eliminated <owner is scalar replaced> (a " + k.getName().asString() + ")");
141           } else if (monitor.owner() != null) {
142             printLockedObjectClassName(tty, monitor.owner(), "eliminated");
143           }
144           continue;
145         }
146         if (monitor.owner() != null) {
147           // the monitor is associated with an object, i.e., it is locked
148           String lockState = "locked";
149           if (!foundFirstMonitor && frameCount == 0) {
150             // If this is the first frame and we haven't found an owned
151             // monitor before, then we need to see if we have completed
152             // the lock or if we are blocked trying to acquire it. Only
153             // an inflated monitor that is first on the monitor list in
154             // the first frame can block us on a monitor enter.
155             lockState = identifyLockState(monitor, "waiting to lock");
156           }
157           printLockedObjectClassName(tty, monitor.owner(), lockState);
158           foundFirstMonitor = true;
159         }
160       }
161     }
162   }
163 
164   /** Printing operations */
165 
166   //
167   // FIXME: implement visitor pattern for traversing vframe contents?
168   //
169 
170   public void print() {
171     printOn(System.out);
172   }
173 
174   public void printOn(PrintStream tty) {
175     super.printOn(tty);
176 
177     tty.print("\t");
178     getMethod().printValueOn(tty);
179     tty.println();
180     tty.println("\tbci:\t" + getBCI());
181 
182     printStackValuesOn(tty, "locals",      getLocals());
183     printStackValuesOn(tty, "expressions", getExpressions());
184   }
185 
186   public void printActivation(int index) {
187     printActivationOn(System.out, index);
188   }
189 
190   public void printActivationOn(PrintStream tty, int index) {
191     // frame number and method
192     tty.print(index + " - ");
193     printValueOn(tty);
194     tty.println();
195 
196     if (VM.getVM().wizardMode()) {
197       printOn(tty);
198       tty.println();
199     }
200   }
201 
202   /** Verification operations */
203   public void verify() {
204   }
205 
206   public boolean equals(Object o) {
207       if (!(o instanceof JavaVFrame other)) {
208           return false;
209       }
210 
211     // Check static part
212       if (!getMethod().equals(other.getMethod())) {
213           return false;
214       }
215 
216       if (getBCI() != other.getBCI()) {
217           return false;
218       }
219 
220       // dynamic part - we just compare the frame pointer
221       if (! getFrame().equals(other.getFrame())) {
222           return false;
223       }
224       return true;
225   }
226 
227   public int hashCode() {
228       return getMethod().hashCode() ^ getBCI() ^ getFrame().hashCode();
229   }
230 
231   /** Structural compare */
232   public boolean structuralCompare(JavaVFrame other) {
233     // Check static part
234     if (!getMethod().equals(other.getMethod())) {
235       return false;
236     }
237 
238     if (getBCI() != other.getBCI()) {
239       return false;
240     }
241 
242     // Check locals
243     StackValueCollection locs      = getLocals();
244     StackValueCollection otherLocs = other.getLocals();
245     if (Assert.ASSERTS_ENABLED) {
246       Assert.that(locs.size() == otherLocs.size(), "sanity check");
247     }
248     for (int i = 0; i < locs.size(); i++) {
249       // it might happen the compiler reports a conflict and
250       // the interpreter reports a bogus int.
251       if (      isCompiledFrame() && (locs.get(i)).getType()      == BasicType.getTConflict()) continue;
252       if (other.isCompiledFrame() && (otherLocs.get(i)).getType() == BasicType.getTConflict()) continue;
253 
254       if (!locs.get(i).equals(otherLocs.get(i))) {
255         return false;
256       }
257     }
258 
259     // Check expressions
260     StackValueCollection exprs      = getExpressions();
261     StackValueCollection otherExprs = other.getExpressions();
262     if (Assert.ASSERTS_ENABLED) {
263       Assert.that(exprs.size() == otherExprs.size(), "sanity check");
264     }
265     for (int i = 0; i < exprs.size(); i++) {
266       if (!exprs.get(i).equals(otherExprs.get(i))) {
267         return false;
268       }
269     }
270 
271     return true;
272   }
273 
274   //--------------------------------------------------------------------------------
275   // Internals only below this point
276   //
277 
278   private void printStackValuesOn(PrintStream tty, String title, StackValueCollection values) {
279     if (values.isEmpty()) {
280       return;
281     }
282     tty.println("\t" + title + ":");
283     for (int index = 0; index < values.size(); index++) {
284       tty.print("\t" + index + "\t");
285       values.get(index).printOn(tty);
286       tty.println();
287     }
288   }
289 }