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