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/A#include "util.h"
0N/A#include "stream.h"
0N/A#include "inStream.h"
0N/A#include "transport.h"
0N/A#include "bag.h"
0N/A#include "commonRef.h"
0N/A#include "FrameID.h"
0N/A
0N/A#define INITIAL_REF_ALLOC 50
0N/A#define SMALLEST(a, b) ((a) < (b)) ? (a) : (b)
0N/A
0N/A/*
0N/A * TO DO: Support processing of replies through command input streams.
0N/A */
0N/Avoid
0N/AinStream_init(PacketInputStream *stream, jdwpPacket packet)
0N/A{
0N/A stream->packet = packet;
0N/A stream->error = JDWP_ERROR(NONE);
0N/A stream->left = packet.type.cmd.len;
0N/A stream->current = packet.type.cmd.data;
0N/A stream->refs = bagCreateBag(sizeof(jobject), INITIAL_REF_ALLOC);
0N/A if (stream->refs == NULL) {
0N/A stream->error = JDWP_ERROR(OUT_OF_MEMORY);
0N/A }
0N/A}
0N/A
0N/Ajint
0N/AinStream_id(PacketInputStream *stream)
0N/A{
0N/A return stream->packet.type.cmd.id;
0N/A}
0N/A
0N/Ajbyte
0N/AinStream_command(PacketInputStream *stream)
0N/A{
0N/A return stream->packet.type.cmd.cmd;
0N/A}
0N/A
0N/Astatic jdwpError
0N/AreadBytes(PacketInputStream *stream, void *dest, int size)
0N/A{
0N/A if (stream->error) {
0N/A return stream->error;
0N/A }
0N/A
0N/A if (size > stream->left) {
0N/A stream->error = JDWP_ERROR(INTERNAL);
0N/A return stream->error;
0N/A }
0N/A
0N/A if (dest) {
0N/A (void)memcpy(dest, stream->current, size);
0N/A }
0N/A stream->current += size;
0N/A stream->left -= size;
0N/A
0N/A return stream->error;
0N/A}
0N/A
0N/AjdwpError
0N/AinStream_skipBytes(PacketInputStream *stream, jint size) {
0N/A return readBytes(stream, NULL, size);
0N/A}
0N/A
0N/Ajboolean
0N/AinStream_readBoolean(PacketInputStream *stream)
0N/A{
0N/A jbyte flag = 0;
0N/A (void)readBytes(stream, &flag, sizeof(flag));
0N/A if (stream->error) {
0N/A return 0;
0N/A } else {
0N/A return flag ? JNI_TRUE : JNI_FALSE;
0N/A }
0N/A}
0N/A
0N/Ajbyte
0N/AinStream_readByte(PacketInputStream *stream)
0N/A{
0N/A jbyte val = 0;
0N/A (void)readBytes(stream, &val, sizeof(val));
0N/A return val;
0N/A}
0N/A
0N/Ajbyte *
0N/AinStream_readBytes(PacketInputStream *stream, int length, jbyte *buf)
0N/A{
0N/A (void)readBytes(stream, buf, length);
0N/A return buf;
0N/A}
0N/A
0N/Ajchar
0N/AinStream_readChar(PacketInputStream *stream)
0N/A{
0N/A jchar val = 0;
0N/A (void)readBytes(stream, &val, sizeof(val));
0N/A return JAVA_TO_HOST_CHAR(val);
0N/A}
0N/A
0N/Ajshort
0N/AinStream_readShort(PacketInputStream *stream)
0N/A{
0N/A jshort val = 0;
0N/A (void)readBytes(stream, &val, sizeof(val));
0N/A return JAVA_TO_HOST_SHORT(val);
0N/A}
0N/A
0N/Ajint
0N/AinStream_readInt(PacketInputStream *stream)
0N/A{
0N/A jint val = 0;
0N/A (void)readBytes(stream, &val, sizeof(val));
0N/A return JAVA_TO_HOST_INT(val);
0N/A}
0N/A
0N/Ajlong
0N/AinStream_readLong(PacketInputStream *stream)
0N/A{
0N/A jlong val = 0;
0N/A (void)readBytes(stream, &val, sizeof(val));
0N/A return JAVA_TO_HOST_LONG(val);
0N/A}
0N/A
0N/Ajfloat
0N/AinStream_readFloat(PacketInputStream *stream)
0N/A{
0N/A jfloat val = 0;
0N/A (void)readBytes(stream, &val, sizeof(val));
0N/A return JAVA_TO_HOST_FLOAT(val);
0N/A}
0N/A
0N/Ajdouble
0N/AinStream_readDouble(PacketInputStream *stream)
0N/A{
0N/A jdouble val = 0;
0N/A (void)readBytes(stream, &val, sizeof(val));
0N/A return JAVA_TO_HOST_DOUBLE(val);
0N/A}
0N/A
0N/A/*
0N/A * Read an object from the stream. The ID used in the wire protocol
0N/A * is converted to a reference which is returned. The reference is
0N/A * global and strong, but it should *not* be deleted by the caller
0N/A * since it is freed when this stream is destroyed.
0N/A */
0N/Ajobject
0N/AinStream_readObjectRef(JNIEnv *env, PacketInputStream *stream)
0N/A{
0N/A jobject ref;
0N/A jobject *refPtr;
0N/A jlong id = inStream_readLong(stream);
0N/A if (stream->error) {
0N/A return NULL;
0N/A }
0N/A if (id == NULL_OBJECT_ID) {
0N/A return NULL;
0N/A }
0N/A
0N/A ref = commonRef_idToRef(env, id);
0N/A if (ref == NULL) {
0N/A stream->error = JDWP_ERROR(INVALID_OBJECT);
0N/A return NULL;
0N/A }
0N/A
0N/A refPtr = bagAdd(stream->refs);
0N/A if (refPtr == NULL) {
0N/A commonRef_idToRef_delete(env, ref);
0N/A return NULL;
0N/A }
0N/A
0N/A *refPtr = ref;
0N/A return ref;
0N/A}
0N/A
0N/A/*
0N/A * Read a raw object id from the stream. This should be used rarely.
0N/A * Normally, inStream_readObjectRef is preferred since it takes care
0N/A * of reference conversion and tracking. Only code that needs to
0N/A * perform maintence of the commonRef hash table uses this function.
0N/A */
0N/Ajlong
0N/AinStream_readObjectID(PacketInputStream *stream)
0N/A{
0N/A return inStream_readLong(stream);
0N/A}
0N/A
0N/Ajclass
0N/AinStream_readClassRef(JNIEnv *env, PacketInputStream *stream)
0N/A{
0N/A jobject object = inStream_readObjectRef(env, stream);
0N/A if (object == NULL) {
0N/A /*
0N/A * Could be error or just the null reference. In either case,
0N/A * stop now.
0N/A */
0N/A return NULL;
0N/A }
0N/A if (!isClass(object)) {
0N/A stream->error = JDWP_ERROR(INVALID_CLASS);
0N/A return NULL;
0N/A }
0N/A return object;
0N/A}
0N/A
0N/Ajthread
0N/AinStream_readThreadRef(JNIEnv *env, PacketInputStream *stream)
0N/A{
0N/A jobject object = inStream_readObjectRef(env, stream);
0N/A if (object == NULL) {
0N/A /*
0N/A * Could be error or just the null reference. In either case,
0N/A * stop now.
0N/A */
0N/A return NULL;
0N/A }
0N/A if (!isThread(object)) {
0N/A stream->error = JDWP_ERROR(INVALID_THREAD);
0N/A return NULL;
0N/A }
0N/A return object;
0N/A}
0N/A
0N/AjthreadGroup
0N/AinStream_readThreadGroupRef(JNIEnv *env, PacketInputStream *stream)
0N/A{
0N/A jobject object = inStream_readObjectRef(env, stream);
0N/A if (object == NULL) {
0N/A /*
0N/A * Could be error or just the null reference. In either case,
0N/A * stop now.
0N/A */
0N/A return NULL;
0N/A }
0N/A if (!isThreadGroup(object)) {
0N/A stream->error = JDWP_ERROR(INVALID_THREAD_GROUP);
0N/A return NULL;
0N/A }
0N/A return object;
0N/A}
0N/A
0N/Ajstring
0N/AinStream_readStringRef(JNIEnv *env, PacketInputStream *stream)
0N/A{
0N/A jobject object = inStream_readObjectRef(env, stream);
0N/A if (object == NULL) {
0N/A /*
0N/A * Could be error or just the null reference. In either case,
0N/A * stop now.
0N/A */
0N/A return NULL;
0N/A }
0N/A if (!isString(object)) {
0N/A stream->error = JDWP_ERROR(INVALID_STRING);
0N/A return NULL;
0N/A }
0N/A return object;
0N/A}
0N/A
0N/Ajclass
0N/AinStream_readClassLoaderRef(JNIEnv *env, PacketInputStream *stream)
0N/A{
0N/A jobject object = inStream_readObjectRef(env, stream);
0N/A if (object == NULL) {
0N/A /*
0N/A * Could be error or just the null reference. In either case,
0N/A * stop now.
0N/A */
0N/A return NULL;
0N/A }
0N/A if (!isClassLoader(object)) {
0N/A stream->error = JDWP_ERROR(INVALID_CLASS_LOADER);
0N/A return NULL;
0N/A }
0N/A return object;
0N/A}
0N/A
0N/Ajarray
0N/AinStream_readArrayRef(JNIEnv *env, PacketInputStream *stream)
0N/A{
0N/A jobject object = inStream_readObjectRef(env, stream);
0N/A if (object == NULL) {
0N/A /*
0N/A * Could be error or just the null reference. In either case,
0N/A * stop now.
0N/A */
0N/A return NULL;
0N/A }
0N/A if (!isArray(object)) {
0N/A stream->error = JDWP_ERROR(INVALID_ARRAY);
0N/A return NULL;
0N/A }
0N/A return object;
0N/A}
0N/A
0N/A/*
0N/A * Next 3 functions read an Int and convert to a Pointer!?
0N/A * If sizeof(jxxxID) == 8 we must read these values as Longs.
0N/A */
0N/AFrameID
0N/AinStream_readFrameID(PacketInputStream *stream)
0N/A{
0N/A if (sizeof(FrameID) == 8) {
0N/A /*LINTED*/
0N/A return (FrameID)inStream_readLong(stream);
0N/A } else {
0N/A /*LINTED*/
0N/A return (FrameID)inStream_readInt(stream);
0N/A }
0N/A}
0N/A
0N/AjmethodID
0N/AinStream_readMethodID(PacketInputStream *stream)
0N/A{
0N/A if (sizeof(jmethodID) == 8) {
0N/A /*LINTED*/
0N/A return (jmethodID)(intptr_t)inStream_readLong(stream);
0N/A } else {
0N/A /*LINTED*/
0N/A return (jmethodID)(intptr_t)inStream_readInt(stream);
0N/A }
0N/A}
0N/A
0N/AjfieldID
0N/AinStream_readFieldID(PacketInputStream *stream)
0N/A{
0N/A if (sizeof(jfieldID) == 8) {
0N/A /*LINTED*/
0N/A return (jfieldID)(intptr_t)inStream_readLong(stream);
0N/A } else {
0N/A /*LINTED*/
0N/A return (jfieldID)(intptr_t)inStream_readInt(stream);
0N/A }
0N/A}
0N/A
0N/Ajlocation
0N/AinStream_readLocation(PacketInputStream *stream)
0N/A{
0N/A return (jlocation)inStream_readLong(stream);
0N/A}
0N/A
0N/Achar *
0N/AinStream_readString(PacketInputStream *stream)
0N/A{
0N/A int length;
0N/A char *string;
0N/A
0N/A length = inStream_readInt(stream);
0N/A string = jvmtiAllocate(length + 1);
0N/A if (string != NULL) {
0N/A int new_length;
0N/A
0N/A (void)readBytes(stream, string, length);
0N/A string[length] = '\0';
0N/A
0N/A /* This is Standard UTF-8, convert to Modified UTF-8 if necessary */
0N/A new_length = (gdata->npt->utf8sToUtf8mLength)
0N/A (gdata->npt->utf, (jbyte*)string, length);
0N/A if ( new_length != length ) {
0N/A char *new_string;
0N/A
0N/A new_string = jvmtiAllocate(new_length+1);
0N/A (gdata->npt->utf8sToUtf8m)
0N/A (gdata->npt->utf, (jbyte*)string, length,
0N/A (jbyte*)new_string, new_length);
0N/A jvmtiDeallocate(string);
0N/A return new_string;
0N/A }
0N/A }
0N/A return string;
0N/A}
0N/A
0N/Ajboolean
0N/AinStream_endOfInput(PacketInputStream *stream)
0N/A{
0N/A return (stream->left > 0);
0N/A}
0N/A
0N/AjdwpError
0N/AinStream_error(PacketInputStream *stream)
0N/A{
0N/A return stream->error;
0N/A}
0N/A
0N/Avoid
0N/AinStream_clearError(PacketInputStream *stream) {
0N/A stream->error = JDWP_ERROR(NONE);
0N/A}
0N/A
0N/Ajvalue
0N/AinStream_readValue(PacketInputStream *stream, jbyte *typeKeyPtr)
0N/A{
0N/A jvalue value;
0N/A jbyte typeKey = inStream_readByte(stream);
0N/A if (stream->error) {
0N/A value.j = 0L;
0N/A return value;
0N/A }
0N/A
0N/A if (isObjectTag(typeKey)) {
0N/A value.l = inStream_readObjectRef(getEnv(), stream);
0N/A } else {
0N/A switch (typeKey) {
0N/A case JDWP_TAG(BYTE):
0N/A value.b = inStream_readByte(stream);
0N/A break;
0N/A
0N/A case JDWP_TAG(CHAR):
0N/A value.c = inStream_readChar(stream);
0N/A break;
0N/A
0N/A case JDWP_TAG(FLOAT):
0N/A value.f = inStream_readFloat(stream);
0N/A break;
0N/A
0N/A case JDWP_TAG(DOUBLE):
0N/A value.d = inStream_readDouble(stream);
0N/A break;
0N/A
0N/A case JDWP_TAG(INT):
0N/A value.i = inStream_readInt(stream);
0N/A break;
0N/A
0N/A case JDWP_TAG(LONG):
0N/A value.j = inStream_readLong(stream);
0N/A break;
0N/A
0N/A case JDWP_TAG(SHORT):
0N/A value.s = inStream_readShort(stream);
0N/A break;
0N/A
0N/A case JDWP_TAG(BOOLEAN):
0N/A value.z = inStream_readBoolean(stream);
0N/A break;
0N/A default:
0N/A stream->error = JDWP_ERROR(INVALID_TAG);
0N/A break;
0N/A }
0N/A }
0N/A if (typeKeyPtr) {
0N/A *typeKeyPtr = typeKey;
0N/A }
0N/A return value;
0N/A}
0N/A
0N/Astatic jboolean
0N/AdeleteRef(void *elementPtr, void *arg)
0N/A{
0N/A JNIEnv *env = arg;
0N/A jobject *refPtr = elementPtr;
0N/A commonRef_idToRef_delete(env, *refPtr);
0N/A return JNI_TRUE;
0N/A}
0N/A
0N/Avoid
0N/AinStream_destroy(PacketInputStream *stream)
0N/A{
0N/A if (stream->packet.type.cmd.data != NULL) {
0N/A jvmtiDeallocate(stream->packet.type.cmd.data);
0N/A }
0N/A
0N/A (void)bagEnumerateOver(stream->refs, deleteRef, (void *)getEnv());
0N/A bagDestroyBag(stream->refs);
0N/A}