1:
38:
39:
40: package ;
41:
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52:
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59:
60:
65: public class VirtualMachineCommandSet
66: extends CommandSet
67: {
68: public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command)
69: throws JdwpException
70: {
71: boolean shutdown = false;
72: try
73: {
74: switch (command)
75: {
76: case JdwpConstants.CommandSet.VirtualMachine.VERSION:
77: executeVersion(bb, os);
78: break;
79: case JdwpConstants.CommandSet.VirtualMachine.CLASSES_BY_SIGNATURE:
80: executeClassesBySignature(bb, os);
81: break;
82: case JdwpConstants.CommandSet.VirtualMachine.ALL_CLASSES:
83: executeAllClasses(bb, os);
84: break;
85: case JdwpConstants.CommandSet.VirtualMachine.ALL_THREADS:
86: executeAllThreads(bb, os);
87: break;
88: case JdwpConstants.CommandSet.VirtualMachine.TOP_LEVEL_THREAD_GROUPS:
89: executeTopLevelThreadGroups(bb, os);
90: break;
91: case JdwpConstants.CommandSet.VirtualMachine.IDSIZES:
92: executeIDsizes(bb, os);
93: break;
94: case JdwpConstants.CommandSet.VirtualMachine.DISPOSE:
95: shutdown = true;
96: executeDispose(bb, os);
97: break;
98: case JdwpConstants.CommandSet.VirtualMachine.SUSPEND:
99: executeSuspend(bb, os);
100: break;
101: case JdwpConstants.CommandSet.VirtualMachine.RESUME:
102: executeResume(bb, os);
103: break;
104: case JdwpConstants.CommandSet.VirtualMachine.EXIT:
105: shutdown = true;
106: executeExit(bb, os);
107: break;
108: case JdwpConstants.CommandSet.VirtualMachine.CREATE_STRING:
109: executeCreateString(bb, os);
110: break;
111: case JdwpConstants.CommandSet.VirtualMachine.CAPABILITIES:
112: executeCapabilities(bb, os);
113: break;
114: case JdwpConstants.CommandSet.VirtualMachine.CLASS_PATHS:
115: executeClassPaths(bb, os);
116: break;
117: case JdwpConstants.CommandSet.VirtualMachine.DISPOSE_OBJECTS:
118: executeDisposeObjects(bb, os);
119: break;
120: case JdwpConstants.CommandSet.VirtualMachine.HOLD_EVENTS:
121: executeHoldEvents(bb, os);
122: break;
123: case JdwpConstants.CommandSet.VirtualMachine.RELEASE_EVENTS:
124: executeReleaseEvents(bb, os);
125: break;
126: case JdwpConstants.CommandSet.VirtualMachine.CAPABILITIES_NEW:
127: executeCapabilitiesNew(bb, os);
128: break;
129: case JdwpConstants.CommandSet.VirtualMachine.REDEFINE_CLASSES:
130: executeRedefineClasses(bb, os);
131: break;
132: case JdwpConstants.CommandSet.VirtualMachine.SET_DEFAULT_STRATUM:
133: executeSetDefaultStratum(bb, os);
134: break;
135: case JdwpConstants.CommandSet.VirtualMachine.ALL_CLASSES_WITH_GENERIC:
136: executeAllClassesWithGeneric(bb, os);
137: break;
138: default:
139: throw new NotImplementedException("Command " + command +
140: " not found in VirtualMachine Command Set.");
141: }
142: }
143: catch (IOException ex)
144: {
145:
146:
147: throw new JdwpInternalErrorException(ex);
148: }
149:
150: return shutdown;
151: }
152:
153: private void executeVersion(ByteBuffer bb, DataOutputStream os)
154: throws JdwpException, IOException
155: {
156:
157: Properties props = System.getProperties();
158:
159: int jdwpMajor = JdwpConstants.Version.MAJOR;
160: int jdwpMinor = JdwpConstants.Version.MINOR;
161:
162: String description = "JDWP version " + jdwpMajor + "." + jdwpMinor
163: + ", JVM version " + props.getProperty("java.vm.name")
164: + " " + props.getProperty("java.vm.version") + " "
165: + props.getProperty("java.version");
166: String vmVersion = props.getProperty("java.version");
167: String vmName = props.getProperty("java.vm.name");
168: JdwpString.writeString(os, description);
169: os.writeInt(jdwpMajor);
170: os.writeInt(jdwpMinor);
171: JdwpString.writeString(os, vmName);
172: JdwpString.writeString(os, vmVersion);
173: }
174:
175: private void executeClassesBySignature(ByteBuffer bb, DataOutputStream os)
176: throws JdwpException, IOException
177: {
178: String sig = JdwpString.readString(bb);
179: ArrayList allMatchingClasses = new ArrayList();
180:
181:
182: Iterator iter = VMVirtualMachine.getAllLoadedClasses();
183:
184: while (iter.hasNext())
185: {
186: Class clazz = (Class) iter.next();
187: String clazzSig = Signature.computeClassSignature(clazz);
188: if (clazzSig.equals(sig))
189: allMatchingClasses.add(clazz);
190: }
191:
192: os.writeInt(allMatchingClasses.size());
193: for (int i = 0; i < allMatchingClasses.size(); i++)
194: {
195: Class clazz = (Class) allMatchingClasses.get(i);
196: ReferenceTypeId id = idMan.getReferenceTypeId(clazz);
197: id.writeTagged(os);
198: int status = VMVirtualMachine.getClassStatus(clazz);
199: os.writeInt(status);
200: }
201: }
202:
203: private void executeAllClasses(ByteBuffer bb, DataOutputStream os)
204: throws JdwpException, IOException
205: {
206:
207:
208:
209:
210:
211: int classCount = VMVirtualMachine.getAllLoadedClassesCount();
212: os.writeInt(classCount);
213:
214:
215: Iterator iter = VMVirtualMachine.getAllLoadedClasses();
216:
217: int count = 0;
218:
219:
220:
221: while (iter.hasNext() && count++ < classCount)
222: {
223: Class clazz = (Class) iter.next();
224: ReferenceTypeId id = idMan.getReferenceTypeId(clazz);
225: id.writeTagged(os);
226: String sig = Signature.computeClassSignature(clazz);
227: JdwpString.writeString(os, sig);
228: int status = VMVirtualMachine.getClassStatus(clazz);
229: os.writeInt(status);
230: }
231: }
232:
233: private void executeAllThreads(ByteBuffer bb, DataOutputStream os)
234: throws JdwpException, IOException
235: {
236: ThreadGroup jdwpGroup = Thread.currentThread().getThreadGroup();
237: ThreadGroup root = getRootThreadGroup(jdwpGroup);
238:
239: int numThreads = root.activeCount();
240: Thread allThreads[] = new Thread[numThreads];
241: root.enumerate(allThreads);
242:
243:
244:
245:
246:
247: numThreads = 0;
248: for (int i = 0; i < allThreads.length; i++)
249: {
250: Thread thread = allThreads[i];
251: if (thread == null)
252: break;
253: if (!thread.getThreadGroup().equals(jdwpGroup))
254: numThreads++;
255: }
256:
257: os.writeInt(numThreads);
258:
259: for (int i = 0; i < allThreads.length; i++)
260: {
261: Thread thread = allThreads[i];
262: if (thread == null)
263: break;
264: if (!thread.getThreadGroup().equals(jdwpGroup))
265: idMan.getObjectId(thread).write(os);
266: }
267: }
268:
269: private void executeTopLevelThreadGroups(ByteBuffer bb, DataOutputStream os)
270: throws JdwpException, IOException
271: {
272: ThreadGroup jdwpGroup = Thread.currentThread().getThreadGroup ();
273: ThreadGroup root = getRootThreadGroup(jdwpGroup);
274:
275: os.writeInt(1);
276: idMan.getObjectId(root);
277: }
278:
279: private void executeDispose(ByteBuffer bb, DataOutputStream os)
280: throws JdwpException
281: {
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294: throw new NotImplementedException(
295: "Command VirtualMachine.Dispose not implemented");
296:
297: }
298:
299: private void executeIDsizes(ByteBuffer bb, DataOutputStream os)
300: throws JdwpException, IOException
301: {
302: os.writeInt(ObjectId.SIZE);
303: os.writeInt(ObjectId.SIZE);
304: os.writeInt(ObjectId.SIZE);
305: os.writeInt(ReferenceTypeId.SIZE);
306: os.writeInt(VMFrame.SIZE);
307: }
308:
309: private void executeSuspend(ByteBuffer bb, DataOutputStream os)
310: throws JdwpException
311: {
312: VMVirtualMachine.suspendAllThreads ();
313: }
314:
315: private void executeResume(ByteBuffer bb, DataOutputStream os)
316: throws JdwpException
317: {
318: VMVirtualMachine.resumeAllThreads ();
319: }
320:
321: private void executeExit(ByteBuffer bb, DataOutputStream os)
322: throws JdwpException, IOException
323: {
324: int exitCode = bb.getInt();
325: System.exit (exitCode);
326: }
327:
328: private void executeCreateString(ByteBuffer bb, DataOutputStream os)
329: throws JdwpException, IOException
330: {
331: String string = JdwpString.readString(bb);
332: ObjectId stringId = idMan.getObjectId(string);
333:
334:
335:
336: stringId.disableCollection();
337: stringId.write(os);
338: }
339:
340: private void executeCapabilities(ByteBuffer bb, DataOutputStream os)
341: throws JdwpException, IOException
342: {
343:
344: os.writeBoolean(false);
345: os.writeBoolean(false);
346: os.writeBoolean(false);
347: os.writeBoolean(false);
348: os.writeBoolean(false);
349: os.writeBoolean(false);
350: os.writeBoolean(false);
351: }
352:
353: private void executeClassPaths(ByteBuffer bb, DataOutputStream os)
354: throws JdwpException, IOException
355: {
356: String baseDir = System.getProperty("user.dir");
357: JdwpString.writeString(os, baseDir);
358:
359:
360: String classPath = System.getProperty("java.class.path");
361: String[] paths = classPath.split(":");
362:
363: os.writeInt(paths.length);
364: for (int i = 0; i < paths.length; i++)
365: JdwpString.writeString(os, paths[i]);
366:
367:
368: String bootPath = System.getProperty("sun.boot.class.path");
369: paths = bootPath.split(":");
370: os.writeInt(paths.length);
371: for (int i = 0; i < paths.length; i++)
372: JdwpString.writeString(os, paths[i]);
373: }
374:
375: private void executeDisposeObjects(ByteBuffer bb, DataOutputStream os)
376: throws JdwpException
377: {
378:
379:
380:
381: }
382:
383: private void executeHoldEvents(ByteBuffer bb, DataOutputStream os)
384: throws JdwpException
385: {
386:
387:
388:
389: throw new NotImplementedException(
390: "Command VirtualMachine.HoldEvents not implemented");
391: }
392:
393:
394: private void executeReleaseEvents(ByteBuffer bb, DataOutputStream os)
395: throws JdwpException
396: {
397: throw new NotImplementedException(
398: "Command VirtualMachine.ReleaseEvents not implemented");
399: }
400:
401: private void executeCapabilitiesNew(ByteBuffer bb, DataOutputStream os)
402: throws JdwpException, IOException
403: {
404:
405: final int CAPABILITIES_NEW_SIZE = 32;
406: os.writeBoolean(false);
407: os.writeBoolean(false);
408: os.writeBoolean(false);
409: os.writeBoolean(false);
410: os.writeBoolean(false);
411: os.writeBoolean(false);
412: os.writeBoolean(false);
413: os.writeBoolean(false);
414: os.writeBoolean(false);
415: os.writeBoolean(false);
416: os.writeBoolean(false);
417: os.writeBoolean(false);
418: os.writeBoolean(false);
419: os.writeBoolean(false);
420: os.writeBoolean(false);
421: for (int i = 15; i < CAPABILITIES_NEW_SIZE; i++)
422:
423:
424: os.writeBoolean(false);
425: }
426:
427: private void executeRedefineClasses(ByteBuffer bb, DataOutputStream os)
428: throws JdwpException
429: {
430:
431: throw new NotImplementedException(
432: "Command VirtualMachine.RedefineClasses not implemented");
433: }
434:
435: private void executeSetDefaultStratum(ByteBuffer bb, DataOutputStream os)
436: throws JdwpException
437: {
438:
439: throw new NotImplementedException(
440: "Command VirtualMachine.SetDefaultStratum not implemented");
441: }
442:
443: private void executeAllClassesWithGeneric(ByteBuffer bb, DataOutputStream os)
444: throws JdwpException
445: {
446:
447: throw new NotImplementedException(
448: "Command VirtualMachine.AllClassesWithGeneric not implemented");
449: }
450:
451:
454: private ThreadGroup getRootThreadGroup(ThreadGroup group)
455: {
456: ThreadGroup parent = group.getParent();
457:
458: while (parent != null)
459: {
460: group = parent;
461: parent = group.getParent();
462: }
463: return group;
464: }
465: }