2
|
1 |
/*
|
715
|
2 |
* Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.tools.example.debug.gui;
|
|
27 |
|
|
28 |
import java.io.*;
|
|
29 |
import java.util.*;
|
|
30 |
import java.util.List; // Must import explicitly due to conflict with javax.awt.List
|
|
31 |
|
|
32 |
import javax.swing.*;
|
|
33 |
import javax.swing.tree.*;
|
|
34 |
import javax.swing.event.*;
|
|
35 |
import java.awt.*;
|
|
36 |
import java.awt.event.*;
|
|
37 |
|
|
38 |
import com.sun.jdi.*;
|
|
39 |
import com.sun.tools.example.debug.event.*;
|
|
40 |
import com.sun.tools.example.debug.bdi.*;
|
|
41 |
|
|
42 |
//### Bug: If the name of a thread is changed via Thread.setName(), the
|
|
43 |
//### thread tree view does not reflect this. The name of the thread at
|
|
44 |
//### the time it is created is used throughout its lifetime.
|
|
45 |
|
|
46 |
public class ThreadTreeTool extends JPanel {
|
|
47 |
|
|
48 |
private Environment env;
|
|
49 |
|
|
50 |
private ExecutionManager runtime;
|
|
51 |
private SourceManager sourceManager;
|
|
52 |
private ClassManager classManager;
|
|
53 |
|
|
54 |
private JTree tree;
|
|
55 |
private DefaultTreeModel treeModel;
|
|
56 |
private ThreadTreeNode root;
|
|
57 |
private SearchPath sourcePath;
|
|
58 |
|
|
59 |
private CommandInterpreter interpreter;
|
|
60 |
|
|
61 |
private static String HEADING = "THREADS";
|
|
62 |
|
|
63 |
public ThreadTreeTool(Environment env) {
|
|
64 |
|
|
65 |
super(new BorderLayout());
|
|
66 |
|
|
67 |
this.env = env;
|
|
68 |
this.runtime = env.getExecutionManager();
|
|
69 |
this.sourceManager = env.getSourceManager();
|
|
70 |
|
|
71 |
this.interpreter = new CommandInterpreter(env);
|
|
72 |
|
|
73 |
root = createThreadTree(HEADING);
|
|
74 |
treeModel = new DefaultTreeModel(root);
|
|
75 |
|
|
76 |
// Create a tree that allows one selection at a time.
|
|
77 |
|
|
78 |
tree = new JTree(treeModel);
|
|
79 |
tree.setSelectionModel(new SingleLeafTreeSelectionModel());
|
|
80 |
|
|
81 |
MouseListener ml = new MouseAdapter() {
|
|
82 |
public void mouseClicked(MouseEvent e) {
|
|
83 |
int selRow = tree.getRowForLocation(e.getX(), e.getY());
|
|
84 |
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
|
|
85 |
if(selRow != -1) {
|
|
86 |
if(e.getClickCount() == 1) {
|
|
87 |
ThreadTreeNode node =
|
|
88 |
(ThreadTreeNode)selPath.getLastPathComponent();
|
|
89 |
// If user clicks on leaf, select it, and issue 'thread' command.
|
|
90 |
if (node.isLeaf()) {
|
|
91 |
tree.setSelectionPath(selPath);
|
|
92 |
interpreter.executeCommand("thread " +
|
|
93 |
node.getThreadId() +
|
|
94 |
" (\"" +
|
|
95 |
node.getName() + "\")");
|
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|
|
99 |
}
|
|
100 |
};
|
|
101 |
|
|
102 |
tree.addMouseListener(ml);
|
|
103 |
|
|
104 |
JScrollPane treeView = new JScrollPane(tree);
|
|
105 |
add(treeView);
|
|
106 |
|
|
107 |
// Create listener.
|
|
108 |
ThreadTreeToolListener listener = new ThreadTreeToolListener();
|
|
109 |
runtime.addJDIListener(listener);
|
|
110 |
runtime.addSessionListener(listener);
|
|
111 |
|
|
112 |
//### remove listeners on exit!
|
|
113 |
}
|
|
114 |
|
|
115 |
HashMap<ThreadReference, List<String>> threadTable = new HashMap<ThreadReference, List<String>>();
|
|
116 |
|
|
117 |
private List<String> threadPath(ThreadReference thread) {
|
|
118 |
// May exit abnormally if VM disconnects.
|
|
119 |
List<String> l = new ArrayList<String>();
|
|
120 |
l.add(0, thread.name());
|
|
121 |
ThreadGroupReference group = thread.threadGroup();
|
|
122 |
while (group != null) {
|
|
123 |
l.add(0, group.name());
|
|
124 |
group = group.parent();
|
|
125 |
}
|
|
126 |
return l;
|
|
127 |
}
|
|
128 |
|
|
129 |
private class ThreadTreeToolListener extends JDIAdapter
|
|
130 |
implements JDIListener, SessionListener {
|
|
131 |
|
|
132 |
// SessionListener
|
|
133 |
|
|
134 |
public void sessionStart(EventObject e) {
|
|
135 |
try {
|
51
|
136 |
for (ThreadReference thread : runtime.allThreads()) {
|
2
|
137 |
root.addThread(thread);
|
|
138 |
}
|
|
139 |
} catch (VMDisconnectedException ee) {
|
|
140 |
// VM went away unexpectedly.
|
|
141 |
} catch (NoSessionException ee) {
|
|
142 |
// Ignore. Should not happen.
|
|
143 |
}
|
|
144 |
}
|
|
145 |
|
|
146 |
public void sessionInterrupt(EventObject e) {}
|
|
147 |
public void sessionContinue(EventObject e) {}
|
|
148 |
|
|
149 |
|
|
150 |
// JDIListener
|
|
151 |
|
|
152 |
public void threadStart(ThreadStartEventSet e) {
|
|
153 |
root.addThread(e.getThread());
|
|
154 |
}
|
|
155 |
|
|
156 |
public void threadDeath(ThreadDeathEventSet e) {
|
|
157 |
root.removeThread(e.getThread());
|
|
158 |
}
|
|
159 |
|
|
160 |
public void vmDisconnect(VMDisconnectEventSet e) {
|
|
161 |
// Clear the contents of this view.
|
|
162 |
root = createThreadTree(HEADING);
|
|
163 |
treeModel = new DefaultTreeModel(root);
|
|
164 |
tree.setModel(treeModel);
|
|
165 |
threadTable = new HashMap<ThreadReference, List<String>>();
|
|
166 |
}
|
|
167 |
|
|
168 |
}
|
|
169 |
|
|
170 |
ThreadTreeNode createThreadTree(String label) {
|
|
171 |
return new ThreadTreeNode(label, null);
|
|
172 |
}
|
|
173 |
|
|
174 |
class ThreadTreeNode extends DefaultMutableTreeNode {
|
|
175 |
|
|
176 |
String name;
|
|
177 |
ThreadReference thread; // null if thread group
|
|
178 |
long uid;
|
|
179 |
String description;
|
|
180 |
|
|
181 |
ThreadTreeNode(String name, ThreadReference thread) {
|
|
182 |
if (name == null) {
|
|
183 |
name = "<unnamed>";
|
|
184 |
}
|
|
185 |
this.name = name;
|
|
186 |
this.thread = thread;
|
|
187 |
if (thread == null) {
|
|
188 |
this.uid = -1;
|
|
189 |
this.description = name;
|
|
190 |
} else {
|
|
191 |
this.uid = thread.uniqueID();
|
|
192 |
this.description = name + " (t@" + Long.toHexString(uid) + ")";
|
|
193 |
}
|
|
194 |
}
|
|
195 |
|
|
196 |
public String toString() {
|
|
197 |
return description;
|
|
198 |
}
|
|
199 |
|
|
200 |
public String getName() {
|
|
201 |
return name;
|
|
202 |
}
|
|
203 |
|
|
204 |
public ThreadReference getThread() {
|
|
205 |
return thread;
|
|
206 |
}
|
|
207 |
|
|
208 |
public String getThreadId() {
|
|
209 |
return "t@" + Long.toHexString(uid);
|
|
210 |
}
|
|
211 |
|
|
212 |
private boolean isThreadGroup() {
|
|
213 |
return (thread == null);
|
|
214 |
}
|
|
215 |
|
|
216 |
public boolean isLeaf() {
|
|
217 |
return !isThreadGroup();
|
|
218 |
}
|
|
219 |
|
|
220 |
public void addThread(ThreadReference thread) {
|
|
221 |
// This can fail if the VM disconnects.
|
|
222 |
// It is important to do all necessary JDI calls
|
|
223 |
// before modifying the tree, so we don't abort
|
|
224 |
// midway through!
|
|
225 |
if (threadTable.get(thread) == null) {
|
|
226 |
// Add thread only if not already present.
|
|
227 |
try {
|
|
228 |
List<String> path = threadPath(thread);
|
|
229 |
// May not get here due to exception.
|
|
230 |
// If we get here, we are committed.
|
|
231 |
// We must not leave the tree partially updated.
|
|
232 |
try {
|
|
233 |
threadTable.put(thread, path);
|
|
234 |
addThread(path, thread);
|
|
235 |
} catch (Throwable tt) {
|
|
236 |
//### Assertion failure.
|
|
237 |
throw new RuntimeException("ThreadTree corrupted");
|
|
238 |
}
|
|
239 |
} catch (VMDisconnectedException ee) {
|
|
240 |
// Ignore. Thread will not be added.
|
|
241 |
}
|
|
242 |
}
|
|
243 |
}
|
|
244 |
|
51
|
245 |
private void addThread(List<String> threadPath, ThreadReference thread) {
|
2
|
246 |
int size = threadPath.size();
|
|
247 |
if (size == 0) {
|
|
248 |
return;
|
|
249 |
} else if (size == 1) {
|
51
|
250 |
String name = threadPath.get(0);
|
2
|
251 |
insertNode(name, thread);
|
|
252 |
} else {
|
51
|
253 |
String head = threadPath.get(0);
|
|
254 |
List<String> tail = threadPath.subList(1, size);
|
2
|
255 |
ThreadTreeNode child = insertNode(head, null);
|
|
256 |
child.addThread(tail, thread);
|
|
257 |
}
|
|
258 |
}
|
|
259 |
|
|
260 |
private ThreadTreeNode insertNode(String name, ThreadReference thread) {
|
|
261 |
for (int i = 0; i < getChildCount(); i++) {
|
|
262 |
ThreadTreeNode child = (ThreadTreeNode)getChildAt(i);
|
|
263 |
int cmp = name.compareTo(child.getName());
|
|
264 |
if (cmp == 0 && thread == null) {
|
|
265 |
// A like-named interior node already exists.
|
|
266 |
return child;
|
|
267 |
} else if (cmp < 0) {
|
|
268 |
// Insert new node before the child.
|
|
269 |
ThreadTreeNode newChild = new ThreadTreeNode(name, thread);
|
|
270 |
treeModel.insertNodeInto(newChild, this, i);
|
|
271 |
return newChild;
|
|
272 |
}
|
|
273 |
}
|
|
274 |
// Insert new node after last child.
|
|
275 |
ThreadTreeNode newChild = new ThreadTreeNode(name, thread);
|
|
276 |
treeModel.insertNodeInto(newChild, this, getChildCount());
|
|
277 |
return newChild;
|
|
278 |
}
|
|
279 |
|
|
280 |
public void removeThread(ThreadReference thread) {
|
|
281 |
List<String> threadPath = threadTable.get(thread);
|
|
282 |
// Only remove thread if we recorded it in table.
|
|
283 |
// Original add may have failed due to VM disconnect.
|
|
284 |
if (threadPath != null) {
|
|
285 |
removeThread(threadPath, thread);
|
|
286 |
}
|
|
287 |
}
|
|
288 |
|
51
|
289 |
private void removeThread(List<String> threadPath, ThreadReference thread) {
|
2
|
290 |
int size = threadPath.size();
|
|
291 |
if (size == 0) {
|
|
292 |
return;
|
|
293 |
} else if (size == 1) {
|
51
|
294 |
String name = threadPath.get(0);
|
2
|
295 |
ThreadTreeNode child = findLeafNode(thread, name);
|
|
296 |
treeModel.removeNodeFromParent(child);
|
|
297 |
} else {
|
51
|
298 |
String head = threadPath.get(0);
|
|
299 |
List<String> tail = threadPath.subList(1, size);
|
2
|
300 |
ThreadTreeNode child = findInternalNode(head);
|
|
301 |
child.removeThread(tail, thread);
|
|
302 |
if (child.isThreadGroup() && child.getChildCount() < 1) {
|
|
303 |
// Prune non-leaf nodes with no children.
|
|
304 |
treeModel.removeNodeFromParent(child);
|
|
305 |
}
|
|
306 |
}
|
|
307 |
}
|
|
308 |
|
|
309 |
private ThreadTreeNode findLeafNode(ThreadReference thread, String name) {
|
|
310 |
for (int i = 0; i < getChildCount(); i++) {
|
|
311 |
ThreadTreeNode child = (ThreadTreeNode)getChildAt(i);
|
|
312 |
if (child.getThread() == thread) {
|
|
313 |
if (!name.equals(child.getName())) {
|
|
314 |
//### Assertion failure.
|
|
315 |
throw new RuntimeException("name mismatch");
|
|
316 |
}
|
|
317 |
return child;
|
|
318 |
}
|
|
319 |
}
|
|
320 |
//### Assertion failure.
|
|
321 |
throw new RuntimeException("not found");
|
|
322 |
}
|
|
323 |
|
|
324 |
private ThreadTreeNode findInternalNode(String name) {
|
|
325 |
for (int i = 0; i < getChildCount(); i++) {
|
|
326 |
ThreadTreeNode child = (ThreadTreeNode)getChildAt(i);
|
|
327 |
if (name.equals(child.getName())) {
|
|
328 |
return child;
|
|
329 |
}
|
|
330 |
}
|
|
331 |
//### Assertion failure.
|
|
332 |
throw new RuntimeException("not found");
|
|
333 |
}
|
|
334 |
|
|
335 |
}
|
|
336 |
|
|
337 |
}
|