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