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