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.tty;
|
|
27 |
|
|
28 |
import com.sun.jdi.ThreadReference;
|
|
29 |
import com.sun.jdi.ThreadGroupReference;
|
|
30 |
import com.sun.jdi.IncompatibleThreadStateException;
|
|
31 |
import com.sun.jdi.StackFrame;
|
|
32 |
import java.util.List;
|
|
33 |
import java.util.ArrayList;
|
|
34 |
import java.util.Collections;
|
|
35 |
import java.util.Iterator;
|
|
36 |
import java.io.*;
|
|
37 |
|
|
38 |
class ThreadInfo {
|
|
39 |
// This is a list of all known ThreadInfo objects. It survives
|
|
40 |
// ThreadInfo.invalidateAll, unlike the other static fields below.
|
|
41 |
private static List<ThreadInfo> threads = Collections.synchronizedList(new ArrayList<ThreadInfo>());
|
|
42 |
private static boolean gotInitialThreads = false;
|
|
43 |
|
|
44 |
private static ThreadInfo current = null;
|
|
45 |
private static ThreadGroupReference group = null;
|
|
46 |
|
|
47 |
private final ThreadReference thread;
|
|
48 |
private int currentFrameIndex = 0;
|
|
49 |
|
|
50 |
private ThreadInfo(ThreadReference thread) {
|
|
51 |
this.thread = thread;
|
|
52 |
if (thread == null) {
|
|
53 |
MessageOutput.fatalError("Internal error: null ThreadInfo created");
|
|
54 |
}
|
|
55 |
}
|
|
56 |
|
|
57 |
private static void initThreads() {
|
|
58 |
if (!gotInitialThreads) {
|
51
|
59 |
for (ThreadReference thread : Env.vm().allThreads()) {
|
2
|
60 |
threads.add(new ThreadInfo(thread));
|
|
61 |
}
|
|
62 |
gotInitialThreads = true;
|
|
63 |
}
|
|
64 |
}
|
|
65 |
|
|
66 |
static void addThread(ThreadReference thread) {
|
|
67 |
synchronized (threads) {
|
|
68 |
initThreads();
|
|
69 |
ThreadInfo ti = new ThreadInfo(thread);
|
|
70 |
// Guard against duplicates. Duplicates can happen during
|
|
71 |
// initialization when a particular thread might be added both
|
|
72 |
// by a thread start event and by the initial call to threads()
|
|
73 |
if (getThreadInfo(thread) == null) {
|
|
74 |
threads.add(ti);
|
|
75 |
}
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
static void removeThread(ThreadReference thread) {
|
|
80 |
if (thread.equals(ThreadInfo.current)) {
|
|
81 |
// Current thread has died.
|
|
82 |
|
|
83 |
// Be careful getting the thread name. If its death happens
|
|
84 |
// as part of VM termination, it may be too late to get the
|
|
85 |
// information, and an exception will be thrown.
|
|
86 |
String currentThreadName;
|
|
87 |
try {
|
|
88 |
currentThreadName = "\"" + thread.name() + "\"";
|
|
89 |
} catch (Exception e) {
|
|
90 |
currentThreadName = "";
|
|
91 |
}
|
|
92 |
|
|
93 |
setCurrentThread(null);
|
|
94 |
|
|
95 |
MessageOutput.println();
|
|
96 |
MessageOutput.println("Current thread died. Execution continuing...",
|
|
97 |
currentThreadName);
|
|
98 |
}
|
|
99 |
threads.remove(getThreadInfo(thread));
|
|
100 |
}
|
|
101 |
|
|
102 |
static List<ThreadInfo> threads() {
|
|
103 |
synchronized(threads) {
|
|
104 |
initThreads();
|
|
105 |
// Make a copy to allow iteration without synchronization
|
|
106 |
return new ArrayList<ThreadInfo>(threads);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
|
110 |
static void invalidateAll() {
|
|
111 |
current = null;
|
|
112 |
group = null;
|
|
113 |
synchronized (threads) {
|
51
|
114 |
for (ThreadInfo ti : threads()) {
|
2
|
115 |
ti.invalidate();
|
|
116 |
}
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
static void setThreadGroup(ThreadGroupReference tg) {
|
|
121 |
group = tg;
|
|
122 |
}
|
|
123 |
|
|
124 |
static void setCurrentThread(ThreadReference tr) {
|
|
125 |
if (tr == null) {
|
|
126 |
setCurrentThreadInfo(null);
|
|
127 |
} else {
|
|
128 |
ThreadInfo tinfo = getThreadInfo(tr);
|
|
129 |
setCurrentThreadInfo(tinfo);
|
|
130 |
}
|
|
131 |
}
|
|
132 |
|
|
133 |
static void setCurrentThreadInfo(ThreadInfo tinfo) {
|
|
134 |
current = tinfo;
|
|
135 |
if (current != null) {
|
|
136 |
current.invalidate();
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
/**
|
|
141 |
* Get the current ThreadInfo object.
|
|
142 |
*
|
|
143 |
* @return the ThreadInfo for the current thread.
|
|
144 |
*/
|
|
145 |
static ThreadInfo getCurrentThreadInfo() {
|
|
146 |
return current;
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Get the thread from this ThreadInfo object.
|
|
151 |
*
|
|
152 |
* @return the Thread wrapped by this ThreadInfo.
|
|
153 |
*/
|
|
154 |
ThreadReference getThread() {
|
|
155 |
return thread;
|
|
156 |
}
|
|
157 |
|
|
158 |
static ThreadGroupReference group() {
|
|
159 |
if (group == null) {
|
|
160 |
// Current thread group defaults to the first top level
|
|
161 |
// thread group.
|
51
|
162 |
setThreadGroup(Env.vm().topLevelThreadGroups().get(0));
|
2
|
163 |
}
|
|
164 |
return group;
|
|
165 |
}
|
|
166 |
|
|
167 |
static ThreadInfo getThreadInfo(long id) {
|
|
168 |
ThreadInfo retInfo = null;
|
|
169 |
|
|
170 |
synchronized (threads) {
|
51
|
171 |
for (ThreadInfo ti : threads()) {
|
2
|
172 |
if (ti.thread.uniqueID() == id) {
|
|
173 |
retInfo = ti;
|
|
174 |
break;
|
|
175 |
}
|
|
176 |
}
|
|
177 |
}
|
|
178 |
return retInfo;
|
|
179 |
}
|
|
180 |
|
|
181 |
static ThreadInfo getThreadInfo(ThreadReference tr) {
|
|
182 |
return getThreadInfo(tr.uniqueID());
|
|
183 |
}
|
|
184 |
|
|
185 |
static ThreadInfo getThreadInfo(String idToken) {
|
|
186 |
ThreadInfo tinfo = null;
|
|
187 |
if (idToken.startsWith("t@")) {
|
|
188 |
idToken = idToken.substring(2);
|
|
189 |
}
|
|
190 |
try {
|
|
191 |
long threadId = Long.decode(idToken).longValue();
|
|
192 |
tinfo = getThreadInfo(threadId);
|
|
193 |
} catch (NumberFormatException e) {
|
|
194 |
tinfo = null;
|
|
195 |
}
|
|
196 |
return tinfo;
|
|
197 |
}
|
|
198 |
|
|
199 |
/**
|
|
200 |
* Get the thread stack frames.
|
|
201 |
*
|
|
202 |
* @return a <code>List</code> of the stack frames.
|
|
203 |
*/
|
51
|
204 |
List<StackFrame> getStack() throws IncompatibleThreadStateException {
|
2
|
205 |
return thread.frames();
|
|
206 |
}
|
|
207 |
|
|
208 |
/**
|
|
209 |
* Get the current stackframe.
|
|
210 |
*
|
|
211 |
* @return the current stackframe.
|
|
212 |
*/
|
|
213 |
StackFrame getCurrentFrame() throws IncompatibleThreadStateException {
|
|
214 |
if (thread.frameCount() == 0) {
|
|
215 |
return null;
|
|
216 |
}
|
|
217 |
return thread.frame(currentFrameIndex);
|
|
218 |
}
|
|
219 |
|
|
220 |
/**
|
|
221 |
* Invalidate the current stackframe index.
|
|
222 |
*/
|
|
223 |
void invalidate() {
|
|
224 |
currentFrameIndex = 0;
|
|
225 |
}
|
|
226 |
|
|
227 |
/* Throw IncompatibleThreadStateException if not suspended */
|
|
228 |
private void assureSuspended() throws IncompatibleThreadStateException {
|
|
229 |
if (!thread.isSuspended()) {
|
|
230 |
throw new IncompatibleThreadStateException();
|
|
231 |
}
|
|
232 |
}
|
|
233 |
|
|
234 |
/**
|
|
235 |
* Get the current stackframe index.
|
|
236 |
*
|
|
237 |
* @return the number of the current stackframe. Frame zero is the
|
|
238 |
* closest to the current program counter
|
|
239 |
*/
|
|
240 |
int getCurrentFrameIndex() {
|
|
241 |
return currentFrameIndex;
|
|
242 |
}
|
|
243 |
|
|
244 |
/**
|
|
245 |
* Set the current stackframe to a specific frame.
|
|
246 |
*
|
|
247 |
* @param nFrame the number of the desired stackframe. Frame zero is the
|
|
248 |
* closest to the current program counter
|
|
249 |
* @exception IllegalAccessError when the thread isn't
|
|
250 |
* suspended or waiting at a breakpoint
|
|
251 |
* @exception ArrayIndexOutOfBoundsException when the
|
|
252 |
* requested frame is beyond the stack boundary
|
|
253 |
*/
|
|
254 |
void setCurrentFrameIndex(int nFrame) throws IncompatibleThreadStateException {
|
|
255 |
assureSuspended();
|
|
256 |
if ((nFrame < 0) || (nFrame >= thread.frameCount())) {
|
|
257 |
throw new ArrayIndexOutOfBoundsException();
|
|
258 |
}
|
|
259 |
currentFrameIndex = nFrame;
|
|
260 |
}
|
|
261 |
|
|
262 |
/**
|
|
263 |
* Change the current stackframe to be one or more frames higher
|
|
264 |
* (as in, away from the current program counter).
|
|
265 |
*
|
|
266 |
* @param nFrames the number of stackframes
|
|
267 |
* @exception IllegalAccessError when the thread isn't
|
|
268 |
* suspended or waiting at a breakpoint
|
|
269 |
* @exception ArrayIndexOutOfBoundsException when the
|
|
270 |
* requested frame is beyond the stack boundary
|
|
271 |
*/
|
|
272 |
void up(int nFrames) throws IncompatibleThreadStateException {
|
|
273 |
setCurrentFrameIndex(currentFrameIndex + nFrames);
|
|
274 |
}
|
|
275 |
|
|
276 |
/**
|
|
277 |
* Change the current stackframe to be one or more frames lower
|
|
278 |
* (as in, toward the current program counter). *
|
|
279 |
* @param nFrames the number of stackframes
|
|
280 |
* @exception IllegalAccessError when the thread isn't
|
|
281 |
* suspended or waiting at a breakpoint
|
|
282 |
* @exception ArrayIndexOutOfBoundsException when the
|
|
283 |
* requested frame is beyond the stack boundary
|
|
284 |
*/
|
|
285 |
void down(int nFrames) throws IncompatibleThreadStateException {
|
|
286 |
setCurrentFrameIndex(currentFrameIndex - nFrames);
|
|
287 |
}
|
|
288 |
|
|
289 |
}
|