Source for gnu.classpath.jdwp.VMVirtualMachine

   1: /* VMVirtualMachine.java -- A reference implementation of a JDWP virtual
   2:    machine
   3: 
   4:    Copyright (C) 2005, 2006, 2007 Free Software Foundation
   5: 
   6: This file is part of GNU Classpath.
   7: 
   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)
  11: any later version.
  12: 
  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.
  17: 
  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
  21: 02110-1301 USA.
  22: 
  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
  26: combination.
  27: 
  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. */
  40: 
  41: 
  42: package gnu.classpath.jdwp;
  43: 
  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;
  49: import gnu.classpath.jdwp.value.Value;
  50: 
  51: import java.util.ArrayList;
  52: import java.util.Collection;
  53: import java.util.Hashtable;
  54: 
  55: /**
  56:  * A virtual machine according to JDWP.
  57:  *
  58:  * @author Keith Seitz  <keiths@redhat.com>
  59:  */
  60: public class VMVirtualMachine
  61: {
  62:   // VM Capabilities
  63:   public static final boolean canWatchFieldModification = false;
  64:   public static final boolean canWatchFieldAccess = false;
  65:   public static final boolean canGetBytecodes = false;
  66:   public static final boolean canGetSyntheticAttribute = false;
  67:   public static final boolean canGetOwnedMonitorInfo = false;
  68:   public static final boolean canGetCurrentContendedMonitor = false;
  69:   public static final boolean canGetMonitorInfo = false;
  70:   public static final boolean canRedefineClasses = false;
  71:   public static final boolean canAddMethod = false;
  72:   public static final boolean canUnrestrictedlyRedefineClasses = false;
  73:   public static final boolean canPopFrames = false;
  74:   public static final boolean canUseInstanceFilters = false;
  75:   public static final boolean canGetSourceDebugExtension = false;
  76:   public static final boolean canRequestVMDeathEvent = false;
  77:   public static final boolean canSetDefaultStratum = false;
  78: 
  79:   // Thread suspension table. Maps Thread to suspend count (Integer)
  80:   private static Hashtable _jdwp_suspend_counts;
  81: 
  82:   // List of stepping threads: maps Thread -> stepping info
  83:   static Hashtable _stepping_threads;
  84:   
  85:   // List of co-located JVMTI events
  86:   static ArrayList _event_list;
  87: 
  88:   public static native void initialize ();
  89: 
  90:   /**
  91:    * Suspend a thread
  92:    *
  93:    * @param  thread  the thread to suspend
  94:    */
  95:   public static native void suspendThread (Thread thread)
  96:     throws JdwpException;
  97: 
  98:   /**
  99:    * Suspend all threads
 100:    */
 101:   public static void suspendAllThreads ()
 102:     throws JdwpException
 103:   {
 104:     // Our JDWP thread group -- don't suspend any of those threads
 105:     Thread current = Thread.currentThread ();
 106:     ThreadGroup jdwpGroup = Jdwp.getDefault().getJdwpThreadGroup();
 107: 
 108:     // Find the root ThreadGroup
 109:     ThreadGroup group = jdwpGroup;
 110:     ThreadGroup parent = group.getParent ();
 111:     while (parent != null)
 112:       {
 113:     group = parent;
 114:     parent = group.getParent ();
 115:       }
 116:     
 117:     // Get all the threads in the system
 118:     int num = group.activeCount ();
 119:     Thread[] threads = new Thread[num];
 120:     group.enumerate (threads);
 121: 
 122:     for (int i = 0; i < num; ++i)
 123:       {
 124:     Thread t = threads[i];
 125:     if (t != null)
 126:       {
 127:         if (t.getThreadGroup () == jdwpGroup || t == current)
 128:           {
 129:         // Don't suspend the current thread or any JDWP thread
 130:         continue;
 131:           }
 132:         else
 133:           suspendThread (t);
 134:       }
 135:       }
 136: 
 137:     // Now suspend the current thread
 138:     if (current.getThreadGroup() != jdwpGroup)
 139:       suspendThread (current);
 140:   }
 141: 
 142:   /**
 143:    * Resume a thread. A thread must be resumed as many times
 144:    * as it has been suspended.
 145:    *
 146:    * @param  thread  the thread to resume
 147:    */
 148:   public static native void resumeThread (Thread thread)
 149:     throws JdwpException;
 150: 
 151:   /**
 152:    * Resume all threads. This simply decrements the thread's
 153:    * suspend count. It can not be used to force the application
 154:    * to run.
 155:    */
 156:   public static void resumeAllThreads ()
 157:     throws JdwpException
 158:   {
 159:     // Our JDWP thread group -- don't resume
 160:     Thread current = Thread.currentThread ();
 161:     ThreadGroup jdwpGroup = current.getThreadGroup ();
 162: 
 163:     // Find the root ThreadGroup
 164:     ThreadGroup group = jdwpGroup;
 165:     ThreadGroup parent = group.getParent ();
 166:     while (parent != null)
 167:       {
 168:     group = parent;
 169:     parent = group.getParent ();
 170:       }
 171:     
 172:     // Get all the threads in the system
 173:     int num = group.activeCount ();
 174:     Thread[] threads = new Thread[num];
 175:     group.enumerate (threads);
 176: 
 177:     for (int i = 0; i < num; ++i)
 178:       {
 179:     Thread t = threads[i];
 180:     if (t != null)
 181:       {
 182:         if (t.getThreadGroup () == jdwpGroup || t == current)
 183:           {
 184:         // Don't resume the current thread or any JDWP thread
 185:         continue;
 186:           }
 187:         else
 188:           resumeThread (t);
 189:       }
 190:       }
 191:   }
 192: 
 193:   /**
 194:    * Get the suspend count for a give thread
 195:    *
 196:    * @param  thread  the thread whose suspend count is desired
 197:    * @return the number of times the thread has been suspended
 198:    */
 199:   public static native int getSuspendCount (Thread thread)
 200:     throws JdwpException;
 201:  
 202:   /**
 203:    * Returns a Collection of all classes loaded in the VM
 204:    */
 205:   public static native Collection getAllLoadedClasses ()
 206:     throws JdwpException;
 207: 
 208:   /**
 209:    * Returns the status of the given class
 210:    *
 211:    * @param  clazz  the class whose status is desired
 212:    * @return a flag containing the class's status
 213:    * @see JdwpConstants.ClassStatus
 214:    */
 215:   public static native int getClassStatus (Class clazz)
 216:     throws JdwpException;
 217: 
 218:   /**
 219:    * Returns all of the methods defined in the given class. This
 220:    * includes all methods, constructors, and class initializers.
 221:    *
 222:    * @param  klass  the class whose methods are desired
 223:    * @return an array of virtual machine methods
 224:    */
 225:   public static native VMMethod[] getAllClassMethods (Class klass)
 226:     throws JdwpException;
 227: 
 228:   /**
 229:    * A factory method for getting valid virtual machine methods
 230:    * which may be passed to/from the debugger.
 231:    *
 232:    * @param klass the class in which the method is defined
 233:    * @param id    the ID of the desired method
 234:    * @return the desired internal representation of the method
 235:    * @throws InvalidMethodException if the method is not defined
 236:    *           in the class
 237:    * @throws JdwpException for any other error
 238:    */
 239:   public static native VMMethod getClassMethod(Class klass, long id)
 240:     throws JdwpException;
 241: 
 242:   /**
 243:    * Returns the thread's call stack
 244:    *
 245:    * @param  thread  thread for which to get call stack
 246:    * @param  start   index of first frame to return
 247:    * @param  length  number of frames to return (-1 for all frames)
 248:    * @return a list of frames
 249:    */
 250:   public static native ArrayList getFrames (Thread thread, int start,
 251:                         int length)
 252:     throws JdwpException;
 253: 
 254:   /**
 255:    * Returns the frame for a given thread with the frame ID in
 256:    * the buffer
 257:    *
 258:    * I don't like this.
 259:    *
 260:    * @param  thread  the frame's thread
 261:    * @param  bb      buffer containing the frame's ID
 262:    * @return the desired frame
 263:    */
 264:   public static native VMFrame getFrame (Thread thread, long frameID)
 265:     throws JdwpException;
 266: 
 267:   /**
 268:    * Returns the number of frames in the thread's stack
 269:    *
 270:    * @param  thread  the thread for which to get a frame count
 271:    * @return the number of frames in the thread's stack
 272:    */
 273:   public static native int getFrameCount (Thread thread)
 274:     throws JdwpException;
 275: 
 276: 
 277:   /**
 278:    * Returns the status of a thread
 279:    *
 280:    * @param  thread  the thread for which to get status
 281:    * @return integer status of the thread
 282:    * @see JdwpConstants.ThreadStatus
 283:    */
 284:   public static native int getThreadStatus (Thread thread)
 285:     throws JdwpException;
 286: 
 287:   /**
 288:    * Returns a list of all classes which this class loader has been
 289:    * requested to load
 290:    *
 291:    * @param  cl  the class loader
 292:    * @return a list of all visible classes
 293:    */
 294:   public static native ArrayList getLoadRequests (ClassLoader cl)
 295:     throws JdwpException;
 296: 
 297:   /**
 298:    * Executes a method in the virtual machine. The thread must already
 299:    * be suspended by a previous event. When the method invocation is
 300:    * complete, the thread (or all threads if INVOKE_SINGLE_THREADED is
 301:    * not set in options) must be suspended before this method returns.
 302:    *
 303:    * @param  obj         instance in which to invoke method (null for static)
 304:    * @param  thread      the thread in which to invoke the method
 305:    * @param  clazz       the class in which the method is defined
 306:    * @param  method      the method to invoke
 307:    * @param  values      arguments to pass to method
 308:    * @param  options     invocation options
 309:    * @return a result object containing the results of the invocation
 310:    */
 311:   public static native MethodResult executeMethod (Object obj, Thread thread,
 312:                         Class clazz, VMMethod method,
 313:                         Value[] values,
 314:                         int options)
 315:     throws JdwpException;
 316: 
 317:   /**
 318:    * "Returns the name of source file in which a reference type was declared"
 319:    *
 320:    * @param  clazz  the class for which to return a source file
 321:    * @return a string containing the source file name; "no path information
 322:    *         for the file is included"
 323:    */
 324:   public static native String getSourceFile (Class clazz)
 325:     throws JdwpException;
 326: 
 327:   /**
 328:    * Register a request from the debugger
 329:    *
 330:    * Virtual machines have two options. Either do nothing and allow
 331:    * the event manager to take care of the request (useful for broadcast-type
 332:    * events like class prepare/load/unload, thread start/end, etc.)
 333:    * or do some internal work to set up the event notification (useful for
 334:    * execution-related events like breakpoints, single-stepping, etc.).
 335:    */
 336:   public static native void registerEvent (EventRequest request)
 337:     throws JdwpException;
 338: 
 339:   /**
 340:    * Unregisters the given request
 341:    *
 342:    * @param  request  the request to unregister
 343:    */
 344:   public static native void unregisterEvent (EventRequest request)
 345:     throws JdwpException;
 346: 
 347: 
 348:   /**
 349:    * Clear all events of the given kind
 350:    *
 351:    * @param  kind  the type of events to clear
 352:    */
 353:   public static native void clearEvents (byte kind)
 354:     throws JdwpException;
 355: 
 356:   /**
 357:    * Redefines the given types. VM must support canRedefineClasses
 358:    * capability (may also require canAddMethod and/or
 359:    * canUnrestrictedlyRedefineClasses capabilities)
 360:    *
 361:    * @param types the classes to redefine
 362:    * @param bytecodes the new bytecode definitions for the classes
 363:    */
 364:   public static native void redefineClasses(Class[] types, byte[][] bytecodes)
 365:     throws JdwpException;
 366: 
 367:   /**
 368:    * Sets the default stratum. VM must support the
 369:    * canSetDefaultStratum capability.
 370:    *
 371:    * @param stratum the new default stratum or empty string to
 372:    *        use the reference default
 373:    */
 374:   public static native void setDefaultStratum(String stratum)
 375:     throws JdwpException;
 376: 
 377:   /**
 378:    * Returns the source debug extension. VM must support the
 379:    * canGetSourceDebugExtension capability.
 380:    *
 381:    * @param klass the class for which to return information
 382:    * @returns the source debug extension
 383:    */
 384:   public static native String getSourceDebugExtension(Class klass)
 385:     throws JdwpException;
 386: 
 387:   /**
 388:    * Returns the bytecode for the given method. VM must support the
 389:    * canGetBytecodes capability.
 390:    *
 391:    * @param method the method for which to get bytecodes
 392:    * @returns the bytecodes
 393:    */
 394:   public static native byte[] getBytecodes(VMMethod method)
 395:     throws JdwpException;
 396: 
 397:   /**
 398:    * Returns monitor information about an object. VM must support
 399:    * the canGetMonitorInformation capability.
 400:    *
 401:    * @param obj the object
 402:    * @returns monitor information (owner, entry count, waiters)
 403:    */
 404:   public static native MonitorInfo getMonitorInfo(Object obj)
 405:     throws JdwpException;
 406: 
 407:   /**
 408:    * Returns a list of owned monitors. VM must support the
 409:    * canGetOwnedMonitorInfo capability.
 410:    *
 411:    * @param thread a thread
 412:    * @returns the list of monitors owned by this thread
 413:    */
 414:   public static native Object[] getOwnedMonitors(Thread thread)
 415:     throws JdwpException;
 416: 
 417:   /**
 418:    * Returns the current contended monitor for a thread. VM must
 419:    * support canGetCurrentContendedMonitor capability.
 420:    *
 421:    * @param thread the thread
 422:    * @returns the contended monitor
 423:    */
 424:   public static native Object getCurrentContendedMonitor(Thread thread)
 425:     throws JdwpException;
 426: 
 427:   /**
 428:    * Pop all frames up to and including the given frame. VM must
 429:    * support canPopFrames capability. It is the responsibility
 430:    * of the VM to check if the thread is suspended. If it is not,
 431:    * the VM should throw ThreadNotSuspendedException.
 432:    *
 433:    * @param thread the thread
 434:    * @param frame the frame ID
 435:    */
 436:   public static native void popFrames(Thread thread, long frameId);
 437: }