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 boolean supportsValueClasses() {
 839         return versionInfo().jdwpMajor >= 27;
 840     }
 841 
 842     public void setDebugTraceMode(int traceFlags) {
 843         validateVM();
 844         this.traceFlags = traceFlags;
 845         this.traceReceives = (traceFlags & TRACE_RECEIVES) != 0;
 846     }
 847 
 848     void printTrace(String string) {
 849         System.err.println("[JDI: " + string + "]");
 850     }
 851 
 852     void printReceiveTrace(int depth, String string) {
 853         StringBuilder sb = new StringBuilder("Receiving:");
 854         for (int i = depth; i > 0; --i) {
 855             sb.append("    ");
 856         }
 857         sb.append(string);
 858         printTrace(sb.toString());
 859     }
 860 
 861     private synchronized ReferenceTypeImpl addReferenceType(long id,
 862                                                             int tag,
 863                                                             String signature) {
 864         if (typesByID == null) {
 865             initReferenceTypes();
 866         }
 867         ReferenceTypeImpl type = null;
 868         switch(tag) {
 869             case JDWP.TypeTag.CLASS:
 870                 type = new ClassTypeImpl(vm, id);
 871                 break;
 872             case JDWP.TypeTag.INTERFACE:
 873                 type = new InterfaceTypeImpl(vm, id);
 874                 break;
 875             case JDWP.TypeTag.ARRAY:
 876                 type = new ArrayTypeImpl(vm, id);
 877                 break;
 878             default:
 879                 throw new InternalException("Invalid reference type tag");
 880         }
 881 
 882         if (signature == null && retrievedAllTypes) {
 883             // do not cache if signature is not provided
 884             return type;
 885         }
 886 
 887         typesByID.put(id, type);
 888         typesBySignature.add(type);
 889 
 890         if ((vm.traceFlags & VirtualMachine.TRACE_REFTYPES) != 0) {
 891            vm.printTrace("Caching new ReferenceType, sig=" + signature +
 892                          ", id=" + id);
 893         }
 894 
 895         return type;
 896     }
 897 
 898     synchronized void removeReferenceType(String signature) {
 899         if (typesByID == null) {
 900             return;
 901         }
 902         /*
 903          * There can be multiple classes with the same name. Since
 904          * we can't differentiate here, we first remove all
 905          * matching classes from our cache...
 906          */
 907         Iterator<ReferenceType> iter = typesBySignature.iterator();
 908         int matches = 0;
 909         while (iter.hasNext()) {
 910             ReferenceTypeImpl type = (ReferenceTypeImpl)iter.next();
 911             int comp = signature.compareTo(type.signature());
 912             if (comp == 0) {
 913                 matches++;
 914                 iter.remove();
 915                 typesByID.remove(type.ref());
 916                 if ((vm.traceFlags & VirtualMachine.TRACE_REFTYPES) != 0) {
 917                    vm.printTrace("Uncaching ReferenceType, sig=" + signature +
 918                                  ", id=" + type.ref());
 919                 }
 920                 // fix for 4359077, don't break out. list is no longer sorted
 921                 // in the order we think
 922             }
 923         }
 924 
 925         /*
 926          * ...and if there was more than one, re-retrieve the classes
 927          * with that name
 928          */
 929         if (matches > 1) {
 930             retrieveClassesBySignature(signature);
 931         }
 932     }
 933 
 934     private synchronized List<ReferenceType> findReferenceTypes(String signature) {
 935         if (typesByID == null) {
 936             return new ArrayList<>(0);
 937         }
 938         Iterator<ReferenceType> iter = typesBySignature.iterator();
 939         List<ReferenceType> list = new ArrayList<>();
 940         while (iter.hasNext()) {
 941             ReferenceTypeImpl type = (ReferenceTypeImpl)iter.next();
 942             int comp = signature.compareTo(type.signature());
 943             if (comp == 0) {
 944                 list.add(type);
 945                 // fix for 4359077, don't break out. list is no longer sorted
 946                 // in the order we think
 947             }
 948         }
 949         return list;
 950     }
 951 
 952     private void initReferenceTypes() {
 953         typesByID = new HashMap<>(300);
 954         typesBySignature = new HashSet<>();
 955     }
 956 
 957     ReferenceTypeImpl referenceType(long ref, byte tag) {
 958         return referenceType(ref, tag, null);
 959     }
 960 
 961     ClassTypeImpl classType(long ref) {
 962         return (ClassTypeImpl)referenceType(ref, JDWP.TypeTag.CLASS, null);
 963     }
 964 
 965     InterfaceTypeImpl interfaceType(long ref) {
 966         return (InterfaceTypeImpl)referenceType(ref, JDWP.TypeTag.INTERFACE, null);
 967     }
 968 
 969     ArrayTypeImpl arrayType(long ref) {
 970         return (ArrayTypeImpl)referenceType(ref, JDWP.TypeTag.ARRAY, null);
 971     }
 972 
 973     ReferenceTypeImpl referenceType(long id, int tag, String signature) {
 974         if ((vm.traceFlags & VirtualMachine.TRACE_REFTYPES) != 0) {
 975             StringBuilder sb = new StringBuilder();
 976             sb.append("Looking up ");
 977             if (tag == JDWP.TypeTag.CLASS) {
 978                 sb.append("Class");
 979             } else if (tag == JDWP.TypeTag.INTERFACE) {
 980                 sb.append("Interface");
 981             } else if (tag == JDWP.TypeTag.ARRAY) {
 982                 sb.append("ArrayType");
 983             } else {
 984                 sb.append("UNKNOWN TAG: ").append(tag);
 985             }
 986             if (signature != null) {
 987                 sb.append(", signature='").append(signature).append('\'');
 988             }
 989             sb.append(", id=").append(id);
 990             vm.printTrace(sb.toString());
 991         }
 992         if (id == 0) {
 993             return null;
 994         } else {
 995             ReferenceTypeImpl retType = null;
 996             synchronized (this) {
 997                 if (typesByID != null) {
 998                     retType = (ReferenceTypeImpl)typesByID.get(id);
 999                 }
1000                 if (retType == null) {
1001                     retType = addReferenceType(id, tag, signature);
1002                 }
1003                 if (signature != null) {
1004                     retType.setSignature(signature);
1005                 }
1006             }
1007             return retType;
1008         }
1009     }
1010 
1011     private JDWP.VirtualMachine.Capabilities capabilities() {
1012         if (capabilities == null) {
1013             try {
1014                 capabilities = JDWP.VirtualMachine
1015                                  .Capabilities.process(vm);
1016             } catch (JDWPException exc) {
1017                 throw exc.toJDIException();
1018             }
1019         }
1020         return capabilities;
1021     }
1022 
1023     private JDWP.VirtualMachine.CapabilitiesNew capabilitiesNew() {
1024         if (capabilitiesNew == null) {
1025             try {
1026                 capabilitiesNew = JDWP.VirtualMachine
1027                                  .CapabilitiesNew.process(vm);
1028             } catch (JDWPException exc) {
1029                 throw exc.toJDIException();
1030             }
1031         }
1032         return capabilitiesNew;
1033     }
1034 
1035     private synchronized ModuleReference addModule(long id) {
1036         if (modulesByID == null) {
1037             modulesByID = new HashMap<>(77);
1038         }
1039         ModuleReference module = new ModuleReferenceImpl(vm, id);
1040         modulesByID.put(id, module);
1041         return module;
1042     }
1043 
1044     ModuleReference getModule(long id) {
1045         if (id == 0) {
1046             return null;
1047         } else {
1048             ModuleReference module = null;
1049             synchronized (this) {
1050                 if (modulesByID != null) {
1051                     module = modulesByID.get(id);
1052                 }
1053                 if (module == null) {
1054                     module = addModule(id);
1055                 }
1056             }
1057             return module;
1058         }
1059     }
1060 
1061     private synchronized List<ModuleReference> retrieveAllModules() {
1062         ModuleReferenceImpl[] reqModules;
1063         try {
1064             reqModules = JDWP.VirtualMachine.AllModules.process(vm).modules;
1065         } catch (JDWPException exc) {
1066             throw exc.toJDIException();
1067         }
1068         ArrayList<ModuleReference> modules = new ArrayList<>();
1069         for (int i = 0; i < reqModules.length; i++) {
1070             long moduleRef = reqModules[i].ref();
1071             ModuleReference module = getModule(moduleRef);
1072             modules.add(module);
1073         }
1074         return modules;
1075     }
1076 
1077     private List<ReferenceType> retrieveClassesBySignature(String signature) {
1078         if ((vm.traceFlags & VirtualMachine.TRACE_REFTYPES) != 0) {
1079             vm.printTrace("Retrieving matching ReferenceTypes, sig=" + signature);
1080         }
1081         JDWP.VirtualMachine.ClassesBySignature.ClassInfo[] cinfos;
1082         try {
1083             cinfos = JDWP.VirtualMachine.ClassesBySignature.
1084                                       process(vm, signature).classes;
1085         } catch (JDWPException exc) {
1086             throw exc.toJDIException();
1087         }
1088 
1089         int count = cinfos.length;
1090         List<ReferenceType> list = new ArrayList<>(count);
1091 
1092         // Hold lock during processing to improve performance
1093         synchronized (this) {
1094             for (int i = 0; i < count; i++) {
1095                 JDWP.VirtualMachine.ClassesBySignature.ClassInfo ci =
1096                                                                cinfos[i];
1097                 ReferenceTypeImpl type = referenceType(ci.typeID,
1098                                                        ci.refTypeTag,
1099                                                        signature);
1100                 type.setStatus(ci.status);
1101                 list.add(type);
1102             }
1103         }
1104         return list;
1105     }
1106 
1107     private void retrieveAllClasses1_4() {
1108         JDWP.VirtualMachine.AllClasses.ClassInfo[] cinfos;
1109         try {
1110             cinfos = JDWP.VirtualMachine.AllClasses.process(vm).classes;
1111         } catch (JDWPException exc) {
1112             throw exc.toJDIException();
1113         }
1114 
1115         // Hold lock during processing to improve performance
1116         // and to have safe check/set of retrievedAllTypes
1117         synchronized (this) {
1118             if (!retrievedAllTypes) {
1119                 // Number of classes
1120                 int count = cinfos.length;
1121                 for (int i = 0; i < count; i++) {
1122                     JDWP.VirtualMachine.AllClasses.ClassInfo ci = cinfos[i];
1123                     ReferenceTypeImpl type = referenceType(ci.typeID,
1124                                                            ci.refTypeTag,
1125                                                            ci.signature);
1126                     type.setStatus(ci.status);
1127                 }
1128                 retrievedAllTypes = true;
1129             }
1130         }
1131     }
1132 
1133     private void retrieveAllClasses() {
1134         if ((vm.traceFlags & VirtualMachine.TRACE_REFTYPES) != 0) {
1135             vm.printTrace("Retrieving all ReferenceTypes");
1136         }
1137 
1138         if (!vm.canGet1_5LanguageFeatures()) {
1139             retrieveAllClasses1_4();
1140             return;
1141         }
1142 
1143         /*
1144          * To save time (assuming the caller will be
1145          * using then) we will get the generic sigs too.
1146          */
1147         JDWP.VirtualMachine.AllClassesWithGeneric.ClassInfo[] cinfos;
1148         try {
1149             cinfos = JDWP.VirtualMachine.AllClassesWithGeneric.process(vm).classes;
1150         } catch (JDWPException exc) {
1151             throw exc.toJDIException();
1152         }
1153 
1154         // Hold lock during processing to improve performance
1155         // and to have safe check/set of retrievedAllTypes
1156         synchronized (this) {
1157             if (!retrievedAllTypes) {
1158                 // Number of classes
1159                 int count = cinfos.length;
1160                 for (int i = 0; i < count; i++) {
1161                     JDWP.VirtualMachine.AllClassesWithGeneric.ClassInfo ci =
1162                                                                cinfos[i];
1163                     ReferenceTypeImpl type = referenceType(ci.typeID,
1164                                                            ci.refTypeTag,
1165                                                            ci.signature);
1166                     type.setGenericSignature(ci.genericSignature);
1167                     type.setStatus(ci.status);
1168                 }
1169                 retrievedAllTypes = true;
1170             }
1171         }
1172     }
1173 
1174     void sendToTarget(Packet packet) {
1175         target.send(packet);
1176     }
1177 
1178     void waitForTargetReply(Packet packet) {
1179         target.waitForReply(packet);
1180         /*
1181          * If any object disposes have been batched up, send them now.
1182          */
1183         processBatchedDisposes();
1184     }
1185 
1186     Type findBootType(String signature) throws ClassNotLoadedException {
1187         List<ReferenceType> types = retrieveClassesBySignature(signature);
1188         for (ReferenceType type : types) {
1189             if (type.classLoader() == null) {
1190                 return type;
1191             }
1192         }
1193         JNITypeParser parser = new JNITypeParser(signature);
1194         throw new ClassNotLoadedException(parser.typeName(),
1195                                          "Type " + parser.typeName() + " not loaded");
1196     }
1197 
1198     BooleanType theBooleanType() {
1199         if (theBooleanType == null) {
1200             synchronized(this) {
1201                 if (theBooleanType == null) {
1202                     theBooleanType = new BooleanTypeImpl(this);
1203                 }
1204             }
1205         }
1206         return theBooleanType;
1207     }
1208 
1209     ByteType theByteType() {
1210         if (theByteType == null) {
1211             synchronized(this) {
1212                 if (theByteType == null) {
1213                     theByteType = new ByteTypeImpl(this);
1214                 }
1215             }
1216         }
1217         return theByteType;
1218     }
1219 
1220     CharType theCharType() {
1221         if (theCharType == null) {
1222             synchronized(this) {
1223                 if (theCharType == null) {
1224                     theCharType = new CharTypeImpl(this);
1225                 }
1226             }
1227         }
1228         return theCharType;
1229     }
1230 
1231     ShortType theShortType() {
1232         if (theShortType == null) {
1233             synchronized(this) {
1234                 if (theShortType == null) {
1235                     theShortType = new ShortTypeImpl(this);
1236                 }
1237             }
1238         }
1239         return theShortType;
1240     }
1241 
1242     IntegerType theIntegerType() {
1243         if (theIntegerType == null) {
1244             synchronized(this) {
1245                 if (theIntegerType == null) {
1246                     theIntegerType = new IntegerTypeImpl(this);
1247                 }
1248             }
1249         }
1250         return theIntegerType;
1251     }
1252 
1253     LongType theLongType() {
1254         if (theLongType == null) {
1255             synchronized(this) {
1256                 if (theLongType == null) {
1257                     theLongType = new LongTypeImpl(this);
1258                 }
1259             }
1260         }
1261         return theLongType;
1262     }
1263 
1264     FloatType theFloatType() {
1265         if (theFloatType == null) {
1266             synchronized(this) {
1267                 if (theFloatType == null) {
1268                     theFloatType = new FloatTypeImpl(this);
1269                 }
1270             }
1271         }
1272         return theFloatType;
1273     }
1274 
1275     DoubleType theDoubleType() {
1276         if (theDoubleType == null) {
1277             synchronized(this) {
1278                 if (theDoubleType == null) {
1279                     theDoubleType = new DoubleTypeImpl(this);
1280                 }
1281             }
1282         }
1283         return theDoubleType;
1284     }
1285 
1286     VoidType theVoidType() {
1287         if (theVoidType == null) {
1288             synchronized(this) {
1289                 if (theVoidType == null) {
1290                     theVoidType = new VoidTypeImpl(this);
1291                 }
1292             }
1293         }
1294         return theVoidType;
1295     }
1296 
1297     PrimitiveType primitiveTypeMirror(byte tag) {
1298         switch (tag) {
1299             case JDWP.Tag.BOOLEAN:
1300                 return theBooleanType();
1301             case JDWP.Tag.BYTE:
1302                 return theByteType();
1303             case JDWP.Tag.CHAR:
1304                 return theCharType();
1305             case JDWP.Tag.SHORT:
1306                 return theShortType();
1307             case JDWP.Tag.INT:
1308                 return theIntegerType();
1309             case JDWP.Tag.LONG:
1310                 return theLongType();
1311             case JDWP.Tag.FLOAT:
1312                 return theFloatType();
1313             case JDWP.Tag.DOUBLE:
1314                 return theDoubleType();
1315             default:
1316                 throw new IllegalArgumentException("Unrecognized primitive tag " + tag);
1317         }
1318     }
1319 
1320     private void processBatchedDisposes() {
1321         if (shutdown) {
1322             return;
1323         }
1324 
1325         JDWP.VirtualMachine.DisposeObjects.Request[] requests = null;
1326         synchronized(batchedDisposeRequests) {
1327             int size = batchedDisposeRequests.size();
1328             if (size >= DISPOSE_THRESHOLD) {
1329                 if ((traceFlags & TRACE_OBJREFS) != 0) {
1330                     printTrace("Dispose threshold reached. Will dispose "
1331                                + size + " object references...");
1332                 }
1333                 requests = new JDWP.VirtualMachine.DisposeObjects.Request[size];
1334                 for (int i = 0; i < requests.length; i++) {
1335                     SoftObjectReference ref = batchedDisposeRequests.get(i);
1336                     if ((traceFlags & TRACE_OBJREFS) != 0) {
1337                         printTrace("Disposing object " + ref.key().longValue() +
1338                                    " (ref count = " + ref.count() + ")");
1339                     }
1340 
1341                     // This is kludgy. We temporarily re-create an object
1342                     // reference so that we can correctly pass its id to the
1343                     // JDWP command.
1344                     requests[i] =
1345                         new JDWP.VirtualMachine.DisposeObjects.Request(
1346                             new ObjectReferenceImpl(this, ref.key().longValue()),
1347                             ref.count());
1348                 }
1349                 batchedDisposeRequests.clear();
1350             }
1351         }
1352         if (requests != null) {
1353             try {
1354                 JDWP.VirtualMachine.DisposeObjects.process(vm, requests);
1355             } catch (JDWPException exc) {
1356                 throw exc.toJDIException();
1357             }
1358         }
1359     }
1360 
1361     private void batchForDispose(SoftObjectReference ref) {
1362         if ((traceFlags & TRACE_OBJREFS) != 0) {
1363             printTrace("Batching object " + ref.key().longValue() +
1364                        " for dispose (ref count = " + ref.count() + ")");
1365         }
1366         batchedDisposeRequests.add(ref);
1367     }
1368 
1369     private void processQueue() {
1370         Reference<?> ref;
1371         //if ((traceFlags & TRACE_OBJREFS) != 0) {
1372         //    printTrace("Checking for softly reachable objects");
1373         //}
1374         boolean found = false;
1375         while ((ref = referenceQueue.poll()) != null) {
1376             SoftObjectReference softRef = (SoftObjectReference)ref;
1377             removeObjectMirror(softRef);
1378             batchForDispose(softRef);
1379             found = true;
1380         }
1381 
1382         if (found) {
1383             // If we batched any ObjectReferences for disposing, we can dispose them now.
1384             processBatchedDisposes();
1385         }
1386     }
1387 
1388     synchronized ObjectReferenceImpl objectMirror(long id, int tag) {
1389 
1390         // Handle any queue elements that are not strongly reachable
1391         processQueue();
1392 
1393         if (id == 0) {
1394             return null;
1395         }
1396         ObjectReferenceImpl object = null;
1397         Long key = id;
1398 
1399         /*
1400          * Attempt to retrieve an existing object reference
1401          */
1402         SoftObjectReference ref = objectsByID.get(key);
1403         if (ref != null) {
1404             object = ref.object();
1405         }
1406 
1407         /*
1408          * If the object wasn't in the table, or it's soft reference was
1409          * cleared, create a new instance.
1410          */
1411         if (object == null) {
1412             switch (tag) {
1413                 case JDWP.Tag.OBJECT:
1414                     object = new ObjectReferenceImpl(vm, id);
1415                     break;
1416                 case JDWP.Tag.STRING:
1417                     object = new StringReferenceImpl(vm, id);
1418                     break;
1419                 case JDWP.Tag.ARRAY:
1420                     object = new ArrayReferenceImpl(vm, id);
1421                     break;
1422                 case JDWP.Tag.THREAD:
1423                     ThreadReferenceImpl thread =
1424                         new ThreadReferenceImpl(vm, id);
1425                     thread.addListener(this);
1426                     object = thread;
1427                     break;
1428                 case JDWP.Tag.THREAD_GROUP:
1429                     object = new ThreadGroupReferenceImpl(vm, id);
1430                     break;
1431                 case JDWP.Tag.CLASS_LOADER:
1432                     object = new ClassLoaderReferenceImpl(vm, id);
1433                     break;
1434                 case JDWP.Tag.CLASS_OBJECT:
1435                     object = new ClassObjectReferenceImpl(vm, id);
1436                     break;
1437                 default:
1438                     throw new IllegalArgumentException("Invalid object tag: " + tag);
1439             }
1440             ref = new SoftObjectReference(key, object, referenceQueue);
1441 
1442             /*
1443              * If there was no previous entry in the table, we add one here
1444              * If the previous entry was cleared, we replace it here.
1445              */
1446             objectsByID.put(key, ref);
1447             if ((traceFlags & TRACE_OBJREFS) != 0) {
1448                 printTrace("Creating new " +
1449                            object.getClass().getName() + " (id = " + id + ")");
1450             }
1451         } else {
1452             ref.incrementCount();
1453         }
1454 
1455         return object;
1456     }
1457 
1458     private synchronized void removeObjectMirror(SoftObjectReference ref) {
1459         /*
1460          * This will remove the soft reference if it has not been
1461          * replaced in the cache.
1462          */
1463         objectsByID.remove(ref.key());
1464     }
1465 
1466     ObjectReferenceImpl objectMirror(long id) {
1467         return objectMirror(id, JDWP.Tag.OBJECT);
1468     }
1469 
1470     StringReferenceImpl stringMirror(long id) {
1471         return (StringReferenceImpl)objectMirror(id, JDWP.Tag.STRING);
1472     }
1473 
1474     ArrayReferenceImpl arrayMirror(long id) {
1475        return (ArrayReferenceImpl)objectMirror(id, JDWP.Tag.ARRAY);
1476     }
1477 
1478     ThreadReferenceImpl threadMirror(long id) {
1479         return (ThreadReferenceImpl)objectMirror(id, JDWP.Tag.THREAD);
1480     }
1481 
1482     ThreadGroupReferenceImpl threadGroupMirror(long id) {
1483         return (ThreadGroupReferenceImpl)objectMirror(id,
1484                                                       JDWP.Tag.THREAD_GROUP);
1485     }
1486 
1487     ClassLoaderReferenceImpl classLoaderMirror(long id) {
1488         return (ClassLoaderReferenceImpl)objectMirror(id,
1489                                                       JDWP.Tag.CLASS_LOADER);
1490     }
1491 
1492     ClassObjectReferenceImpl classObjectMirror(long id) {
1493         return (ClassObjectReferenceImpl)objectMirror(id,
1494                                                       JDWP.Tag.CLASS_OBJECT);
1495     }
1496 
1497     ModuleReferenceImpl moduleMirror(long id) {
1498         return (ModuleReferenceImpl)getModule(id);
1499     }
1500 
1501     /*
1502      * Implementation of PathSearchingVirtualMachine
1503      */
1504     private JDWP.VirtualMachine.ClassPaths getClasspath() {
1505         if (pathInfo == null) {
1506             try {
1507                 pathInfo = JDWP.VirtualMachine.ClassPaths.process(vm);
1508             } catch (JDWPException exc) {
1509                 throw exc.toJDIException();
1510             }
1511         }
1512         return pathInfo;
1513     }
1514 
1515    public List<String> classPath() {
1516        return Arrays.asList(getClasspath().classpaths);
1517    }
1518 
1519    public List<String> bootClassPath() {
1520        return Collections.emptyList();
1521    }
1522 
1523    public String baseDirectory() {
1524        return getClasspath().baseDir;
1525    }
1526 
1527     public void setDefaultStratum(String stratum) {
1528         defaultStratum = stratum;
1529         if (stratum == null) {
1530             stratum = "";
1531         }
1532         try {
1533             JDWP.VirtualMachine.SetDefaultStratum.process(vm,
1534                                                           stratum);
1535         } catch (JDWPException exc) {
1536             throw exc.toJDIException();
1537         }
1538     }
1539 
1540     public String getDefaultStratum() {
1541         return defaultStratum;
1542     }
1543 
1544     ThreadGroup threadGroupForJDI() {
1545         return threadGroupForJDI;
1546     }
1547 
1548    private static class SoftObjectReference extends SoftReference<ObjectReferenceImpl> {
1549        int count;
1550        Long key;
1551 
1552        SoftObjectReference(Long key, ObjectReferenceImpl mirror,
1553                            ReferenceQueue<ObjectReferenceImpl> queue) {
1554            super(mirror, queue);
1555            this.count = 1;
1556            this.key = key;
1557        }
1558 
1559        int count() {
1560            return count;
1561        }
1562 
1563        void incrementCount() {
1564            count++;
1565        }
1566 
1567        Long key() {
1568            return key;
1569        }
1570 
1571        ObjectReferenceImpl object() {
1572            return get();
1573        }
1574    }
1575 }