1 /*
   2  * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.jdi;
  27 
  28 import java.lang.ref.Reference;
  29 import java.lang.ref.ReferenceQueue;
  30 import java.lang.ref.SoftReference;
  31 import java.text.MessageFormat;
  32 import java.util.ArrayList;
  33 import java.util.Arrays;
  34 import java.util.Collections;
  35 import java.util.HashMap;
  36 import java.util.HashSet;
  37 import java.util.Iterator;
  38 import java.util.List;
  39 import java.util.Map;
  40 import java.util.Set;
  41 import java.util.function.Consumer;
  42 
  43 import com.sun.jdi.BooleanType;
  44 import com.sun.jdi.BooleanValue;
  45 import com.sun.jdi.ByteType;
  46 import com.sun.jdi.ByteValue;
  47 import com.sun.jdi.CharType;
  48 import com.sun.jdi.CharValue;
  49 import com.sun.jdi.ClassNotLoadedException;
  50 import com.sun.jdi.DoubleType;
  51 import com.sun.jdi.DoubleValue;
  52 import com.sun.jdi.FloatType;
  53 import com.sun.jdi.FloatValue;
  54 import com.sun.jdi.IntegerType;
  55 import com.sun.jdi.IntegerValue;
  56 import com.sun.jdi.InternalException;
  57 import com.sun.jdi.LongType;
  58 import com.sun.jdi.LongValue;
  59 import com.sun.jdi.ModuleReference;
  60 import com.sun.jdi.ObjectCollectedException;
  61 import com.sun.jdi.PathSearchingVirtualMachine;
  62 import com.sun.jdi.PrimitiveType;
  63 import com.sun.jdi.ReferenceType;
  64 import com.sun.jdi.ShortType;
  65 import com.sun.jdi.ShortValue;
  66 import com.sun.jdi.StringReference;
  67 import com.sun.jdi.ThreadGroupReference;
  68 import com.sun.jdi.ThreadReference;
  69 import com.sun.jdi.Type;
  70 import com.sun.jdi.VMDisconnectedException;
  71 import com.sun.jdi.VirtualMachine;
  72 import com.sun.jdi.VirtualMachineManager;
  73 import com.sun.jdi.VoidType;
  74 import com.sun.jdi.VoidValue;
  75 import com.sun.jdi.connect.spi.Connection;
  76 import com.sun.jdi.event.EventQueue;
  77 import com.sun.jdi.request.BreakpointRequest;
  78 import com.sun.jdi.request.EventRequest;
  79 import com.sun.jdi.request.EventRequestManager;
  80 
  81 class VirtualMachineImpl extends MirrorImpl
  82              implements PathSearchingVirtualMachine, ThreadListener {
  83     // VM Level exported variables, these
  84     // are unique to a given vm
  85     public final int sizeofFieldRef;
  86     public final int sizeofMethodRef;
  87     public final int sizeofObjectRef;
  88     public final int sizeofClassRef;
  89     public final int sizeofFrameRef;
  90     public final int sizeofModuleRef;
  91 
  92     final int sequenceNumber;
  93 
  94     private final TargetVM target;
  95     private final EventQueueImpl eventQueue;
  96     private final EventRequestManagerImpl internalEventRequestManager;
  97     private final EventRequestManagerImpl eventRequestManager;
  98     final VirtualMachineManagerImpl vmManager;
  99     private final ThreadGroup threadGroupForJDI;
 100 
 101     // Allow direct access to this field so that that tracing code slows down
 102     // JDI as little as possible when not enabled.
 103     int traceFlags = TRACE_NONE;
 104 
 105     static int TRACE_RAW_SENDS     = 0x01000000;
 106     static int TRACE_RAW_RECEIVES  = 0x02000000;
 107 
 108     boolean traceReceives = false;   // pre-compute because of frequency
 109 
 110     // ReferenceType access - updated with class prepare and unload events
 111     // Protected by "synchronized(this)". "retrievedAllTypes" may be
 112     // tested unsynchronized (since once true, it stays true), but must
 113     // be set synchronously
 114     private Map<Long, ReferenceType> typesByID;
 115     private Set<ReferenceType> typesBySignature;
 116     private boolean retrievedAllTypes = false;
 117 
 118     private Map<Long, ModuleReference> modulesByID;
 119 
 120     // For other languages support
 121     private String defaultStratum = null;
 122 
 123     // ObjectReference cache
 124     // "objectsByID" protected by "synchronized(this)".
 125     private final Map<Long, SoftObjectReference> objectsByID = new HashMap<>();
 126     private final ReferenceQueue<ObjectReferenceImpl> referenceQueue = new ReferenceQueue<>();
 127     private static final int DISPOSE_THRESHOLD = 50;
 128     private final List<SoftObjectReference> batchedDisposeRequests =
 129             Collections.synchronizedList(new ArrayList<>(DISPOSE_THRESHOLD + 10));
 130 
 131     // These are cached once for the life of the VM
 132     private JDWP.VirtualMachine.Version versionInfo;
 133     private JDWP.VirtualMachine.ClassPaths pathInfo;
 134     private JDWP.VirtualMachine.Capabilities capabilities = null;
 135     private JDWP.VirtualMachine.CapabilitiesNew capabilitiesNew = null;
 136 
 137     // Per-vm singletons for primitive types and for void.
 138     // singleton-ness protected by "synchronized(this)".
 139     private BooleanType theBooleanType;
 140     private ByteType    theByteType;
 141     private CharType    theCharType;
 142     private ShortType   theShortType;
 143     private IntegerType theIntegerType;
 144     private LongType    theLongType;
 145     private FloatType   theFloatType;
 146     private DoubleType  theDoubleType;
 147 
 148     private VoidType    theVoidType;
 149 
 150     private VoidValue voidVal;
 151 
 152     // Launched debuggee process
 153     private Process process;
 154 
 155     // coordinates state changes and corresponding listener notifications
 156     private VMState state = new VMState(this);
 157 
 158     private Object initMonitor = new Object();
 159     private boolean initComplete = false;
 160     private boolean shutdown = false;
 161 
 162     private void notifyInitCompletion() {
 163         synchronized(initMonitor) {
 164             initComplete = true;
 165             initMonitor.notifyAll();
 166         }
 167     }
 168 
 169     void waitInitCompletion() {
 170         synchronized(initMonitor) {
 171             while (!initComplete) {
 172                 try {
 173                     initMonitor.wait();
 174                 } catch (InterruptedException e) {
 175                     // ignore
 176                 }
 177             }
 178         }
 179     }
 180 
 181     VMState state() {
 182         return state;
 183     }
 184 
 185     /*
 186      * ThreadListener implementation
 187      */
 188     public boolean threadResumable(ThreadAction action) {
 189         /*
 190          * If any thread is resumed, the VM is considered not suspended.
 191          * Just one thread is being resumed so pass it to thaw.
 192          */
 193         state.thaw(action.thread());
 194         return true;
 195     }
 196 
 197     VirtualMachineImpl(VirtualMachineManager manager,
 198                        Connection connection, Process process,
 199                        int sequenceNumber) {
 200         super(null);  // Can't use super(this)
 201         vm = this;
 202 
 203         this.vmManager = (VirtualMachineManagerImpl)manager;
 204         this.process = process;
 205         this.sequenceNumber = sequenceNumber;
 206 
 207         /* Create ThreadGroup to be used by all threads servicing
 208          * this VM.
 209          */
 210         threadGroupForJDI = new ThreadGroup(vmManager.mainGroupForJDI(),
 211                                             "JDI [" +
 212                                             this.hashCode() + "]");
 213 
 214         /*
 215          * Set up a thread to communicate with the target VM over
 216          * the specified transport.
 217          */
 218         target = new TargetVM(this, connection);
 219 
 220         /*
 221          * Set up a thread to handle events processed internally
 222          * the JDI implementation.
 223          */
 224         EventQueueImpl internalEventQueue = new EventQueueImpl(this, target);
 225         new InternalEventHandler(this, internalEventQueue);
 226         /*
 227          * Initialize client access to event setting and handling
 228          */
 229         eventQueue = new EventQueueImpl(this, target);
 230         eventRequestManager = new EventRequestManagerImpl(this);
 231 
 232         target.start();
 233 
 234         /*
 235          * Many ids are variably sized, depending on target VM.
 236          * Find out the sizes right away.
 237          */
 238         JDWP.VirtualMachine.IDSizes idSizes;
 239         try {
 240             idSizes = JDWP.VirtualMachine.IDSizes.process(vm);
 241         } catch (JDWPException exc) {
 242             throw exc.toJDIException();
 243         }
 244         sizeofFieldRef  = idSizes.fieldIDSize;
 245         sizeofMethodRef = idSizes.methodIDSize;
 246         sizeofObjectRef = idSizes.objectIDSize;
 247         sizeofClassRef = idSizes.referenceTypeIDSize;
 248         sizeofFrameRef  = idSizes.frameIDSize;
 249         sizeofModuleRef = idSizes.objectIDSize;
 250 
 251         /**
 252          * Set up requests needed by internal event handler.
 253          * Make sure they are distinguished by creating them with
 254          * an internal event request manager.
 255          *
 256          * Warning: create events only with SUSPEND_NONE policy.
 257          * In the current implementation other policies will not
 258          * be handled correctly when the event comes in. (notfiySuspend()
 259          * will not be properly called, and if the event is combined
 260          * with external events in the same set, suspend policy is not
 261          * correctly determined for the internal vs. external event sets)
 262          */
 263         internalEventRequestManager = new EventRequestManagerImpl(this);
 264         EventRequest er = internalEventRequestManager.createClassPrepareRequest();
 265         er.setSuspendPolicy(EventRequest.SUSPEND_NONE);
 266         er.enable();
 267         er = internalEventRequestManager.createClassUnloadRequest();
 268         er.setSuspendPolicy(EventRequest.SUSPEND_NONE);
 269         er.enable();
 270 
 271         /*
 272          * Tell other threads, notably TargetVM, that initialization
 273          * is complete.
 274          */
 275         notifyInitCompletion();
 276     }
 277 
 278     EventRequestManagerImpl getInternalEventRequestManager() {
 279         return internalEventRequestManager;
 280     }
 281 
 282     void validateVM() {
 283         /*
 284          * We no longer need to do this.  The spec now says
 285          * that a VMDisconnected _may_ be thrown in these
 286          * cases, not that it _will_ be thrown.
 287          * So, to simplify things we will just let the
 288          * caller's of this method proceed with their business.
 289          * If the debuggee is disconnected, either because it
 290          * crashed or finished or something, or because the
 291          * debugger called exit() or dispose(), then if
 292          * we end up trying to communicate with the debuggee,
 293          * code in TargetVM will throw a VMDisconnectedException.
 294          * This means that if we can satisfy a request without
 295          * talking to the debuggee, (eg, with cached data) then
 296          * VMDisconnectedException will _not_ be thrown.
 297          * if (shutdown) {
 298          *    throw new VMDisconnectedException();
 299          * }
 300          */
 301     }
 302 
 303     public boolean equals(Object obj) {
 304         return this == obj;
 305     }
 306 
 307     public int hashCode() {
 308         return System.identityHashCode(this);
 309     }
 310 
 311     public List<ModuleReference> allModules() {
 312         validateVM();
 313         List<ModuleReference> modules = retrieveAllModules();
 314         return Collections.unmodifiableList(modules);
 315     }
 316 
 317     public List<ReferenceType> classesByName(String className) {
 318         validateVM();
 319         return classesBySignature(JNITypeParser.typeNameToSignature(className));
 320     }
 321 
 322     List<ReferenceType> classesBySignature(String signature) {
 323         validateVM();
 324         List<ReferenceType> list;
 325         if (retrievedAllTypes) {
 326             list = findReferenceTypes(signature);
 327         } else {
 328             list = retrieveClassesBySignature(signature);
 329         }
 330         return Collections.unmodifiableList(list);
 331     }
 332 
 333     public List<ReferenceType> allClasses() {
 334         validateVM();
 335 
 336         if (!retrievedAllTypes) {
 337             retrieveAllClasses();
 338         }
 339         ArrayList<ReferenceType> a;
 340         synchronized (this) {
 341             a = new ArrayList<>(typesBySignature);
 342         }
 343         return Collections.unmodifiableList(a);
 344     }
 345 
 346     /**
 347      * Performs an action for each loaded type.
 348      */
 349     public void forEachClass(Consumer<ReferenceType> action) {
 350         for (ReferenceType type : allClasses()) {
 351             try {
 352                 action.accept(type);
 353             } catch (ObjectCollectedException ex) {
 354                 // Some classes might be unloaded and garbage collected since
 355                 // we retrieved the copy of all loaded classes and started
 356                 // iterating over them. In this case calling methods on such types
 357                 // might result in com.sun.jdi.ObjectCollectedException
 358                 // being thrown. We ignore such classes and keep iterating.
 359                 if ((vm.traceFlags & VirtualMachine.TRACE_OBJREFS) != 0) {
 360                     vm.printTrace("ObjectCollectedException was thrown while " +
 361                             "accessing unloaded class " + type.name());
 362                 }
 363             }
 364         }
 365     }
 366 
 367     public void
 368         redefineClasses(Map<? extends ReferenceType, byte[]> classToBytes)
 369     {
 370         int cnt = classToBytes.size();
 371         JDWP.VirtualMachine.RedefineClasses.ClassDef[] defs =
 372             new JDWP.VirtualMachine.RedefineClasses.ClassDef[cnt];
 373         validateVM();
 374         if (!canRedefineClasses()) {
 375             throw new UnsupportedOperationException();
 376         }
 377         Iterator<?> it = classToBytes.entrySet().iterator();
 378         for (int i = 0; it.hasNext(); i++) {
 379             Map.Entry<?, ?> entry = (Map.Entry)it.next();
 380             ReferenceTypeImpl refType = (ReferenceTypeImpl)entry.getKey();
 381             validateMirror(refType);
 382             defs[i] = new JDWP.VirtualMachine.RedefineClasses
 383                        .ClassDef(refType, (byte[])entry.getValue());
 384         }
 385 
 386         // flush caches and disable caching until the next suspend
 387         vm.state().thaw();
 388 
 389         try {
 390             JDWP.VirtualMachine.RedefineClasses.
 391                 process(vm, defs);
 392         } catch (JDWPException exc) {
 393             switch (exc.errorCode()) {
 394             case JDWP.Error.INVALID_CLASS_FORMAT :
 395                 throw new ClassFormatError(
 396                     "class not in class file format");
 397             case JDWP.Error.CIRCULAR_CLASS_DEFINITION :
 398                 throw new ClassCircularityError(
 399                     "circularity has been detected while initializing a class");
 400             case JDWP.Error.FAILS_VERIFICATION :
 401                 throw new VerifyError(
 402                     "verifier detected internal inconsistency or security problem");
 403             case JDWP.Error.UNSUPPORTED_VERSION :
 404                 throw new UnsupportedClassVersionError(
 405                     "version numbers of class are not supported");
 406             case JDWP.Error.ADD_METHOD_NOT_IMPLEMENTED:
 407                 throw new UnsupportedOperationException(
 408                     "add method not implemented");
 409             case JDWP.Error.SCHEMA_CHANGE_NOT_IMPLEMENTED :
 410                 throw new UnsupportedOperationException(
 411                     "schema change not implemented");
 412             case JDWP.Error.HIERARCHY_CHANGE_NOT_IMPLEMENTED:
 413                 throw new UnsupportedOperationException(
 414                     "hierarchy change not implemented");
 415             case JDWP.Error.DELETE_METHOD_NOT_IMPLEMENTED :
 416                 throw new UnsupportedOperationException(
 417                     "delete method not implemented");
 418             case JDWP.Error.CLASS_MODIFIERS_CHANGE_NOT_IMPLEMENTED:
 419                 throw new UnsupportedOperationException(
 420                     "changes to class modifiers not implemented");
 421             case JDWP.Error.METHOD_MODIFIERS_CHANGE_NOT_IMPLEMENTED :
 422                 throw new UnsupportedOperationException(
 423                     "changes to method modifiers not implemented");
 424             case JDWP.Error.CLASS_ATTRIBUTE_CHANGE_NOT_IMPLEMENTED :
 425                 throw new UnsupportedOperationException(
 426                     "changes to class attribute not implemented");
 427             case JDWP.Error.NAMES_DONT_MATCH :
 428                 throw new NoClassDefFoundError(
 429                     "class names do not match");
 430             default:
 431                 throw exc.toJDIException();
 432             }
 433         }
 434 
 435         // Delete any record of the breakpoints
 436         List<BreakpointRequest> toDelete = new ArrayList<>();
 437         EventRequestManager erm = eventRequestManager();
 438         it = erm.breakpointRequests().iterator();
 439         while (it.hasNext()) {
 440             BreakpointRequest req = (BreakpointRequest)it.next();
 441             if (classToBytes.containsKey(req.location().declaringType())) {
 442                 toDelete.add(req);
 443             }
 444         }
 445         erm.deleteEventRequests(toDelete);
 446 
 447         // Invalidate any information cached for the classes just redefined.
 448         it = classToBytes.keySet().iterator();
 449         while (it.hasNext()) {
 450             ReferenceTypeImpl rti = (ReferenceTypeImpl)it.next();
 451             rti.noticeRedefineClass();
 452         }
 453     }
 454 
 455     public List<ThreadReference> allThreads() {
 456         validateVM();
 457         return state.allThreads();
 458     }
 459 
 460     public List<ThreadGroupReference> topLevelThreadGroups() {
 461         validateVM();
 462         return state.topLevelThreadGroups();
 463     }
 464 
 465     /*
 466      * Sends a command to the back end which is defined to do an
 467      * implicit vm-wide resume. The VM can no longer be considered
 468      * suspended, so certain cached data must be invalidated.
 469      */
 470     PacketStream sendResumingCommand(CommandSender sender) {
 471         return state.thawCommand(sender);
 472     }
 473 
 474     /*
 475      * The VM has been suspended. Additional caching can be done
 476      * as long as there are no pending resumes.
 477      */
 478     void notifySuspend() {
 479         state.freeze();
 480     }
 481 
 482     public void suspend() {
 483         validateVM();
 484         try {
 485             JDWP.VirtualMachine.Suspend.process(vm);
 486         } catch (JDWPException exc) {
 487             throw exc.toJDIException();
 488         }
 489         notifySuspend();
 490     }
 491 
 492     public void resume() {
 493         validateVM();
 494         CommandSender sender =
 495             new CommandSender() {
 496                 public PacketStream send() {
 497                     return JDWP.VirtualMachine.Resume.enqueueCommand(vm);
 498                 }
 499         };
 500         try {
 501             PacketStream stream = state.thawCommand(sender);
 502             JDWP.VirtualMachine.Resume.waitForReply(vm, stream);
 503         } catch (VMDisconnectedException exc) {
 504             /*
 505              * If the debugger makes a VMDeathRequest with SUSPEND_ALL,
 506              * then when it does an EventSet.resume after getting the
 507              * VMDeathEvent, the normal flow of events is that the
 508              * BE shuts down, but the waitForReply comes back ok.  In this
 509              * case, the run loop in TargetVM that is waiting for a packet
 510              * gets an EOF because the socket closes. It generates a
 511              * VMDisconnectedEvent and everyone is happy.
 512              * However, sometimes, the BE gets shutdown before this
 513              * waitForReply completes.  In this case, TargetVM.waitForReply
 514              * gets awakened with no reply and so gens a VMDisconnectedException
 515              * which is not what we want.  It might be possible to fix this
 516              * in the BE, but it is ok to just ignore the VMDisconnectedException
 517              * here.  This will allow the VMDisconnectedEvent to be generated
 518              * correctly.  And, if the debugger should happen to make another
 519              * request, it will get a VMDisconnectedException at that time.
 520              */
 521         } catch (JDWPException exc) {
 522             switch (exc.errorCode()) {
 523                 case JDWP.Error.VM_DEAD:
 524                     return;
 525                 default:
 526                     throw exc.toJDIException();
 527             }
 528         }
 529     }
 530 
 531     public EventQueue eventQueue() {
 532         /*
 533          * No VM validation here. We allow access to the event queue
 534          * after disconnection, so that there is access to the terminating
 535          * events.
 536          */
 537         return eventQueue;
 538     }
 539 
 540     public EventRequestManager eventRequestManager() {
 541         validateVM();
 542         return eventRequestManager;
 543     }
 544 
 545     EventRequestManagerImpl eventRequestManagerImpl() {
 546         return eventRequestManager;
 547     }
 548 
 549     public BooleanValue mirrorOf(boolean value) {
 550         validateVM();
 551         return new BooleanValueImpl(this,value);
 552     }
 553 
 554     public ByteValue mirrorOf(byte value) {
 555         validateVM();
 556         return new ByteValueImpl(this,value);
 557     }
 558 
 559     public CharValue mirrorOf(char value) {
 560         validateVM();
 561         return new CharValueImpl(this,value);
 562     }
 563 
 564     public ShortValue mirrorOf(short value) {
 565         validateVM();
 566         return new ShortValueImpl(this,value);
 567     }
 568 
 569     public IntegerValue mirrorOf(int value) {
 570         validateVM();
 571         return new IntegerValueImpl(this,value);
 572     }
 573 
 574     public LongValue mirrorOf(long value) {
 575         validateVM();
 576         return new LongValueImpl(this,value);
 577     }
 578 
 579     public FloatValue mirrorOf(float value) {
 580         validateVM();
 581         return new FloatValueImpl(this,value);
 582     }
 583 
 584     public DoubleValue mirrorOf(double value) {
 585         validateVM();
 586         return new DoubleValueImpl(this,value);
 587     }
 588 
 589     public StringReference mirrorOf(String value) {
 590         validateVM();
 591         try {
 592             return JDWP.VirtualMachine.CreateString.
 593                 process(vm, value).stringObject;
 594         } catch (JDWPException exc) {
 595             throw exc.toJDIException();
 596         }
 597     }
 598 
 599     public VoidValue mirrorOfVoid() {
 600         if (voidVal == null) {
 601             voidVal = new VoidValueImpl(this);
 602         }
 603         return voidVal;
 604     }
 605 
 606     public long[] instanceCounts(List<? extends ReferenceType> classes) {
 607         if (!canGetInstanceInfo()) {
 608             throw new UnsupportedOperationException(
 609                 "target does not support getting instances");
 610         }
 611         long[] retValue ;
 612         ReferenceTypeImpl[] rtArray = new ReferenceTypeImpl[classes.size()];
 613         int ii = 0;
 614         for (ReferenceType rti: classes) {
 615             validateMirror(rti);
 616             rtArray[ii++] = (ReferenceTypeImpl)rti;
 617         }
 618         try {
 619             retValue = JDWP.VirtualMachine.InstanceCounts.
 620                                 process(vm, rtArray).counts;
 621         } catch (JDWPException exc) {
 622             throw exc.toJDIException();
 623         }
 624 
 625         return retValue;
 626     }
 627 
 628     public void dispose() {
 629         validateVM();
 630         shutdown = true;
 631         try {
 632             JDWP.VirtualMachine.Dispose.process(vm);
 633         } catch (JDWPException exc) {
 634             throw exc.toJDIException();
 635         }
 636         target.stopListening();
 637     }
 638 
 639     public void exit(int exitCode) {
 640         validateVM();
 641         shutdown = true;
 642         try {
 643             JDWP.VirtualMachine.Exit.process(vm, exitCode);
 644         } catch (JDWPException exc) {
 645             throw exc.toJDIException();
 646         }
 647         target.stopListening();
 648     }
 649 
 650     public Process process() {
 651         validateVM();
 652         return process;
 653     }
 654 
 655     private JDWP.VirtualMachine.Version versionInfo() {
 656        try {
 657            if (versionInfo == null) {
 658                // Need not be synchronized since it is static information
 659                versionInfo = JDWP.VirtualMachine.Version.process(vm);
 660            }
 661            return versionInfo;
 662        } catch (JDWPException exc) {
 663            throw exc.toJDIException();
 664        }
 665     }
 666 
 667     public String description() {
 668         validateVM();
 669 
 670         return MessageFormat.format(vmManager.getString("version_format"),
 671                                     "" + vmManager.majorInterfaceVersion(),
 672                                     "" + vmManager.minorInterfaceVersion(),
 673                                      versionInfo().description);
 674     }
 675 
 676     public String version() {
 677         validateVM();
 678         return versionInfo().vmVersion;
 679     }
 680 
 681     public String name() {
 682         validateVM();
 683         return versionInfo().vmName;
 684     }
 685 
 686     public boolean canWatchFieldModification() {
 687         validateVM();
 688         return capabilities().canWatchFieldModification;
 689     }
 690 
 691     public boolean canWatchFieldAccess() {
 692         validateVM();
 693         return capabilities().canWatchFieldAccess;
 694     }
 695 
 696     public boolean canGetBytecodes() {
 697         validateVM();
 698         return capabilities().canGetBytecodes;
 699     }
 700 
 701     public boolean canGetSyntheticAttribute() {
 702         validateVM();
 703         return capabilities().canGetSyntheticAttribute;
 704     }
 705 
 706     public boolean canGetOwnedMonitorInfo() {
 707         validateVM();
 708         return capabilities().canGetOwnedMonitorInfo;
 709     }
 710 
 711     public boolean canGetCurrentContendedMonitor() {
 712         validateVM();
 713         return capabilities().canGetCurrentContendedMonitor;
 714     }
 715 
 716     public boolean canGetMonitorInfo() {
 717         validateVM();
 718         return capabilities().canGetMonitorInfo;
 719     }
 720 
 721     private boolean hasNewCapabilities() {
 722         return versionInfo().jdwpMajor > 1 ||
 723             versionInfo().jdwpMinor >= 4;
 724     }
 725 
 726     boolean canGet1_5LanguageFeatures() {
 727         return versionInfo().jdwpMajor > 1 ||
 728             versionInfo().jdwpMinor >= 5;
 729     }
 730 
 731     public boolean canUseInstanceFilters() {
 732         validateVM();
 733         return hasNewCapabilities() &&
 734             capabilitiesNew().canUseInstanceFilters;
 735     }
 736 
 737     public boolean canRedefineClasses() {
 738         validateVM();
 739         return hasNewCapabilities() &&
 740             capabilitiesNew().canRedefineClasses;
 741     }
 742 
 743     @Deprecated(since="15")
 744     public boolean canAddMethod() {
 745         validateVM();
 746         return hasNewCapabilities() &&
 747             capabilitiesNew().canAddMethod;
 748     }
 749 
 750     @Deprecated(since="15")
 751     public boolean canUnrestrictedlyRedefineClasses() {
 752         validateVM();
 753         return hasNewCapabilities() &&
 754             capabilitiesNew().canUnrestrictedlyRedefineClasses;
 755     }
 756 
 757     public boolean canPopFrames() {
 758         validateVM();
 759         return hasNewCapabilities() &&
 760             capabilitiesNew().canPopFrames;
 761     }
 762 
 763     public boolean canGetMethodReturnValues() {
 764         return versionInfo().jdwpMajor > 1 ||
 765             versionInfo().jdwpMinor >= 6;
 766     }
 767 
 768     public boolean canGetInstanceInfo() {
 769         if (versionInfo().jdwpMajor > 1 ||
 770             versionInfo().jdwpMinor >= 6) {
 771             validateVM();
 772             return hasNewCapabilities() &&
 773                 capabilitiesNew().canGetInstanceInfo;
 774         } else {
 775             return false;
 776         }
 777     }
 778 
 779     public boolean canUseSourceNameFilters() {
 780         return versionInfo().jdwpMajor > 1 ||
 781             versionInfo().jdwpMinor >= 6;
 782     }
 783 
 784     public boolean canForceEarlyReturn() {
 785         validateVM();
 786         return hasNewCapabilities() &&
 787             capabilitiesNew().canForceEarlyReturn;
 788     }
 789 
 790     public boolean canBeModified() {
 791         return true;
 792     }
 793 
 794     public boolean canGetSourceDebugExtension() {
 795         validateVM();
 796         return hasNewCapabilities() &&
 797             capabilitiesNew().canGetSourceDebugExtension;
 798     }
 799 
 800     public boolean canGetClassFileVersion() {
 801         return versionInfo().jdwpMajor > 1 ||
 802             versionInfo().jdwpMinor >= 6;
 803     }
 804 
 805     public boolean canGetConstantPool() {
 806         validateVM();
 807         return hasNewCapabilities() &&
 808             capabilitiesNew().canGetConstantPool;
 809     }
 810 
 811     public boolean canRequestVMDeathEvent() {
 812         validateVM();
 813         return hasNewCapabilities() &&
 814             capabilitiesNew().canRequestVMDeathEvent;
 815     }
 816 
 817     public boolean canRequestMonitorEvents() {
 818         validateVM();
 819         return hasNewCapabilities() &&
 820             capabilitiesNew().canRequestMonitorEvents;
 821     }
 822 
 823     public boolean canGetMonitorFrameInfo() {
 824         validateVM();
 825         return hasNewCapabilities() &&
 826             capabilitiesNew().canGetMonitorFrameInfo;
 827     }
 828 
 829     public boolean canGetModuleInfo() {
 830         validateVM();
 831         return versionInfo().jdwpMajor >= 9;
 832     }
 833 
 834     boolean mayCreateVirtualThreads() {
 835         return versionInfo().jdwpMajor >= 19;
 836     }
 837 
 838     public void setDebugTraceMode(int traceFlags) {
 839         validateVM();
 840         this.traceFlags = traceFlags;
 841         this.traceReceives = (traceFlags & TRACE_RECEIVES) != 0;
 842     }
 843 
 844     void printTrace(String string) {
 845         System.err.println("[JDI: " + string + "]");
 846     }
 847 
 848     void printReceiveTrace(int depth, String string) {
 849         StringBuilder sb = new StringBuilder("Receiving:");
 850         for (int i = depth; i > 0; --i) {
 851             sb.append("    ");
 852         }
 853         sb.append(string);
 854         printTrace(sb.toString());
 855     }
 856 
 857     private synchronized ReferenceTypeImpl addReferenceType(long id,
 858                                                             int tag,
 859                                                             String signature) {
 860         if (typesByID == null) {
 861             initReferenceTypes();
 862         }
 863         ReferenceTypeImpl type = null;
 864         switch(tag) {
 865             case JDWP.TypeTag.CLASS:
 866                 type = new ClassTypeImpl(vm, id);
 867                 break;
 868             case JDWP.TypeTag.INTERFACE:
 869                 type = new InterfaceTypeImpl(vm, id);
 870                 break;
 871             case JDWP.TypeTag.ARRAY:
 872                 type = new ArrayTypeImpl(vm, id);
 873                 break;
 874             default:
 875                 throw new InternalException("Invalid reference type tag");
 876         }
 877 
 878         if (signature == null && retrievedAllTypes) {
 879             // do not cache if signature is not provided
 880             return type;
 881         }
 882 
 883         typesByID.put(id, type);
 884         typesBySignature.add(type);
 885 
 886         if ((vm.traceFlags & VirtualMachine.TRACE_REFTYPES) != 0) {
 887            vm.printTrace("Caching new ReferenceType, sig=" + signature +
 888                          ", id=" + id);
 889         }
 890 
 891         return type;
 892     }
 893 
 894     synchronized void removeReferenceType(String signature) {
 895         if (typesByID == null) {
 896             return;
 897         }
 898         /*
 899          * There can be multiple classes with the same name. Since
 900          * we can't differentiate here, we first remove all
 901          * matching classes from our cache...
 902          */
 903         Iterator<ReferenceType> iter = typesBySignature.iterator();
 904         int matches = 0;
 905         while (iter.hasNext()) {
 906             ReferenceTypeImpl type = (ReferenceTypeImpl)iter.next();
 907             int comp = signature.compareTo(type.signature());
 908             if (comp == 0) {
 909                 matches++;
 910                 iter.remove();
 911                 typesByID.remove(type.ref());
 912                 if ((vm.traceFlags & VirtualMachine.TRACE_REFTYPES) != 0) {
 913                    vm.printTrace("Uncaching ReferenceType, sig=" + signature +
 914                                  ", id=" + type.ref());
 915                 }
 916                 // fix for 4359077, don't break out. list is no longer sorted
 917                 // in the order we think
 918             }
 919         }
 920 
 921         /*
 922          * ...and if there was more than one, re-retrieve the classes
 923          * with that name
 924          */
 925         if (matches > 1) {
 926             retrieveClassesBySignature(signature);
 927         }
 928     }
 929 
 930     private synchronized List<ReferenceType> findReferenceTypes(String signature) {
 931         if (typesByID == null) {
 932             return new ArrayList<>(0);
 933         }
 934         Iterator<ReferenceType> iter = typesBySignature.iterator();
 935         List<ReferenceType> list = new ArrayList<>();
 936         while (iter.hasNext()) {
 937             ReferenceTypeImpl type = (ReferenceTypeImpl)iter.next();
 938             int comp = signature.compareTo(type.signature());
 939             if (comp == 0) {
 940                 list.add(type);
 941                 // fix for 4359077, don't break out. list is no longer sorted
 942                 // in the order we think
 943             }
 944         }
 945         return list;
 946     }
 947 
 948     private void initReferenceTypes() {
 949         typesByID = new HashMap<>(300);
 950         typesBySignature = new HashSet<>();
 951     }
 952 
 953     ReferenceTypeImpl referenceType(long ref, byte tag) {
 954         return referenceType(ref, tag, null);
 955     }
 956 
 957     ClassTypeImpl classType(long ref) {
 958         return (ClassTypeImpl)referenceType(ref, JDWP.TypeTag.CLASS, null);
 959     }
 960 
 961     InterfaceTypeImpl interfaceType(long ref) {
 962         return (InterfaceTypeImpl)referenceType(ref, JDWP.TypeTag.INTERFACE, null);
 963     }
 964 
 965     ArrayTypeImpl arrayType(long ref) {
 966         return (ArrayTypeImpl)referenceType(ref, JDWP.TypeTag.ARRAY, null);
 967     }
 968 
 969     ReferenceTypeImpl referenceType(long id, int tag, String signature) {
 970         if ((vm.traceFlags & VirtualMachine.TRACE_REFTYPES) != 0) {
 971             StringBuilder sb = new StringBuilder();
 972             sb.append("Looking up ");
 973             if (tag == JDWP.TypeTag.CLASS) {
 974                 sb.append("Class");
 975             } else if (tag == JDWP.TypeTag.INTERFACE) {
 976                 sb.append("Interface");
 977             } else if (tag == JDWP.TypeTag.ARRAY) {
 978                 sb.append("ArrayType");
 979             } else {
 980                 sb.append("UNKNOWN TAG: ").append(tag);
 981             }
 982             if (signature != null) {
 983                 sb.append(", signature='").append(signature).append('\'');
 984             }
 985             sb.append(", id=").append(id);
 986             vm.printTrace(sb.toString());
 987         }
 988         if (id == 0) {
 989             return null;
 990         } else {
 991             ReferenceTypeImpl retType = null;
 992             synchronized (this) {
 993                 if (typesByID != null) {
 994                     retType = (ReferenceTypeImpl)typesByID.get(id);
 995                 }
 996                 if (retType == null) {
 997                     retType = addReferenceType(id, tag, signature);
 998                 }
 999                 if (signature != null) {
1000                     retType.setSignature(signature);
1001                 }
1002             }
1003             return retType;
1004         }
1005     }
1006 
1007     private JDWP.VirtualMachine.Capabilities capabilities() {
1008         if (capabilities == null) {
1009             try {
1010                 capabilities = JDWP.VirtualMachine
1011                                  .Capabilities.process(vm);
1012             } catch (JDWPException exc) {
1013                 throw exc.toJDIException();
1014             }
1015         }
1016         return capabilities;
1017     }
1018 
1019     private JDWP.VirtualMachine.CapabilitiesNew capabilitiesNew() {
1020         if (capabilitiesNew == null) {
1021             try {
1022                 capabilitiesNew = JDWP.VirtualMachine
1023                                  .CapabilitiesNew.process(vm);
1024             } catch (JDWPException exc) {
1025                 throw exc.toJDIException();
1026             }
1027         }
1028         return capabilitiesNew;
1029     }
1030 
1031     private synchronized ModuleReference addModule(long id) {
1032         if (modulesByID == null) {
1033             modulesByID = new HashMap<>(77);
1034         }
1035         ModuleReference module = new ModuleReferenceImpl(vm, id);
1036         modulesByID.put(id, module);
1037         return module;
1038     }
1039 
1040     ModuleReference getModule(long id) {
1041         if (id == 0) {
1042             return null;
1043         } else {
1044             ModuleReference module = null;
1045             synchronized (this) {
1046                 if (modulesByID != null) {
1047                     module = modulesByID.get(id);
1048                 }
1049                 if (module == null) {
1050                     module = addModule(id);
1051                 }
1052             }
1053             return module;
1054         }
1055     }
1056 
1057     private synchronized List<ModuleReference> retrieveAllModules() {
1058         ModuleReferenceImpl[] reqModules;
1059         try {
1060             reqModules = JDWP.VirtualMachine.AllModules.process(vm).modules;
1061         } catch (JDWPException exc) {
1062             throw exc.toJDIException();
1063         }
1064         ArrayList<ModuleReference> modules = new ArrayList<>();
1065         for (int i = 0; i < reqModules.length; i++) {
1066             long moduleRef = reqModules[i].ref();
1067             ModuleReference module = getModule(moduleRef);
1068             modules.add(module);
1069         }
1070         return modules;
1071     }
1072 
1073     private List<ReferenceType> retrieveClassesBySignature(String signature) {
1074         if ((vm.traceFlags & VirtualMachine.TRACE_REFTYPES) != 0) {
1075             vm.printTrace("Retrieving matching ReferenceTypes, sig=" + signature);
1076         }
1077         JDWP.VirtualMachine.ClassesBySignature.ClassInfo[] cinfos;
1078         try {
1079             cinfos = JDWP.VirtualMachine.ClassesBySignature.
1080                                       process(vm, signature).classes;
1081         } catch (JDWPException exc) {
1082             throw exc.toJDIException();
1083         }
1084 
1085         int count = cinfos.length;
1086         List<ReferenceType> list = new ArrayList<>(count);
1087 
1088         // Hold lock during processing to improve performance
1089         synchronized (this) {
1090             for (int i = 0; i < count; i++) {
1091                 JDWP.VirtualMachine.ClassesBySignature.ClassInfo ci =
1092                                                                cinfos[i];
1093                 ReferenceTypeImpl type = referenceType(ci.typeID,
1094                                                        ci.refTypeTag,
1095                                                        signature);
1096                 type.setStatus(ci.status);
1097                 list.add(type);
1098             }
1099         }
1100         return list;
1101     }
1102 
1103     private void retrieveAllClasses1_4() {
1104         JDWP.VirtualMachine.AllClasses.ClassInfo[] cinfos;
1105         try {
1106             cinfos = JDWP.VirtualMachine.AllClasses.process(vm).classes;
1107         } catch (JDWPException exc) {
1108             throw exc.toJDIException();
1109         }
1110 
1111         // Hold lock during processing to improve performance
1112         // and to have safe check/set of retrievedAllTypes
1113         synchronized (this) {
1114             if (!retrievedAllTypes) {
1115                 // Number of classes
1116                 int count = cinfos.length;
1117                 for (int i = 0; i < count; i++) {
1118                     JDWP.VirtualMachine.AllClasses.ClassInfo ci = cinfos[i];
1119                     ReferenceTypeImpl type = referenceType(ci.typeID,
1120                                                            ci.refTypeTag,
1121                                                            ci.signature);
1122                     type.setStatus(ci.status);
1123                 }
1124                 retrievedAllTypes = true;
1125             }
1126         }
1127     }
1128 
1129     private void retrieveAllClasses() {
1130         if ((vm.traceFlags & VirtualMachine.TRACE_REFTYPES) != 0) {
1131             vm.printTrace("Retrieving all ReferenceTypes");
1132         }
1133 
1134         if (!vm.canGet1_5LanguageFeatures()) {
1135             retrieveAllClasses1_4();
1136             return;
1137         }
1138 
1139         /*
1140          * To save time (assuming the caller will be
1141          * using then) we will get the generic sigs too.
1142          */
1143         JDWP.VirtualMachine.AllClassesWithGeneric.ClassInfo[] cinfos;
1144         try {
1145             cinfos = JDWP.VirtualMachine.AllClassesWithGeneric.process(vm).classes;
1146         } catch (JDWPException exc) {
1147             throw exc.toJDIException();
1148         }
1149 
1150         // Hold lock during processing to improve performance
1151         // and to have safe check/set of retrievedAllTypes
1152         synchronized (this) {
1153             if (!retrievedAllTypes) {
1154                 // Number of classes
1155                 int count = cinfos.length;
1156                 for (int i = 0; i < count; i++) {
1157                     JDWP.VirtualMachine.AllClassesWithGeneric.ClassInfo ci =
1158                                                                cinfos[i];
1159                     ReferenceTypeImpl type = referenceType(ci.typeID,
1160                                                            ci.refTypeTag,
1161                                                            ci.signature);
1162                     type.setGenericSignature(ci.genericSignature);
1163                     type.setStatus(ci.status);
1164                 }
1165                 retrievedAllTypes = true;
1166             }
1167         }
1168     }
1169 
1170     void sendToTarget(Packet packet) {
1171         target.send(packet);
1172     }
1173 
1174     void waitForTargetReply(Packet packet) {
1175         target.waitForReply(packet);
1176         /*
1177          * If any object disposes have been batched up, send them now.
1178          */
1179         processBatchedDisposes();
1180     }
1181 
1182     Type findBootType(String signature) throws ClassNotLoadedException {
1183         List<ReferenceType> types = retrieveClassesBySignature(signature);
1184         for (ReferenceType type : types) {
1185             if (type.classLoader() == null) {
1186                 return type;
1187             }
1188         }
1189         JNITypeParser parser = new JNITypeParser(signature);
1190         throw new ClassNotLoadedException(parser.typeName(),
1191                                          "Type " + parser.typeName() + " not loaded");
1192     }
1193 
1194     BooleanType theBooleanType() {
1195         if (theBooleanType == null) {
1196             synchronized(this) {
1197                 if (theBooleanType == null) {
1198                     theBooleanType = new BooleanTypeImpl(this);
1199                 }
1200             }
1201         }
1202         return theBooleanType;
1203     }
1204 
1205     ByteType theByteType() {
1206         if (theByteType == null) {
1207             synchronized(this) {
1208                 if (theByteType == null) {
1209                     theByteType = new ByteTypeImpl(this);
1210                 }
1211             }
1212         }
1213         return theByteType;
1214     }
1215 
1216     CharType theCharType() {
1217         if (theCharType == null) {
1218             synchronized(this) {
1219                 if (theCharType == null) {
1220                     theCharType = new CharTypeImpl(this);
1221                 }
1222             }
1223         }
1224         return theCharType;
1225     }
1226 
1227     ShortType theShortType() {
1228         if (theShortType == null) {
1229             synchronized(this) {
1230                 if (theShortType == null) {
1231                     theShortType = new ShortTypeImpl(this);
1232                 }
1233             }
1234         }
1235         return theShortType;
1236     }
1237 
1238     IntegerType theIntegerType() {
1239         if (theIntegerType == null) {
1240             synchronized(this) {
1241                 if (theIntegerType == null) {
1242                     theIntegerType = new IntegerTypeImpl(this);
1243                 }
1244             }
1245         }
1246         return theIntegerType;
1247     }
1248 
1249     LongType theLongType() {
1250         if (theLongType == null) {
1251             synchronized(this) {
1252                 if (theLongType == null) {
1253                     theLongType = new LongTypeImpl(this);
1254                 }
1255             }
1256         }
1257         return theLongType;
1258     }
1259 
1260     FloatType theFloatType() {
1261         if (theFloatType == null) {
1262             synchronized(this) {
1263                 if (theFloatType == null) {
1264                     theFloatType = new FloatTypeImpl(this);
1265                 }
1266             }
1267         }
1268         return theFloatType;
1269     }
1270 
1271     DoubleType theDoubleType() {
1272         if (theDoubleType == null) {
1273             synchronized(this) {
1274                 if (theDoubleType == null) {
1275                     theDoubleType = new DoubleTypeImpl(this);
1276                 }
1277             }
1278         }
1279         return theDoubleType;
1280     }
1281 
1282     VoidType theVoidType() {
1283         if (theVoidType == null) {
1284             synchronized(this) {
1285                 if (theVoidType == null) {
1286                     theVoidType = new VoidTypeImpl(this);
1287                 }
1288             }
1289         }
1290         return theVoidType;
1291     }
1292 
1293     PrimitiveType primitiveTypeMirror(byte tag) {
1294         switch (tag) {
1295             case JDWP.Tag.BOOLEAN:
1296                 return theBooleanType();
1297             case JDWP.Tag.BYTE:
1298                 return theByteType();
1299             case JDWP.Tag.CHAR:
1300                 return theCharType();
1301             case JDWP.Tag.SHORT:
1302                 return theShortType();
1303             case JDWP.Tag.INT:
1304                 return theIntegerType();
1305             case JDWP.Tag.LONG:
1306                 return theLongType();
1307             case JDWP.Tag.FLOAT:
1308                 return theFloatType();
1309             case JDWP.Tag.DOUBLE:
1310                 return theDoubleType();
1311             default:
1312                 throw new IllegalArgumentException("Unrecognized primitive tag " + tag);
1313         }
1314     }
1315 
1316     private void processBatchedDisposes() {
1317         if (shutdown) {
1318             return;
1319         }
1320 
1321         JDWP.VirtualMachine.DisposeObjects.Request[] requests = null;
1322         synchronized(batchedDisposeRequests) {
1323             int size = batchedDisposeRequests.size();
1324             if (size >= DISPOSE_THRESHOLD) {
1325                 if ((traceFlags & TRACE_OBJREFS) != 0) {
1326                     printTrace("Dispose threshold reached. Will dispose "
1327                                + size + " object references...");
1328                 }
1329                 requests = new JDWP.VirtualMachine.DisposeObjects.Request[size];
1330                 for (int i = 0; i < requests.length; i++) {
1331                     SoftObjectReference ref = batchedDisposeRequests.get(i);
1332                     if ((traceFlags & TRACE_OBJREFS) != 0) {
1333                         printTrace("Disposing object " + ref.key().longValue() +
1334                                    " (ref count = " + ref.count() + ")");
1335                     }
1336 
1337                     // This is kludgy. We temporarily re-create an object
1338                     // reference so that we can correctly pass its id to the
1339                     // JDWP command.
1340                     requests[i] =
1341                         new JDWP.VirtualMachine.DisposeObjects.Request(
1342                             new ObjectReferenceImpl(this, ref.key().longValue()),
1343                             ref.count());
1344                 }
1345                 batchedDisposeRequests.clear();
1346             }
1347         }
1348         if (requests != null) {
1349             try {
1350                 JDWP.VirtualMachine.DisposeObjects.process(vm, requests);
1351             } catch (JDWPException exc) {
1352                 throw exc.toJDIException();
1353             }
1354         }
1355     }
1356 
1357     private void batchForDispose(SoftObjectReference ref) {
1358         if ((traceFlags & TRACE_OBJREFS) != 0) {
1359             printTrace("Batching object " + ref.key().longValue() +
1360                        " for dispose (ref count = " + ref.count() + ")");
1361         }
1362         batchedDisposeRequests.add(ref);
1363     }
1364 
1365     private void processQueue() {
1366         Reference<?> ref;
1367         //if ((traceFlags & TRACE_OBJREFS) != 0) {
1368         //    printTrace("Checking for softly reachable objects");
1369         //}
1370         boolean found = false;
1371         while ((ref = referenceQueue.poll()) != null) {
1372             SoftObjectReference softRef = (SoftObjectReference)ref;
1373             removeObjectMirror(softRef);
1374             batchForDispose(softRef);
1375             found = true;
1376         }
1377 
1378         if (found) {
1379             // If we batched any ObjectReferences for disposing, we can dispose them now.
1380             processBatchedDisposes();
1381         }
1382     }
1383 
1384     synchronized ObjectReferenceImpl objectMirror(long id, int tag) {
1385 
1386         // Handle any queue elements that are not strongly reachable
1387         processQueue();
1388 
1389         if (id == 0) {
1390             return null;
1391         }
1392         ObjectReferenceImpl object = null;
1393         Long key = id;
1394 
1395         /*
1396          * Attempt to retrieve an existing object reference
1397          */
1398         SoftObjectReference ref = objectsByID.get(key);
1399         if (ref != null) {
1400             object = ref.object();
1401         }
1402 
1403         /*
1404          * If the object wasn't in the table, or it's soft reference was
1405          * cleared, create a new instance.
1406          */
1407         if (object == null) {
1408             switch (tag) {
1409                 case JDWP.Tag.OBJECT:
1410                     object = new ObjectReferenceImpl(vm, id);
1411                     break;
1412                 case JDWP.Tag.STRING:
1413                     object = new StringReferenceImpl(vm, id);
1414                     break;
1415                 case JDWP.Tag.ARRAY:
1416                     object = new ArrayReferenceImpl(vm, id);
1417                     break;
1418                 case JDWP.Tag.THREAD:
1419                     ThreadReferenceImpl thread =
1420                         new ThreadReferenceImpl(vm, id);
1421                     thread.addListener(this);
1422                     object = thread;
1423                     break;
1424                 case JDWP.Tag.THREAD_GROUP:
1425                     object = new ThreadGroupReferenceImpl(vm, id);
1426                     break;
1427                 case JDWP.Tag.CLASS_LOADER:
1428                     object = new ClassLoaderReferenceImpl(vm, id);
1429                     break;
1430                 case JDWP.Tag.CLASS_OBJECT:
1431                     object = new ClassObjectReferenceImpl(vm, id);
1432                     break;
1433                 default:
1434                     throw new IllegalArgumentException("Invalid object tag: " + tag);
1435             }
1436             ref = new SoftObjectReference(key, object, referenceQueue);
1437 
1438             /*
1439              * If there was no previous entry in the table, we add one here
1440              * If the previous entry was cleared, we replace it here.
1441              */
1442             objectsByID.put(key, ref);
1443             if ((traceFlags & TRACE_OBJREFS) != 0) {
1444                 printTrace("Creating new " +
1445                            object.getClass().getName() + " (id = " + id + ")");
1446             }
1447         } else {
1448             ref.incrementCount();
1449         }
1450 
1451         return object;
1452     }
1453 
1454     private synchronized void removeObjectMirror(SoftObjectReference ref) {
1455         /*
1456          * This will remove the soft reference if it has not been
1457          * replaced in the cache.
1458          */
1459         objectsByID.remove(ref.key());
1460     }
1461 
1462     ObjectReferenceImpl objectMirror(long id) {
1463         return objectMirror(id, JDWP.Tag.OBJECT);
1464     }
1465 
1466     StringReferenceImpl stringMirror(long id) {
1467         return (StringReferenceImpl)objectMirror(id, JDWP.Tag.STRING);
1468     }
1469 
1470     ArrayReferenceImpl arrayMirror(long id) {
1471        return (ArrayReferenceImpl)objectMirror(id, JDWP.Tag.ARRAY);
1472     }
1473 
1474     ThreadReferenceImpl threadMirror(long id) {
1475         return (ThreadReferenceImpl)objectMirror(id, JDWP.Tag.THREAD);
1476     }
1477 
1478     ThreadGroupReferenceImpl threadGroupMirror(long id) {
1479         return (ThreadGroupReferenceImpl)objectMirror(id,
1480                                                       JDWP.Tag.THREAD_GROUP);
1481     }
1482 
1483     ClassLoaderReferenceImpl classLoaderMirror(long id) {
1484         return (ClassLoaderReferenceImpl)objectMirror(id,
1485                                                       JDWP.Tag.CLASS_LOADER);
1486     }
1487 
1488     ClassObjectReferenceImpl classObjectMirror(long id) {
1489         return (ClassObjectReferenceImpl)objectMirror(id,
1490                                                       JDWP.Tag.CLASS_OBJECT);
1491     }
1492 
1493     ModuleReferenceImpl moduleMirror(long id) {
1494         return (ModuleReferenceImpl)getModule(id);
1495     }
1496 
1497     /*
1498      * Implementation of PathSearchingVirtualMachine
1499      */
1500     private JDWP.VirtualMachine.ClassPaths getClasspath() {
1501         if (pathInfo == null) {
1502             try {
1503                 pathInfo = JDWP.VirtualMachine.ClassPaths.process(vm);
1504             } catch (JDWPException exc) {
1505                 throw exc.toJDIException();
1506             }
1507         }
1508         return pathInfo;
1509     }
1510 
1511    public List<String> classPath() {
1512        return Arrays.asList(getClasspath().classpaths);
1513    }
1514 
1515    public List<String> bootClassPath() {
1516        return Collections.emptyList();
1517    }
1518 
1519    public String baseDirectory() {
1520        return getClasspath().baseDir;
1521    }
1522 
1523     public void setDefaultStratum(String stratum) {
1524         defaultStratum = stratum;
1525         if (stratum == null) {
1526             stratum = "";
1527         }
1528         try {
1529             JDWP.VirtualMachine.SetDefaultStratum.process(vm,
1530                                                           stratum);
1531         } catch (JDWPException exc) {
1532             throw exc.toJDIException();
1533         }
1534     }
1535 
1536     public String getDefaultStratum() {
1537         return defaultStratum;
1538     }
1539 
1540     ThreadGroup threadGroupForJDI() {
1541         return threadGroupForJDI;
1542     }
1543 
1544    private static class SoftObjectReference extends SoftReference<ObjectReferenceImpl> {
1545        int count;
1546        Long key;
1547 
1548        SoftObjectReference(Long key, ObjectReferenceImpl mirror,
1549                            ReferenceQueue<ObjectReferenceImpl> queue) {
1550            super(mirror, queue);
1551            this.count = 1;
1552            this.key = key;
1553        }
1554 
1555        int count() {
1556            return count;
1557        }
1558 
1559        void incrementCount() {
1560            count++;
1561        }
1562 
1563        Long key() {
1564            return key;
1565        }
1566 
1567        ObjectReferenceImpl object() {
1568            return get();
1569        }
1570    }
1571 }