annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2002-2004 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.ui;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.util.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import java.io.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import java.awt.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import java.awt.event.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import javax.swing.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import javax.swing.text.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 import sun.jvm.hotspot.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
35 import sun.jvm.hotspot.oops.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
36 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
37 import sun.jvm.hotspot.utilities.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
38
a61af66fc99e Initial load
duke
parents:
diff changeset
39 /** Finds a given (Address) value in the heap. Only intended for use
a61af66fc99e Initial load
duke
parents:
diff changeset
40 in a debugging system. */
a61af66fc99e Initial load
duke
parents:
diff changeset
41
a61af66fc99e Initial load
duke
parents:
diff changeset
42 public class FindInHeapPanel extends JPanel {
a61af66fc99e Initial load
duke
parents:
diff changeset
43 private RawHeapVisitor iterator;
a61af66fc99e Initial load
duke
parents:
diff changeset
44 private long addressSize;
a61af66fc99e Initial load
duke
parents:
diff changeset
45 private long usedSize;
a61af66fc99e Initial load
duke
parents:
diff changeset
46 private long iterated;
a61af66fc99e Initial load
duke
parents:
diff changeset
47 private Address value;
a61af66fc99e Initial load
duke
parents:
diff changeset
48 private ProgressBarPanel progressBar;
a61af66fc99e Initial load
duke
parents:
diff changeset
49 private HistoryComboBox addressField;
a61af66fc99e Initial load
duke
parents:
diff changeset
50 private JButton findButton;
a61af66fc99e Initial load
duke
parents:
diff changeset
51 private JTextArea textArea;
a61af66fc99e Initial load
duke
parents:
diff changeset
52 private ArrayList updates;
a61af66fc99e Initial load
duke
parents:
diff changeset
53 private double lastFrac;
a61af66fc99e Initial load
duke
parents:
diff changeset
54
a61af66fc99e Initial load
duke
parents:
diff changeset
55 static final double minUpdateFraction = 0.05;
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57 public FindInHeapPanel() {
a61af66fc99e Initial load
duke
parents:
diff changeset
58 super();
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 setLayout(new BorderLayout());
a61af66fc99e Initial load
duke
parents:
diff changeset
61
a61af66fc99e Initial load
duke
parents:
diff changeset
62 JPanel topPanel = new JPanel();
a61af66fc99e Initial load
duke
parents:
diff changeset
63 topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 JPanel panel = new JPanel();
a61af66fc99e Initial load
duke
parents:
diff changeset
66 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
a61af66fc99e Initial load
duke
parents:
diff changeset
67 panel.add(new JLabel("Address to search for:"));
a61af66fc99e Initial load
duke
parents:
diff changeset
68
a61af66fc99e Initial load
duke
parents:
diff changeset
69 addressField = new HistoryComboBox();
a61af66fc99e Initial load
duke
parents:
diff changeset
70 panel.add(addressField);
a61af66fc99e Initial load
duke
parents:
diff changeset
71
a61af66fc99e Initial load
duke
parents:
diff changeset
72 addressSize = VM.getVM().getAddressSize();
a61af66fc99e Initial load
duke
parents:
diff changeset
73
a61af66fc99e Initial load
duke
parents:
diff changeset
74 iterator = new RawHeapVisitor() {
a61af66fc99e Initial load
duke
parents:
diff changeset
75 boolean error = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
76
a61af66fc99e Initial load
duke
parents:
diff changeset
77 public void prologue(long used) {
a61af66fc99e Initial load
duke
parents:
diff changeset
78 usedSize = used;
a61af66fc99e Initial load
duke
parents:
diff changeset
79 iterated = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
80 lastFrac = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
81 error = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
82 updates = new ArrayList();
a61af66fc99e Initial load
duke
parents:
diff changeset
83 }
a61af66fc99e Initial load
duke
parents:
diff changeset
84
a61af66fc99e Initial load
duke
parents:
diff changeset
85 public void visitAddress(Address addr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
86 if (error) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 Address val = addr.getAddressAt(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
89 if (AddressOps.equal(val, value)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
90 error = reportResult(addr);
a61af66fc99e Initial load
duke
parents:
diff changeset
91 }
a61af66fc99e Initial load
duke
parents:
diff changeset
92 iterated += addressSize;
a61af66fc99e Initial load
duke
parents:
diff changeset
93 updateProgressBar();
a61af66fc99e Initial load
duke
parents:
diff changeset
94 }
a61af66fc99e Initial load
duke
parents:
diff changeset
95
a61af66fc99e Initial load
duke
parents:
diff changeset
96 public void epilogue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
97 iterated = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
98 updateProgressBar();
a61af66fc99e Initial load
duke
parents:
diff changeset
99 findButton.setEnabled(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
100 }
a61af66fc99e Initial load
duke
parents:
diff changeset
101 };
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 findButton = new JButton("Find");
a61af66fc99e Initial load
duke
parents:
diff changeset
104 ActionListener listener = new ActionListener() {
a61af66fc99e Initial load
duke
parents:
diff changeset
105 public void actionPerformed(ActionEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
106 clearResultWindow();
a61af66fc99e Initial load
duke
parents:
diff changeset
107 // Parse text
a61af66fc99e Initial load
duke
parents:
diff changeset
108 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
109 Address val = VM.getVM().getDebugger().parseAddress(addressField.getText());
a61af66fc99e Initial load
duke
parents:
diff changeset
110 value = val;
a61af66fc99e Initial load
duke
parents:
diff changeset
111
a61af66fc99e Initial load
duke
parents:
diff changeset
112 findButton.setEnabled(false);
a61af66fc99e Initial load
duke
parents:
diff changeset
113
a61af66fc99e Initial load
duke
parents:
diff changeset
114 java.lang.Thread t = new java.lang.Thread(new Runnable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
115 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
116 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
117 VM.getVM().getObjectHeap().iterateRaw(iterator);
a61af66fc99e Initial load
duke
parents:
diff changeset
118 } finally {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 SwingUtilities.invokeLater(new Runnable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 findButton.setEnabled(true);
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }
a61af66fc99e Initial load
duke
parents:
diff changeset
123 });
a61af66fc99e Initial load
duke
parents:
diff changeset
124 }
a61af66fc99e Initial load
duke
parents:
diff changeset
125 }
a61af66fc99e Initial load
duke
parents:
diff changeset
126 });
a61af66fc99e Initial load
duke
parents:
diff changeset
127 t.start();
a61af66fc99e Initial load
duke
parents:
diff changeset
128 } catch (Exception ex) {
a61af66fc99e Initial load
duke
parents:
diff changeset
129 textArea.setText("Error parsing address");
a61af66fc99e Initial load
duke
parents:
diff changeset
130 }
a61af66fc99e Initial load
duke
parents:
diff changeset
131 }
a61af66fc99e Initial load
duke
parents:
diff changeset
132 };
a61af66fc99e Initial load
duke
parents:
diff changeset
133 panel.add(findButton);
a61af66fc99e Initial load
duke
parents:
diff changeset
134 findButton.addActionListener(listener);
a61af66fc99e Initial load
duke
parents:
diff changeset
135 addressField.addActionListener(listener);
a61af66fc99e Initial load
duke
parents:
diff changeset
136 topPanel.add(panel);
a61af66fc99e Initial load
duke
parents:
diff changeset
137
a61af66fc99e Initial load
duke
parents:
diff changeset
138 progressBar = new ProgressBarPanel(ProgressBarPanel.HORIZONTAL, "Search progress:");
a61af66fc99e Initial load
duke
parents:
diff changeset
139 topPanel.add(progressBar);
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 add(topPanel, BorderLayout.NORTH);
a61af66fc99e Initial load
duke
parents:
diff changeset
142
a61af66fc99e Initial load
duke
parents:
diff changeset
143 textArea = new JTextArea();
a61af66fc99e Initial load
duke
parents:
diff changeset
144 JScrollPane scroller = new JScrollPane(textArea);
a61af66fc99e Initial load
duke
parents:
diff changeset
145 add(scroller, BorderLayout.CENTER);
a61af66fc99e Initial load
duke
parents:
diff changeset
146 }
a61af66fc99e Initial load
duke
parents:
diff changeset
147
a61af66fc99e Initial load
duke
parents:
diff changeset
148 private boolean pendingUpdate = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
149
a61af66fc99e Initial load
duke
parents:
diff changeset
150 private boolean reportResult(final Address addr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
151 synchronized (this) {
a61af66fc99e Initial load
duke
parents:
diff changeset
152 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
153 updates.add("found at " + addr + "\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
154 if (!pendingUpdate) {
a61af66fc99e Initial load
duke
parents:
diff changeset
155 pendingUpdate = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
156 SwingUtilities.invokeLater(new Runnable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
157 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
158 updateResultWindow();
a61af66fc99e Initial load
duke
parents:
diff changeset
159 }
a61af66fc99e Initial load
duke
parents:
diff changeset
160 });
a61af66fc99e Initial load
duke
parents:
diff changeset
161 }
a61af66fc99e Initial load
duke
parents:
diff changeset
162 } catch (Throwable t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
163 t.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
164 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
165 }
a61af66fc99e Initial load
duke
parents:
diff changeset
166 }
a61af66fc99e Initial load
duke
parents:
diff changeset
167
a61af66fc99e Initial load
duke
parents:
diff changeset
168 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
169 }
a61af66fc99e Initial load
duke
parents:
diff changeset
170
a61af66fc99e Initial load
duke
parents:
diff changeset
171 private void clearResultWindow() {
a61af66fc99e Initial load
duke
parents:
diff changeset
172 SwingUtilities.invokeLater(new Runnable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
173 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
174
a61af66fc99e Initial load
duke
parents:
diff changeset
175 Document d = textArea.getDocument();
a61af66fc99e Initial load
duke
parents:
diff changeset
176 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
177 d.remove(0, d.getLength());
a61af66fc99e Initial load
duke
parents:
diff changeset
178 } catch (BadLocationException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
179 }
a61af66fc99e Initial load
duke
parents:
diff changeset
180 }
a61af66fc99e Initial load
duke
parents:
diff changeset
181 });
a61af66fc99e Initial load
duke
parents:
diff changeset
182 }
a61af66fc99e Initial load
duke
parents:
diff changeset
183
a61af66fc99e Initial load
duke
parents:
diff changeset
184 private synchronized void updateResultWindow() {
a61af66fc99e Initial load
duke
parents:
diff changeset
185 if (updates.size() > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
186 Iterator i = updates.iterator();
a61af66fc99e Initial load
duke
parents:
diff changeset
187 while (i.hasNext()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
188 textArea.append((String)i.next());
a61af66fc99e Initial load
duke
parents:
diff changeset
189 }
a61af66fc99e Initial load
duke
parents:
diff changeset
190 updates = new ArrayList();;
a61af66fc99e Initial load
duke
parents:
diff changeset
191 }
a61af66fc99e Initial load
duke
parents:
diff changeset
192 pendingUpdate = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
193 }
a61af66fc99e Initial load
duke
parents:
diff changeset
194
a61af66fc99e Initial load
duke
parents:
diff changeset
195 private void invokeInDispatchThread(Runnable runnable) {
a61af66fc99e Initial load
duke
parents:
diff changeset
196 if (EventQueue.isDispatchThread()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
197 runnable.run();
a61af66fc99e Initial load
duke
parents:
diff changeset
198 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
199 SwingUtilities.invokeLater(runnable);
a61af66fc99e Initial load
duke
parents:
diff changeset
200 }
a61af66fc99e Initial load
duke
parents:
diff changeset
201 }
a61af66fc99e Initial load
duke
parents:
diff changeset
202
a61af66fc99e Initial load
duke
parents:
diff changeset
203 private void updateProgressBar() {
a61af66fc99e Initial load
duke
parents:
diff changeset
204 final double frac = (double) iterated / (double) usedSize;
a61af66fc99e Initial load
duke
parents:
diff changeset
205 if (frac == 0.0 || (frac - lastFrac > minUpdateFraction)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
206 lastFrac = frac;
a61af66fc99e Initial load
duke
parents:
diff changeset
207 if (iterated > usedSize) {
a61af66fc99e Initial load
duke
parents:
diff changeset
208 System.out.println("iterated " + iterated + " usedSize " + usedSize);
a61af66fc99e Initial load
duke
parents:
diff changeset
209 }
a61af66fc99e Initial load
duke
parents:
diff changeset
210 SwingUtilities.invokeLater(new Runnable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
211 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
212 progressBar.setValue(frac);
a61af66fc99e Initial load
duke
parents:
diff changeset
213 }
a61af66fc99e Initial load
duke
parents:
diff changeset
214 });
a61af66fc99e Initial load
duke
parents:
diff changeset
215 }
a61af66fc99e Initial load
duke
parents:
diff changeset
216 }
a61af66fc99e Initial load
duke
parents:
diff changeset
217 }