1 7 34 35 package com.sun.tools.example.debug.tty; 36 37 import com.sun.jdi.ThreadReference; 38 import com.sun.jdi.ThreadGroupReference; 39 import com.sun.jdi.IncompatibleThreadStateException; 40 import com.sun.jdi.StackFrame; 41 import java.util.List ; 42 import java.util.ArrayList ; 43 import java.util.Collections ; 44 import java.util.Iterator ; 45 import java.io.*; 46 47 class ThreadInfo { 48 private static List threads = Collections.synchronizedList(new ArrayList ()); 51 private static boolean gotInitialThreads = false; 52 53 private static ThreadInfo current = null; 54 private static ThreadGroupReference group = null; 55 56 private final ThreadReference thread; 57 private int currentFrameIndex = 0; 58 59 private ThreadInfo(ThreadReference thread) { 60 this.thread = thread; 61 if (thread == null) { 62 MessageOutput.fatalError("Internal error: null ThreadInfo created"); 63 } 64 } 65 66 private static void initThreads() { 67 if (!gotInitialThreads) { 68 Iterator iter = Env.vm().allThreads().iterator(); 69 while (iter.hasNext()) { 70 ThreadReference thread = (ThreadReference)iter.next(); 71 threads.add(new ThreadInfo(thread)); 72 } 73 gotInitialThreads = true; 74 } 75 } 76 77 static void addThread(ThreadReference thread) { 78 synchronized (threads) { 79 initThreads(); 80 ThreadInfo ti = new ThreadInfo(thread); 81 if (getThreadInfo(thread) == null) { 85 threads.add(ti); 86 } 87 } 88 } 89 90 static void removeThread(ThreadReference thread) { 91 if (thread.equals(ThreadInfo.current)) { 92 94 String currentThreadName; 98 try { 99 currentThreadName = "\"" + thread.name() + "\""; 100 } catch (Exception e) { 101 currentThreadName = ""; 102 } 103 104 setCurrentThread(null); 105 106 MessageOutput.println(); 107 MessageOutput.println("Current thread died. Execution continuing...", 108 currentThreadName); 109 } 110 threads.remove(getThreadInfo(thread)); 111 } 112 113 static List threads() { 114 synchronized(threads) { 115 initThreads(); 116 return new ArrayList (threads); 118 } 119 } 120 121 static void invalidateAll() { 122 current = null; 123 group = null; 124 synchronized (threads) { 125 Iterator iter = threads().iterator(); 126 while (iter.hasNext()) { 127 ThreadInfo ti = (ThreadInfo)iter.next(); 128 ti.invalidate(); 129 } 130 } 131 } 132 133 static void setThreadGroup(ThreadGroupReference tg) { 134 group = tg; 135 } 136 137 static void setCurrentThread(ThreadReference tr) { 138 if (tr == null) { 139 setCurrentThreadInfo(null); 140 } else { 141 ThreadInfo tinfo = getThreadInfo(tr); 142 setCurrentThreadInfo(tinfo); 143 } 144 } 145 146 static void setCurrentThreadInfo(ThreadInfo tinfo) { 147 current = tinfo; 148 if (current != null) { 149 current.invalidate(); 150 } 151 } 152 153 158 static ThreadInfo getCurrentThreadInfo() { 159 return current; 160 } 161 162 167 ThreadReference getThread() { 168 return thread; 169 } 170 171 static ThreadGroupReference group() { 172 if (group == null) { 173 setThreadGroup((ThreadGroupReference) 176 Env.vm().topLevelThreadGroups().get(0)); 177 } 178 return group; 179 } 180 181 static ThreadInfo getThreadInfo(long id) { 182 ThreadInfo retInfo = null; 183 184 synchronized (threads) { 185 Iterator iter = threads().iterator(); 186 while (iter.hasNext()) { 187 ThreadInfo ti = (ThreadInfo)iter.next(); 188 if (ti.thread.uniqueID() == id) { 189 retInfo = ti; 190 break; 191 } 192 } 193 } 194 return retInfo; 195 } 196 197 static ThreadInfo getThreadInfo(ThreadReference tr) { 198 return getThreadInfo(tr.uniqueID()); 199 } 200 201 static ThreadInfo getThreadInfo(String idToken) { 202 ThreadInfo tinfo = null; 203 if (idToken.startsWith("t@")) { 204 idToken = idToken.substring(2); 205 } 206 try { 207 long threadId = Long.decode(idToken).longValue(); 208 tinfo = getThreadInfo(threadId); 209 } catch (NumberFormatException e) { 210 tinfo = null; 211 } 212 return tinfo; 213 } 214 215 220 List getStack() throws IncompatibleThreadStateException { 221 return thread.frames(); 222 } 223 224 229 StackFrame getCurrentFrame() throws IncompatibleThreadStateException { 230 if (thread.frameCount() == 0) { 231 return null; 232 } 233 return thread.frame(currentFrameIndex); 234 } 235 236 239 void invalidate() { 240 currentFrameIndex = 0; 241 } 242 243 244 private void assureSuspended() throws IncompatibleThreadStateException { 245 if (!thread.isSuspended()) { 246 throw new IncompatibleThreadStateException(); 247 } 248 } 249 250 256 int getCurrentFrameIndex() { 257 return currentFrameIndex; 258 } 259 260 270 void setCurrentFrameIndex(int nFrame) throws IncompatibleThreadStateException { 271 assureSuspended(); 272 if ((nFrame < 0) || (nFrame >= thread.frameCount())) { 273 throw new ArrayIndexOutOfBoundsException (); 274 } 275 currentFrameIndex = nFrame; 276 } 277 278 288 void up(int nFrames) throws IncompatibleThreadStateException { 289 setCurrentFrameIndex(currentFrameIndex + nFrames); 290 } 291 292 301 void down(int nFrames) throws IncompatibleThreadStateException { 302 setCurrentFrameIndex(currentFrameIndex - nFrames); 303 } 304 305 } 306 307 | Popular Tags |