jdk/src/share/back/transport.c
author sla
Tue, 05 Mar 2013 19:25:35 +0100
changeset 16057 16a81953a291
parent 14698 9294fcf94c46
child 16726 f76b2e6bd199
permissions -rw-r--r--
8009397: test/com/sun/jdi/PrivateTransportTest.sh: ERROR: transport library missing onLoad entry: private_dt_socket Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
     2
 * Copyright (c) 1998, 2012, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "transport.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "debugLoop.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "sys.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
static jdwpTransportEnv *transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
static jrawMonitorID listenerLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
static jrawMonitorID sendLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * data structure used for passing transport info from thread to thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
typedef struct TransportInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    char *name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    jdwpTransportEnv *transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    char *address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    long timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
} TransportInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
static struct jdwpTransportCallback callback = {jvmtiAllocate, jvmtiDeallocate};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Print the last transport error
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
printLastError(jdwpTransportEnv *t, jdwpTransportError err)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    char  *msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    jbyte *utf8msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    jdwpTransportError rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    msg     = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    utf8msg = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    rv = (*t)->GetLastError(t, &msg); /* This is a platform encoded string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    if ( msg != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int maxlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        /* Convert this string to UTF8 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        len = (int)strlen(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        maxlen = len+len/2+2; /* Should allow for plenty of room */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        utf8msg = (jbyte*)jvmtiAllocate(maxlen+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        (void)(gdata->npt->utf8FromPlatform)(gdata->npt->utf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            msg, len, utf8msg, maxlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        utf8msg[maxlen] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    if (rv == JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        ERROR_MESSAGE(("transport error %d: %s",err, utf8msg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    } else if ( msg!=NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        ERROR_MESSAGE(("transport error %d: %s",err, utf8msg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        ERROR_MESSAGE(("transport error %d: %s",err, "UNKNOWN"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    jvmtiDeallocate(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    jvmtiDeallocate(utf8msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
/* Find OnLoad symbol */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
static jdwpTransport_OnLoad_t
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
findTransportOnLoad(void *handle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    jdwpTransport_OnLoad_t onLoad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    onLoad = (jdwpTransport_OnLoad_t)NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    if (handle == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return onLoad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    onLoad = (jdwpTransport_OnLoad_t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                 dbgsysFindLibraryEntry(handle, "jdwpTransport_OnLoad");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    return onLoad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
/* Load transport library (directory=="" means do system search) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
static void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
loadTransportLibrary(char *libdir, char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    void *handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    char libname[MAXPATHLEN+2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    char buf[MAXPATHLEN*2+100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    char *plibdir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /* Convert libdir from UTF-8 to platform encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    plibdir = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    if ( libdir != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int  len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        len = (int)strlen(libdir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        (void)(gdata->npt->utf8ToPlatform)(gdata->npt->utf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            (jbyte*)libdir, len, buf, (int)sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        plibdir = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /* Construct library name (simple name or full path) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    dbgsysBuildLibName(libname, sizeof(libname), plibdir, name);
16057
16a81953a291 8009397: test/com/sun/jdi/PrivateTransportTest.sh: ERROR: transport library missing onLoad entry: private_dt_socket
sla
parents: 14698
diff changeset
   120
    if (strlen(libname) == 0) {
16a81953a291 8009397: test/com/sun/jdi/PrivateTransportTest.sh: ERROR: transport library missing onLoad entry: private_dt_socket
sla
parents: 14698
diff changeset
   121
        return NULL;
16a81953a291 8009397: test/com/sun/jdi/PrivateTransportTest.sh: ERROR: transport library missing onLoad entry: private_dt_socket
sla
parents: 14698
diff changeset
   122
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /* dlopen (unix) / LoadLibrary (windows) the transport library */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    handle = dbgsysLoadLibrary(libname, buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    return handle;
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
 * loadTransport() is adapted from loadJVMHelperLib() in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * JDK 1.2 javai.c v1.61
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
static jdwpError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
loadTransport(char *name, jdwpTransportEnv **transportPtr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    JNIEnv                 *env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    jdwpTransport_OnLoad_t  onLoad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    void                   *handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    char                   *libdir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /* Make sure library name is not empty */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    if (name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        ERROR_MESSAGE(("library name is empty"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return JDWP_ERROR(TRANSPORT_LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /* First, look in sun.boot.library.path. This should find the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *  dt_socket and dt_shmem transport libraries, or any library
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *  that was delivered with the J2SE.
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
   150
     *  Note: Since 6819213 fixed, Java property sun.boot.library.path can
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
   151
     *  contain multiple paths. Dll_dir is the first entry and
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 5506
diff changeset
   152
     *  -Dsun.boot.library.path entries are appended.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    libdir = gdata->property_sun_boot_library_path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    if (libdir == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        ERROR_MESSAGE(("Java property sun.boot.library.path is not set"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return JDWP_ERROR(TRANSPORT_LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    handle = loadTransportLibrary(libdir, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    if (handle == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        /* Second, look along the path used by the native dlopen/LoadLibrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         *  functions. This should effectively try and load the simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         *  library name, which will cause the default system library
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         *  search technique to happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         *  We should only reach here if the transport library wasn't found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         *  in the J2SE directory, e.g. it's a custom transport library
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         *  not installed in the J2SE like dt_socket and dt_shmem is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         *  Note: Why not use java.library.path? Several reasons:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         *        a) This matches existing agentlib search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         *        b) These are technically not JNI libraries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        handle = loadTransportLibrary("", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /* See if a library was found with this name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    if (handle == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        ERROR_MESSAGE(("transport library not found: %s", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return JDWP_ERROR(TRANSPORT_LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /* Find the onLoad address */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    onLoad = findTransportOnLoad(handle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    if (onLoad == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        ERROR_MESSAGE(("transport library missing onLoad entry: %s", name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return JDWP_ERROR(TRANSPORT_LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /* Get transport interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    env = getEnv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    if ( env != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        jdwpTransportEnv *t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        JavaVM           *jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        jint              ver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        JNI_FUNC_PTR(env,GetJavaVM)(env, &jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        ver = (*onLoad)(jvm, &callback, JDWPTRANSPORT_VERSION_1_0, &t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (ver != JNI_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            switch (ver) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                case JNI_ENOMEM :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    ERROR_MESSAGE(("insufficient memory to complete initialization"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                case JNI_EVERSION :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    ERROR_MESSAGE(("transport doesn't recognize version %x",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        JDWPTRANSPORT_VERSION_1_0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                case JNI_EEXIST :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    ERROR_MESSAGE(("transport doesn't support multiple environments"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    ERROR_MESSAGE(("unrecognized error %d from transport", ver));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return JDWP_ERROR(TRANSPORT_INIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        *transportPtr = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return JDWP_ERROR(TRANSPORT_LOAD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    return JDWP_ERROR(NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
connectionInitiated(jdwpTransportEnv *t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    jint isValid = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    debugMonitorEnter(listenerLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Don't allow a connection until initialization is complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    debugInit_waitInitComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /* Are we the first transport to get a connection? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    if (transport == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        transport = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        isValid = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (transport == t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            /* connected with the same transport as before */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            isValid = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
             * Another transport got a connection - multiple transports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
             * not fully supported yet so shouldn't get here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            (*t)->Close(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            JDI_ASSERT(JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    if (isValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        debugMonitorNotifyAll(listenerLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    debugMonitorExit(listenerLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    if (isValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        debugLoop_run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 * Set the transport property (sun.jdwp.listenerAddress) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
setTransportProperty(JNIEnv* env, char* value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    char* prop_value = (value == NULL) ? "" : value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    setAgentPropertyValue(env, "sun.jdwp.listenerAddress", prop_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
transport_waitForConnection(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * If the VM is suspended on debugger initialization, we wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * for a connection before continuing. This ensures that all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * events are delivered to the debugger. (We might as well do this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * this since the VM won't continue until a remote debugger attaches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * and resumes it.) If not suspending on initialization, we must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * just drop any packets (i.e. events) so that the VM can continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * to run. The debugger may not attach until much later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    if (debugInit_suspendOnInit()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        debugMonitorEnter(listenerLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        while (transport == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            debugMonitorWait(listenerLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        debugMonitorExit(listenerLock);
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 void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
acceptThread(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void* arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    TransportInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    jdwpTransportEnv *t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    jdwpTransportError rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    LOG_MISC(("Begin accept thread"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    info = (TransportInfo*)(void*)arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    t = info->transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    rc = (*t)->Accept(t, info->timeout, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /* System property no longer needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    setTransportProperty(jni_env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    if (rc != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * If accept fails it probably means a timeout, or another fatal error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * We thus exit the VM after stopping the listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        printLastError(t, rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        (*t)->StopListening(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        EXIT_ERROR(JVMTI_ERROR_NONE, "could not connect, timeout or fatal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        (*t)->StopListening(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        connectionInitiated(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    LOG_MISC(("End accept thread"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
attachThread(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void* arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    LOG_MISC(("Begin attach thread"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    connectionInitiated((jdwpTransportEnv *)(void*)arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    LOG_MISC(("End attach thread"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
transport_initialize(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    transport = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    listenerLock = debugMonitorCreate("JDWP Transport Listener Monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    sendLock = debugMonitorCreate("JDWP Transport Send Monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
transport_reset(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Reset the transport by closing any listener (will silently fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * with JDWPTRANSPORT_ERROR_ILLEGAL_STATE if not listening), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * closing any connection (will also fail silently if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * connected).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Note: There's an assumption here that we don't yet support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * multiple transports. When we do then we need a clear transition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * from the current transport to the new transport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    if (transport != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        setTransportProperty(getEnv(), NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        (*transport)->StopListening(transport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        (*transport)->Close(transport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
static jdwpError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
launch(char *command, char *name, char *address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    char *commandLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    int  len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /* Construct complete command line (all in UTF-8) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    commandLine = jvmtiAllocate((int)strlen(command) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                 (int)strlen(name) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                 (int)strlen(address) + 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    if (commandLine == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return JDWP_ERROR(OUT_OF_MEMORY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    (void)strcpy(commandLine, command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    (void)strcat(commandLine, " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    (void)strcat(commandLine, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    (void)strcat(commandLine, " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    (void)strcat(commandLine, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /* Convert commandLine from UTF-8 to platform encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    len = (int)strlen(commandLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    buf = jvmtiAllocate(len*3+3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    (void)(gdata->npt->utf8ToPlatform)(gdata->npt->utf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        (jbyte*)commandLine, len, buf, len*3+3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /* Exec commandLine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    rc = dbgsysExec(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /* Free up buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    jvmtiDeallocate(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    jvmtiDeallocate(commandLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /* And non-zero exit status means we had an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    if (rc != SYS_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return JDWP_ERROR(TRANSPORT_INIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    return JDWP_ERROR(NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
jdwpError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
transport_startTransport(jboolean isServer, char *name, char *address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                         long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    jvmtiStartFunction func;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    jdwpTransportEnv *trans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    char threadName[MAXPATHLEN + 100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    jint err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    jdwpError serror;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * If the transport is already loaded then use it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Note: We're assuming here that we don't support multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * transports - when we do then we need to handle the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * where the transport library only supports a single environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * That probably means we have a bag a transport environments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * to correspond to the transports bag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    if (transport != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        trans = transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        serror = loadTransport(name, &trans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (serror != JDWP_ERROR(NONE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            return serror;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    if (isServer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        char *retAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        char *launchCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        TransportInfo *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        char* prop_value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        info = jvmtiAllocate(sizeof(*info));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (info == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            return JDWP_ERROR(OUT_OF_MEMORY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        info->name = jvmtiAllocate((int)strlen(name)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        (void)strcpy(info->name, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        info->address = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        info->timeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (info->name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            serror = JDWP_ERROR(OUT_OF_MEMORY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            goto handleError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (address != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            info->address = jvmtiAllocate((int)strlen(address)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            (void)strcpy(info->address, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            if (info->address == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                serror = JDWP_ERROR(OUT_OF_MEMORY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                goto handleError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        info->transport = trans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        err = (*trans)->StartListening(trans, address, &retAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        if (err != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            printLastError(trans, err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            serror = JDWP_ERROR(TRANSPORT_INIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            goto handleError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         * Record listener address in a system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         */
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   481
        len = (int)strlen(name) + (int)strlen(retAddress) + 2; /* ':' and '\0' */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        prop_value = (char*)jvmtiAllocate(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        strcpy(prop_value, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        strcat(prop_value, ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        strcat(prop_value, retAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        setTransportProperty(getEnv(), prop_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        jvmtiDeallocate(prop_value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        (void)strcpy(threadName, "JDWP Transport Listener: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        (void)strcat(threadName, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        func = &acceptThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        error = spawnNewThread(func, (void*)info, threadName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (error != JVMTI_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            serror = map2jdwpError(error);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            goto handleError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        launchCommand = debugInit_launchOnInit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (launchCommand != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            serror = launch(launchCommand, name, retAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            if (serror != JDWP_ERROR(NONE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                goto handleError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            if ( ! gdata->quiet ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                TTY_MESSAGE(("Listening for transport %s at address: %s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    name, retAddress));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        return JDWP_ERROR(NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
handleError:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        jvmtiDeallocate(info->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        jvmtiDeallocate(info->address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        jvmtiDeallocate(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
         * Note that we don't attempt to do a launch here. Launching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
         * is currently supported only in server mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         * If we're connecting to another process, there shouldn't be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         * any concurrent listens, so its ok if we block here in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
         * thread, waiting for the attach to finish.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
         err = (*trans)->Attach(trans, address, timeout, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
         if (err != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
             printLastError(trans, err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
             serror = JDWP_ERROR(TRANSPORT_INIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
             return serror;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
         /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
          * Start the transport loop in a separate thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
          */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
         (void)strcpy(threadName, "JDWP Transport Listener: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
         (void)strcat(threadName, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         func = &attachThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         err = spawnNewThread(func, (void*)trans, threadName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         serror = map2jdwpError(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    return serror;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
transport_close(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    if ( transport != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        (*transport)->Close(transport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
transport_is_open(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    jboolean is_open = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    if ( transport != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        is_open = (*transport)->IsOpen(transport);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    return is_open;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
transport_sendPacket(jdwpPacket *packet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    jdwpTransportError err = JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    jint rc = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    if (transport != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if ( (*transport)->IsOpen(transport) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            debugMonitorEnter(sendLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            err = (*transport)->WritePacket(transport, packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            debugMonitorExit(sendLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (err != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            if ((*transport)->IsOpen(transport)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                printLastError(transport, err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
             * The users of transport_sendPacket except 0 for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
             * success; non-0 otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            rc = (jint)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    } /* else, bit bucket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    return rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
transport_receivePacket(jdwpPacket *packet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    jdwpTransportError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    err = (*transport)->ReadPacket(transport, packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    if (err != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         * If transport has been closed return EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if (!(*transport)->IsOpen(transport)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            packet->type.cmd.len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        printLastError(transport, err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         * Users of transport_receivePacket expect 0 for success,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         * non-0 otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return (jint)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
}