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 handledJdwpRequest(); 816 } 817 } 818 819 public List topLevelThreadGroups() { 820 initJdwpRequest(); 822 try { 823 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.VM_TOP_LEVEL_THREAD_GROUPS); 824 defaultReplyErrorHandler(replyPacket.errorCode()); 825 826 DataInputStream replyData = replyPacket.dataInStream(); 827 int nrGroups = readInt("nr of groups", replyData); ArrayList result = new ArrayList (nrGroups); 829 for (int i = 0; i < nrGroups; i++) { 830 ThreadGroupReferenceImpl threadGroup = ThreadGroupReferenceImpl.read(this, replyData); 831 result.add(threadGroup); 832 } 833 return result; 834 } catch (IOException e) { 835 defaultIOExceptionHandler(e); 836 return null; 837 } finally { 838 handledJdwpRequest(); 839 } 840 } 841 842 845 public String name() { 846 getVersionInfo(); 847 return fVMName; 848 } 849 850 853 public String version() { 854 getVersionInfo(); 855 return fVMVersion; 856 } 857 858 861 public String description() { 862 getVersionInfo(); 863 return fVersionDescription; 864 } 865 866 869 private void resetThreadEventFlags() { 870 Iterator iter = allThreads().iterator(); 871 ThreadReferenceImpl thread; 872 while (iter.hasNext()) { 873 thread = (ThreadReferenceImpl)iter.next(); 874 thread.resetEventFlags(); 875 } 876 } 877 878 881 private void getIDSizes() { 882 if (fGotIDSizes) 883 return; 884 885 889 fGotIDSizes = true; 890 891 MirrorImpl mirror = new VoidValueImpl(this); 893 894 mirror.initJdwpRequest(); 895 try { 896 JdwpReplyPacket replyPacket = mirror.requestVM(JdwpCommandPacket.VM_ID_SIZES); 897 mirror.defaultReplyErrorHandler(replyPacket.errorCode()); 898 DataInputStream replyData = replyPacket.dataInStream(); 899 900 fFieldIDSize = mirror.readInt("field ID size", replyData); fMethodIDSize = mirror.readInt("method ID size", replyData); fObjectIDSize = mirror.readInt("object ID size", replyData); fReferenceTypeIDSize = mirror.readInt("refType ID size", replyData); fFrameIDSize = mirror.readInt("frame ID size", replyData); } catch (IOException e) { 906 fGotIDSizes = false; 907 mirror.defaultIOExceptionHandler(e); 908 } finally { 909 mirror.handledJdwpRequest(); 910 } 911 } 912 913 916 public void getVersionInfo() { 917 if (fVersionDescription != null) 918 return; 919 920 initJdwpRequest(); 921 try { 922 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.VM_VERSION); 923 defaultReplyErrorHandler(replyPacket.errorCode()); 924 DataInputStream replyData = replyPacket.dataInStream(); 925 926 fVersionDescription = readString("version descr.", replyData); fJdwpMajorVersion = readInt("major version", replyData); fJdwpMinorVersion = readInt("minor version", replyData); fVMVersion = readString("version", replyData); fVMName = readString("name", replyData); 932 if ((fVMName != null) && fVMName.equals("KVM")) { eventRequestManagerImpl().enableInternalClassPrepareEvent(); 936 } 937 938 } catch (IOException e) { 939 e.printStackTrace(); 940 fVersionDescription = null; 941 defaultIOExceptionHandler(e); 942 } finally { 943 handledJdwpRequest(); 944 } 945 } 946 947 950 public void getHCRCapabilities() { 951 if (fHcrCapabilities != null) 952 return; 953 fHcrCapabilities = new boolean[HCR_CAN_REENTER_ON_EXIT + 1]; 954 955 if (isHCRSupported()) { 956 initJdwpRequest(); 957 try { 958 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.HCR_CAPABILITIES); 959 defaultReplyErrorHandler(replyPacket.errorCode()); 960 DataInputStream replyData = replyPacket.dataInStream(); 961 962 fHcrCapabilities[HCR_CAN_RELOAD_CLASSES] = readBoolean("reload classes", replyData); fHcrCapabilities[HCR_CAN_GET_CLASS_VERSION] = readBoolean("get class version", replyData); fHcrCapabilities[HCR_CAN_DO_RETURN] = readBoolean("do return", replyData); fHcrCapabilities[HCR_CAN_REENTER_ON_EXIT] = readBoolean("reenter on exit", replyData); } catch (IOException e) { 967 fHcrCapabilities = null; 968 defaultIOExceptionHandler(e); 969 } finally { 970 handledJdwpRequest(); 971 } 972 } else { 973 for (int i = 0; i < fHcrCapabilities.length; i++) { 974 fHcrCapabilities[i] = false; 975 } 976 } 977 } 978 979 982 public boolean canReloadClasses() { 983 getHCRCapabilities(); 984 return fHcrCapabilities[HCR_CAN_RELOAD_CLASSES]; 985 } 986 987 990 public boolean canGetClassFileVersion1() { 991 getHCRCapabilities(); 992 return fHcrCapabilities[HCR_CAN_GET_CLASS_VERSION]; 993 } 994 995 999 public boolean canGetClassFileVersion() { 1000 return isJdwpVersionGreaterOrEqual(1, 6); 1001 } 1002 1003 1007 public boolean canGetConstantPool() { 1008 getCapabilities(); 1009 return fCanGetConstantPool; 1010 } 1011 1012 1015 public boolean canDoReturn() { 1016 getHCRCapabilities(); 1017 return fHcrCapabilities[HCR_CAN_DO_RETURN]; 1018 } 1019 1020 1023 public boolean canReenterOnExit() { 1024 getHCRCapabilities(); 1025 return fHcrCapabilities[HCR_CAN_REENTER_ON_EXIT]; 1026 } 1027 1028 1032 public int classesHaveChanged(String [] names) { 1033 checkHCRSupported(); 1034 String [] signatures = new String [names.length]; 1036 1037 initJdwpRequest(); 1038 try { 1039 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 1040 DataOutputStream outData = new DataOutputStream (outBytes); 1041 writeInt(names.length, "length", outData); for (int i = 0; i < names.length; i++) { 1043 signatures[i] = TypeImpl.classNameToSignature(names[i]); 1044 writeString(signatures[i], "signature", outData); } 1046 1047 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.HCR_CLASSES_HAVE_CHANGED, outBytes); 1048 defaultReplyErrorHandler(replyPacket.errorCode()); 1049 DataInputStream replyData = replyPacket.dataInStream(); 1050 1051 byte resultFlag = readByte("result", resultHCRMap(), replyData); switch (resultFlag) { 1053 case HCR_RELOAD_SUCCESS: 1054 return RELOAD_SUCCESS; 1055 case HCR_RELOAD_FAILURE: 1056 return RELOAD_FAILURE; 1057 case HCR_RELOAD_IGNORED: 1058 return RELOAD_IGNORED; 1059 } 1060 throw new InternalError (JDIMessages.VirtualMachineImpl_Invalid_result_flag_in_Classes_Have_Changed_response___3 + resultFlag + JDIMessages.VirtualMachineImpl__4); } catch (IOException e) { 1062 defaultIOExceptionHandler(e); 1063 return 0; 1064 } finally { 1065 handledJdwpRequest(); 1066 } 1067 } 1068 1069 1072 public String toString() { 1073 try { 1074 return name(); 1075 } catch (Exception e) { 1076 return fDescription; 1077 } 1078 } 1079 1080 1083 public static void getConstantMaps() { 1084 if (fgHCRResultMap != null) { 1085 return; 1086 } 1087 1088 Field [] fields = VirtualMachineImpl.class.getDeclaredFields(); 1089 fgHCRResultMap = new HashMap (); 1090 for (int i = 0; i < fields.length; i++) { 1091 Field field = fields[i]; 1092 if ((field.getModifiers() & Modifier.PUBLIC) == 0 || (field.getModifiers() & Modifier.STATIC) == 0 || (field.getModifiers() & Modifier.FINAL) == 0) { 1093 continue; 1094 } 1095 1096 try { 1097 String name = field.getName(); 1098 if (name.startsWith("HCR_RELOAD_")) { Integer intValue = new Integer (field.getInt(null)); 1100 name = name.substring(4); 1101 fgHCRResultMap.put(intValue, name); 1102 } 1103 } catch (IllegalAccessException e) { 1104 } catch (IllegalArgumentException e) { 1106 } 1110 } 1111 } 1112 1113 1116 public static Map resultHCRMap() { 1117 getConstantMaps(); 1118 return fgHCRResultMap; 1119 } 1120 1121 1124 public void setRequestTimeout(int timeout) { 1125 fRequestTimeout = timeout; 1126 } 1127 1128 1131 public int getRequestTimeout() { 1132 return fRequestTimeout; 1133 } 1134 1135 1144 public boolean isJdwpVersionGreaterOrEqual(int major, int minor) { 1145 getVersionInfo(); 1146 return (fJdwpMajorVersion > major) || 1147 (fJdwpMajorVersion == major && fJdwpMinorVersion >= minor); 1148 } 1149 1150 public void redefineClasses(Map typesToBytes) { 1151 if (!canRedefineClasses()) { 1152 throw new UnsupportedOperationException (); 1153 } 1154 1155 initJdwpRequest(); 1156 try { 1157 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 1158 DataOutputStream outData = new DataOutputStream (outBytes); 1159 writeInt(typesToBytes.size(), "classes", outData); 1161 Set types = typesToBytes.keySet(); 1162 Iterator iter = types.iterator(); 1163 while (iter.hasNext()) { 1164 ReferenceTypeImpl type = (ReferenceTypeImpl) iter.next(); 1165 type.write(this, outData); 1166 byte[] bytes = (byte[]) typesToBytes.get(type); 1167 writeInt(bytes.length, "classfile", outData); for (int i=0; i < bytes.length; i++) { 1169 writeByte(bytes[i], "classByte", outData); } 1171 fCachedReftypes.remove(type.getRefTypeID()); } 1173 1174 JdwpReplyPacket reply = requestVM(JdwpCommandPacket.VM_REDEFINE_CLASSES, outBytes); 1175 switch (reply.errorCode()) { 1176 case JdwpReplyPacket.UNSUPPORTED_VERSION: 1177 throw new UnsupportedClassVersionError (); 1178 case JdwpReplyPacket.INVALID_CLASS_FORMAT: 1179 throw new ClassFormatError (); 1180 case JdwpReplyPacket.CIRCULAR_CLASS_DEFINITION: 1181 throw new ClassCircularityError (); 1182 case JdwpReplyPacket.FAILS_VERIFICATION: 1183 throw new VerifyError (); 1184 case JdwpReplyPacket.NAMES_DONT_MATCH: 1185 throw new NoClassDefFoundError (); 1186 case JdwpReplyPacket.ADD_METHOD_NOT_IMPLEMENTED: 1187 throw new UnsupportedOperationException (JDIMessages.VirtualMachineImpl_Add_method_not_implemented_1); 1188 case JdwpReplyPacket.SCHEMA_CHANGE_NOT_IMPLEMENTED: 1189 throw new UnsupportedOperationException (JDIMessages.VirtualMachineImpl_Scheme_change_not_implemented_2); 1190 case JdwpReplyPacket.HIERARCHY_CHANGE_NOT_IMPLEMENTED: 1191 throw new UnsupportedOperationException (JDIMessages.VirtualMachineImpl_Hierarchy_change_not_implemented_3); 1192 case JdwpReplyPacket.DELETE_METHOD_NOT_IMPLEMENTED: 1193 throw new UnsupportedOperationException (JDIMessages.VirtualMachineImpl_Delete_method_not_implemented_4); 1194 case JdwpReplyPacket.CLASS_MODIFIERS_CHANGE_NOT_IMPLEMENTED: 1195 throw new UnsupportedOperationException (JDIMessages.VirtualMachineImpl_Class_modifiers_change_not_implemented_5); 1196 case JdwpReplyPacket.METHOD_MODIFIERS_CHANGE_NOT_IMPLEMENTED: 1197 throw new UnsupportedOperationException (JDIMessages.VirtualMachineImpl_Method_modifiers_change_not_implemented_6); 1198 default: 1199 defaultReplyErrorHandler(reply.errorCode()); 1200 } 1201 } catch (IOException ioe) { 1202 defaultIOExceptionHandler(ioe); 1203 return; 1204 } finally { 1205 handledJdwpRequest(); 1206 } 1207 } 1208 1209 1212 public boolean canRedefineClasses() { 1213 getCapabilities(); 1214 return fCanRedefineClasses; 1215 } 1216 1217 1220 public boolean canUseInstanceFilters() { 1221 getCapabilities(); 1222 return fCanUseInstanceFilters; 1223 } 1224 1225 1228 public boolean canAddMethod() { 1229 getCapabilities(); 1230 return fCanAddMethod; 1231 } 1232 1233 1236 public boolean canUnrestrictedlyRedefineClasses() { 1237 getCapabilities(); 1238 return fCanUnrestrictedlyRedefineClasses; 1239 } 1240 1241 1245 public boolean canUseSourceNameFilters() { 1246 getCapabilities(); 1247 return fCanUseSourceNameFilters; 1248 } 1249 1250 1253 public boolean canPopFrames() { 1254 getCapabilities(); 1255 return fCanPopFrames; 1256 } 1257 1258 1261 public boolean canGetSourceDebugExtension() { 1262 getCapabilities(); 1263 return fCanGetSourceDebugExtension; 1264 } 1265 1266 1269 public boolean canRequestVMDeathEvent() { 1270 getCapabilities(); 1271 return fCanRequestVMDeathEvent; 1272 } 1273 1274 public boolean canSetDefaultStratum() { 1275 getCapabilities(); 1276 return fCanSetDefaultStratum; 1277 } 1278 1279 1282 public void setDefaultStratum(String stratum) { 1283 fDefaultStratum= stratum; 1284 1285 if (!canSetDefaultStratum()) { 1286 return; 1288 } 1289 if (stratum == null) { 1290 stratum= ""; } 1292 initJdwpRequest(); 1293 try { 1294 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 1295 DataOutputStream outData = new DataOutputStream (outBytes); 1296 writeString(stratum, "stratum ID", outData); 1298 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.VM_SET_DEFAULT_STRATUM, outBytes); 1299 defaultReplyErrorHandler(replyPacket.errorCode()); 1300 1301 } catch (IOException e) { 1302 defaultIOExceptionHandler(e); 1303 } finally { 1304 handledJdwpRequest(); 1305 } 1306 } 1307 1308 1311 public String getDefaultStratum() { 1312 return fDefaultStratum; 1313 } 1314 1315 1319 public long[] instanceCounts(List refTypes) { 1320 if(refTypes == null) { 1321 throw new NullPointerException (JDIMessages.VirtualMachineImpl_2); 1322 } 1323 int size = refTypes.size(); 1324 if(size == 0) { 1325 if (isJdwpVersionGreaterOrEqual(1, 6)) { 1326 return new long[0]; 1327 } else { 1328 throw new UnsupportedOperationException (JDIMessages.ReferenceTypeImpl_27); 1329 } 1330 } 1331 try { 1332 ByteArrayOutputStream outBytes = new ByteArrayOutputStream (); 1333 DataOutputStream outData = new DataOutputStream (outBytes); 1334 writeInt(size, "size", outData); for(int i = 0; i < size; i++) { 1336 ((ReferenceTypeImpl)refTypes.get(i)).getRefTypeID().write(outData); 1337 } 1338 JdwpReplyPacket replyPacket = requestVM(JdwpCommandPacket.VM_INSTANCE_COUNTS, outBytes); 1339 switch(replyPacket.errorCode()) { 1340 case JdwpReplyPacket.INVALID_CLASS: 1341 case JdwpReplyPacket.INVALID_OBJECT: 1342 throw new ObjectCollectedException(JDIMessages.class_or_object_not_known); 1343 case JdwpReplyPacket.ILLEGAL_ARGUMENT: 1344 throw new IllegalArgumentException (JDIMessages.VirtualMachineImpl_count_less_than_zero); 1345 case JdwpReplyPacket.NOT_IMPLEMENTED: 1346 throw new UnsupportedOperationException (JDIMessages.ReferenceTypeImpl_27); 1347 case JdwpReplyPacket.VM_DEAD: 1348 throw new VMDisconnectedException(JDIMessages.vm_dead); 1349 } 1350 defaultReplyErrorHandler(replyPacket.errorCode()); 1351 1352 DataInputStream replyData = replyPacket.dataInStream(); 1353 int counts = readInt("counts", replyData); if(counts != size) { 1355 throw new InternalError (JDIMessages.VirtualMachineImpl_3); 1356 } 1357 long[] ret = new long[counts]; 1358 for(int i = 0; i < counts; i++) { 1359 ret[i] = readLong("ref count", replyData); } 1361 return ret; 1362 } 1363 catch(IOException e) { 1364 defaultIOExceptionHandler(e); 1365 return null; 1366 } finally { 1367 handledJdwpRequest(); 1368 } 1369 } 1370 1371 1376 public boolean isDisconnected() { 1377 return fIsDisconnected; 1378 } 1379 1380 1385 public synchronized void setDisconnected(boolean disconnected) { 1386 fIsDisconnected = disconnected; 1387 } 1388 1389 1392 protected BooleanTypeImpl getBooleanType() { 1393 if (fBooleanType == null) { 1394 fBooleanType= new BooleanTypeImpl(this); 1395 } 1396 return fBooleanType; 1397 } 1398 1399 1402 protected ByteTypeImpl getByteType() { 1403 if (fByteType == null) { 1404 fByteType= new ByteTypeImpl(this); 1405 } 1406 return fByteType; 1407 } 1408 1409 1412 protected CharTypeImpl getCharType() { 1413 if (fCharType == null) { 1414 fCharType= new CharTypeImpl(this); 1415 } 1416 return fCharType; 1417 } 1418 1419 1422 protected DoubleTypeImpl getDoubleType() { 1423 if (fDoubleType == null) { 1424 fDoubleType= new DoubleTypeImpl(this); 1425 } 1426 return fDoubleType; 1427 } 1428 1429 1432 protected FloatTypeImpl getFloatType() { 1433 if (fFloatType == null) { 1434 fFloatType= new FloatTypeImpl(this); 1435 } 1436 return fFloatType; 1437 } 1438 1439 1442 protected IntegerTypeImpl getIntegerType() { 1443 if (fIntegerType == null) { 1444 fIntegerType= new IntegerTypeImpl(this); 1445 } 1446 return fIntegerType; 1447 } 1448 1449 1452 protected LongTypeImpl getLongType() { 1453 if (fLongType == null) { 1454 fLongType= new LongTypeImpl(this); 1455 } 1456 return fLongType; 1457 } 1458 1459 1462 protected ShortTypeImpl getShortType() { 1463 if (fShortType == null) { 1464 fShortType= new ShortTypeImpl(this); 1465 } 1466 return fShortType; 1467 } 1468 1469 1473 public boolean canBeModified() { 1474 return true; 1475 } 1476 1477} 1478 | Popular Tags |