comparison agent/src/share/classes/sun/jvm/hotspot/ui/FindInHeapPanel.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children ba764ed4b6f2
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2002-2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 package sun.jvm.hotspot.ui;
26
27 import java.util.*;
28 import java.io.*;
29 import java.awt.*;
30 import java.awt.event.*;
31 import javax.swing.*;
32 import javax.swing.text.*;
33
34 import sun.jvm.hotspot.debugger.*;
35 import sun.jvm.hotspot.oops.*;
36 import sun.jvm.hotspot.runtime.*;
37 import sun.jvm.hotspot.utilities.*;
38
39 /** Finds a given (Address) value in the heap. Only intended for use
40 in a debugging system. */
41
42 public class FindInHeapPanel extends JPanel {
43 private RawHeapVisitor iterator;
44 private long addressSize;
45 private long usedSize;
46 private long iterated;
47 private Address value;
48 private ProgressBarPanel progressBar;
49 private HistoryComboBox addressField;
50 private JButton findButton;
51 private JTextArea textArea;
52 private ArrayList updates;
53 private double lastFrac;
54
55 static final double minUpdateFraction = 0.05;
56
57 public FindInHeapPanel() {
58 super();
59
60 setLayout(new BorderLayout());
61
62 JPanel topPanel = new JPanel();
63 topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
64
65 JPanel panel = new JPanel();
66 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
67 panel.add(new JLabel("Address to search for:"));
68
69 addressField = new HistoryComboBox();
70 panel.add(addressField);
71
72 addressSize = VM.getVM().getAddressSize();
73
74 iterator = new RawHeapVisitor() {
75 boolean error = false;
76
77 public void prologue(long used) {
78 usedSize = used;
79 iterated = 0;
80 lastFrac = 0;
81 error = false;
82 updates = new ArrayList();
83 }
84
85 public void visitAddress(Address addr) {
86 if (error) return;
87
88 Address val = addr.getAddressAt(0);
89 if (AddressOps.equal(val, value)) {
90 error = reportResult(addr);
91 }
92 iterated += addressSize;
93 updateProgressBar();
94 }
95
96 public void epilogue() {
97 iterated = 0;
98 updateProgressBar();
99 findButton.setEnabled(true);
100 }
101 };
102
103 findButton = new JButton("Find");
104 ActionListener listener = new ActionListener() {
105 public void actionPerformed(ActionEvent e) {
106 clearResultWindow();
107 // Parse text
108 try {
109 Address val = VM.getVM().getDebugger().parseAddress(addressField.getText());
110 value = val;
111
112 findButton.setEnabled(false);
113
114 java.lang.Thread t = new java.lang.Thread(new Runnable() {
115 public void run() {
116 try {
117 VM.getVM().getObjectHeap().iterateRaw(iterator);
118 } finally {
119 SwingUtilities.invokeLater(new Runnable() {
120 public void run() {
121 findButton.setEnabled(true);
122 }
123 });
124 }
125 }
126 });
127 t.start();
128 } catch (Exception ex) {
129 textArea.setText("Error parsing address");
130 }
131 }
132 };
133 panel.add(findButton);
134 findButton.addActionListener(listener);
135 addressField.addActionListener(listener);
136 topPanel.add(panel);
137
138 progressBar = new ProgressBarPanel(ProgressBarPanel.HORIZONTAL, "Search progress:");
139 topPanel.add(progressBar);
140
141 add(topPanel, BorderLayout.NORTH);
142
143 textArea = new JTextArea();
144 JScrollPane scroller = new JScrollPane(textArea);
145 add(scroller, BorderLayout.CENTER);
146 }
147
148 private boolean pendingUpdate = false;
149
150 private boolean reportResult(final Address addr) {
151 synchronized (this) {
152 try {
153 updates.add("found at " + addr + "\n");
154 if (!pendingUpdate) {
155 pendingUpdate = true;
156 SwingUtilities.invokeLater(new Runnable() {
157 public void run() {
158 updateResultWindow();
159 }
160 });
161 }
162 } catch (Throwable t) {
163 t.printStackTrace();
164 return true;
165 }
166 }
167
168 return false;
169 }
170
171 private void clearResultWindow() {
172 SwingUtilities.invokeLater(new Runnable() {
173 public void run() {
174
175 Document d = textArea.getDocument();
176 try {
177 d.remove(0, d.getLength());
178 } catch (BadLocationException e) {
179 }
180 }
181 });
182 }
183
184 private synchronized void updateResultWindow() {
185 if (updates.size() > 0) {
186 Iterator i = updates.iterator();
187 while (i.hasNext()) {
188 textArea.append((String)i.next());
189 }
190 updates = new ArrayList();;
191 }
192 pendingUpdate = false;
193 }
194
195 private void invokeInDispatchThread(Runnable runnable) {
196 if (EventQueue.isDispatchThread()) {
197 runnable.run();
198 } else {
199 SwingUtilities.invokeLater(runnable);
200 }
201 }
202
203 private void updateProgressBar() {
204 final double frac = (double) iterated / (double) usedSize;
205 if (frac == 0.0 || (frac - lastFrac > minUpdateFraction)) {
206 lastFrac = frac;
207 if (iterated > usedSize) {
208 System.out.println("iterated " + iterated + " usedSize " + usedSize);
209 }
210 SwingUtilities.invokeLater(new Runnable() {
211 public void run() {
212 progressBar.setValue(frac);
213 }
214 });
215 }
216 }
217 }