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.io.PrintWriter ; 19 import java.util.Map ; 20 21 import org.eclipse.jdi.Bootstrap; 22 import org.eclipse.jdi.internal.jdwp.JdwpCommandPacket; 23 import org.eclipse.jdi.internal.jdwp.JdwpPacket; 24 import org.eclipse.jdi.internal.jdwp.JdwpReplyPacket; 25 import org.eclipse.jdi.internal.jdwp.JdwpString; 26 27 import com.sun.jdi.ClassNotPreparedException; 28 import com.sun.jdi.InternalException; 29 import com.sun.jdi.InvalidStackFrameException; 30 import com.sun.jdi.Mirror; 31 import com.sun.jdi.NativeMethodException; 32 import com.sun.jdi.ObjectCollectedException; 33 import com.sun.jdi.VMDisconnectedException; 34 import com.sun.jdi.VMMismatchException; 35 import com.sun.jdi.VMOutOfMemoryException; 36 import com.sun.jdi.VirtualMachine; 37 38 44 public class MirrorImpl implements Mirror { 45 46 47 protected String fDescription; 48 49 private VirtualMachineImpl fVirtualMachineImpl; 50 51 protected VerboseWriter fVerboseWriter = null; 52 53 private boolean fPendingJdwpRequest = false; 54 55 58 public MirrorImpl(String description) { 59 fDescription = description; 60 fVirtualMachineImpl = (VirtualMachineImpl)this; 61 PrintWriter writer = ((VirtualMachineManagerImpl)org.eclipse.jdi.Bootstrap.virtualMachineManager()).verbosePrintWriter(); 62 if (writer != null) 63 fVerboseWriter = new VerboseWriter(writer); 64 } 65 66 69 public MirrorImpl(String description, VirtualMachineImpl virtualMachineImpl) { 70 fVirtualMachineImpl = virtualMachineImpl; 71 fDescription = description; 72 PrintWriter writer = ((VirtualMachineManagerImpl)org.eclipse.jdi.Bootstrap.virtualMachineManager()).verbosePrintWriter(); 73 if (writer != null) 74 fVerboseWriter = new VerboseWriter(writer); 75 } 76 77 80 public String toString() { 81 return fDescription; 82 } 83 84 87 public VirtualMachine virtualMachine() { 88 return fVirtualMachineImpl; 89 } 90 91 94 public VirtualMachineImpl virtualMachineImpl() { 95 return fVirtualMachineImpl; 96 } 97 98 101 public void initJdwpEventSet(JdwpCommandPacket commandPacket) { 102 if (fVerboseWriter != null) { 103 fVerboseWriter.println("Received event set"); fVerboseWriter.println("length", commandPacket.getLength()); fVerboseWriter.println("id", commandPacket.getId()); fVerboseWriter.println("flags", commandPacket.getFlags(), JdwpPacket.getFlagMap()); fVerboseWriter.println("command set", (byte)(commandPacket.getCommand() >>> 8)); fVerboseWriter.println("command", (byte)commandPacket.getCommand()); } 110 } 111 112 115 public void handledJdwpEventSet() { 116 if (fVerboseWriter != null) { 117 fVerboseWriter.println(); 118 fVerboseWriter.flush(); 119 } 120 } 121 122 126 public void initJdwpRequest() { 127 if (fVerboseWriter != null) { 128 fVerboseWriter.gotoPosition(6); 129 } 130 } 131 132 135 public void writeVerboseCommandPacketHeader(JdwpCommandPacket commandPacket) { 136 if (fVerboseWriter != null) { 137 int command = commandPacket.getCommand(); 138 int currentPosition = fVerboseWriter.position(); 139 fVerboseWriter.gotoPosition(0); 140 fVerboseWriter.print("Sending command ("); fVerboseWriter.printValue(command, JdwpCommandPacket.commandMap()); 142 fVerboseWriter.println(")"); fVerboseWriter.println("length", commandPacket.getLength()); fVerboseWriter.println("id", commandPacket.getId()); fVerboseWriter.println("flags", commandPacket.getFlags(), JdwpPacket.getFlagMap()); fVerboseWriter.println("command set", (byte)(command >>> 8)); fVerboseWriter.println("command", (byte)command); fVerboseWriter.gotoPosition(currentPosition); 149 } 150 } 151 152 155 public void handledJdwpRequest() { 156 if (fVerboseWriter != null && fPendingJdwpRequest) { 157 fVerboseWriter.println(); 158 fVerboseWriter.flush(); 159 } 160 fPendingJdwpRequest = false; 161 } 162 163 167 public JdwpReplyPacket requestVM(int command, byte[] outData) { 168 JdwpCommandPacket commandPacket = new JdwpCommandPacket(command); 169 commandPacket.setData(outData); 170 fVirtualMachineImpl.packetSendManager().sendPacket(commandPacket); 171 fPendingJdwpRequest = true; 172 writeVerboseCommandPacketHeader(commandPacket); 173 174 JdwpReplyPacket reply = fVirtualMachineImpl.packetReceiveManager().getReply(commandPacket); 175 if (fVerboseWriter != null) { 176 fVerboseWriter.println(); 177 fVerboseWriter.println("Received reply"); fVerboseWriter.println("length", reply.getLength()); fVerboseWriter.println("id", reply.getId()); fVerboseWriter.println("flags", reply.getFlags(), JdwpPacket.getFlagMap()); fVerboseWriter.println("error code", reply.errorCode(), JdwpReplyPacket.errorMap()); } 183 184 return reply; 185 } 186 187 191 public JdwpReplyPacket requestVM(int command, ByteArrayOutputStream outData) { 192 return requestVM(command, outData.toByteArray()); 193 } 194 195 199 public JdwpReplyPacket requestVM(int command, ObjectReferenceImpl object) { 200 ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream (); 201 DataOutputStream dataOutStream = new DataOutputStream (byteOutStream); 202 try { 203 object.write(this, dataOutStream); 204 } catch (IOException e) { 205 defaultIOExceptionHandler(e); 206 } 207 return requestVM(command, byteOutStream); 208 } 209 210 214 public JdwpReplyPacket requestVM(int command, ReferenceTypeImpl refType) { 215 ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream (); 216 DataOutputStream dataOutStream = new DataOutputStream (byteOutStream); 217 try { 218 refType.write(this, dataOutStream); 219 } catch (IOException e) { 220 defaultIOExceptionHandler(e); 221 } 222 return requestVM(command, byteOutStream); 223 } 224 225 229 public JdwpReplyPacket requestVM(int command) { 230 return requestVM(command, (byte[])null); 231 } 232 233 236 public void defaultReplyErrorHandler(int error) { 237 switch (error) { 238 case JdwpReplyPacket.NONE: 239 break; 240 case JdwpReplyPacket.INVALID_OBJECT: 241 throw new ObjectCollectedException(); 242 case JdwpReplyPacket.INVALID_CLASS: 243 throw new ClassNotPreparedException(); 244 case JdwpReplyPacket.CLASS_NOT_PREPARED: 245 throw new ClassNotPreparedException(); 246 case JdwpReplyPacket.OUT_OF_MEMORY: 247 throw new VMOutOfMemoryException(); 248 case JdwpReplyPacket.ILLEGAL_ARGUMENT: 249 throw new IllegalArgumentException (); 250 case JdwpReplyPacket.NATIVE_METHOD: 251 throw new NativeMethodException(); 252 case JdwpReplyPacket.INVALID_FRAMEID: 253 throw new InvalidStackFrameException(); 254 case JdwpReplyPacket.NOT_IMPLEMENTED: 255 throw new UnsupportedOperationException (); 256 case JdwpReplyPacket.HCR_OPERATION_REFUSED: 257 throw new org.eclipse.jdi.hcr.OperationRefusedException(); 258 case JdwpReplyPacket.VM_DEAD: 259 throw new VMDisconnectedException(); 260 default: 261 throw new InternalException(JDIMessages.MirrorImpl_Got_error_code_in_reply___1 + error); 262 } 263 } 264 265 268 public void defaultIOExceptionHandler(Exception e) { 269 throw new InternalException(JDIMessages.MirrorImpl_Got_invalid_data___2 + e); 270 } 271 272 276 public final JdwpCommandPacket getCommandVM(int command, long timeout) throws InterruptedException { 277 return fVirtualMachineImpl.packetReceiveManager().getCommand(command, timeout); 278 } 279 280 283 public void checkVM(Mirror mirror) throws VMMismatchException { 284 if (((MirrorImpl)mirror).virtualMachineImpl() != this.virtualMachineImpl()) 285 throw new VMMismatchException(); 286 } 287 288 291 public void disconnectVM() { 292 fVirtualMachineImpl.setDisconnected(true); 293 fVirtualMachineImpl.packetSendManager().disconnectVM(); 294 fVirtualMachineImpl.packetReceiveManager().disconnectVM(); 295 ((VirtualMachineManagerImpl) Bootstrap.virtualMachineManager()).removeConnectedVM(fVirtualMachineImpl); 296 } 297 298 302 public byte readByte(String description, DataInputStream in) throws IOException { 303 byte result = in.readByte(); 304 if (fVerboseWriter != null) { 305 fVerboseWriter.println(description, result); 306 } 307 return result; 308 } 309 310 314 public short readShort(String description, DataInputStream in) throws IOException { 315 short result = in.readShort(); 316 if (fVerboseWriter != null) { 317 fVerboseWriter.println(description, result); 318 } 319 return result; 320 } 321 322 326 public int readInt(String description, DataInputStream in) throws IOException { 327 int result = in.readInt(); 328 if (fVerboseWriter != null) { 329 fVerboseWriter.println(description, result); 330 } 331 return result; 332 } 333 334 338 public long readLong(String description, DataInputStream in) throws IOException { 339 long result = in.readLong(); 340 if (fVerboseWriter != null) { 341 fVerboseWriter.println(description, result); 342 } 343 return result; 344 } 345 346 350 public byte readByte(String description, Map valueToString, DataInputStream in) throws IOException { 351 byte result = in.readByte(); 352 if (fVerboseWriter != null) { 353 fVerboseWriter.println(description, result, valueToString); 354 } 355 return result; 356 } 357 358 362 public short readShort(String description, Map valueToString, DataInputStream in) throws IOException { 363 short result = in.readShort(); 364 if (fVerboseWriter != null) { 365 fVerboseWriter.println(description, result, valueToString); 366 } 367 return result; 368 } 369 370 374 public int readInt(String description, Map valueToString, DataInputStream in) throws IOException { 375 int result = in.readInt(); 376 if (fVerboseWriter != null) { 377 fVerboseWriter.println(description, result, valueToString); 378 } 379 return result; 380 } 381 382 386 public String readString(String description, DataInputStream in) throws IOException { 387 String result = JdwpString.read(in); 388 if (fVerboseWriter != null) { 389 fVerboseWriter.println(description, result); 390 } 391 return result; 392 } 393 394 398 public boolean readBoolean(String description, DataInputStream in) throws IOException { 399 boolean result = in.readBoolean(); 400 if (fVerboseWriter != null) { 401 fVerboseWriter.println(description, result); 402 } 403 return result; 404 } 405 406 410 public char readChar(String description, DataInputStream in) throws IOException { 411 char result = in.readChar(); 412 if (fVerboseWriter != null) { 413 fVerboseWriter.println(description, result); 414 } 415 return result; 416 } 417 418 422 public double readDouble(String description, DataInputStream in) throws IOException { 423 double result = in.readDouble(); 424 if (fVerboseWriter != null) { 425 fVerboseWriter.println(description, result); 426 } 427 return result; 428 } 429 430 434 public float readFloat(String description, DataInputStream in) throws IOException { 435 float result = in.readFloat(); 436 if (fVerboseWriter != null) { 437 fVerboseWriter.println(description, result); 438 } 439 return result; 440 } 441 442 446 public byte[] readByteArray(int length, String description, DataInputStream in) throws IOException { 447 byte[] result = new byte[length]; 448 in.readFully(result); 449 if (fVerboseWriter != null) { 450 fVerboseWriter.println(description, result); 451 } 452 return result; 453 } 454 455 458 public void writeByte(byte value, String description, DataOutputStream out) throws IOException { 459 out.writeByte(value); 460 if (fVerboseWriter != null) { 461 fVerboseWriter.println(description, value); 462 } 463 } 464 465 468 public void writeShort(short value, String description, DataOutputStream out) throws IOException { 469 out.writeShort(value); 470 if (fVerboseWriter != null) { 471 fVerboseWriter.println(description, value); 472 } 473 } 474 475 478 public void writeInt(int value, String description, DataOutputStream out) throws IOException { 479 out.writeInt(value); 480 if (fVerboseWriter != null) { 481 fVerboseWriter.println(description, value); 482 } 483 } 484 485 488 public void writeLong(long value, String description, DataOutputStream out) throws IOException { 489 out.writeLong(value); 490 if (fVerboseWriter != null) { 491 fVerboseWriter.println(description, value); 492 } 493 } 494 495 498 public void writeByte(byte value, String description, Map valueToString, DataOutputStream out) throws IOException { 499 out.writeByte(value); 500 if (fVerboseWriter != null) { 501 fVerboseWriter.println(description, value, valueToString); 502 } 503 } 504 505 508 public void writeShort(short value, String description, Map valueToString, DataOutputStream out) throws IOException { 509 out.writeShort(value); 510 if (fVerboseWriter != null) { 511 fVerboseWriter.println(description, value, valueToString); 512 } 513 } 514 515 518 public void writeInt(int value, String description, Map valueToString, DataOutputStream out) throws IOException { 519 out.writeInt(value); 520 if (fVerboseWriter != null) { 521 fVerboseWriter.println(description, value, valueToString); 522 } 523 } 524 525 528 public void writeString(String value, String description, DataOutputStream out) throws IOException { 529 JdwpString.write(value, out); 530 if (fVerboseWriter != null) { 531 fVerboseWriter.println(description, value); 532 } 533 } 534 535 538 public void writeBoolean(boolean value, String description, DataOutputStream out) throws IOException { 539 out.writeBoolean(value); 540 if (fVerboseWriter != null) { 541 fVerboseWriter.println(description, value); 542 } 543 } 544 545 548 public void writeChar(char value, String description, DataOutputStream out) throws IOException { 549 out.writeChar(value); 550 if (fVerboseWriter != null) { 551 fVerboseWriter.println(description, value); 552 } 553 } 554 555 558 public void writeDouble(double value, String description, DataOutputStream out) throws IOException { 559 out.writeDouble(value); 560 if (fVerboseWriter != null) { 561 fVerboseWriter.println(description, value); 562 } 563 } 564 565 568 public void writeFloat(float value, String description, DataOutputStream out) throws IOException { 569 out.writeFloat(value); 570 if (fVerboseWriter != null) { 571 fVerboseWriter.println(description, value); 572 } 573 } 574 575 578 public void writeShort(short value, String description, String [] bitNames, DataOutputStream out) throws IOException { 579 out.writeShort(value); 580 if (fVerboseWriter != null) { 581 fVerboseWriter.println(description, value, bitNames); 582 } 583 } 584 585 588 public void writeInt(int value, String description, String [] bitNames, DataOutputStream out) throws IOException { 589 out.writeInt(value); 590 if (fVerboseWriter != null) { 591 fVerboseWriter.println(description, value, bitNames); 592 } 593 } 594 595 599 public byte readByte(String description, String [] bitNames, DataInputStream in) throws IOException { 600 byte result = in.readByte(); 601 if (fVerboseWriter != null) { 602 fVerboseWriter.println(description, result, bitNames); 603 } 604 return result; 605 } 606 607 611 public short readShort(String description, String [] bitNames, DataInputStream in) throws IOException { 612 short result = in.readShort(); 613 if (fVerboseWriter != null) { 614 fVerboseWriter.println(description, result, bitNames); 615 } 616 return result; 617 } 618 619 623 public int readInt(String description, String [] bitNames, DataInputStream in) throws IOException { 624 int result = in.readInt(); 625 if (fVerboseWriter != null) { 626 fVerboseWriter.println(description, result, bitNames); 627 } 628 return result; 629 } 630 631 634 public void writeByte(byte value, String description, String [] bitNames, DataOutputStream out) throws IOException { 635 out.writeByte(value); 636 if (fVerboseWriter != null) { 637 fVerboseWriter.println(description, value, bitNames); 638 } 639 } 640 641 644 public VerboseWriter verboseWriter() { 645 return fVerboseWriter; 646 } 647 } 648 | Popular Tags |