src/jdk.jdi/share/native/libdt_shmem/shmemBack.c
author clanger
Tue, 12 Dec 2017 09:16:12 +0100
changeset 48242 61e60548c0cf
parent 47216 71c04702a3d5
child 49440 396ea30afbd5
permissions -rw-r--r--
8193258: Better usage of JDWP HEADER SIZE Reviewed-by: sspitsyn, cjplummer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48242
61e60548c0cf 8193258: Better usage of JDWP HEADER SIZE
clanger
parents: 47216
diff changeset
     2
 * Copyright (c) 1999, 2017, 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: 1247
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: 1247
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: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jdwpTransport.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "shmemBase.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "sysShmem.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "sys.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The Shared Memory Transport Library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This module is an implementation of the Java Debug Wire Protocol Transport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Service Provider Interface - see src/share/javavm/export/jdwpTransport.h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
static SharedMemoryTransport *transport = NULL;  /* maximum of 1 transport */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
static SharedMemoryConnection *connection = NULL;  /* maximum of 1 connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
static jdwpTransportCallback *callbacks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
static jboolean initialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static struct jdwpTransportNativeInterface_ interface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
static jdwpTransportEnv single_env = (jdwpTransportEnv)&interface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Thread-local index to the per-thread error message
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
static int tlsIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Return an error and record the error message associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * the error. Note the if (1==1) { } usage here is to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * compilers complaining that a statement isn't reached which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * will arise if the semicolon (;) appears after the macro,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#define RETURN_ERROR(err, msg)          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (1==1) {                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            setLastError(err, msg);     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            return err;                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Return an I/O error and record the error message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#define RETURN_IO_ERROR(msg)    RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Set the error message for this thread. If the error is an I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * error then augment the supplied error message with the textual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * representation of the I/O error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
setLastError(int err, char *newmsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    char buf[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    char *msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /* get any I/O first in case any system calls override errno */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    if (err == JDWPTRANSPORT_ERROR_IO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (shmemBase_getlasterror(buf, sizeof(buf)) != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            buf[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /* free any current error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    msg = (char *)sysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        (*callbacks->free)(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * For I/O errors append the I/O error message with to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * supplied message. For all other errors just use the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    if (err == JDWPTRANSPORT_ERROR_IO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        char *join_str = ": ";
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
    99
        int msg_len = (int)strlen(newmsg) + (int)strlen(join_str) +
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   100
                      (int)strlen(buf) + 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        msg = (*callbacks->alloc)(msg_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            strcpy(msg, newmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            strcat(msg, join_str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            strcat(msg, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    } else {
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   108
        msg = (*callbacks->alloc)((int)strlen(newmsg)+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            strcpy(msg, newmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /* Put a pointer to the message in TLS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    sysTlsPut(tlsIndex, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
static jdwpTransportError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
handshake()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    char *hello = "JDWP-Handshake";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    unsigned int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    for (i=0; i<strlen(hello); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        jbyte b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int rv = shmemBase_receiveByte(connection, &b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (rv != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            RETURN_IO_ERROR("receive failed during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if ((char)b != hello[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            RETURN_IO_ERROR("handshake failed - debugger sent unexpected message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    for (i=0; i<strlen(hello); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int rv = shmemBase_sendByte(connection, (jbyte)hello[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (rv != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            RETURN_IO_ERROR("write failed during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * Return the capabilities of the shared memory transport. The shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * memory transport supports both the attach and accept timeouts but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * doesn't support a handshake timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
shmemGetCapabilities(jdwpTransportEnv* env, JDWPTransportCapabilities *capabilitiesPtr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    JDWPTransportCapabilities result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    memset(&result, 0, sizeof(result));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    result.can_timeout_attach = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    result.can_timeout_accept = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    result.can_timeout_handshake = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    *capabilitiesPtr = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
shmemStartListening(jdwpTransportEnv* env, const char *address, char **actualAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    if (connection != NULL || transport != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "already connected or already listening");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    rc = shmemBase_listen(address, &transport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * If a name was selected by the function above, find it and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * it in place of the original arg.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    if (rc == SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        char *name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        char *name2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        rc = shmemBase_name(transport, &name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (rc == SYS_OK) {
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   187
            name2 = (callbacks->alloc)((int)strlen(name) + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (name2 == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                strcpy(name2, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                *actualAddress = name2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        RETURN_IO_ERROR("failed to create shared memory listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
shmemAccept(jdwpTransportEnv* env, jlong acceptTimeout, jlong handshakeTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    if (connection != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    if (transport == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "transport not listening");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    rc = shmemBase_accept(transport, (long)acceptTimeout, &connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    if (rc != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (rc == SYS_TIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "Timed out waiting for connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            RETURN_IO_ERROR("failed to accept shared memory connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    rc = handshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    if (rc != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        shmemBase_closeConnection(connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        connection = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
shmemStopListening(jdwpTransportEnv* env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    if (transport != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        shmemBase_closeTransport(transport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        transport = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
shmemAttach(jdwpTransportEnv* env, const char *address, jlong attachTimeout, jlong handshakeTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    if (connection != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    rc = shmemBase_attach(address, (long)attachTimeout, &connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    if (rc != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (rc == SYS_NOMEM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (rc == SYS_TIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "Timed out waiting to attach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        RETURN_IO_ERROR("failed to attach to shared memory connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    rc = handshake();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    if (rc != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        shmemBase_closeConnection(connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        connection = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
shmemWritePacket(jdwpTransportEnv* env, const jdwpPacket *packet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    if (packet == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
48242
61e60548c0cf 8193258: Better usage of JDWP HEADER SIZE
clanger
parents: 47216
diff changeset
   273
    if (packet->type.cmd.len < JDWP_HEADER_SIZE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "invalid length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    if (connection == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    if (shmemBase_sendPacket(connection, packet) == SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        RETURN_IO_ERROR("write packet failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
shmemReadPacket(jdwpTransportEnv* env, jdwpPacket *packet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    if (packet == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    if (connection == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    if (shmemBase_receivePacket(connection, packet) == SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        RETURN_IO_ERROR("receive packet failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
static jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
shmemIsOpen(jdwpTransportEnv* env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    if (connection != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
shmemClose(jdwpTransportEnv* env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    SharedMemoryConnection* current_connection = connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    if (current_connection != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        connection = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        shmemBase_closeConnection(current_connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * Return the error message for this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
static jdwpTransportError  JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
shmemGetLastError(jdwpTransportEnv* env, char **msgP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    char *msg = (char *)sysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    if (msg == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return JDWPTRANSPORT_ERROR_MSG_NOT_AVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   333
    *msgP = (*callbacks->alloc)((int)strlen(msg)+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    if (*msgP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return JDWPTRANSPORT_ERROR_OUT_OF_MEMORY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    strcpy(*msgP, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
36134
63514f38c74c 8150497: 32 jshell tests failed on Windows 32 bit
erikj
parents: 25859
diff changeset
   341
jint JNICALL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
jdwpTransport_OnLoad(JavaVM *vm, jdwpTransportCallback* cbTablePtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                     jint version, jdwpTransportEnv** result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    if (version != JDWPTRANSPORT_VERSION_1_0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return JNI_EVERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    if (initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         * This library doesn't support multiple environments (yet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return JNI_EEXIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    initialized = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /* initialize base shared memory system */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
   (void) shmemBase_initialize(vm, cbTablePtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /* save callbacks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    callbacks = cbTablePtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /* initialize interface table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    interface.GetCapabilities = &shmemGetCapabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    interface.Attach = &shmemAttach;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    interface.StartListening = &shmemStartListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    interface.StopListening = &shmemStopListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    interface.Accept = &shmemAccept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    interface.IsOpen = &shmemIsOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    interface.Close = &shmemClose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    interface.ReadPacket = &shmemReadPacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    interface.WritePacket = &shmemWritePacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    interface.GetLastError = &shmemGetLastError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    *result = &single_env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /* initialized TLS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    tlsIndex = sysTlsAlloc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    return JNI_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
}