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         if (!thread.getAddress().equals(owner)) {
  79           if (!thread.isLockOwned(owner)) {
  80             tty.println("    WARNING! _owner doesn't fall in " + thread +
  81                         "'s stack space");
  82           }
  83         }
  84       }
  85     }
  86     tty.println("  _contentions: " + mon.contentions());
  87     tty.println("  _waiters: " + mon.waiters());
  88     tty.println("  _recursions: " + mon.recursions());
  89   }
  90 
  91   private void dumpOn(PrintStream tty) {
  92     Iterator i = ObjectSynchronizer.objectMonitorIterator();
  93     if (i == null) {
  94       tty.println("This version of HotSpot VM doesn't support monitor cache dump.");
  95       tty.println("You need 1.4.0_04, 1.4.1_01 or later versions");
  96       return;
  97     }
  98     ObjectMonitor mon;
  99     while (i.hasNext()) {
 100       mon = (ObjectMonitor)i.next();
 101       if (mon.contentions() != 0 || mon.waiters() != 0 || mon.owner() != null) {
 102         OopHandle object = mon.object();
 103         if (object == null) {
 104           dumpMonitor(tty, mon, true);
 105         } else {
 106           dumpMonitor(tty, mon, false);
 107         }
 108       }
 109     }
 110   }
 111 
 112   private static Threads threads = VM.getVM().getThreads();
 113   private static ObjectHeap heap = VM.getVM().getObjectHeap();
 114 }