jdk/src/jdk.jdi/share/native/libdt_shmem/SharedMemoryConnection.c
author jwilhelm
Sat, 18 Feb 2017 03:23:28 +0100
changeset 43926 55bfd293eb16
parent 42098 546406cc9afc
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <stddef.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <jni.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "SharedMemory.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "com_sun_tools_jdi_SharedMemoryConnection.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jdwpTransport.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "shmemBase.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "sys.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * JNI interface to the shared memory transport. These JNI methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * call the base shared memory support to do the real work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * That is, this is the front-ends interface to our shared memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * communication code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Cached architecture
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
static int byte_ordering_known;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
static int is_big_endian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Returns 1 if big endian architecture
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static int isBigEndian() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    if (!byte_ordering_known) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        unsigned int i = 0xff000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (((char *)(&i))[0] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            is_big_endian = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            is_big_endian = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        byte_ordering_known = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    return is_big_endian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Convert to big endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
static jint intToBigInt(jint i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    unsigned int b[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    if (isBigEndian()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    b[0] = (i >> 24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    b[1] = (i >> 16) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    b[2] = (i >> 8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    b[3] = i & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * It doesn't matter that jint is signed as we are or'ing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * and hence end up with the correct bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * Convert unsigned short to big endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
static unsigned short shortToBigShort(unsigned short s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    unsigned int b[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    if (isBigEndian()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    b[0] = (s >> 8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    b[1] = s & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    return (b[1] << 8) + b[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * Create a byte[] from a packet struct. All data in the byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * is JDWP packet suitable for wire transmission. That is, all fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * and data are in big-endian format as required by the JDWP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
static jbyteArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
packetToByteArray(JNIEnv *env, jdwpPacket *str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    jbyteArray array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    jsize data_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    jint total_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    jint tmpInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    total_length = str->type.cmd.len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    data_length = total_length - 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /* total packet length is header + data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    array = (*env)->NewByteArray(env, total_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /* First 4 bytes of packet are the length (in big endian format) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    tmpInt = intToBigInt((unsigned int)total_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    (*env)->SetByteArrayRegion(env, array, 0, 4, (const jbyte *)&tmpInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /* Next 4 bytes are the id field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    tmpInt = intToBigInt(str->type.cmd.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    (*env)->SetByteArrayRegion(env, array, 4, 4, (const jbyte *)&tmpInt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /* next byte is the flags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    (*env)->SetByteArrayRegion(env, array, 8, 1, (const jbyte *)&(str->type.cmd.flags));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /* next two bytes are either the error code or the command set/command */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    if (str->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        short s = shortToBigShort(str->type.reply.errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        (*env)->SetByteArrayRegion(env, array, 9, 2, (const jbyte *)&(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        (*env)->SetByteArrayRegion(env, array, 9, 1, (const jbyte *)&(str->type.cmd.cmdSet));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        (*env)->SetByteArrayRegion(env, array, 10, 1, (const jbyte *)&(str->type.cmd.cmd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /* finally the data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    if (data_length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        (*env)->SetByteArrayRegion(env, array, 11,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                   data_length, str->type.cmd.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * Fill a packet struct from a byte array. The byte array is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * JDWP packet suitable for wire transmission. That is, all fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * and data are in big-endian format as required by the JDWP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * specification. We thus need to convert the fields from big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * endian to the platform endian.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * The jbyteArray provided to this function is assumed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * of a length than is equal or greater than the length of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * the JDWP packet that is contains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
byteArrayToPacket(JNIEnv *env, jbyteArray b, jdwpPacket *str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    jsize total_length, data_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    jbyte *data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    unsigned char pktHeader[11]; /* sizeof length + id + flags + cmdSet + cmd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Get the packet header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    (*env)->GetByteArrayRegion(env, b, 0, sizeof(pktHeader), pktHeader);
42098
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   177
    if ((*env)->ExceptionOccurred(env)) {
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   178
        /* b shorter than sizeof(pktHeader) */
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   179
        return;
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   180
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    total_length = (int)pktHeader[3] | ((int)pktHeader[2] << 8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                   ((int)pktHeader[1] << 16) | ((int)pktHeader[0] << 24);
42098
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   184
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   185
    if (total_length < sizeof(pktHeader)) {
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   186
        throwException(env, "java/lang/IllegalArgumentException",
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   187
                            "JDWP header is incorrect");
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   188
        return;
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   189
    }
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   190
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * The id field is in big endian (also errorCode field in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * of reply packets).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    str->type.cmd.id = (int)pktHeader[7] | ((int)pktHeader[6] << 8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                       ((int)pktHeader[5] << 16) | ((int)pktHeader[4] << 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    str->type.cmd.flags = (jbyte)pktHeader[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    if (str->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        str->type.reply.errorCode = (int)pktHeader[9] + ((int)pktHeader[10] << 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        /* command packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        str->type.cmd.cmdSet = (jbyte)pktHeader[9];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        str->type.cmd.cmd = (jbyte)pktHeader[10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /*
42098
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   209
     * The length of the JDWP packet is sizeof(pktHeader) + data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
42098
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   211
    data_length = total_length - sizeof(pktHeader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    if (data_length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        data = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        data = malloc(data_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throwException(env, "java/lang/OutOfMemoryError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                           "Unable to allocate command data buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
42098
546406cc9afc 8164501: Uninitialised memory in byteArrayToPacket of SharedMemoryConnection.c
rehn
parents: 25859
diff changeset
   223
        (*env)->GetByteArrayRegion(env, b, sizeof(pktHeader), /*sizeof(CmdPacket)+4*/ data_length, data);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            free(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    str->type.cmd.len = total_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    str->type.cmd.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
freePacketData(jdwpPacket *packet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    if (packet->type.cmd.len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        free(packet->type.cmd.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * Class:     com_sun_tools_jdi_SharedMemoryConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * Method:    close0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 * Signature: (J)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
JNIEXPORT void JNICALL Java_com_sun_tools_jdi_SharedMemoryConnection_close0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
  (JNIEnv *env, jobject thisObject, jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    SharedMemoryConnection *connection = ID_TO_CONNECTION(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    shmemBase_closeConnection(connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * Class:     com_sun_tools_jdi_SharedMemoryConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 * Method:    receiveByte0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * Signature: (J)B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
JNIEXPORT jbyte JNICALL Java_com_sun_tools_jdi_SharedMemoryConnection_receiveByte0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
  (JNIEnv *env, jobject thisObject, jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    SharedMemoryConnection *connection = ID_TO_CONNECTION(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    jbyte b = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    rc = shmemBase_receiveByte(connection, &b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    if (rc != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        throwShmemException(env, "shmemBase_receiveByte failed", rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 * Class:     com_sun_tools_jdi_SharedMemoryConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 * Method:    receivePacket0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 * Signature: (JLcom/sun/tools/jdi/Packet;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
JNIEXPORT jbyteArray JNICALL Java_com_sun_tools_jdi_SharedMemoryConnection_receivePacket0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
  (JNIEnv *env, jobject thisObject, jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    SharedMemoryConnection *connection = ID_TO_CONNECTION(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    jdwpPacket packet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    rc = shmemBase_receivePacket(connection, &packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    if (rc != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        throwShmemException(env, "shmemBase_receivePacket failed", rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        jbyteArray array = packetToByteArray(env, &packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        /* Free the packet even if there was an exception above */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        freePacketData(&packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * Class:     com_sun_tools_jdi_SharedMemoryConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * Method:    sendByte0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 * Signature: (JB)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
JNIEXPORT void JNICALL Java_com_sun_tools_jdi_SharedMemoryConnection_sendByte0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
  (JNIEnv *env, jobject thisObject, jlong id, jbyte b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    SharedMemoryConnection *connection = ID_TO_CONNECTION(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    rc = shmemBase_sendByte(connection, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    if (rc != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        throwShmemException(env, "shmemBase_sendByte failed", rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 * Class:     com_sun_tools_jdi_SharedMemoryConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 * Method:    sendPacket0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 * Signature: (JLcom/sun/tools/jdi/Packet;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
JNIEXPORT void JNICALL Java_com_sun_tools_jdi_SharedMemoryConnection_sendPacket0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
  (JNIEnv *env, jobject thisObject, jlong id, jbyteArray b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    SharedMemoryConnection *connection = ID_TO_CONNECTION(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    jdwpPacket packet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    byteArrayToPacket(env, b, &packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    rc = shmemBase_sendPacket(connection, &packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    if (rc != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        throwShmemException(env, "shmemBase_sendPacket failed", rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    freePacketData(&packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
}