0N/A/*
2362N/A * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.jdi;
0N/A
0N/Aimport com.sun.jdi.*;
0N/Aimport java.util.*;
0N/Aimport java.io.ByteArrayOutputStream;
0N/A
0N/Aclass PacketStream {
0N/A final VirtualMachineImpl vm;
0N/A private int inCursor = 0;
0N/A final Packet pkt;
0N/A private ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
0N/A private boolean isCommitted = false;
0N/A
0N/A PacketStream(VirtualMachineImpl vm, int cmdSet, int cmd) {
0N/A this.vm = vm;
0N/A this.pkt = new Packet();
0N/A pkt.cmdSet = (short)cmdSet;
0N/A pkt.cmd = (short)cmd;
0N/A }
0N/A
0N/A PacketStream(VirtualMachineImpl vm, Packet pkt) {
0N/A this.vm = vm;
0N/A this.pkt = pkt;
0N/A this.isCommitted = true; /* read only stream */
0N/A }
0N/A
0N/A int id() {
0N/A return pkt.id;
0N/A }
0N/A
0N/A void send() {
0N/A if (!isCommitted) {
0N/A pkt.data = dataStream.toByteArray();
0N/A vm.sendToTarget(pkt);
0N/A isCommitted = true;
0N/A }
0N/A }
0N/A
0N/A void waitForReply() throws JDWPException {
0N/A if (!isCommitted) {
0N/A throw new InternalException("waitForReply without send");
0N/A }
0N/A
0N/A vm.waitForTargetReply(pkt);
0N/A
0N/A if (pkt.errorCode != Packet.ReplyNoError) {
0N/A throw new JDWPException(pkt.errorCode);
0N/A }
0N/A }
0N/A
0N/A void writeBoolean(boolean data) {
0N/A if(data) {
0N/A dataStream.write( 1 );
0N/A } else {
0N/A dataStream.write( 0 );
0N/A }
0N/A }
0N/A
0N/A void writeByte(byte data) {
0N/A dataStream.write( data );
0N/A }
0N/A
0N/A void writeChar(char data) {
0N/A dataStream.write( (byte)((data >>> 8) & 0xFF) );
0N/A dataStream.write( (byte)((data >>> 0) & 0xFF) );
0N/A }
0N/A
0N/A void writeShort(short data) {
0N/A dataStream.write( (byte)((data >>> 8) & 0xFF) );
0N/A dataStream.write( (byte)((data >>> 0) & 0xFF) );
0N/A }
0N/A
0N/A void writeInt(int data) {
0N/A dataStream.write( (byte)((data >>> 24) & 0xFF) );
0N/A dataStream.write( (byte)((data >>> 16) & 0xFF) );
0N/A dataStream.write( (byte)((data >>> 8) & 0xFF) );
0N/A dataStream.write( (byte)((data >>> 0) & 0xFF) );
0N/A }
0N/A
0N/A void writeLong(long data) {
0N/A dataStream.write( (byte)((data >>> 56) & 0xFF) );
0N/A dataStream.write( (byte)((data >>> 48) & 0xFF) );
0N/A dataStream.write( (byte)((data >>> 40) & 0xFF) );
0N/A dataStream.write( (byte)((data >>> 32) & 0xFF) );
0N/A
0N/A dataStream.write( (byte)((data >>> 24) & 0xFF) );
0N/A dataStream.write( (byte)((data >>> 16) & 0xFF) );
0N/A dataStream.write( (byte)((data >>> 8) & 0xFF) );
0N/A dataStream.write( (byte)((data >>> 0) & 0xFF) );
0N/A }
0N/A
0N/A void writeFloat(float data) {
0N/A writeInt(Float.floatToIntBits(data));
0N/A }
0N/A
0N/A void writeDouble(double data) {
0N/A writeLong(Double.doubleToLongBits(data));
0N/A }
0N/A
0N/A void writeID(int size, long data) {
0N/A switch (size) {
0N/A case 8:
0N/A writeLong(data);
0N/A break;
0N/A case 4:
0N/A writeInt((int)data);
0N/A break;
0N/A case 2:
0N/A writeShort((short)data);
0N/A break;
0N/A default:
0N/A throw new UnsupportedOperationException("JDWP: ID size not supported: " + size);
0N/A }
0N/A }
0N/A
0N/A void writeNullObjectRef() {
0N/A writeObjectRef(0);
0N/A }
0N/A
0N/A void writeObjectRef(long data) {
0N/A writeID(vm.sizeofObjectRef, data);
0N/A }
0N/A
0N/A void writeClassRef(long data) {
0N/A writeID(vm.sizeofClassRef, data);
0N/A }
0N/A
0N/A void writeMethodRef(long data) {
0N/A writeID(vm.sizeofMethodRef, data);
0N/A }
0N/A
0N/A void writeFieldRef(long data) {
0N/A writeID(vm.sizeofFieldRef, data);
0N/A }
0N/A
0N/A void writeFrameRef(long data) {
0N/A writeID(vm.sizeofFrameRef, data);
0N/A }
0N/A
0N/A void writeByteArray(byte[] data) {
0N/A dataStream.write(data, 0, data.length);
0N/A }
0N/A
0N/A void writeString(String string) {
0N/A try {
0N/A byte[] stringBytes = string.getBytes("UTF8");
0N/A writeInt(stringBytes.length);
0N/A writeByteArray(stringBytes);
0N/A } catch (java.io.UnsupportedEncodingException e) {
0N/A throw new InternalException("Cannot convert string to UTF8 bytes");
0N/A }
0N/A }
0N/A
0N/A void writeLocation(Location location) {
0N/A ReferenceTypeImpl refType = (ReferenceTypeImpl)location.declaringType();
0N/A byte tag;
0N/A if (refType instanceof ClassType) {
0N/A tag = JDWP.TypeTag.CLASS;
0N/A } else if (refType instanceof InterfaceType) {
0N/A // It's possible to have executable code in an interface
0N/A tag = JDWP.TypeTag.INTERFACE;
0N/A } else {
0N/A throw new InternalException("Invalid Location");
0N/A }
0N/A writeByte(tag);
0N/A writeClassRef(refType.ref());
0N/A writeMethodRef(((MethodImpl)location.method()).ref());
0N/A writeLong(location.codeIndex());
0N/A }
0N/A
0N/A void writeValue(Value val) {
0N/A try {
0N/A writeValueChecked(val);
0N/A } catch (InvalidTypeException exc) { // should never happen
0N/A throw new RuntimeException(
0N/A "Internal error: Invalid Tag/Type pair");
0N/A }
0N/A }
0N/A
0N/A void writeValueChecked(Value val) throws InvalidTypeException {
0N/A writeByte(ValueImpl.typeValueKey(val));
0N/A writeUntaggedValue(val);
0N/A }
0N/A
0N/A void writeUntaggedValue(Value val) {
0N/A try {
0N/A writeUntaggedValueChecked(val);
0N/A } catch (InvalidTypeException exc) { // should never happen
0N/A throw new RuntimeException(
0N/A "Internal error: Invalid Tag/Type pair");
0N/A }
0N/A }
0N/A
0N/A void writeUntaggedValueChecked(Value val) throws InvalidTypeException {
0N/A byte tag = ValueImpl.typeValueKey(val);
0N/A if (isObjectTag(tag)) {
0N/A if (val == null) {
0N/A writeObjectRef(0);
0N/A } else {
0N/A if (!(val instanceof ObjectReference)) {
0N/A throw new InvalidTypeException();
0N/A }
0N/A writeObjectRef(((ObjectReferenceImpl)val).ref());
0N/A }
0N/A } else {
0N/A switch (tag) {
0N/A case JDWP.Tag.BYTE:
0N/A if(!(val instanceof ByteValue))
0N/A throw new InvalidTypeException();
0N/A
0N/A writeByte(((PrimitiveValue)val).byteValue());
0N/A break;
0N/A
0N/A case JDWP.Tag.CHAR:
0N/A if(!(val instanceof CharValue))
0N/A throw new InvalidTypeException();
0N/A
0N/A writeChar(((PrimitiveValue)val).charValue());
0N/A break;
0N/A
0N/A case JDWP.Tag.FLOAT:
0N/A if(!(val instanceof FloatValue))
0N/A throw new InvalidTypeException();
0N/A
0N/A writeFloat(((PrimitiveValue)val).floatValue());
0N/A break;
0N/A
0N/A case JDWP.Tag.DOUBLE:
0N/A if(!(val instanceof DoubleValue))
0N/A throw new InvalidTypeException();
0N/A
0N/A writeDouble(((PrimitiveValue)val).doubleValue());
0N/A break;
0N/A
0N/A case JDWP.Tag.INT:
0N/A if(!(val instanceof IntegerValue))
0N/A throw new InvalidTypeException();
0N/A
0N/A writeInt(((PrimitiveValue)val).intValue());
0N/A break;
0N/A
0N/A case JDWP.Tag.LONG:
0N/A if(!(val instanceof LongValue))
0N/A throw new InvalidTypeException();
0N/A
0N/A writeLong(((PrimitiveValue)val).longValue());
0N/A break;
0N/A
0N/A case JDWP.Tag.SHORT:
0N/A if(!(val instanceof ShortValue))
0N/A throw new InvalidTypeException();
0N/A
0N/A writeShort(((PrimitiveValue)val).shortValue());
0N/A break;
0N/A
0N/A case JDWP.Tag.BOOLEAN:
0N/A if(!(val instanceof BooleanValue))
0N/A throw new InvalidTypeException();
0N/A
0N/A writeBoolean(((PrimitiveValue)val).booleanValue());
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Read byte represented as one bytes.
0N/A */
0N/A byte readByte() {
0N/A byte ret = pkt.data[inCursor];
0N/A inCursor += 1;
0N/A return ret;
0N/A }
0N/A
0N/A /**
0N/A * Read boolean represented as one byte.
0N/A */
0N/A boolean readBoolean() {
0N/A byte ret = readByte();
0N/A return (ret != 0);
0N/A }
0N/A
0N/A /**
0N/A * Read char represented as two bytes.
0N/A */
0N/A char readChar() {
0N/A int b1, b2;
0N/A
0N/A b1 = pkt.data[inCursor++] & 0xff;
0N/A b2 = pkt.data[inCursor++] & 0xff;
0N/A
0N/A return (char)((b1 << 8) + b2);
0N/A }
0N/A
0N/A /**
0N/A * Read short represented as two bytes.
0N/A */
0N/A short readShort() {
0N/A int b1, b2;
0N/A
0N/A b1 = pkt.data[inCursor++] & 0xff;
0N/A b2 = pkt.data[inCursor++] & 0xff;
0N/A
0N/A return (short)((b1 << 8) + b2);
0N/A }
0N/A
0N/A /**
0N/A * Read int represented as four bytes.
0N/A */
0N/A int readInt() {
0N/A int b1,b2,b3,b4;
0N/A
0N/A b1 = pkt.data[inCursor++] & 0xff;
0N/A b2 = pkt.data[inCursor++] & 0xff;
0N/A b3 = pkt.data[inCursor++] & 0xff;
0N/A b4 = pkt.data[inCursor++] & 0xff;
0N/A
0N/A return ((b1 << 24) + (b2 << 16) + (b3 << 8) + b4);
0N/A }
0N/A
0N/A /**
0N/A * Read long represented as eight bytes.
0N/A */
0N/A long readLong() {
0N/A long b1,b2,b3,b4;
0N/A long b5,b6,b7,b8;
0N/A
0N/A b1 = pkt.data[inCursor++] & 0xff;
0N/A b2 = pkt.data[inCursor++] & 0xff;
0N/A b3 = pkt.data[inCursor++] & 0xff;
0N/A b4 = pkt.data[inCursor++] & 0xff;
0N/A
0N/A b5 = pkt.data[inCursor++] & 0xff;
0N/A b6 = pkt.data[inCursor++] & 0xff;
0N/A b7 = pkt.data[inCursor++] & 0xff;
0N/A b8 = pkt.data[inCursor++] & 0xff;
0N/A
0N/A return ((b1 << 56) + (b2 << 48) + (b3 << 40) + (b4 << 32)
0N/A + (b5 << 24) + (b6 << 16) + (b7 << 8) + b8);
0N/A }
0N/A
0N/A /**
0N/A * Read float represented as four bytes.
0N/A */
0N/A float readFloat() {
0N/A return Float.intBitsToFloat(readInt());
0N/A }
0N/A
0N/A /**
0N/A * Read double represented as eight bytes.
0N/A */
0N/A double readDouble() {
0N/A return Double.longBitsToDouble(readLong());
0N/A }
0N/A
0N/A /**
0N/A * Read string represented as four byte length followed by
0N/A * characters of the string.
0N/A */
0N/A String readString() {
0N/A String ret;
0N/A int len = readInt();
0N/A
0N/A try {
0N/A ret = new String(pkt.data, inCursor, len, "UTF8");
0N/A } catch(java.io.UnsupportedEncodingException e) {
0N/A System.err.println(e);
0N/A ret = "Conversion error!";
0N/A }
0N/A inCursor += len;
0N/A return ret;
0N/A }
0N/A
0N/A private long readID(int size) {
0N/A switch (size) {
0N/A case 8:
0N/A return readLong();
0N/A case 4:
0N/A return (long)readInt();
0N/A case 2:
0N/A return (long)readShort();
0N/A default:
0N/A throw new UnsupportedOperationException("JDWP: ID size not supported: " + size);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Read object represented as vm specific byte sequence.
0N/A */
0N/A long readObjectRef() {
0N/A return readID(vm.sizeofObjectRef);
0N/A }
0N/A
0N/A long readClassRef() {
0N/A return readID(vm.sizeofClassRef);
0N/A }
0N/A
0N/A ObjectReferenceImpl readTaggedObjectReference() {
0N/A byte typeKey = readByte();
0N/A return vm.objectMirror(readObjectRef(), typeKey);
0N/A }
0N/A
0N/A ObjectReferenceImpl readObjectReference() {
0N/A return vm.objectMirror(readObjectRef());
0N/A }
0N/A
0N/A StringReferenceImpl readStringReference() {
0N/A long ref = readObjectRef();
0N/A return vm.stringMirror(ref);
0N/A }
0N/A
0N/A ArrayReferenceImpl readArrayReference() {
0N/A long ref = readObjectRef();
0N/A return vm.arrayMirror(ref);
0N/A }
0N/A
0N/A ThreadReferenceImpl readThreadReference() {
0N/A long ref = readObjectRef();
0N/A return vm.threadMirror(ref);
0N/A }
0N/A
0N/A ThreadGroupReferenceImpl readThreadGroupReference() {
0N/A long ref = readObjectRef();
0N/A return vm.threadGroupMirror(ref);
0N/A }
0N/A
0N/A ClassLoaderReferenceImpl readClassLoaderReference() {
0N/A long ref = readObjectRef();
0N/A return vm.classLoaderMirror(ref);
0N/A }
0N/A
0N/A ClassObjectReferenceImpl readClassObjectReference() {
0N/A long ref = readObjectRef();
0N/A return vm.classObjectMirror(ref);
0N/A }
0N/A
0N/A ReferenceTypeImpl readReferenceType() {
0N/A byte tag = readByte();
0N/A long ref = readObjectRef();
0N/A return vm.referenceType(ref, tag);
0N/A }
0N/A
0N/A /**
0N/A * Read method reference represented as vm specific byte sequence.
0N/A */
0N/A long readMethodRef() {
0N/A return readID(vm.sizeofMethodRef);
0N/A }
0N/A
0N/A /**
0N/A * Read field reference represented as vm specific byte sequence.
0N/A */
0N/A long readFieldRef() {
0N/A return readID(vm.sizeofFieldRef);
0N/A }
0N/A
0N/A /**
0N/A * Read field represented as vm specific byte sequence.
0N/A */
0N/A Field readField() {
28N/A ReferenceTypeImpl refType = readReferenceType();
0N/A long fieldRef = readFieldRef();
0N/A return refType.getFieldMirror(fieldRef);
0N/A }
0N/A
0N/A /**
0N/A * Read frame represented as vm specific byte sequence.
0N/A */
0N/A long readFrameRef() {
0N/A return readID(vm.sizeofFrameRef);
0N/A }
0N/A
0N/A /**
0N/A * Read a value, first byte describes type of value to read.
0N/A */
0N/A ValueImpl readValue() {
0N/A byte typeKey = readByte();
0N/A return readUntaggedValue(typeKey);
0N/A }
0N/A
0N/A ValueImpl readUntaggedValue(byte typeKey) {
0N/A ValueImpl val = null;
0N/A
0N/A if (isObjectTag(typeKey)) {
0N/A val = vm.objectMirror(readObjectRef(), typeKey);
0N/A } else {
0N/A switch(typeKey) {
0N/A case JDWP.Tag.BYTE:
0N/A val = new ByteValueImpl(vm, readByte());
0N/A break;
0N/A
0N/A case JDWP.Tag.CHAR:
0N/A val = new CharValueImpl(vm, readChar());
0N/A break;
0N/A
0N/A case JDWP.Tag.FLOAT:
0N/A val = new FloatValueImpl(vm, readFloat());
0N/A break;
0N/A
0N/A case JDWP.Tag.DOUBLE:
0N/A val = new DoubleValueImpl(vm, readDouble());
0N/A break;
0N/A
0N/A case JDWP.Tag.INT:
0N/A val = new IntegerValueImpl(vm, readInt());
0N/A break;
0N/A
0N/A case JDWP.Tag.LONG:
0N/A val = new LongValueImpl(vm, readLong());
0N/A break;
0N/A
0N/A case JDWP.Tag.SHORT:
0N/A val = new ShortValueImpl(vm, readShort());
0N/A break;
0N/A
0N/A case JDWP.Tag.BOOLEAN:
0N/A val = new BooleanValueImpl(vm, readBoolean());
0N/A break;
0N/A
0N/A case JDWP.Tag.VOID:
0N/A val = new VoidValueImpl(vm);
0N/A break;
0N/A }
0N/A }
0N/A return val;
0N/A }
0N/A
0N/A /**
0N/A * Read location represented as vm specific byte sequence.
0N/A */
0N/A Location readLocation() {
0N/A byte tag = readByte();
0N/A long classRef = readObjectRef();
0N/A long methodRef = readMethodRef();
0N/A long codeIndex = readLong();
0N/A if (classRef != 0) {
0N/A /* Valid location */
0N/A ReferenceTypeImpl refType = vm.referenceType(classRef, tag);
0N/A return new LocationImpl(vm, refType, methodRef, codeIndex);
0N/A } else {
0N/A /* Null location (example: uncaught exception) */
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A byte[] readByteArray(int length) {
0N/A byte[] array = new byte[length];
0N/A System.arraycopy(pkt.data, inCursor, array, 0, length);
0N/A inCursor += length;
0N/A return array;
0N/A }
0N/A
0N/A List<Value> readArrayRegion() {
0N/A byte typeKey = readByte();
0N/A int length = readInt();
0N/A List<Value> list = new ArrayList<Value>(length);
0N/A boolean gettingObjects = isObjectTag(typeKey);
0N/A for (int i = 0; i < length; i++) {
0N/A /*
0N/A * Each object comes back with a type key which might
0N/A * identify a more specific type than the type key we
0N/A * passed in, so we use it in the decodeValue call.
0N/A * (For primitives, we just use the original one)
0N/A */
0N/A if (gettingObjects) {
0N/A typeKey = readByte();
0N/A }
0N/A Value value = readUntaggedValue(typeKey);
0N/A list.add(value);
0N/A }
0N/A
0N/A return list;
0N/A }
0N/A
0N/A void writeArrayRegion(List<Value> srcValues) {
0N/A writeInt(srcValues.size());
0N/A for (int i = 0; i < srcValues.size(); i++) {
0N/A Value value = srcValues.get(i);
0N/A writeUntaggedValue(value);
0N/A }
0N/A }
0N/A
0N/A int skipBytes(int n) {
0N/A inCursor += n;
0N/A return n;
0N/A }
0N/A
0N/A byte command() {
0N/A return (byte)pkt.cmd;
0N/A }
0N/A
0N/A static boolean isObjectTag(byte tag) {
0N/A return (tag == JDWP.Tag.OBJECT) ||
0N/A (tag == JDWP.Tag.ARRAY) ||
0N/A (tag == JDWP.Tag.STRING) ||
0N/A (tag == JDWP.Tag.THREAD) ||
0N/A (tag == JDWP.Tag.THREAD_GROUP) ||
0N/A (tag == JDWP.Tag.CLASS_LOADER) ||
0N/A (tag == JDWP.Tag.CLASS_OBJECT);
0N/A }
0N/A}