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     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             @SuppressWarnings("rawtypes")
 380             Map.Entry<?, ?> entry = (Map.Entry)it.next();
 381             ReferenceTypeImpl refType = (ReferenceTypeImpl)entry.getKey();
 382             validateMirror(refType);
 383             defs[i] = new JDWP.VirtualMachine.RedefineClasses
 384                        .ClassDef(refType, (byte[])entry.getValue());
 385         }
 386 
 387         // flush caches and disable caching until the next suspend
 388         vm.state().thaw();
 389 
 390         try {
 391             JDWP.VirtualMachine.RedefineClasses.
 392                 process(vm, defs);
 393         } catch (JDWPException exc) {
 394             switch (exc.errorCode()) {
 395             case JDWP.Error.INVALID_CLASS_FORMAT :
 396                 throw new ClassFormatError(
 397                     "class not in class file format");
 398             case JDWP.Error.CIRCULAR_CLASS_DEFINITION :
 399                 throw new ClassCircularityError(
 400                     "circularity has been detected while initializing a class");
 401             case JDWP.Error.FAILS_VERIFICATION :
 402                 throw new VerifyError(
 403                     "verifier detected internal inconsistency or security problem");
 404             case JDWP.Error.UNSUPPORTED_VERSION :
 405                 throw new UnsupportedClassVersionError(
 406                     "version numbers of class are not supported");
 407             case JDWP.Error.ADD_METHOD_NOT_IMPLEMENTED:
 408                 throw new UnsupportedOperationException(
 409                     "add method not implemented");
 410             case JDWP.Error.SCHEMA_CHANGE_NOT_IMPLEMENTED :
 411                 throw new UnsupportedOperationException(
 412                     "schema change not implemented");
 413             case JDWP.Error.HIERARCHY_CHANGE_NOT_IMPLEMENTED:
 414                 throw new UnsupportedOperationException(
 415                     "hierarchy change not implemented");
 416             case JDWP.Error.DELETE_METHOD_NOT_IMPLEMENTED :
 417                 throw new UnsupportedOperationException(
 418                     "delete method not implemented");
 419             case JDWP.Error.CLASS_MODIFIERS_CHANGE_NOT_IMPLEMENTED:
 420                 throw new UnsupportedOperationException(
 421                     "changes to class modifiers not implemented");
 422             case JDWP.Error.METHOD_MODIFIERS_CHANGE_NOT_IMPLEMENTED :
 423                 throw new UnsupportedOperationException(
 424                     "changes to method modifiers not implemented");
 425             case JDWP.Error.CLASS_ATTRIBUTE_CHANGE_NOT_IMPLEMENTED :
 426                 throw new UnsupportedOperationException(
 427                     "changes to class attribute not implemented");
 428             case JDWP.Error.NAMES_DONT_MATCH :
 429                 throw new NoClassDefFoundError(
 430                     "class names do not match");
 431             default:
 432                 throw exc.toJDIException();
 433             }
 434         }
 435 
 436         // Delete any record of the breakpoints
 437         List<BreakpointRequest> toDelete = new ArrayList<>();
 438         EventRequestManager erm = eventRequestManager();
 439         it = erm.breakpointRequests().iterator();
 440         while (it.hasNext()) {
 441             BreakpointRequest req = (BreakpointRequest)it.next();
 442             if (classToBytes.containsKey(req.location().declaringType())) {
 443                 toDelete.add(req);
 444             }
 445         }
 446         erm.deleteEventRequests(toDelete);
 447 
 448         // Invalidate any information cached for the classes just redefined.
 449         it = classToBytes.keySet().iterator();
 450         while (it.hasNext()) {
 451             ReferenceTypeImpl rti = (ReferenceTypeImpl)it.next();
 452             rti.noticeRedefineClass();
 453         }
 454     }
 455 
 456     public List<ThreadReference> allThreads() {
 457         validateVM();
 458         return state.allThreads();
 459     }
 460 
 461     public List<ThreadGroupReference> topLevelThreadGroups() {
 462         validateVM();
 463         return state.topLevelThreadGroups();
 464     }
 465 
 466     /*
 467      * Sends a command to the back end which is defined to do an
 468      * implicit vm-wide resume. The VM can no longer be considered
 469      * suspended, so certain cached data must be invalidated.
 470      */
 471     PacketStream sendResumingCommand(CommandSender sender) {
 472         return state.thawCommand(sender);
 473     }
 474 
 475     /*
 476      * The VM has been suspended. Additional caching can be done
 477      * as long as there are no pending resumes.
 478      */
 479     void notifySuspend() {
 480         state.freeze();
 481     }
 482 
 483     public void suspend() {
 484         validateVM();
 485         try {
 486             JDWP.VirtualMachine.Suspend.process(vm);
 487         } catch (JDWPException exc) {
 488             throw exc.toJDIException();
 489         }
 490         notifySuspend();
 491     }
 492 
 493     public void resume() {
 494         validateVM();
 495         CommandSender sender =
 496             new CommandSender() {
 497                 public PacketStream send() {
 498                     return JDWP.VirtualMachine.Resume.enqueueCommand(vm);
 499                 }
 500         };
 501         try {
 502             PacketStream stream = state.thawCommand(sender);
 503             JDWP.VirtualMachine.Resume.waitForReply(vm, stream);
 504         } catch (VMDisconnectedException exc) {
 505             /*
 506              * If the debugger makes a VMDeathRequest with SUSPEND_ALL,
 507              * then when it does an EventSet.resume after getting the
 508              * VMDeathEvent, the normal flow of events is that the
 509              * BE shuts down, but the waitForReply comes back ok.  In this
 510              * case, the run loop in TargetVM that is waiting for a packet
 511              * gets an EOF because the socket closes. It generates a
 512              * VMDisconnectedEvent and everyone is happy.
 513              * However, sometimes, the BE gets shutdown before this
 514              * waitForReply completes.  In this case, TargetVM.waitForReply
 515              * gets awakened with no reply and so gens a VMDisconnectedException
 516              * which is not what we want.  It might be possible to fix this
 517              * in the BE, but it is ok to just ignore the VMDisconnectedException
 518              * here.  This will allow the VMDisconnectedEvent to be generated
 519              * correctly.  And, if the debugger should happen to make another
 520              * request, it will get a VMDisconnectedException at that time.
 521              */
 522         } catch (JDWPException exc) {
 523             switch (exc.errorCode()) {
 524                 case JDWP.Error.VM_DEAD:
 525                     return;
 526                 default:
 527                     throw exc.toJDIException();
 528             }
 529         }
 530     }
 531 
 532     public EventQueue eventQueue() {
 533         /*
 534          * No VM validation here. We allow access to the event queue
 535          * after disconnection, so that there is access to the terminating
 536          * events.
 537          */
 538         return eventQueue;
 539     }
 540 
 541     public EventRequestManager eventRequestManager() {
 542         validateVM();
 543         return eventRequestManager;
 544     }
 545 
 546     EventRequestManagerImpl eventRequestManagerImpl() {
 547         return eventRequestManager;
 548     }
 549 
 550     public BooleanValue mirrorOf(boolean value) {
 551         validateVM();
 552         return new BooleanValueImpl(this,value);
 553     }
 554 
 555     public ByteValue mirrorOf(byte value) {
 556         validateVM();
 557         return new ByteValueImpl(this,value);
 558     }
 559 
 560     public CharValue mirrorOf(char value) {
 561         validateVM();
 562         return new CharValueImpl(this,value);
 563     }
 564 
 565     public ShortValue mirrorOf(short value) {
 566         validateVM();
 567         return new ShortValueImpl(this,value);
 568     }
 569 
 570     public IntegerValue mirrorOf(int value) {
 571         validateVM();
 572         return new IntegerValueImpl(this,value);
 573     }
 574 
 575     public LongValue mirrorOf(long value) {
 576         validateVM();
 577         return new LongValueImpl(this,value);
 578     }
 579 
 580     public FloatValue mirrorOf(float value) {
 581         validateVM();
 582         return new FloatValueImpl(this,value);
 583     }
 584 
 585     public DoubleValue mirrorOf(double value) {
 586         validateVM();
 587         return new DoubleValueImpl(this,value);
 588     }
 589 
 590     public StringReference mirrorOf(String value) {
 591         validateVM();
 592         try {
 593             return JDWP.VirtualMachine.CreateString.
 594                 process(vm, value).stringObject;
 595         } catch (JDWPException exc) {
 596             throw exc.toJDIException();
 597         }
 598     }
 599 
 600     public VoidValue mirrorOfVoid() {
 601         if (voidVal == null) {
 602             voidVal = new VoidValueImpl(this);
 603         }
 604         return voidVal;
 605     }
 606 
 607     public long[] instanceCounts(List<? extends ReferenceType> classes) {
 608         if (!canGetInstanceInfo()) {
 609             throw new UnsupportedOperationException(
 610                 "target does not support getting instances");
 611         }
 612         long[] retValue ;
 613         ReferenceTypeImpl[] rtArray = new ReferenceTypeImpl[classes.size()];
 614         int ii = 0;
 615         for (ReferenceType rti: classes) {
 616             validateMirror(rti);
 617             rtArray[ii++] = (ReferenceTypeImpl)rti;
 618         }
 619         try {
 620             retValue = JDWP.VirtualMachine.InstanceCounts.
 621                                 process(vm, rtArray).counts;
 622         } catch (JDWPException exc) {
 623             throw exc.toJDIException();
 624         }
 625 
 626         return retValue;
 627     }
 628 
 629     public void dispose() {
 630         validateVM();
 631         shutdown = true;
 632         try {
 633             JDWP.VirtualMachine.Dispose.process(vm);
 634         } catch (JDWPException exc) {
 635             throw exc.toJDIException();
 636         }
 637         target.stopListening();
 638     }
 639 
 640     public void exit(int exitCode) {
 641         validateVM();
 642         shutdown = true;
 643         try {
 644             JDWP.VirtualMachine.Exit.process(vm, exitCode);
 645         } catch (JDWPException exc) {
 646             throw exc.toJDIException();
 647         }
 648         target.stopListening();
 649     }
 650 
 651     public Process process() {
 652         validateVM();
 653         return process;
 654     }
 655 
 656     private JDWP.VirtualMachine.Version versionInfo() {
 657        try {
 658            if (versionInfo == null) {
 659                // Need not be synchronized since it is static information
 660                versionInfo = JDWP.VirtualMachine.Version.process(vm);
 661            }
 662            return versionInfo;
 663        } catch (JDWPException exc) {
 664            throw exc.toJDIException();
 665        }
 666     }
 667 
 668     public String description() {
 669         validateVM();
 670 
 671         return MessageFormat.format(vmManager.getString("version_format"),
 672                                     "" + vmManager.majorInterfaceVersion(),
 673                                     "" + vmManager.minorInterfaceVersion(),
 674                                      versionInfo().description);
 675     }
 676 
 677     public String version() {
 678         validateVM();
 679         return versionInfo().vmVersion;
 680     }
 681 
 682     public String name() {
 683         validateVM();
 684         return versionInfo().vmName;
 685     }
 686 
 687     public boolean canWatchFieldModification() {
 688         validateVM();
 689         return capabilities().canWatchFieldModification;
 690     }
 691 
 692     public boolean canWatchFieldAccess() {
 693         validateVM();
 694         return capabilities().canWatchFieldAccess;
 695     }
 696 
 697     public boolean canGetBytecodes() {
 698         validateVM();
 699         return capabilities().canGetBytecodes;
 700     }
 701 
 702     public boolean canGetSyntheticAttribute() {
 703         validateVM();
 704         return capabilities().canGetSyntheticAttribute;
 705     }
 706 
 707     public boolean canGetOwnedMonitorInfo() {
 708         validateVM();
 709         return capabilities().canGetOwnedMonitorInfo;
 710     }
 711 
 712     public boolean canGetCurrentContendedMonitor() {
 713         validateVM();
 714         return capabilities().canGetCurrentContendedMonitor;
 715     }
 716 
 717     public boolean canGetMonitorInfo() {
 718         validateVM();
 719         return capabilities().canGetMonitorInfo;
 720     }
 721 
 722     private boolean hasNewCapabilities() {
 723         return versionInfo().jdwpMajor > 1 ||
 724             versionInfo().jdwpMinor >= 4;
 725     }
 726 
 727     boolean canGet1_5LanguageFeatures() {
 728         return versionInfo().jdwpMajor > 1 ||
 729             versionInfo().jdwpMinor >= 5;
 730     }
 731 
 732     public boolean canUseInstanceFilters() {
 733         validateVM();
 734         return hasNewCapabilities() &&
 735             capabilitiesNew().canUseInstanceFilters;
 736     }
 737 
 738     public boolean canRedefineClasses() {
 739         validateVM();
 740         return hasNewCapabilities() &&
 741             capabilitiesNew().canRedefineClasses;
 742     }
 743 
 744     @Deprecated(since="15")
 745     public boolean canAddMethod() {
 746         validateVM();
 747         return hasNewCapabilities() &&
 748             capabilitiesNew().canAddMethod;
 749     }
 750 
 751     @Deprecated(since="15")
 752     public boolean canUnrestrictedlyRedefineClasses() {
 753         validateVM();
 754         return hasNewCapabilities() &&
 755             capabilitiesNew().canUnrestrictedlyRedefineClasses;
 756     }
 757 
 758     public boolean canPopFrames() {
 759         validateVM();
 760         return hasNewCapabilities() &&
 761             capabilitiesNew().canPopFrames;
 762     }
 763 
 764     public boolean canGetMethodReturnValues() {
 765         return versionInfo().jdwpMajor > 1 ||
 766             versionInfo().jdwpMinor >= 6;
 767     }
 768 
 769     public boolean canGetInstanceInfo() {
 770         if (versionInfo().jdwpMajor > 1 ||
 771             versionInfo().jdwpMinor >= 6) {
 772             validateVM();
 773             return hasNewCapabilities() &&
 774                 capabilitiesNew().canGetInstanceInfo;
 775         } else {
 776             return false;
 777         }
 778     }
 779 
 780     public boolean canUseSourceNameFilters() {
 781         return versionInfo().jdwpMajor > 1 ||
 782             versionInfo().jdwpMinor >= 6;
 783     }
 784 
 785     public boolean canForceEarlyReturn() {
 786         validateVM();
 787         return hasNewCapabilities() &&
 788             capabilitiesNew().canForceEarlyReturn;
 789     }
 790 
 791     public boolean canBeModified() {
 792         return true;
 793     }
 794 
 795     public boolean canGetSourceDebugExtension() {
 796         validateVM();
 797         return hasNewCapabilities() &&
 798             capabilitiesNew().canGetSourceDebugExtension;
 799     }
 800 
 801     public boolean canGetClassFileVersion() {
 802         return versionInfo().jdwpMajor > 1 ||
 803             versionInfo().jdwpMinor >= 6;
 804     }
 805 
 806     public boolean canGetConstantPool() {
 807         validateVM();
 808         return hasNewCapabilities() &&
 809             capabilitiesNew().canGetConstantPool;
 810     }
 811 
 812     public boolean canRequestVMDeathEvent() {
 813         validateVM();
 814         return hasNewCapabilities() &&
 815             capabilitiesNew().canRequestVMDeathEvent;
 816     }
 817 
 818     public boolean canRequestMonitorEvents() {
 819         validateVM();
 820         return hasNewCapabilities() &&
 821             capabilitiesNew().canRequestMonitorEvents;
 822     }
 823 
 824     public boolean canGetMonitorFrameInfo() {
 825         validateVM();
 826         return hasNewCapabilities() &&
 827             capabilitiesNew().canGetMonitorFrameInfo;
 828     }
 829 
 830     public boolean canGetModuleInfo() {
 831         validateVM();
 832         return versionInfo().jdwpMajor >= 9;
 833     }
 834 







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