Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/ui/FindInHeapPanel.java
41161 views
/*1* Copyright (c) 2002, 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.ui;2526import java.util.*;27import java.io.*;28import java.awt.*;29import java.awt.event.*;30import javax.swing.*;31import javax.swing.text.*;3233import sun.jvm.hotspot.debugger.*;34import sun.jvm.hotspot.oops.*;35import sun.jvm.hotspot.runtime.*;36import sun.jvm.hotspot.utilities.*;3738/** Finds a given (Address) value in the heap. Only intended for use39in a debugging system. */4041public class FindInHeapPanel extends JPanel {42private RawHeapVisitor iterator;43private long addressSize;44private long usedSize;45private long iterated;46private Address value;47private ProgressBarPanel progressBar;48private HistoryComboBox addressField;49private JButton findButton;50private JTextArea textArea;51private ArrayList<String> updates;52private double lastFrac;5354static final double minUpdateFraction = 0.05;5556public FindInHeapPanel() {57super();5859setLayout(new BorderLayout());6061JPanel topPanel = new JPanel();62topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));6364JPanel panel = new JPanel();65panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));66panel.add(new JLabel("Address to search for:"));6768addressField = new HistoryComboBox();69panel.add(addressField);7071addressSize = VM.getVM().getAddressSize();7273iterator = new RawHeapVisitor() {74boolean error = false;7576public void prologue(long used) {77usedSize = used;78iterated = 0;79lastFrac = 0;80error = false;81updates = new ArrayList<>();82}8384public void visitAddress(Address addr) {85if (error) return;8687Address val = addr.getAddressAt(0);88if (AddressOps.equal(val, value)) {89error = reportResult(addr);90}91iterated += addressSize;92updateProgressBar();93}94public void visitCompOopAddress(Address addr) {95if (error) return;9697Address val = addr.getCompOopAddressAt(0);98if (AddressOps.equal(val, value)) {99error = reportResult(addr);100}101iterated += addressSize;102updateProgressBar();103104}105public void epilogue() {106iterated = 0;107updateProgressBar();108findButton.setEnabled(true);109}110};111112findButton = new JButton("Find");113ActionListener listener = new ActionListener() {114public void actionPerformed(ActionEvent e) {115clearResultWindow();116// Parse text117try {118Address val = VM.getVM().getDebugger().parseAddress(addressField.getText());119value = val;120121findButton.setEnabled(false);122123java.lang.Thread t = new java.lang.Thread(new Runnable() {124public void run() {125try {126VM.getVM().getObjectHeap().iterateRaw(iterator);127} finally {128SwingUtilities.invokeLater(new Runnable() {129public void run() {130findButton.setEnabled(true);131}132});133}134}135});136t.start();137} catch (Exception ex) {138textArea.setText("Error parsing address");139}140}141};142panel.add(findButton);143findButton.addActionListener(listener);144addressField.addActionListener(listener);145topPanel.add(panel);146147progressBar = new ProgressBarPanel(ProgressBarPanel.HORIZONTAL, "Search progress:");148topPanel.add(progressBar);149150add(topPanel, BorderLayout.NORTH);151152textArea = new JTextArea();153JScrollPane scroller = new JScrollPane(textArea);154add(scroller, BorderLayout.CENTER);155}156157private boolean pendingUpdate = false;158159private boolean reportResult(final Address addr) {160synchronized (this) {161try {162updates.add("found at " + addr + "\n");163if (!pendingUpdate) {164pendingUpdate = true;165SwingUtilities.invokeLater(new Runnable() {166public void run() {167updateResultWindow();168}169});170}171} catch (Throwable t) {172t.printStackTrace();173return true;174}175}176177return false;178}179180private void clearResultWindow() {181SwingUtilities.invokeLater(new Runnable() {182public void run() {183184Document d = textArea.getDocument();185try {186d.remove(0, d.getLength());187} catch (BadLocationException e) {188}189}190});191}192193private synchronized void updateResultWindow() {194if (updates.size() > 0) {195Iterator i = updates.iterator();196while (i.hasNext()) {197textArea.append((String)i.next());198}199updates = new ArrayList<>();;200}201pendingUpdate = false;202}203204private void invokeInDispatchThread(Runnable runnable) {205if (EventQueue.isDispatchThread()) {206runnable.run();207} else {208SwingUtilities.invokeLater(runnable);209}210}211212private void updateProgressBar() {213final double frac = (double) iterated / (double) usedSize;214if (frac == 0.0 || (frac - lastFrac > minUpdateFraction)) {215lastFrac = frac;216if (iterated > usedSize) {217System.out.println("iterated " + iterated + " usedSize " + usedSize);218}219SwingUtilities.invokeLater(new Runnable() {220public void run() {221progressBar.setValue(frac);222}223});224}225}226}227228229