Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/ui/FindInCodeCachePanel.java
41161 views
/*1* Copyright (c) 2004, 2010, 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.io.*;27import java.awt.*;28import java.awt.event.*;29import javax.swing.*;30import javax.swing.event.*;31import javax.swing.text.*;3233import sun.jvm.hotspot.debugger.*;34import sun.jvm.hotspot.oops.*;35import sun.jvm.hotspot.runtime.*;36import sun.jvm.hotspot.utilities.*;37import sun.jvm.hotspot.code.*;38import sun.jvm.hotspot.ui.classbrowser.*;3940/** Finds a given (Address) value in the code cache. Only intended for use41in a debugging system. */4243public class FindInCodeCachePanel extends SAPanel {44private Visitor iterator;45private long usedSize;46private long iterated;47private Address value;48private ProgressBarPanel progressBar;49private HistoryComboBox addressField;50private JButton findButton;51private SAEditorPane contentEditor;5253class Visitor implements CodeCacheVisitor {54Address base;55StringBuffer result;56boolean searching;5758public void prologue(Address start, Address end) {59searching = true;60base = start;61usedSize = end.minus(start);62iterated = 0;63result = new StringBuffer();64clearResultWindow();65}6667public void visit(CodeBlob blob) {68Address begin = blob.headerBegin();69Address end = begin.addOffsetTo(blob.getSize());70long addressSize = VM.getVM().getAddressSize();7172boolean found = false;73while (!found && begin.lessThan(end)) {74Address val = begin.getAddressAt(0);75if (AddressOps.equal(val, value)) {76reportResult(result, blob);77found = true;78}79begin = begin.addOffsetTo(addressSize);80}81iterated = end.minus(base);;82updateProgressBar(null);83}8485public void epilogue() {86}8788public void cleanup() {89iterated = 0;90updateProgressBar(result);91searching = false;92result = null;93}9495private void search() {96// Parse text97Address val = null;98try {99val = VM.getVM().getDebugger().parseAddress(addressField.getText());100} catch (Exception ex) {101contentEditor.setText("<b>Error parsing address</b>");102return;103}104105// make sure we don't start up multiple search threads in parallel106synchronized (iterator) {107if (searching && value.equals(val)) {108return;109}110111value = val;112contentEditor.setText("");113findButton.setEnabled(false);114115System.out.println("Searching " + value);116java.lang.Thread t = new java.lang.Thread(new Runnable() {117public void run() {118synchronized (iterator) {119try {120VM.getVM().getCodeCache().iterate(iterator);121} finally {122iterator.cleanup();123}124}125}126});127t.start();128}129}130}131132133public FindInCodeCachePanel() {134super();135136setLayout(new BorderLayout());137138JPanel topPanel = new JPanel();139topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));140141JPanel panel = new JPanel();142panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));143panel.add(new JLabel("Address to search for:"));144145addressField = new HistoryComboBox();146panel.add(addressField);147148iterator = new Visitor();149150findButton = new JButton("Find");151ActionListener listener = new ActionListener() {152public void actionPerformed(ActionEvent e) {153iterator.search();154}155};156panel.add(findButton);157findButton.addActionListener(listener);158addressField.addActionListener(listener);159topPanel.add(panel);160161progressBar = new ProgressBarPanel(ProgressBarPanel.HORIZONTAL, "Search progress:");162topPanel.add(progressBar);163164add(topPanel, BorderLayout.NORTH);165166contentEditor = new SAEditorPane();167168HyperlinkListener hyperListener = new HyperlinkListener() {169public void hyperlinkUpdate(HyperlinkEvent e) {170if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {171String description = e.getDescription();172int index = description.indexOf(':');173if (index != -1) {174String item = description.substring(0, index);175if (item.equals("blob")) {176Address blob = VM.getVM().getDebugger().parseAddress(description.substring(index + 1));177showCodeViewer(blob);178}179}180}181}182};183184contentEditor.addHyperlinkListener(hyperListener);185186JScrollPane scroller = new JScrollPane(contentEditor);187add(scroller, BorderLayout.CENTER);188}189190private void reportResult(StringBuffer result, CodeBlob blob) {191result.append("<a href='blob:");192result.append(blob.contentBegin().toString());193result.append("'>");194result.append(blob.getName());195result.append("@");196result.append(blob.contentBegin());197result.append("</a><br>");198}199200private void clearResultWindow() {201SwingUtilities.invokeLater(new Runnable() {202public void run() {203contentEditor.setText("");204}205});206}207208private void updateProgressBar(final StringBuffer result) {209SwingUtilities.invokeLater(new Runnable() {210public void run() {211progressBar.setValue((double) iterated / (double) usedSize);212if (result != null) {213String s = "<html> <head> </head> <body>\n" + result + " </body> </html>";214contentEditor.setText(s);215findButton.setEnabled(true);216}217}218});219}220}221222223