Path: blob/master/src/jdk.jdi/share/native/libdt_shmem/SharedMemoryTransport.c
41152 views
/*1* Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24#include <stdlib.h>25#include <jni.h>26#include "SharedMemory.h"27#include "com_sun_tools_jdi_SharedMemoryTransportService.h"28#include "jdwpTransport.h"29#include "shmemBase.h"30#include "sys.h"3132/*33* JNI interface to the shared memory transport. These JNI methods34* call the base shared memory support to do the real work.35*36* That is, this is the front-ends interface to our shared memory37* transport establishment code.38*/3940/*41* When initializing the transport from the front end, we use42* standard malloc and free for allocation.43*/44static void *allocateWrapper(jint size) {45return malloc(size);46}47static jdwpTransportCallback callbacks = {allocateWrapper, free};4849void50throwException(JNIEnv *env, char *exceptionClassName, char *message)51{52jclass excClass = (*env)->FindClass(env, exceptionClassName);53if ((*env)->ExceptionOccurred(env)) {54return;55}56(*env)->ThrowNew(env, excClass, message);57}5859void60throwShmemException(JNIEnv *env, char *message, jint errorCode)61{62char msg[80];63char buf[255];6465if (shmemBase_getlasterror(msg, sizeof(msg)) == SYS_OK) {66sprintf(buf, "%s: %s\n", message, msg);67} else {68sprintf(buf, "%s, error code = %d", message, errorCode);69}70throwException(env, "java/io/IOException", buf);71}7273/*74* Class: com_sun_tools_jdi_SharedMemoryTransport75* Method: accept076* Signature: (J)J77*/78JNIEXPORT jlong JNICALL Java_com_sun_tools_jdi_SharedMemoryTransportService_accept079(JNIEnv *env, jobject thisObject, jlong id, jlong timeout)80{81SharedMemoryConnection *connection = NULL;82SharedMemoryTransport *transport = ID_TO_TRANSPORT(id);83jint rc;8485rc = shmemBase_accept(transport, (long)timeout, &connection);86if (rc != SYS_OK) {87if (rc == SYS_TIMEOUT) {88throwException(env, "com/sun/jdi/connect/TransportTimeoutException",89"Timed out waiting for target VM to connect");90} else {91throwShmemException(env, "shmemBase_accept failed", rc);92}93return -1;94}95return CONNECTION_TO_ID(connection);96}9798/*99* Class: com_sun_tools_jdi_SharedMemoryTransport100* Method: attach0101* Signature: (Ljava/lang/String;)J102*/103JNIEXPORT jlong JNICALL Java_com_sun_tools_jdi_SharedMemoryTransportService_attach0104(JNIEnv *env, jobject thisObject, jstring address, jlong timeout)105{106SharedMemoryConnection *connection = NULL;107jint rc;108const char *addrChars;109110addrChars = (*env)->GetStringUTFChars(env, address, NULL);111if ((*env)->ExceptionOccurred(env)) {112return CONNECTION_TO_ID(connection);113} else if (addrChars == NULL) {114throwException(env, "java/lang/InternalError", "GetStringUTFChars failed");115return CONNECTION_TO_ID(connection);116}117118rc = shmemBase_attach(addrChars, (long)timeout, &connection);119if (rc != SYS_OK) {120throwShmemException(env, "shmemBase_attach failed", rc);121}122123(*env)->ReleaseStringUTFChars(env, address, addrChars);124125return CONNECTION_TO_ID(connection);126}127128/*129* Class: com_sun_tools_jdi_SharedMemoryTransport130* Method: name131* Signature: (J)Ljava/lang/String;132*/133JNIEXPORT jstring JNICALL Java_com_sun_tools_jdi_SharedMemoryTransportService_name134(JNIEnv *env, jobject thisObject, jlong id)135{136char *namePtr;137jstring nameString = NULL;138139SharedMemoryTransport *transport = ID_TO_TRANSPORT(id);140jint rc = shmemBase_name(transport, &namePtr);141if (rc != SYS_OK) {142throwShmemException(env, "shmemBase_name failed", rc);143} else {144nameString = (*env)->NewStringUTF(env, namePtr);145if ((nameString == NULL) && !(*env)->ExceptionOccurred(env)) {146throwException(env, "java/lang/InternalError", "Unable to create string");147}148}149return nameString;150}151152/*153* Class: com_sun_tools_jdi_SharedMemoryTransport154* Method: initialize155* Signature: ()V156*/157JNIEXPORT void JNICALL Java_com_sun_tools_jdi_SharedMemoryTransportService_initialize158(JNIEnv *env, jobject thisObject)159{160JavaVM *vm;161jint rc;162163rc = (*env)->GetJavaVM(env, &vm);164if (rc != 0) {165throwException(env, "java/lang/InternalError", "Unable to access Java VM");166return;167}168169rc = shmemBase_initialize(vm, &callbacks);170if (rc != SYS_OK) {171throwException(env, "java/lang/InternalError", "Unable to initialize Shared Memory transport");172return;173}174}175176177/*178* Class: com_sun_tools_jdi_SharedMemoryTransport179* Method: startListening0180* Signature: (Ljava/lang/String;)J181*/182JNIEXPORT jlong JNICALL Java_com_sun_tools_jdi_SharedMemoryTransportService_startListening0183(JNIEnv *env, jobject thisObject, jstring address)184{185const char *addrChars = NULL;186jint rc;187jstring retAddress = NULL;188SharedMemoryTransport *transport = NULL;189190191if (address != NULL) {192addrChars = (*env)->GetStringUTFChars(env, address, NULL);193if ((*env)->ExceptionOccurred(env)) {194return TRANSPORT_TO_ID(transport);195} else if (addrChars == NULL) {196throwException(env, "java/lang/InternalError", "GetStringUTFChars failed");197return TRANSPORT_TO_ID(transport);198}199}200201rc = shmemBase_listen(addrChars, &transport);202if (rc != SYS_OK) {203throwShmemException(env, "shmemBase_listen failed", rc);204}205206if (addrChars != NULL) {207(*env)->ReleaseStringUTFChars(env, address, addrChars);208}209210return TRANSPORT_TO_ID(transport);211}212213/*214* Class: com_sun_tools_jdi_SharedMemoryTransport215* Method: stopListening0216* Signature: (J)V217*/218JNIEXPORT void JNICALL Java_com_sun_tools_jdi_SharedMemoryTransportService_stopListening0219(JNIEnv *env, jobject thisObject, jlong id)220{221SharedMemoryTransport *transport = ID_TO_TRANSPORT(id);222shmemBase_closeTransport(transport);223}224225226