src/jdk.jdi/share/native/libdt_shmem/SharedMemoryTransport.c
author pliden
Thu, 23 Aug 2018 10:52:27 +0200
changeset 51503 0265a70ea2a5
parent 47216 71c04702a3d5
permissions -rw-r--r--
8209831: ZGC: Clean up ZRelocationSetSelectorGroup::semi_sort() Reviewed-by: eosterlund, kbarrett
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, 2004, 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 <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <jni.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "SharedMemory.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "com_sun_tools_jdi_SharedMemoryTransportService.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jdwpTransport.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "shmemBase.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "sys.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * JNI interface to the shared memory transport. These JNI methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * call the base shared memory support to do the real work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * That is, this is the front-ends interface to our shared memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * transport establishment code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * When initializing the transport from the front end, we use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * standard malloc and free for allocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
static void *allocateWrapper(jint size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    return malloc(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
static jdwpTransportCallback callbacks = {allocateWrapper, free};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
throwException(JNIEnv *env, char *exceptionClassName, char *message)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    jclass excClass = (*env)->FindClass(env, exceptionClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    (*env)->ThrowNew(env, excClass, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
throwShmemException(JNIEnv *env, char *message, jint errorCode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    char msg[80];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    char buf[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    if (shmemBase_getlasterror(msg, sizeof(msg)) == SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        sprintf(buf, "%s: %s\n", message, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        sprintf(buf, "%s, error code = %d", message, errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    throwException(env, "java/io/IOException", buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Class:     com_sun_tools_jdi_SharedMemoryTransport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * Method:    accept0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Signature: (J)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
JNIEXPORT jlong JNICALL Java_com_sun_tools_jdi_SharedMemoryTransportService_accept0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
  (JNIEnv *env, jobject thisObject, jlong id, jlong timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    SharedMemoryConnection *connection = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    SharedMemoryTransport *transport = ID_TO_TRANSPORT(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    rc = shmemBase_accept(transport, (long)timeout, &connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    if (rc != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (rc == SYS_TIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throwException(env, "com/sun/jdi/connect/TransportTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                "Timed out waiting for target VM to connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throwShmemException(env, "shmemBase_accept failed", rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    return CONNECTION_TO_ID(connection);
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
 * Class:     com_sun_tools_jdi_SharedMemoryTransport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * Method:    attach0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * Signature: (Ljava/lang/String;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
JNIEXPORT jlong JNICALL Java_com_sun_tools_jdi_SharedMemoryTransportService_attach0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
  (JNIEnv *env, jobject thisObject, jstring address, jlong timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    SharedMemoryConnection *connection = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    const char *addrChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    addrChars = (*env)->GetStringUTFChars(env, address, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return CONNECTION_TO_ID(connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    } else if (addrChars == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        throwException(env, "java/lang/InternalError", "GetStringUTFChars failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return CONNECTION_TO_ID(connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    rc = shmemBase_attach(addrChars, (long)timeout, &connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    if (rc != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        throwShmemException(env, "shmemBase_attach failed", rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    (*env)->ReleaseStringUTFChars(env, address, addrChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    return CONNECTION_TO_ID(connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * Class:     com_sun_tools_jdi_SharedMemoryTransport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * Method:    name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * Signature: (J)Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
JNIEXPORT jstring JNICALL Java_com_sun_tools_jdi_SharedMemoryTransportService_name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  (JNIEnv *env, jobject thisObject, jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    char *namePtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    jstring nameString = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    SharedMemoryTransport *transport = ID_TO_TRANSPORT(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    jint rc = shmemBase_name(transport, &namePtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    if (rc != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        throwShmemException(env, "shmemBase_name failed", rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        nameString = (*env)->NewStringUTF(env, namePtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if ((nameString == NULL) && !(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throwException(env, "java/lang/InternalError", "Unable to create string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    return nameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * Class:     com_sun_tools_jdi_SharedMemoryTransport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * Method:    initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
JNIEXPORT void JNICALL Java_com_sun_tools_jdi_SharedMemoryTransportService_initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
  (JNIEnv *env, jobject thisObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    JavaVM *vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    rc = (*env)->GetJavaVM(env, &vm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    if (rc != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        throwException(env, "java/lang/InternalError", "Unable to access Java VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    rc = shmemBase_initialize(vm, &callbacks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    if (rc != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        throwException(env, "java/lang/InternalError", "Unable to initialize Shared Memory transport");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * Class:     com_sun_tools_jdi_SharedMemoryTransport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * Method:    startListening0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * Signature: (Ljava/lang/String;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
JNIEXPORT jlong JNICALL Java_com_sun_tools_jdi_SharedMemoryTransportService_startListening0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
  (JNIEnv *env, jobject thisObject, jstring address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    const char *addrChars = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    jstring retAddress = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    SharedMemoryTransport *transport = 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
    if (address != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        addrChars = (*env)->GetStringUTFChars(env, address, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return TRANSPORT_TO_ID(transport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } else if (addrChars == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            throwException(env, "java/lang/InternalError", "GetStringUTFChars failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return TRANSPORT_TO_ID(transport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    rc = shmemBase_listen(addrChars, &transport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    if (rc != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        throwShmemException(env, "shmemBase_listen failed", rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    if (addrChars != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        (*env)->ReleaseStringUTFChars(env, address, addrChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    return TRANSPORT_TO_ID(transport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * Class:     com_sun_tools_jdi_SharedMemoryTransport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * Method:    stopListening0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * Signature: (J)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
JNIEXPORT void JNICALL Java_com_sun_tools_jdi_SharedMemoryTransportService_stopListening0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
  (JNIEnv *env, jobject thisObject, jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    SharedMemoryTransport *transport = ID_TO_TRANSPORT(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    shmemBase_closeTransport(transport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
}