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:
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58:
59:
64: public class VirtualMachineCommandSet
65: extends CommandSet
66: {
67: public boolean runCommand(ByteBuffer bb, DataOutputStream os, byte command)
68: throws JdwpException
69: {
70: boolean shutdown = false;
71: try
72: {
73: switch (command)
74: {
75: case JdwpConstants.CommandSet.VirtualMachine.VERSION:
76: executeVersion(bb, os);
77: break;
78: case JdwpConstants.CommandSet.VirtualMachine.CLASSES_BY_SIGNATURE:
79: executeClassesBySignature(bb, os);
80: break;
81: case JdwpConstants.CommandSet.VirtualMachine.ALL_CLASSES:
82: executeAllClasses(bb, os);
83: break;
84: case JdwpConstants.CommandSet.VirtualMachine.ALL_THREADS:
85: executeAllThreads(bb, os);
86: break;
87: case JdwpConstants.CommandSet.VirtualMachine.TOP_LEVEL_THREAD_GROUPS:
88: executeTopLevelThreadGroups(bb, os);
89: break;
90: case JdwpConstants.CommandSet.VirtualMachine.IDSIZES:
91: executeIDsizes(bb, os);
92: break;
93: case JdwpConstants.CommandSet.VirtualMachine.DISPOSE:
94: shutdown = true;
95: executeDispose(bb, os);
96: break;
97: case JdwpConstants.CommandSet.VirtualMachine.SUSPEND:
98: executeSuspend(bb, os);
99: break;
100: case JdwpConstants.CommandSet.VirtualMachine.RESUME:
101: executeResume(bb, os);
102: break;
103: case JdwpConstants.CommandSet.VirtualMachine.EXIT:
104: shutdown = true;
105: executeExit(bb, os);
106: break;
107: case JdwpConstants.CommandSet.VirtualMachine.CREATE_STRING:
108: executeCreateString(bb, os);
109: break;
110: case JdwpConstants.CommandSet.VirtualMachine.CAPABILITIES:
111: executeCapabilities(bb, os);
112: break;
113: case JdwpConstants.CommandSet.VirtualMachine.CLASS_PATHS:
114: executeClassPaths(bb, os);
115: break;
116: case JdwpConstants.CommandSet.VirtualMachine.DISPOSE_OBJECTS:
117: executeDisposeObjects(bb, os);
118: break;
119: case JdwpConstants.CommandSet.VirtualMachine.HOLD_EVENTS:
120: executeHoldEvents(bb, os);
121: break;
122: case JdwpConstants.CommandSet.VirtualMachine.RELEASE_EVENTS:
123: executeReleaseEvents(bb, os);
124: break;
125: case JdwpConstants.CommandSet.VirtualMachine.CAPABILITIES_NEW:
126: executeCapabilitiesNew(bb, os);
127: break;
128: case JdwpConstants.CommandSet.VirtualMachine.REDEFINE_CLASSES:
129: executeRedefineClasses(bb, os);
130: break;
131: case JdwpConstants.CommandSet.VirtualMachine.SET_DEFAULT_STRATUM:
132: executeSetDefaultStratum(bb, os);
133: break;
134: case JdwpConstants.CommandSet.VirtualMachine.ALL_CLASSES_WITH_GENERIC:
135: executeAllClassesWithGeneric(bb, os);
136: break;
137: default:
138: throw new NotImplementedException("Command " + command +
139: " not found in VirtualMachine Command Set.");
140: }
141: }
142: catch (IOException ex)
143: {
144:
145:
146: throw new JdwpInternalErrorException(ex);
147: }
148:
149: return shutdown;
150: }
151:
152: private void executeVersion(ByteBuffer bb, DataOutputStream os)
153: throws JdwpException, IOException
154: {
155:
156: Properties props = System.getProperties();
157:
158: int jdwpMajor = JdwpConstants.Version.MAJOR;
159: int jdwpMinor = JdwpConstants.Version.MINOR;
160:
161: String description = "JDWP version " + jdwpMajor + "." + jdwpMinor
162: + ", JVM version " + props.getProperty("java.vm.name")
163: + " " + props.getProperty("java.vm.version") + " "
164: + props.getProperty("java.version");
165: String vmVersion = props.getProperty("java.version");
166: String vmName = props.getProperty("java.vm.name");
167: JdwpString.writeString(os, description);
168: os.writeInt(jdwpMajor);
169: os.writeInt(jdwpMinor);
170: JdwpString.writeString(os, vmName);
171: JdwpString.writeString(os, vmVersion);
172: }
173:
174: private void executeClassesBySignature(ByteBuffer bb, DataOutputStream os)
175: throws JdwpException, IOException
176: {
177: String sig = JdwpString.readString(bb);
178: ArrayList allMatchingClasses = new ArrayList();
179:
180:
181: Iterator iter = VMVirtualMachine.getAllLoadedClasses();
182:
183: while (iter.hasNext())
184: {
185: Class clazz = (Class) iter.next();
186: String clazzSig = Signature.computeClassSignature(clazz);
187: if (clazzSig.equals(sig))
188: allMatchingClasses.add(clazz);
189: }
190:
191: os.writeInt(allMatchingClasses.size());
192: for (int i = 0; i < allMatchingClasses.size(); i++)
193: {
194: Class clazz = (Class) allMatchingClasses.get(i);
195: ReferenceTypeId id = idMan.getReferenceTypeId(clazz);
196: id.writeTagged(os);
197: int status = VMVirtualMachine.getClassStatus(clazz);
198: os.writeInt(status);
199: }
200: }
201:
202: private void executeAllClasses(ByteBuffer bb, DataOutputStream os)
203: throws JdwpException, IOException
204: {
205:
206:
207:
208:
209:
210: int classCount = VMVirtualMachine.getAllLoadedClassesCount();
211: os.writeInt(classCount);
212:
213:
214: Iterator iter = VMVirtualMachine.getAllLoadedClasses();
215:
216: int count = 0;
217:
218:
219:
220: while (iter.hasNext() && count++ < classCount)
221: {
222: Class clazz = (Class) iter.next();
223: ReferenceTypeId id = idMan.getReferenceTypeId(clazz);
224: id.writeTagged(os);
225: String sig = Signature.computeClassSignature(clazz);
226: JdwpString.writeString(os, sig);
227: int status = VMVirtualMachine.getClassStatus(clazz);
228: os.writeInt(status);
229: }
230: }
231:
232: private void executeAllThreads(ByteBuffer bb, DataOutputStream os)
233: throws JdwpException, IOException
234: {
235: ThreadGroup jdwpGroup = Thread.currentThread().getThreadGroup();
236: ThreadGroup root = getRootThreadGroup(jdwpGroup);
237:
238: int numThreads = root.activeCount();
239: Thread allThreads[] = new Thread[numThreads];
240: root.enumerate(allThreads);
241:
242:
243:
244:
245:
246: numThreads = 0;
247: for (int i = 0; i < allThreads.length; i++)
248: {
249: Thread thread = allThreads[i];
250: if (thread == null)
251: break;
252: if (!thread.getThreadGroup().equals(jdwpGroup))
253: numThreads++;
254: }
255:
256: os.writeInt(numThreads);
257:
258: for (int i = 0; i < allThreads.length; i++)
259: {
260: Thread thread = allThreads[i];
261: if (thread == null)
262: break;
263: if (!thread.getThreadGroup().equals(jdwpGroup))
264: idMan.getObjectId(thread).write(os);
265: }
266: }
267:
268: private void executeTopLevelThreadGroups(ByteBuffer bb, DataOutputStream os)
269: throws JdwpException, IOException
270: {
271: ThreadGroup jdwpGroup = Thread.currentThread().getThreadGroup ();
272: ThreadGroup root = getRootThreadGroup(jdwpGroup);
273:
274: os.writeInt(1);
275: idMan.getObjectId(root);
276: }
277:
278: private void executeDispose(ByteBuffer bb, DataOutputStream os)
279: throws JdwpException
280: {
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293: throw new NotImplementedException(
294: "Command VirtualMachine.Dispose not implemented");
295:
296: }
297:
298: private void executeIDsizes(ByteBuffer bb, DataOutputStream os)
299: throws JdwpException, IOException
300: {
301: ObjectId oid = new ObjectId();
302: os.writeInt(oid.size());
303: os.writeInt(oid.size());
304: os.writeInt(oid.size());
305: os.writeInt(new ReferenceTypeId((byte) 0x00).size());
306: os.writeInt(oid.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: }