PacketStream.java revision 2362
0N/A/*
3261N/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
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/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,
2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2362N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.jdi;
0N/A
0N/Aimport com.sun.jdi.*;
2156N/Aimport java.util.*;
2156N/Aimport java.io.ByteArrayOutputStream;
0N/A
2156N/Aclass PacketStream {
2156N/A final VirtualMachineImpl vm;
2156N/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() {
2555N/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
2555N/A void writeBoolean(boolean data) {
2555N/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) {
2555N/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");
} catch(java.io.UnsupportedEncodingException e) {
System.err.println(e);
ret = "Conversion error!";
}
inCursor += len;
return ret;
}
private long readID(int size) {
switch (size) {
case 8:
return readLong();
case 4:
return (long)readInt();
case 2:
return (long)readShort();
default:
throw new UnsupportedOperationException("JDWP: ID size not supported: " + size);
}
}
/**
* Read object represented as vm specific byte sequence.
*/
long readObjectRef() {
return readID(vm.sizeofObjectRef);
}
long readClassRef() {
return readID(vm.sizeofClassRef);
}
ObjectReferenceImpl readTaggedObjectReference() {
byte typeKey = readByte();
return vm.objectMirror(readObjectRef(), typeKey);
}
ObjectReferenceImpl readObjectReference() {
return vm.objectMirror(readObjectRef());
}
StringReferenceImpl readStringReference() {
long ref = readObjectRef();
return vm.stringMirror(ref);
}
ArrayReferenceImpl readArrayReference() {
long ref = readObjectRef();
return vm.arrayMirror(ref);
}
ThreadReferenceImpl readThreadReference() {
long ref = readObjectRef();
return vm.threadMirror(ref);
}
ThreadGroupReferenceImpl readThreadGroupReference() {
long ref = readObjectRef();
return vm.threadGroupMirror(ref);
}
ClassLoaderReferenceImpl readClassLoaderReference() {
long ref = readObjectRef();
return vm.classLoaderMirror(ref);
}
ClassObjectReferenceImpl readClassObjectReference() {
long ref = readObjectRef();
return vm.classObjectMirror(ref);
}
ReferenceTypeImpl readReferenceType() {
byte tag = readByte();
long ref = readObjectRef();
return vm.referenceType(ref, tag);
}
/**
* Read method reference represented as vm specific byte sequence.
*/
long readMethodRef() {
return readID(vm.sizeofMethodRef);
}
/**
* Read field reference represented as vm specific byte sequence.
*/
long readFieldRef() {
return readID(vm.sizeofFieldRef);
}
/**
* Read field represented as vm specific byte sequence.
*/
Field readField() {
ReferenceTypeImpl refType = readReferenceType();
long fieldRef = readFieldRef();
return refType.getFieldMirror(fieldRef);
}
/**
* Read frame represented as vm specific byte sequence.
*/
long readFrameRef() {
return readID(vm.sizeofFrameRef);
}
/**
* Read a value, first byte describes type of value to read.
*/
ValueImpl readValue() {
byte typeKey = readByte();
return readUntaggedValue(typeKey);
}
ValueImpl readUntaggedValue(byte typeKey) {
ValueImpl val = null;
if (isObjectTag(typeKey)) {
val = vm.objectMirror(readObjectRef(), typeKey);
} else {
switch(typeKey) {
case JDWP.Tag.BYTE:
val = new ByteValueImpl(vm, readByte());
break;
case JDWP.Tag.CHAR:
val = new CharValueImpl(vm, readChar());
break;
case JDWP.Tag.FLOAT:
val = new FloatValueImpl(vm, readFloat());
break;
case JDWP.Tag.DOUBLE:
val = new DoubleValueImpl(vm, readDouble());
break;
case JDWP.Tag.INT:
val = new IntegerValueImpl(vm, readInt());
break;
case JDWP.Tag.LONG:
val = new LongValueImpl(vm, readLong());
break;
case JDWP.Tag.SHORT:
val = new ShortValueImpl(vm, readShort());
break;
case JDWP.Tag.BOOLEAN:
val = new BooleanValueImpl(vm, readBoolean());
break;
case JDWP.Tag.VOID:
val = new VoidValueImpl(vm);
break;
}
}
return val;
}
/**
* Read location represented as vm specific byte sequence.
*/
Location readLocation() {
byte tag = readByte();
long classRef = readObjectRef();
long methodRef = readMethodRef();
long codeIndex = readLong();
if (classRef != 0) {
/* Valid location */
ReferenceTypeImpl refType = vm.referenceType(classRef, tag);
return new LocationImpl(vm, refType, methodRef, codeIndex);
} else {
/* Null location (example: uncaught exception) */
return null;
}
}
byte[] readByteArray(int length) {
byte[] array = new byte[length];
System.arraycopy(pkt.data, inCursor, array, 0, length);
inCursor += length;
return array;
}
List<Value> readArrayRegion() {
byte typeKey = readByte();
int length = readInt();
List<Value> list = new ArrayList<Value>(length);
boolean gettingObjects = isObjectTag(typeKey);
for (int i = 0; i < length; i++) {
/*
* Each object comes back with a type key which might
* identify a more specific type than the type key we
* passed in, so we use it in the decodeValue call.
* (For primitives, we just use the original one)
*/
if (gettingObjects) {
typeKey = readByte();
}
Value value = readUntaggedValue(typeKey);
list.add(value);
}
return list;
}
void writeArrayRegion(List<Value> srcValues) {
writeInt(srcValues.size());
for (int i = 0; i < srcValues.size(); i++) {
Value value = srcValues.get(i);
writeUntaggedValue(value);
}
}
int skipBytes(int n) {
inCursor += n;
return n;
}
byte command() {
return (byte)pkt.cmd;
}
static boolean isObjectTag(byte tag) {
return (tag == JDWP.Tag.OBJECT) ||
(tag == JDWP.Tag.ARRAY) ||
(tag == JDWP.Tag.STRING) ||
(tag == JDWP.Tag.THREAD) ||
(tag == JDWP.Tag.THREAD_GROUP) ||
(tag == JDWP.Tag.CLASS_LOADER) ||
(tag == JDWP.Tag.CLASS_OBJECT);
}
}