Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/ConcurrentLocksPrinter.java
41161 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.runtime;2526import java.io.*;27import java.util.*;28import sun.jvm.hotspot.memory.*;29import sun.jvm.hotspot.oops.*;3031public class ConcurrentLocksPrinter {32private Map<JavaThread, List<Oop>> locksMap = new HashMap<>();3334public ConcurrentLocksPrinter() {35fillLocks();36}3738public void print(JavaThread jthread, PrintStream tty) {39List<Oop> locks = locksMap.get(jthread);40tty.println("Locked ownable synchronizers:");41if (locks == null || locks.isEmpty()) {42tty.println(" - None");43} else {44for (Iterator<Oop> itr = locks.iterator(); itr.hasNext();) {45Oop oop = itr.next();46tty.println(" - <" + oop.getHandle() + ">, (a " +47oop.getKlass().getName().asString() + ")");48}49}50}5152//-- Internals only below this point53private JavaThread getOwnerThread(Oop oop) {54Oop threadOop = OopUtilities.abstractOwnableSynchronizerGetOwnerThread(oop);55if (threadOop == null) {56return null;57} else {58return OopUtilities.threadOopGetJavaThread(threadOop);59}60}6162private void fillLocks() {63VM vm = VM.getVM();64SystemDictionary sysDict = vm.getSystemDictionary();65Klass absOwnSyncKlass = sysDict.getAbstractOwnableSynchronizerKlass();66ObjectHeap heap = vm.getObjectHeap();67// may be not loaded at all68if (absOwnSyncKlass != null) {69heap.iterateObjectsOfKlass(new DefaultHeapVisitor() {70public boolean doObj(Oop oop) {71JavaThread thread = getOwnerThread(oop);72if (thread != null) {73List<Oop> locks = locksMap.get(thread);74if (locks == null) {75locks = new LinkedList<>();76locksMap.put(thread, locks);77}78locks.add(oop);79}80return false;81}8283}, absOwnSyncKlass, true);84}85}86}878889