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.bdi.*;
|
|
40 |
|
|
41 |
public class StackTraceTool extends JPanel {
|
|
42 |
|
|
43 |
private Environment env;
|
|
44 |
|
|
45 |
private ExecutionManager runtime;
|
|
46 |
private ContextManager context;
|
|
47 |
|
|
48 |
private ThreadInfo tinfo;
|
|
49 |
|
|
50 |
private JList list;
|
|
51 |
private ListModel stackModel;
|
|
52 |
|
|
53 |
public StackTraceTool(Environment env) {
|
|
54 |
|
|
55 |
super(new BorderLayout());
|
|
56 |
|
|
57 |
this.env = env;
|
|
58 |
this.runtime = env.getExecutionManager();
|
|
59 |
this.context = env.getContextManager();
|
|
60 |
|
|
61 |
stackModel = new DefaultListModel(); // empty
|
|
62 |
|
|
63 |
list = new JList(stackModel);
|
|
64 |
list.setCellRenderer(new StackFrameRenderer());
|
|
65 |
|
|
66 |
JScrollPane listView = new JScrollPane(list);
|
|
67 |
add(listView);
|
|
68 |
|
|
69 |
// Create listener.
|
|
70 |
StackTraceToolListener listener = new StackTraceToolListener();
|
|
71 |
context.addContextListener(listener);
|
|
72 |
list.addListSelectionListener(listener);
|
|
73 |
|
|
74 |
//### remove listeners on exit!
|
|
75 |
}
|
|
76 |
|
|
77 |
private class StackTraceToolListener
|
|
78 |
implements ContextListener, ListSelectionListener
|
|
79 |
{
|
|
80 |
|
|
81 |
// ContextListener
|
|
82 |
|
|
83 |
// If the user selects a new current frame, display it in
|
|
84 |
// this view.
|
|
85 |
|
|
86 |
//### I suspect we handle the case badly that the VM is not interrupted.
|
|
87 |
|
|
88 |
public void currentFrameChanged(CurrentFrameChangedEvent e) {
|
|
89 |
// If the current frame of the thread appearing in this
|
|
90 |
// view is changed, move the selection to track it.
|
|
91 |
int frameIndex = e.getIndex();
|
|
92 |
ThreadInfo ti = e.getThreadInfo();
|
|
93 |
if (e.getInvalidate() || tinfo != ti) {
|
|
94 |
tinfo = ti;
|
|
95 |
showStack(ti, frameIndex);
|
|
96 |
} else {
|
|
97 |
if (frameIndex < stackModel.getSize()) {
|
|
98 |
list.setSelectedIndex(frameIndex);
|
|
99 |
list.ensureIndexIsVisible(frameIndex);
|
|
100 |
}
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
// ListSelectionListener
|
|
105 |
|
|
106 |
public void valueChanged(ListSelectionEvent e) {
|
|
107 |
int index = list.getSelectedIndex();
|
|
108 |
if (index != -1) {
|
|
109 |
//### should use listener?
|
|
110 |
try {
|
|
111 |
context.setCurrentFrameIndex(index);
|
|
112 |
} catch (VMNotInterruptedException exc) {
|
|
113 |
}
|
|
114 |
}
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
private class StackFrameRenderer extends DefaultListCellRenderer {
|
|
119 |
|
|
120 |
public Component getListCellRendererComponent(JList list,
|
|
121 |
Object value,
|
|
122 |
int index,
|
|
123 |
boolean isSelected,
|
|
124 |
boolean cellHasFocus) {
|
|
125 |
|
|
126 |
//### We should indicate the current thread independently of the
|
|
127 |
//### selection, e.g., with an icon, because the user may change
|
|
128 |
//### the selection graphically without affecting the current
|
|
129 |
//### thread.
|
|
130 |
|
|
131 |
super.getListCellRendererComponent(list, value, index,
|
|
132 |
isSelected, cellHasFocus);
|
|
133 |
if (value == null) {
|
|
134 |
this.setText("<unavailable>");
|
|
135 |
} else {
|
|
136 |
StackFrame frame = (StackFrame)value;
|
|
137 |
Location loc = frame.location();
|
|
138 |
Method meth = loc.method();
|
|
139 |
String methName =
|
|
140 |
meth.declaringType().name() + '.' + meth.name();
|
|
141 |
String position = "";
|
51
|
142 |
if (meth.isNative()) {
|
2
|
143 |
position = " (native method)";
|
|
144 |
} else if (loc.lineNumber() != -1) {
|
|
145 |
position = ":" + loc.lineNumber();
|
|
146 |
} else {
|
|
147 |
long pc = loc.codeIndex();
|
|
148 |
if (pc != -1) {
|
|
149 |
position = ", pc = " + pc;
|
|
150 |
}
|
|
151 |
}
|
|
152 |
// Indices are presented to the user starting from 1, not 0.
|
|
153 |
this.setText("[" + (index+1) +"] " + methName + position);
|
|
154 |
}
|
|
155 |
return this;
|
|
156 |
}
|
|
157 |
}
|
|
158 |
|
|
159 |
// Point this view at the given thread and frame.
|
|
160 |
|
|
161 |
private void showStack(ThreadInfo tinfo, int selectFrame) {
|
|
162 |
StackTraceListModel model = new StackTraceListModel(tinfo);
|
|
163 |
stackModel = model;
|
|
164 |
list.setModel(stackModel);
|
|
165 |
list.setSelectedIndex(selectFrame);
|
|
166 |
list.ensureIndexIsVisible(selectFrame);
|
|
167 |
}
|
|
168 |
|
|
169 |
private static class StackTraceListModel extends AbstractListModel {
|
|
170 |
|
|
171 |
private final ThreadInfo tinfo;
|
|
172 |
|
|
173 |
public StackTraceListModel(ThreadInfo tinfo) {
|
|
174 |
this.tinfo = tinfo;
|
|
175 |
}
|
|
176 |
|
|
177 |
public Object getElementAt(int index) {
|
|
178 |
try {
|
|
179 |
return tinfo == null? null : tinfo.getFrame(index);
|
|
180 |
} catch (VMNotInterruptedException e) {
|
|
181 |
//### Is this the right way to handle this?
|
|
182 |
//### Would happen if user scrolled stack trace
|
|
183 |
//### while not interrupted -- should probably
|
|
184 |
//### block user interaction in this case.
|
|
185 |
return null;
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
|
189 |
public int getSize() {
|
|
190 |
try {
|
|
191 |
return tinfo == null? 1 : tinfo.getFrameCount();
|
|
192 |
} catch (VMNotInterruptedException e) {
|
|
193 |
//### Is this the right way to handle this?
|
|
194 |
return 0;
|
|
195 |
}
|
|
196 |
}
|
|
197 |
}
|
|
198 |
}
|