| 1 11 package org.eclipse.jdi.internal; 12 13 14 import java.io.ByteArrayOutputStream ; 15 import java.io.DataInputStream ; 16 import java.io.DataOutputStream ; 17 import java.io.IOException ; 18 import java.lang.reflect.Field ; 19 import java.lang.reflect.Modifier ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import org.eclipse.jdi.Bootstrap; 28 import org.eclipse.jdi.internal.connect.PacketReceiveManager; 29 import org.eclipse.jdi.internal.connect.PacketSendManager; 30 import org.eclipse.jdi.internal.event.EventQueueImpl; 31 import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket; 32 import org.eclipse.jdi.internal.jdwp.JdwpObjectID; 33 import org.eclipse.jdi.internal.jdwp.JdwpReferenceTypeID; 34 import org.eclipse.jdi.internal.jdwp.JdwpReplyPacket; 35 import org.eclipse.jdi.internal.request.EventRequestManagerImpl; 36 37 import com.ibm.icu.text.MessageFormat; 38 import com.sun.jdi.BooleanValue; 39 import com.sun.jdi.ByteValue; 40 import com.sun.jdi.CharValue; 41 import com.sun.jdi.DoubleValue; 42 import com.sun.jdi.FloatValue; 43 import com.sun.jdi.IntegerValue; 44 import com.sun.jdi.LongValue; 45 import com.sun.jdi.ObjectCollectedException; 46 import com.sun.jdi.ShortValue; 47 import com.sun.jdi.StringReference; 48 import com.sun.jdi.VMDisconnectedException; 49 import com.sun.jdi.VirtualMachine; 50 import com.sun.jdi.VoidValue; 51 import com.sun.jdi.connect.spi.Connection; 52 import com.sun.jdi.event.EventQueue; 53 import com.sun.jdi.request.EventRequestManager; 54 55 61 public class VirtualMachineImpl extends MirrorImpl implements VirtualMachine, org.eclipse.jdi.hcr.VirtualMachine, org.eclipse.jdi.VirtualMachine { 62 63 public static final byte HCR_RELOAD_SUCCESS = 0; 64 public static final byte HCR_RELOAD_FAILURE = 1; 65 public static final byte HCR_RELOAD_IGNORED = 2; 66 67 68 private static final int HCR_CAN_RELOAD_CLASSES = 0; 69 private static final int HCR_CAN_GET_CLASS_VERSION = 1; 70 private static final int HCR_CAN_DO_RETURN = 2; 71 private static final int HCR_CAN_REENTER_ON_EXIT = 3; 72 73 protected static final String JAVA_STRATUM_NAME= "Java"; 75 76 private int fRequestTimeout; 77 78 79 private static Map fgHCRResultMap = null; 80 81 82 private EventRequestManagerImpl fEventReqMgr; 83 84 private EventQueueImpl fEventQueue; 85 86 87 private Process fLaunchedProcess; 88 89 94 private ValueCache fCachedReftypes = new ValueCache(); 95 private ValueCache fCachedObjects = new ValueCache(); 96 97 98 private String fVersionDescription = null; private int fJdwpMajorVersion; 100 private int fJdwpMinorVersion; 101 private String fVMVersion; private String fVMName; private boolean fGotIDSizes = false; 104 private int fFieldIDSize; 105 private int fMethodIDSize; 106 private int fObjectIDSize; 107 private int fReferenceTypeIDSize; 108 private int fFrameIDSize; 109 110 private boolean fGotCapabilities = false; 111 private boolean fCanWatchFieldModification; 112 private boolean fCanWatchFieldAccess; 113 private boolean fCanGetBytecodes; 114 private boolean fCanGetSyntheticAttribute; 115 private boolean fCanGetOwnedMonitorInfo; 116 private boolean fCanGetCurrentContendedMonitor; 117 private boolean fCanGetMonitorInfo; 118 private boolean fCanRedefineClasses; 119 private boolean fCanAddMethod; 120 private boolean fCanUnrestrictedlyRedefineClasses; 121 private boolean fCanPopFrames; 122 private boolean fCanUseInstanceFilters; 123 private boolean fCanGetSourceDebugExtension; 124 private boolean fCanRequestVMDeathEvent; 125 private boolean fCanSetDefaultStratum; 126 private boolean fCanGetInstanceInfo; 127 private boolean fCanGetConstantPool; 128 private boolean fCanUseSourceNameFilters; 129 private boolean fCanForceEarlyReturn; 130 private boolean fCanRequestMonitorEvents; 131 private boolean fCanGetMonitorFrameInfo; 132 private boolean[] fHcrCapabilities = null; 133 134 137 private BooleanTypeImpl fBooleanType; 138 private ByteTypeImpl fByteType; 139 private CharTypeImpl fCharType; 140 private DoubleTypeImpl fDoubleType; 141 private FloatTypeImpl fFloatType; 142 private IntegerTypeImpl fIntegerType; 143 private LongTypeImpl fLongType; 144 private ShortTypeImpl fShortType; 145 146 149 private boolean fIsDisconnected = false; 150 151 154 private String fDefaultStratum; 155 private PacketReceiveManager fPacketReceiveManager; 156 private PacketSendManager fPacketSendManager; 157 158 161 public VirtualMachineImpl(Connection connection) { 162 super("VirtualMachine"); fEventReqMgr = new EventRequestManagerImpl(this); 164 fEventQueue = new EventQueueImpl(this); 165 fRequestTimeout = ((VirtualMachineManagerImpl) Bootstrap.virtualMachineManager()).getGlobalRequestTimeout(); 166 167 fPacketReceiveManager = new PacketReceiveManager(connection, this); 168 Thread receiveThread = new Thread (fPacketReceiveManager, JDIMessages.VirtualMachineImpl_0); 169 receiveThread.setDaemon(true); 170 fPacketReceiveManager.setPartnerThread(receiveThread); 171 receiveThread.start(); 172 173 fPacketSendManager = new PacketSendManager(connection); 174 Thread sendThread = new Thread (fPacketSendManager, JDIMessages.VirtualMachineImpl_1); 175 sendThread.setDaemon(true); 176 fPacketReceiveManager.setPartnerThread(sendThread); 177 sendThread.start(); 178 } 179 180 183 public final int fieldIDSize() { 184 return fFieldIDSize; 185 } 186 187 190 public final int methodIDSize() { 191 return fMethodIDSize; 192 } 193 194 197 public final int objectIDSize() { 198 return fObjectIDSize; 199 } 200 201 204 public final int referenceTypeIDSize() { 205 return fReferenceTypeIDSize; 206 } 207 208 211 public final int frameIDSize() { 212 return fFrameIDSize; 213 } 214 215 218 public ReferenceTypeImpl getCachedMirror(JdwpReferenceTypeID ID) { 219 return (ReferenceTypeImpl)fCachedReftypes.get(ID); 220 } 221 222 225 public ObjectReferenceImpl getCachedMirror(JdwpObjectID ID) { 226 return (ObjectReferenceImpl)fCachedObjects.get(ID); 227 } 228 229 232 public void addCachedMirror(ReferenceTypeImpl mirror) { 233 fCachedReftypes.put(mirror.getRefTypeID(), mirror); 234 } 238 239 242 public void addCachedMirror(ObjectReferenceImpl mirror) { 243 fCachedObjects.put(mirror.getObjectID(), mirror); 244 } 245 246 249 public void flushStoredJdwpResults() { 250 Iterator iter = fCachedReftypes.values().iterator(); 252 while (iter.hasNext()) { 253 ReferenceTypeImpl refType = (ReferenceTypeImpl)iter.next(); 254 refType.flushStoredJdwpResults(); 255 } 256 257 fVersionDescription = null; 258 fGotIDSizes = false; 259 fHcrCapabilities = null; 260 } 261 262 267 public final void removeKnownRefType(String signature) { 268 List refTypeList = classesBySignature(signature); 269 if (refTypeList.isEmpty()) 270 return; 271 272 if (refTypeList.size() == 1) { 275 ReferenceTypeImpl refType = (ReferenceTypeImpl)refTypeList.get(0); 276 refType.flushStoredJdwpResults(); 277 fCachedReftypes.remove(refType.getRefTypeID()); 278 return; 279 } 280 281 Iterator iter = refTypeList.iterator(); 283 while (iter.hasNext()) { 284 ReferenceTypeImpl refType = (ReferenceTypeImpl)iter.next(); 285 boolean prepared= false; 286 try { 287 prepared= refType.isPrepared(); 288 } catch (ObjectCollectedException exception) { 289 } 291 if (!prepared) { 292 refType.flushStoredJdwpResults(); 293 iter.remove(); 294 fCachedReftypes.remove(refType.getRefTypeID()); 295 } 296 } 297 } 298 299 302 public void checkHCRSupported() throws UnsupportedOperationException { 303 if (!isHCRSupported()) 304 throw new UnsupportedOperationException (MessageFormat.format(JDIMessages.VirtualMachineImpl_Target_VM__0__does_not_support_Hot_Code_Replacement_1, new String []{name()})); 305 } 306 307 310 public boolean isHCRSupported() throws UnsupportedOperationException { 311 return name().equals("j9"); } 313 314 317 public final PacketReceiveManager packetReceiveManager() { 318 return fPacketReceiveManager; 319 } 320 321 324 public final PacketSendManager packetSendManager() { 325 330 if (!fGotIDSizes) { 331 getIDSizes(); 332 if (!fGotIDSizes) { disconnectVM(); 334 throw new VMDisconnectedException(JDIMessages.VirtualMachineImpl_Failed_to_get_ID_sizes_2); 335 } 336 337 eventRequestManagerImpl().enableInternalClasUnloadEvent(); 340 } 341 342 return fPacketSendManager; 343 } 344 345 349 public List allClasses() { 350 initJdwpRequest(); 352 try { 353 boolean withGenericSignature= virtualMachineImpl().isJdwpVersionGreaterOrEqual(1, 5); 354 int jdwpCommand= withGenericSignature ? JdwpCommandPacket.VM_ALL_CLASSES_WITH_GENERIC : JdwpCommandPacket.VM_ALL_CLASSES; 355 JdwpReplyPacket replyPacket = requestVM(jdwpCommand); 356 defaultReplyErrorHandler(replyPacket.errorCode()); 357 DataInputStream replyData = replyPacket.dataInStream(); 358 int nrOfElements = readInt("elements", replyData); List elements = new ArrayList (nrOfElements); 360 for (int i = 0; i < nrOfElements; i++) { 361 ReferenceTypeImpl elt = ReferenceTypeImpl.readWithTypeTagAndSignature(this, withGenericSignature, replyData); 362 if (elt == null) { 363 continue; 364 } 365 readInt("status", ReferenceTypeImpl.classStatusStrings(), replyData); elements.add(elt); 367 } 368 return elements; 369 } catch (IOException e) { 370 defaultIOExceptionHandler(e); 371 return null; 372 } finally { 373 handledJdwpRequest(); 374 } 375 376 } 377 378 381 protected final Iterator allRefTypes() { 382 return allClasses().iterator(); 383 } 384 385 388 protected final Iterator allCachedRefTypes() { 389 return fCachedReftypes.values().iterator(); 390 } 391 392 396 public List allThreads() { 397 initJdwpRequest(); 399 try { 400 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.VM_ALL_THREADS); 401 defaultReplyErrorHandler(replyPacket.errorCode()); 402 DataInputStream replyData = replyPacket.dataInStream(); 403 int nrOfElements = readInt("elements", replyData); List elements = new ArrayList (nrOfElements); 405 for (int i = 0; i < nrOfElements; i++) { 406 ThreadReferenceImpl elt = ThreadReferenceImpl.read(this, replyData); 407 if (elt == null) { 408 continue; 409 } 410 elements.add(elt); 411 } 412 return elements; 413 } catch (IOException e) { 414 defaultIOExceptionHandler(e); 415 return null; 416 } finally { 417 handledJdwpRequest(); 418 } 419 } 420 421 424 public void getCapabilities() { 425 if (fGotCapabilities) 426 return; 427 428 int command = JdwpCommandPacket.VM_CAPABILITIES; 429 if (isJdwpVersionGreaterOrEqual(1, 4)) { 430 command = JdwpCommandPacket.VM_CAPABILITIES_NEW; 431 } 432 433 initJdwpRequest(); 434 try { 435 JdwpReplyPacket replyPacket = requestVM(command); 436 defaultReplyErrorHandler(replyPacket.errorCode()); 437 DataInputStream replyData = replyPacket.dataInStream(); 438 439 fCanWatchFieldModification = readBoolean("watch field modification", replyData); fCanWatchFieldAccess = readBoolean("watch field access", replyData); fCanGetBytecodes = readBoolean("get bytecodes", replyData); fCanGetSyntheticAttribute = readBoolean("synth. attr", replyData); fCanGetOwnedMonitorInfo = readBoolean("owned monitor info", replyData); fCanGetCurrentContendedMonitor = readBoolean("curr. contended monitor", replyData); fCanGetMonitorInfo = readBoolean("monitor info", replyData); if (command == JdwpCommandPacket.VM_CAPABILITIES_NEW) { 447 fCanRedefineClasses = readBoolean("redefine classes", replyData); fCanAddMethod = readBoolean("add method", replyData); fCanUnrestrictedlyRedefineClasses = readBoolean("unrestrictedly redefine classes", replyData); fCanPopFrames = readBoolean("pop frames", replyData); fCanUseInstanceFilters = readBoolean("use instance filters", replyData); fCanGetSourceDebugExtension = readBoolean("get source debug extension", replyData); fCanRequestVMDeathEvent = readBoolean("request vm death", replyData); fCanSetDefaultStratum= readBoolean("set default stratum", replyData); fCanGetInstanceInfo = readBoolean("instance info", replyData); fCanRequestMonitorEvents = readBoolean("request monitor events", replyData); fCanGetMonitorFrameInfo = readBoolean("monitor frame info", replyData); fCanUseSourceNameFilters = readBoolean("source name filters", replyData); fCanGetConstantPool = readBoolean("constant pool", replyData); fCanForceEarlyReturn = readBoolean("force early return", replyData); } else { 463 fCanRedefineClasses = false; 464 fCanAddMethod = false; 465 fCanUnrestrictedlyRedefineClasses = false; 466 fCanPopFrames = false; 467 fCanUseInstanceFilters = false; 468 fCanGetSourceDebugExtension = false; 469 fCanRequestVMDeathEvent = false; 470 fCanSetDefaultStratum= false; 471 fCanGetInstanceInfo = false; 472 fCanGetConstantPool = false; 473 fCanUseSourceNameFilters = false; 474 fCanForceEarlyReturn = false; 475 fCanRequestMonitorEvents = false; 476 fCanGetMonitorFrameInfo = false; 477 } 478 fGotCapabilities = true; 479 } catch (IOException e) { 480 fGotIDSizes = false; 481 defaultIOExceptionHandler(e); 482 } finally { 483 handledJdwpRequest(); 484 } 485 } 486 487 491 public boolean canForceEarlyReturn() { 492 getCapabilities(); 493 return fCanForceEarlyReturn; 494 } 495 496 499 public boolean canGetBytecodes() { 500 getCapabilities(); 501 return fCanGetBytecodes; 502 } 503 504 507 public boolean canGetCurrentContendedMonitor() { 508 getCapabilities(); 509 return fCanGetCurrentContendedMonitor; 510 } 511 512 516 public boolean canGetInstanceInfo() { 517 getCapabilities(); 518 return fCanGetInstanceInfo; 519 } 520 521 525 public boolean canGetMethodReturnValues() { 526 return isJdwpVersionGreaterOrEqual(1, 6); 527 } 528 529 532 public boolean canGetMonitorInfo() { 533 getCapabilities(); 534 return fCanGetMonitorInfo; 535 } 536 537 541 public boolean canGetMonitorFrameInfo() { 542 getCapabilities(); 543 return fCanGetMonitorFrameInfo; 544 } 545 546 549 public boolean canGetOwnedMonitorInfo() { 550 getCapabilities(); 551 return fCanGetOwnedMonitorInfo; 552 } 553 554 558 public boolean canGetSyntheticAttribute() { 559 getCapabilities(); 560 return fCanGetSyntheticAttribute; 561 } 562 563 567 public boolean canRequestMonitorEvents() { 568 getCapabilities(); 569 return fCanRequestMonitorEvents; 570 } 571 572 575 public boolean canWatchFieldAccess() { 576 getCapabilities(); 577 return fCanWatchFieldAccess; 578 } 579 580 583 public boolean canWatchFieldModification() { 584 getCapabilities(); 585 return fCanWatchFieldModification; 586 } 587 588 591 public List classesBySignature(String signature) { 592 initJdwpRequest(); 594 try { 595 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 596 DataOutputStream outData = new DataOutputStream (outBytes); 597 writeString(signature, "signature", outData); 599 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.VM_CLASSES_BY_SIGNATURE, outBytes); 600 defaultReplyErrorHandler(replyPacket.errorCode()); 601 DataInputStream replyData = replyPacket.dataInStream(); 602 int nrOfElements = readInt("elements", replyData); List elements = new ArrayList (nrOfElements); 604 for (int i = 0; i < nrOfElements; i++) { 605 ReferenceTypeImpl elt = ReferenceTypeImpl.readWithTypeTag(this, replyData); 606 readInt("status", ReferenceTypeImpl.classStatusStrings(), replyData); if (elt == null) { 608 continue; 609 } 610 elements.add(elt); 611 } 612 return elements; 613 } catch (IOException e) { 614 defaultIOExceptionHandler(e); 615 return null; 616 } finally { 617 handledJdwpRequest(); 618 } 619 } 620 621 624 public List classesByName(String name) { 625 String signature = TypeImpl.classNameToSignature(name); 626 return classesBySignature(signature); 627 } 628 629 632 public void dispose() { 633 initJdwpRequest(); 634 try { 635 requestVM(JdwpCommandPacket.VM_DISPOSE); 636 disconnectVM(); 637 } catch (VMDisconnectedException e) { 638 } finally { 640 handledJdwpRequest(); 641 } 642 } 643 644 647 public EventQueue eventQueue() { 648 return fEventQueue; 649 } 650 651 654 public EventRequestManager eventRequestManager() { 655 return fEventReqMgr; 656 } 657 658 661 public EventRequestManagerImpl eventRequestManagerImpl() { 662 return fEventReqMgr; 663 } 664 665 668 public void exit(int exitCode) { 669 initJdwpRequest(); 670 try { 671 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 672 DataOutputStream outData = new DataOutputStream (outBytes); 673 writeInt(exitCode, "exit code", outData); requestVM(JdwpCommandPacket.VM_EXIT, outBytes); 675 disconnectVM(); 676 } catch (VMDisconnectedException e) { 677 } catch (IOException e) { 679 defaultIOExceptionHandler(e); 680 } finally { 681 handledJdwpRequest(); 682 } 683 } 684 685 688 public ByteValue mirrorOf(byte value) { 689 return new ByteValueImpl(virtualMachineImpl(), new Byte (value)); 690 } 691 692 695 public CharValue mirrorOf(char value) { 696 return new CharValueImpl(virtualMachineImpl(), new Character (value)); 697 } 698 699 702 public DoubleValue mirrorOf(double value) { 703 return new DoubleValueImpl(virtualMachineImpl(), new Double (value)); 704 } 705 706 709 public FloatValue mirrorOf(float value) { 710 return new FloatValueImpl(virtualMachineImpl(), new Float (value)); 711 } 712 713 716 public IntegerValue mirrorOf(int value) { 717 return new IntegerValueImpl(virtualMachineImpl(), new Integer (value)); 718 } 719 720 723 public LongValue mirrorOf(long value) { 724 return new LongValueImpl(virtualMachineImpl(), new Long (value)); 725 } 726 727 730 public ShortValue mirrorOf(short value) { 731 return new ShortValueImpl(virtualMachineImpl(), new Short (value)); 732 } 733 734 737 public BooleanValue mirrorOf(boolean value) { 738 return new BooleanValueImpl(virtualMachineImpl(), Boolean.valueOf(value)); 739 } 740 741 744 public StringReference mirrorOf(String value) { 745 initJdwpRequest(); 746 try { 747 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 748 DataOutputStream outData = new DataOutputStream (outBytes); 749 writeString(value, "string value", outData); 751 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.VM_CREATE_STRING, outBytes); 752 defaultReplyErrorHandler(replyPacket.errorCode()); 753 754 DataInputStream replyData = replyPacket.dataInStream(); 755 StringReference result = StringReferenceImpl.read(this, replyData); 756 return result; 757 } catch (IOException e) { 758 defaultIOExceptionHandler(e); 759 return null; 760 } finally { 761 handledJdwpRequest(); 762 } 763 } 764 765 770 public VoidValue mirrorOfVoid() { 771 return new VoidValueImpl(this); 772 } 773 774 777 public Process process() { 778 return fLaunchedProcess; 779 } 780 781 784 public void setLaunchedProcess(Process proc) { 785 fLaunchedProcess = proc; 786 } 787 788 791 public void resume() { 792 initJdwpRequest(); 793 try { 794 resetThreadEventFlags(); 795 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.VM_RESUME); 796 defaultReplyErrorHandler(replyPacket.errorCode()); 797 } finally { 798 handledJdwpRequest(); 799 } 800 } 801 802 public void setDebugTraceMode(int traceFlags) { 803 } 805 806 809 public void suspend() { 810 initJdwpRequest(); 811 try { 812 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.VM_SUSPEND); 813 defaultReplyErrorHandler(replyPacket.errorCode()); 814 } finally { 815 |