1 /* VMVirtualMachine.java -- A reference implementation of a JDWP virtual
4 Copyright (C) 2005, 2006, 2007 Free Software Foundation
6 This file is part of GNU Classpath.
8 GNU Classpath is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU Classpath is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Classpath; see the file COPYING. If not, write to the
20 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Linking this library statically or dynamically with other modules is
24 making a combined work based on this library. Thus, the terms and
25 conditions of the GNU General Public License cover the whole
28 As a special exception, the copyright holders of this library give you
29 permission to link this library with independent modules to produce an
30 executable, regardless of the license terms of these independent
31 modules, and to copy and distribute the resulting executable under
32 terms of your choice, provided that you also meet, for each linked
33 terms of your choice, provided that you also meet, for each linked
34 independent module, the terms and conditions of the license of that
35 module. An independent module is a module which is not derived from
36 or based on this library. If you modify this library, you may extend
37 this exception to your version of the library, but you are not
38 obligated to do so. If you do not wish to do so, delete this
39 exception statement from your version. */
42 package gnu.classpath.jdwp;
44 import gnu.classpath.jdwp.event.EventRequest;
45 import gnu.classpath.jdwp.exception.InvalidMethodException;
46 import gnu.classpath.jdwp.exception.JdwpException;
47 import gnu.classpath.jdwp.util.MethodResult;
48 import gnu.classpath.jdwp.util.MonitorInfo;
50 import java.lang.reflect.Method;
51 import java.nio.ByteBuffer;
52 import java.util.ArrayList;
53 import java.util.Collection;
54 import java.util.Hashtable;
57 * A virtual machine according to JDWP.
59 * @author Keith Seitz <keiths@redhat.com>
61 public class VMVirtualMachine
64 public static final boolean canWatchFieldModification = false;
65 public static final boolean canWatchFieldAccess = false;
66 public static final boolean canGetBytecodes = false;
67 public static final boolean canGetSyntheticAttribute = false;
68 public static final boolean canGetOwnedMonitorInfo = false;
69 public static final boolean canGetCurrentContendedMonitor = false;
70 public static final boolean canGetMonitorInfo = false;
71 public static final boolean canRedefineClasses = false;
72 public static final boolean canAddMethod = false;
73 public static final boolean canUnrestrictedlyRedefineClasses = false;
74 public static final boolean canPopFrames = false;
75 public static final boolean canUseInstanceFilters = false;
76 public static final boolean canGetSourceDebugExtension = false;
77 public static final boolean canRequestVMDeathEvent = false;
78 public static final boolean canSetDefaultStratum = false;
80 // Thread suspension table. Maps Thread to suspend count (Integer)
81 private static Hashtable _jdwp_suspend_counts;
83 // List of stepping threads: maps Thread -> stepping info
84 static Hashtable _stepping_threads;
86 public static native void initialize ();
91 * @param thread the thread to suspend
93 public static native void suspendThread (Thread thread)
99 public static void suspendAllThreads ()
102 // Our JDWP thread group -- don't suspend any of those threads
103 Thread current = Thread.currentThread ();
104 ThreadGroup jdwpGroup = Jdwp.getDefault().getJdwpThreadGroup();
106 // Find the root ThreadGroup
107 ThreadGroup group = jdwpGroup;
108 ThreadGroup parent = group.getParent ();
109 while (parent != null)
112 parent = group.getParent ();
115 // Get all the threads in the system
116 int num = group.activeCount ();
117 Thread[] threads = new Thread[num];
118 group.enumerate (threads);
120 for (int i = 0; i < num; ++i)
122 Thread t = threads[i];
125 if (t.getThreadGroup () == jdwpGroup || t == current)
127 // Don't suspend the current thread or any JDWP thread
135 // Now suspend the current thread
136 if (current.getThreadGroup() != jdwpGroup)
137 suspendThread (current);
141 * Resume a thread. A thread must be resumed as many times
142 * as it has been suspended.
144 * @param thread the thread to resume
146 public static native void resumeThread (Thread thread)
147 throws JdwpException;
150 * Resume all threads. This simply decrements the thread's
151 * suspend count. It can not be used to force the application
154 public static void resumeAllThreads ()
157 // Our JDWP thread group -- don't resume
158 Thread current = Thread.currentThread ();
159 ThreadGroup jdwpGroup = current.getThreadGroup ();
161 // Find the root ThreadGroup
162 ThreadGroup group = jdwpGroup;
163 ThreadGroup parent = group.getParent ();
164 while (parent != null)
167 parent = group.getParent ();
170 // Get all the threads in the system
171 int num = group.activeCount ();
172 Thread[] threads = new Thread[num];
173 group.enumerate (threads);
175 for (int i = 0; i < num; ++i)
177 Thread t = threads[i];
180 if (t.getThreadGroup () == jdwpGroup || t == current)
182 // Don't resume the current thread or any JDWP thread
192 * Get the suspend count for a give thread
194 * @param thread the thread whose suspend count is desired
195 * @return the number of times the thread has been suspended
197 public static native int getSuspendCount (Thread thread)
198 throws JdwpException;
201 * Returns a Collection of all classes loaded in the VM
203 public static native Collection getAllLoadedClasses ()
204 throws JdwpException;
207 * Returns the status of the given class
209 * @param clazz the class whose status is desired
210 * @return a flag containing the class's status
211 * @see JdwpConstants.ClassStatus
213 public static native int getClassStatus (Class clazz)
214 throws JdwpException;
217 * Returns all of the methods defined in the given class. This
218 * includes all methods, constructors, and class initializers.
220 * @param klass the class whose methods are desired
221 * @return an array of virtual machine methods
223 public static native VMMethod[] getAllClassMethods (Class klass)
224 throws JdwpException;
227 * A factory method for getting valid virtual machine methods
228 * which may be passed to/from the debugger.
230 * @param klass the class in which the method is defined
231 * @param id the ID of the desired method
232 * @return the desired internal representation of the method
233 * @throws InvalidMethodException if the method is not defined
235 * @throws JdwpException for any other error
237 public static native VMMethod getClassMethod(Class klass, long id)
238 throws JdwpException;
241 * Returns the thread's call stack
243 * @param thread thread for which to get call stack
244 * @param start index of first frame to return
245 * @param length number of frames to return (-1 for all frames)
246 * @return a list of frames
248 public static native ArrayList getFrames (Thread thread, int start,
250 throws JdwpException;
253 * Returns the frame for a given thread with the frame ID in
258 * @param thread the frame's thread
259 * @param bb buffer containing the frame's ID
260 * @return the desired frame
262 public static native VMFrame getFrame (Thread thread, long frameID)
263 throws JdwpException;
266 * Returns the number of frames in the thread's stack
268 * @param thread the thread for which to get a frame count
269 * @return the number of frames in the thread's stack
271 public static native int getFrameCount (Thread thread)
272 throws JdwpException;
276 * Returns the status of a thread
278 * @param thread the thread for which to get status
279 * @return integer status of the thread
280 * @see JdwpConstants.ThreadStatus
282 public static native int getThreadStatus (Thread thread)
283 throws JdwpException;
286 * Returns a list of all classes which this class loader has been
289 * @param cl the class loader
290 * @return a list of all visible classes
292 public static native ArrayList getLoadRequests (ClassLoader cl)
293 throws JdwpException;
296 * Executes a method in the virtual machine
298 * @param obj instance in which to invoke method (null for static)
299 * @param thread the thread in which to invoke the method
300 * @param clazz the class in which the method is defined
301 * @param method the method to invoke
302 * @param values arguments to pass to method
303 * @param nonVirtual "otherwise, normal virtual invoke
304 * (instance methods only) "
305 * @return a result object containing the results of the invocation
307 public static native MethodResult executeMethod (Object obj, Thread thread,
308 Class clazz, Method method,
311 throws JdwpException;
314 * "Returns the name of source file in which a reference type was declared"
316 * @param clazz the class for which to return a source file
317 * @return a string containing the source file name; "no path information
318 * for the file is included"
320 public static native String getSourceFile (Class clazz)
321 throws JdwpException;
324 * Register a request from the debugger
326 * Virtual machines have two options. Either do nothing and allow
327 * the event manager to take care of the request (useful for broadcast-type
328 * events like class prepare/load/unload, thread start/end, etc.)
329 * or do some internal work to set up the event notification (useful for
330 * execution-related events like breakpoints, single-stepping, etc.).
332 public static native void registerEvent (EventRequest request)
333 throws JdwpException;
336 * Unregisters the given request
338 * @param request the request to unregister
340 public static native void unregisterEvent (EventRequest request)
341 throws JdwpException;
345 * Clear all events of the given kind
347 * @param kind the type of events to clear
349 public static native void clearEvents (byte kind)
350 throws JdwpException;
353 * Redefines the given types. VM must support canRedefineClasses
354 * capability (may also require canAddMethod and/or
355 * canUnrestrictedlyRedefineClasses capabilities)
357 * @param types the classes to redefine
358 * @param bytecodes the new bytecode definitions for the classes
360 public static native void redefineClasses(Class[] types, byte[][] bytecodes)
361 throws JdwpException;
364 * Sets the default stratum. VM must support the
365 * canSetDefaultStratum capability.
367 * @param stratum the new default stratum or empty string to
368 * use the reference default
370 public static native void setDefaultStratum(String stratum)
371 throws JdwpException;
374 * Returns the source debug extension. VM must support the
375 * canGetSourceDebugExtension capability.
377 * @param klass the class for which to return information
378 * @returns the source debug extension
380 public static native String getSourceDebugExtension(Class klass)
381 throws JdwpException;
384 * Returns the bytecode for the given method. VM must support the
385 * canGetBytecodes capability.
387 * @param method the method for which to get bytecodes
388 * @returns the bytecodes
390 public static native byte[] getBytecodes(VMMethod method)
391 throws JdwpException;
394 * Returns monitor information about an object. VM must support
395 * the canGetMonitorInformation capability.
397 * @param obj the object
398 * @returns monitor information (owner, entry count, waiters)
400 public static native MonitorInfo getMonitorInfo(Object obj)
401 throws JdwpException;
404 * Returns a list of owned monitors. VM must support the
405 * canGetOwnedMonitorInfo capability.
407 * @param thread a thread
408 * @returns the list of monitors owned by this thread
410 public static native Object[] getOwnedMonitors(Thread thread)
411 throws JdwpException;
414 * Returns the current contended monitor for a thread. VM must
415 * support canGetCurrentContendedMonitor capability.
417 * @param thread the thread
418 * @returns the contended monitor
420 public static native Object getCurrentContendedMonitor(Thread thread)
421 throws JdwpException;
424 * Pop all frames up to and including the given frame. VM must
425 * support canPopFrames capability. It is the responsibility
426 * of the VM to check if the thread is suspended. If it is not,
427 * the VM should throw ThreadNotSuspendedException.
429 * @param thread the thread
430 * @param frame the frame ID
432 public static native void popFrames(Thread thread, long frameId);