jdk/src/share/back/inStream.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1090 c5805b1672a6
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "stream.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "inStream.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "transport.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "bag.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "commonRef.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "FrameID.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "typedefs.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#define INITIAL_REF_ALLOC 50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#define SMALLEST(a, b) ((a) < (b)) ? (a) : (b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * TO DO: Support processing of replies through command input streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
inStream_init(PacketInputStream *stream, jdwpPacket packet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    stream->packet = packet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    stream->error = JDWP_ERROR(NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    stream->left = packet.type.cmd.len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    stream->current = packet.type.cmd.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    stream->refs = bagCreateBag(sizeof(jobject), INITIAL_REF_ALLOC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    if (stream->refs == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        stream->error = JDWP_ERROR(OUT_OF_MEMORY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
inStream_id(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    return stream->packet.type.cmd.id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
jbyte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
inStream_command(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    return stream->packet.type.cmd.cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
static jdwpError
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
readBytes(PacketInputStream *stream, void *dest, int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    if (stream->error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return stream->error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    if (size > stream->left) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        stream->error = JDWP_ERROR(INTERNAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return stream->error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    if (dest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        (void)memcpy(dest, stream->current, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    stream->current += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    stream->left -= size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    return stream->error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
jdwpError
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
inStream_skipBytes(PacketInputStream *stream, jint size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    return readBytes(stream, NULL, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
inStream_readBoolean(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    jbyte flag = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    (void)readBytes(stream, &flag, sizeof(flag));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    if (stream->error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return flag ? JNI_TRUE : JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
jbyte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
inStream_readByte(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    jbyte val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    (void)readBytes(stream, &val, sizeof(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
jbyte *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
inStream_readBytes(PacketInputStream *stream, int length, jbyte *buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    (void)readBytes(stream, buf, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
jchar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
inStream_readChar(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    jchar val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    (void)readBytes(stream, &val, sizeof(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    return JAVA_TO_HOST_CHAR(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
jshort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
inStream_readShort(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    jshort val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    (void)readBytes(stream, &val, sizeof(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    return JAVA_TO_HOST_SHORT(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
inStream_readInt(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    jint val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    (void)readBytes(stream, &val, sizeof(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    return JAVA_TO_HOST_INT(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
inStream_readLong(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    jlong val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    (void)readBytes(stream, &val, sizeof(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    return JAVA_TO_HOST_LONG(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
jfloat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
inStream_readFloat(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    jfloat val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    (void)readBytes(stream, &val, sizeof(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    return JAVA_TO_HOST_FLOAT(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
jdouble
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
inStream_readDouble(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    jdouble val = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    (void)readBytes(stream, &val, sizeof(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    return JAVA_TO_HOST_DOUBLE(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * Read an object from the stream. The ID used in the wire protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * is converted to a reference which is returned. The reference is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * global and strong, but it should *not* be deleted by the caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * since it is freed when this stream is destroyed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
jobject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
inStream_readObjectRef(JNIEnv *env, PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    jobject ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    jobject *refPtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    jlong id = inStream_readLong(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    if (stream->error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    if (id == NULL_OBJECT_ID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    ref = commonRef_idToRef(env, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    if (ref == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        stream->error = JDWP_ERROR(INVALID_OBJECT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    refPtr = bagAdd(stream->refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    if (refPtr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        commonRef_idToRef_delete(env, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    *refPtr = ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    return ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * Read a raw object id from the stream. This should be used rarely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * Normally, inStream_readObjectRef is preferred since it takes care
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * of reference conversion and tracking. Only code that needs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * perform maintence of the commonRef hash table uses this function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
inStream_readObjectID(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    return inStream_readLong(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
jclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
inStream_readClassRef(JNIEnv *env, PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    jobject object = inStream_readObjectRef(env, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    if (object == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * Could be error or just the null reference. In either case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * stop now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    if (!isClass(object)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        stream->error = JDWP_ERROR(INVALID_CLASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    return object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
jthread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
inStream_readThreadRef(JNIEnv *env, PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    jobject object = inStream_readObjectRef(env, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    if (object == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         * Could be error or just the null reference. In either case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         * stop now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    if (!isThread(object)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        stream->error = JDWP_ERROR(INVALID_THREAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    return object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
jthreadGroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
inStream_readThreadGroupRef(JNIEnv *env, PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    jobject object = inStream_readObjectRef(env, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    if (object == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         * Could be error or just the null reference. In either case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         * stop now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    if (!isThreadGroup(object)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        stream->error = JDWP_ERROR(INVALID_THREAD_GROUP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    return object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
jstring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
inStream_readStringRef(JNIEnv *env, PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    jobject object = inStream_readObjectRef(env, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    if (object == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         * Could be error or just the null reference. In either case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         * stop now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    if (!isString(object)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        stream->error = JDWP_ERROR(INVALID_STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    return object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
jclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
inStream_readClassLoaderRef(JNIEnv *env, PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    jobject object = inStream_readObjectRef(env, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    if (object == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * Could be error or just the null reference. In either case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * stop now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    if (!isClassLoader(object)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        stream->error = JDWP_ERROR(INVALID_CLASS_LOADER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    return object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
jarray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
inStream_readArrayRef(JNIEnv *env, PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    jobject object = inStream_readObjectRef(env, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    if (object == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * Could be error or just the null reference. In either case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * stop now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    if (!isArray(object)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        stream->error = JDWP_ERROR(INVALID_ARRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    return object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 * Next 3 functions read an Int and convert to a Pointer!?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * If sizeof(jxxxID) == 8 we must read these values as Longs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
FrameID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
inStream_readFrameID(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    if (sizeof(FrameID) == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return (FrameID)inStream_readLong(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return (FrameID)inStream_readInt(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
jmethodID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
inStream_readMethodID(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    if (sizeof(jmethodID) == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return (jmethodID)(intptr_t)inStream_readLong(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return (jmethodID)(intptr_t)inStream_readInt(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
jfieldID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
inStream_readFieldID(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    if (sizeof(jfieldID) == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return (jfieldID)(intptr_t)inStream_readLong(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return (jfieldID)(intptr_t)inStream_readInt(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
jlocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
inStream_readLocation(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    return (jlocation)inStream_readLong(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
inStream_readString(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    char *string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    length = inStream_readInt(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    string = jvmtiAllocate(length + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    if (string != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        int new_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        (void)readBytes(stream, string, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        string[length] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        /* This is Standard UTF-8, convert to Modified UTF-8 if necessary */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        new_length = (gdata->npt->utf8sToUtf8mLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                             (gdata->npt->utf, (jbyte*)string, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if ( new_length != length ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            char *new_string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            new_string = jvmtiAllocate(new_length+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            (gdata->npt->utf8sToUtf8m)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                             (gdata->npt->utf, (jbyte*)string, length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                              (jbyte*)new_string, new_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            jvmtiDeallocate(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            return new_string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    return string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
inStream_endOfInput(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    return (stream->left > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
jdwpError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
inStream_error(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    return stream->error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
inStream_clearError(PacketInputStream *stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    stream->error = JDWP_ERROR(NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
jvalue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
inStream_readValue(PacketInputStream *stream, jbyte *typeKeyPtr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    jvalue value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    jbyte typeKey = inStream_readByte(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    if (stream->error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        value.j = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    if (isObjectTag(typeKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        value.l = inStream_readObjectRef(getEnv(), stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        switch (typeKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            case JDWP_TAG(BYTE):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                value.b = inStream_readByte(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            case JDWP_TAG(CHAR):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                value.c = inStream_readChar(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            case JDWP_TAG(FLOAT):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                value.f = inStream_readFloat(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            case JDWP_TAG(DOUBLE):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                value.d = inStream_readDouble(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            case JDWP_TAG(INT):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                value.i = inStream_readInt(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            case JDWP_TAG(LONG):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                value.j = inStream_readLong(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            case JDWP_TAG(SHORT):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                value.s = inStream_readShort(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            case JDWP_TAG(BOOLEAN):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                value.z = inStream_readBoolean(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                stream->error = JDWP_ERROR(INVALID_TAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    if (typeKeyPtr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        *typeKeyPtr = typeKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
deleteRef(void *elementPtr, void *arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    JNIEnv *env = arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    jobject *refPtr = elementPtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    commonRef_idToRef_delete(env, *refPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
inStream_destroy(PacketInputStream *stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    if (stream->packet.type.cmd.data != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    jvmtiDeallocate(stream->packet.type.cmd.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    (void)bagEnumerateOver(stream->refs, deleteRef, (void *)getEnv());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    bagDestroyBag(stream->refs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
}