1 /*
  2  * Copyright (c) 2002, 2019, 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.ui;
 26 
 27 import java.awt.*;
 28 import java.io.*;
 29 import javax.swing.*;
 30 import java.util.*;
 31 
 32 import sun.jvm.hotspot.memory.*;
 33 import sun.jvm.hotspot.runtime.*;
 34 import sun.jvm.hotspot.debugger.*;
 35 import sun.jvm.hotspot.oops.*;
 36 
 37 /** Provides information about monitor cache. */
 38 
 39 public class MonitorCacheDumpPanel extends JPanel {
 40   public MonitorCacheDumpPanel() {
 41     super();
 42 
 43     setLayout(new BorderLayout());
 44 
 45     // Simple at first
 46     JScrollPane scroller = new JScrollPane();
 47     JTextArea textArea = new JTextArea();
 48     textArea = new JTextArea();
 49     textArea.setEditable(false);
 50     textArea.setLineWrap(true);
 51     textArea.setWrapStyleWord(true);
 52     scroller.getViewport().add(textArea);
 53     add(scroller, BorderLayout.CENTER);
 54 
 55     ByteArrayOutputStream bos = new ByteArrayOutputStream();
 56     PrintStream tty = new PrintStream(bos);
 57     tty.println("Monitor Cache Dump (not including JVMTI raw monitors):");
 58     tty.println();
 59     dumpOn(tty);
 60 
 61     textArea.setText(bos.toString());
 62   }
 63 
 64   private static void dumpMonitor(PrintStream tty, ObjectMonitor mon, boolean raw) {
 65     tty.print("ObjectMonitor@" + mon.getAddress());
 66     if (raw) tty.print("(Raw Monitor)");
 67     tty.println();
 68     tty.println("  _header: 0x" + Long.toHexString(mon.header().value()));
 69     OopHandle obj = mon.object();
 70     Oop oop = heap.newOop(obj);
 71     tty.println("  _object: " + obj + ", a " + oop.getKlass().getName().asString());
 72     Address owner = mon.owner();
 73     tty.println("  _owner: " + owner);
 74     if (!raw && owner != null) {
 75       JavaThread thread = threads.owningThreadFromMonitor(mon);
 76       if (thread != null) {
 77         tty.println("    owning thread: " + thread.getThreadName());
 78       }
 79     }
 80     tty.println("  _contentions: " + mon.contentions());
 81     tty.println("  _waiters: " + mon.waiters());
 82     tty.println("  _recursions: " + mon.recursions());
 83   }
 84 
 85   private void dumpOn(PrintStream tty) {
 86     Iterator i = ObjectSynchronizer.objectMonitorIterator();
 87     if (i == null) {
 88       tty.println("This version of HotSpot VM doesn't support monitor cache dump.");
 89       tty.println("You need 1.4.0_04, 1.4.1_01 or later versions");
 90       return;
 91     }
 92     ObjectMonitor mon;
 93     while (i.hasNext()) {
 94       mon = (ObjectMonitor)i.next();
 95       if (mon.contentions() != 0 || mon.waiters() != 0 || mon.owner() != null) {
 96         OopHandle object = mon.object();
 97         if (object == null) {
 98           dumpMonitor(tty, mon, true);
 99         } else {
100           dumpMonitor(tty, mon, false);
101         }
102       }
103     }
104   }
105 
106   private static Threads threads = VM.getVM().getThreads();
107   private static ObjectHeap heap = VM.getVM().getObjectHeap();
108 }