jdk/src/share/transport/socket/socketTransport.c
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 3723 a27278866f80
child 12047 320a714614e9
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: 3723
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: 3723
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: 3723
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: 3723
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3723
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3723
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jdwpTransport.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "sysSocket.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The Socket Transport Library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This module is an implementation of the Java Debug Wire Protocol Transport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Service Provider Interface - see src/share/javavm/export/jdwpTransport.h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
static int serverSocketFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
static int socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static jdwpTransportCallback *callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
static JavaVM *jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
static int tlsIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
static jboolean initialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
static struct jdwpTransportNativeInterface_ interface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
static jdwpTransportEnv single_env = (jdwpTransportEnv)&interface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#define RETURN_ERROR(err, msg) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (1==1) { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            setLastError(err, msg); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            return err; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#define RETURN_IO_ERROR(msg)    RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
#define RETURN_RECV_ERROR(n) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (n == 0) { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            RETURN_ERROR(JDWPTRANSPORT_ERROR_IO_ERROR, "premature EOF"); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        } else { \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            RETURN_IO_ERROR("recv error"); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
#define HEADER_SIZE     11
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#define MAX_DATA_SIZE 1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Record the last error for this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
setLastError(jdwpTransportError err, char *newmsg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    char buf[255];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    char *msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /* get any I/O first in case any system calls override errno */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    if (err == JDWPTRANSPORT_ERROR_IO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        dbgsysGetLastIOError(buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    msg = (char *)dbgsysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        (*callback->free)(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    if (err == JDWPTRANSPORT_ERROR_IO_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        char *join_str = ": ";
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
    88
        int msg_len = (int)strlen(newmsg) + (int)strlen(join_str) +
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
    89
                      (int)strlen(buf) + 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        msg = (*callback->alloc)(msg_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            strcpy(msg, newmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            strcat(msg, join_str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            strcat(msg, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    } else {
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
    97
        msg = (*callback->alloc)((int)strlen(newmsg)+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            strcpy(msg, newmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    dbgsysTlsPut(tlsIndex, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * Return the last error for this thread (may be NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
static char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
getLastError() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    return (char *)dbgsysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
static jdwpTransportError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
setOptions(int fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    jvalue dontcare;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    dontcare.i = 0;  /* keep compiler happy */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    err = dbgsysSetSocketOption(fd, SO_REUSEADDR, JNI_TRUE, dontcare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        RETURN_IO_ERROR("setsockopt SO_REUSEADDR failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    err = dbgsysSetSocketOption(fd, TCP_NODELAY, JNI_TRUE, dontcare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        RETURN_IO_ERROR("setsockopt TCPNODELAY failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
static jdwpTransportError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
handshake(int fd, jlong timeout) {
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   137
    const char *hello = "JDWP-Handshake";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    char b[16];
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   139
    int rv, helloLen, received;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        dbgsysConfigureBlocking(fd, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   144
    helloLen = (int)strlen(hello);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    received = 0;
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   146
    while (received < helloLen) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            rv = dbgsysPoll(fd, JNI_TRUE, JNI_FALSE, (long)timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (rv <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                setLastError(0, "timeout during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        buf = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        buf += received;
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   158
        n = dbgsysRecv(fd, buf, helloLen-received, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            setLastError(0, "handshake failed - connection prematurally closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            RETURN_IO_ERROR("recv failed during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        received += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        dbgsysConfigureBlocking(fd, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   171
    if (strncmp(b, hello, received) != 0) {
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   172
        char msg[80+2*16];
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   173
        b[received] = '\0';
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   174
        /*
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   175
         * We should really use snprintf here but it's not available on Windows.
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   176
         * We can't use jio_snprintf without linking the transport against the VM.
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   177
         */
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   178
        sprintf(msg, "handshake failed - received >%s< - expected >%s<", b, hello);
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   179
        setLastError(0, msg);
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   180
        return JDWPTRANSPORT_ERROR_IO_ERROR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
3723
a27278866f80 6432567: PIT : com/sun/jdi/BadHandshakeTest.java fails due to java.net.ConnectException
alanb
parents: 1247
diff changeset
   183
    if (dbgsysSend(fd, (char*)hello, helloLen, 0) != helloLen) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        RETURN_IO_ERROR("send failed during handshake");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
static jdwpTransportError
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   190
parseAddress(const char *address, struct sockaddr_in *sa, uint32_t defaultHost) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    char *colon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    memset((void *)sa,0,sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    sa->sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /* check for host:port or port */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    colon = strchr(address, ':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    if (colon == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        u_short port = (u_short)atoi(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        sa->sin_port = dbgsysHostToNetworkShort(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        sa->sin_addr.s_addr = dbgsysHostToNetworkLong(defaultHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        char *buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        char *hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        u_short port;
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   206
        uint32_t addr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   208
        buf = (*callback->alloc)((int)strlen(address)+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (buf == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        strcpy(buf, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        buf[colon - address] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        hostname = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        port = atoi(colon + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        sa->sin_port = dbgsysHostToNetworkShort(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * First see if the host is a literal IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * If not then try to resolve it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        addr = dbgsysInetAddr(hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (addr == 0xffffffff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            struct hostent *hp = dbgsysGetHostByName(hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (hp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                /* don't use RETURN_IO_ERROR as unknown host is normal */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                setLastError(0, "gethostbyname: unknown host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                (*callback->free)(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            /* lookup was successful */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            memcpy(&(sa->sin_addr), hp->h_addr_list[0], hp->h_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            sa->sin_addr.s_addr = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        (*callback->free)(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
socketTransport_getCapabilities(jdwpTransportEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        JDWPTransportCapabilities* capabilitiesPtr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    JDWPTransportCapabilities result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    memset(&result, 0, sizeof(result));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    result.can_timeout_attach = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    result.can_timeout_accept = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    result.can_timeout_handshake = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    *capabilitiesPtr = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
socketTransport_startListening(jdwpTransportEnv* env, const char* address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                               char** actualAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    struct sockaddr_in sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    memset((void *)&sa,0,sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    sa.sin_family = AF_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /* no address provided */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    if ((address == NULL) || (address[0] == '\0')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        address = "0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    err = parseAddress(address, &sa, INADDR_ANY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    if (err != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    serverSocketFD = dbgsysSocket(AF_INET, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    if (serverSocketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        RETURN_IO_ERROR("socket creation failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    err = setOptions(serverSocketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    err = dbgsysBind(serverSocketFD, (struct sockaddr *)&sa, sizeof(sa));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        RETURN_IO_ERROR("bind failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    err = dbgsysListen(serverSocketFD, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        RETURN_IO_ERROR("listen failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        char buf[20];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        int len = sizeof(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        jint portNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        err = dbgsysGetSocketName(serverSocketFD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                               (struct sockaddr *)&sa, &len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        portNum = dbgsysNetworkToHostShort(sa.sin_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        sprintf(buf, "%d", portNum);
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   310
        *actualAddress = (*callback->alloc)((int)strlen(buf) + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (*actualAddress == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            strcpy(*actualAddress, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
socketTransport_accept(jdwpTransportEnv* env, jlong acceptTimeout, jlong handshakeTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    int socketLen, err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    struct sockaddr_in socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    jlong startTime = (jlong)0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Use a default handshake timeout if not specified - this avoids an indefinite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * hang in cases where something other than a debugger connects to our port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    if (handshakeTimeout == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        handshakeTimeout = 2000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * If there is an accept timeout then we put the socket in non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         * mode and poll for a connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (acceptTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            dbgsysConfigureBlocking(serverSocketFD, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            startTime = dbgsysCurrentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            rv = dbgsysPoll(serverSocketFD, JNI_TRUE, JNI_FALSE, (long)acceptTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (rv <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                /* set the last error here as could be overridden by configureBlocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "poll failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                /* restore blocking state */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if (rv == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "timed out waiting for connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         * Accept the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        memset((void *)&socket,0,sizeof(struct sockaddr_in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        socketLen = sizeof(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        socketFD = dbgsysAccept(serverSocketFD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                (struct sockaddr *)&socket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                &socketLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        /* set the last error here as could be overridden by configureBlocking */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (socketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            setLastError(JDWPTRANSPORT_ERROR_IO_ERROR, "accept failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         * Restore the blocking state - note that the accepted socket may be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * blocking or non-blocking mode (platform dependent). However as there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         * is a handshake timeout set then it will go into non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         * anyway for the handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (acceptTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            dbgsysConfigureBlocking(serverSocketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (socketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        /* handshake with the debugger */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        err = handshake(socketFD, handshakeTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * If the handshake fails then close the connection. If there if an accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * timeout then we must adjust the timeout for the next poll.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            fprintf(stderr, "Debugger failed to attach: %s\n", getLastError());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            dbgsysSocketClose(socketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            if (acceptTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                long endTime = dbgsysCurrentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                acceptTimeout -= (endTime - startTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                if (acceptTimeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    setLastError(JDWPTRANSPORT_ERROR_IO_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        "timeout waiting for debugger to connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    } while (socketFD < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    return JDWPTRANSPORT_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
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
socketTransport_stopListening(jdwpTransportEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    if (serverSocketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_STATE, "connection not open");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    if (dbgsysSocketClose(serverSocketFD) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        RETURN_IO_ERROR("close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    serverSocketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
socketTransport_attach(jdwpTransportEnv* env, const char* addressString, jlong attachTimeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                       jlong handshakeTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    struct sockaddr_in sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    int err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    if (addressString == NULL || addressString[0] == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "address is missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    err = parseAddress(addressString, &sa, 0x7f000001);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    if (err != JDWPTRANSPORT_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    socketFD = dbgsysSocket(AF_INET, SOCK_STREAM, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    if (socketFD < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        RETURN_IO_ERROR("unable to create socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    err = setOptions(socketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * To do a timed connect we make the socket non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * and poll with a timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    if (attachTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        dbgsysConfigureBlocking(socketFD, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    err = dbgsysConnect(socketFD, (struct sockaddr *)&sa, sizeof(sa));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    if (err == DBG_EINPROGRESS && attachTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        err = dbgsysFinishConnect(socketFD, (long)attachTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (err == DBG_ETIMEOUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            dbgsysConfigureBlocking(socketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            RETURN_ERROR(JDWPTRANSPORT_ERROR_TIMEOUT, "connect timed out");
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
    if (err < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        RETURN_IO_ERROR("connect failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    if (attachTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        dbgsysConfigureBlocking(socketFD, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    err = handshake(socketFD, handshakeTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    if (err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        dbgsysSocketClose(socketFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
static jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
socketTransport_isOpen(jdwpTransportEnv* env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    if (socketFD >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
socketTransport_close(jdwpTransportEnv* env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    int fd = socketFD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    socketFD = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    if (dbgsysSocketClose(fd) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         * close failed - it's pointless to restore socketFD here because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         * any subsequent close will likely fail aswell.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        RETURN_IO_ERROR("close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
socketTransport_writePacket(jdwpTransportEnv* env, const jdwpPacket *packet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    jint len, data_len, id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * room for header and up to MAX_DATA_SIZE data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    char header[HEADER_SIZE + MAX_DATA_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    jbyte *data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /* packet can't be null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    if (packet == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    len = packet->type.cmd.len;         /* includes header */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    data_len = len - HEADER_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /* bad packet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    if (data_len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "invalid length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /* prepare the header for transmission */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    len = (jint)dbgsysHostToNetworkLong(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    id = (jint)dbgsysHostToNetworkLong(packet->type.cmd.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    memcpy(header + 0, &len, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    memcpy(header + 4, &id, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    header[8] = packet->type.cmd.flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        jshort errorCode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            dbgsysHostToNetworkShort(packet->type.reply.errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        memcpy(header + 9, &errorCode, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        header[9] = packet->type.cmd.cmdSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        header[10] = packet->type.cmd.cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    data = packet->type.cmd.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /* Do one send for short packets, two for longer ones */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    if (data_len <= MAX_DATA_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        memcpy(header + HEADER_SIZE, data, data_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (dbgsysSend(socketFD, (char *)&header, HEADER_SIZE + data_len, 0) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            HEADER_SIZE + data_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            RETURN_IO_ERROR("send failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        memcpy(header + HEADER_SIZE, data, MAX_DATA_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (dbgsysSend(socketFD, (char *)&header, HEADER_SIZE + MAX_DATA_SIZE, 0) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            HEADER_SIZE + MAX_DATA_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            RETURN_IO_ERROR("send failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        /* Send the remaining data bytes right out of the data area. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if (dbgsysSend(socketFD, (char *)data + MAX_DATA_SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                       data_len - MAX_DATA_SIZE, 0) != data_len - MAX_DATA_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            RETURN_IO_ERROR("send failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
static jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
recv_fully(int f, char *buf, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    int nbytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    while (nbytes < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        int res = dbgsysRecv(f, buf + nbytes, len - nbytes, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (res < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        } else if (res == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            break; /* eof, return nbytes which is less than len */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        nbytes += res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    return nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
socketTransport_readPacket(jdwpTransportEnv* env, jdwpPacket* packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    jint length, data_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    jint n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    /* packet can't be null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    if (packet == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        RETURN_ERROR(JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT, "packet is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    /* read the length field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    n = recv_fully(socketFD, (char *)&length, sizeof(jint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /* check for EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        packet->type.cmd.len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    if (n != sizeof(jint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    length = (jint)dbgsysNetworkToHostLong(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    packet->type.cmd.len = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    n = recv_fully(socketFD,(char *)&(packet->type.cmd.id),sizeof(jint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    if (n < (int)sizeof(jint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    packet->type.cmd.id = (jint)dbgsysNetworkToHostLong(packet->type.cmd.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    n = recv_fully(socketFD,(char *)&(packet->type.cmd.flags),sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    if (n < (int)sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    if (packet->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        n = recv_fully(socketFD,(char *)&(packet->type.reply.errorCode),sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        if (n < (int)sizeof(jshort)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        /* FIXME - should the error be converted to host order?? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmdSet),sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (n < (int)sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        n = recv_fully(socketFD,(char *)&(packet->type.cmd.cmd),sizeof(jbyte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if (n < (int)sizeof(jbyte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    data_len = length - ((sizeof(jint) * 2) + (sizeof(jbyte) * 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    if (data_len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        setLastError(0, "Badly formed packet received - invalid length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return JDWPTRANSPORT_ERROR_IO_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    } else if (data_len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        packet->type.cmd.data = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        packet->type.cmd.data= (*callback->alloc)(data_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (packet->type.cmd.data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            RETURN_ERROR(JDWPTRANSPORT_ERROR_OUT_OF_MEMORY, "out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        n = recv_fully(socketFD,(char *)packet->type.cmd.data, data_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if (n < data_len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            (*callback->free)(packet->type.cmd.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            RETURN_RECV_ERROR(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
static jdwpTransportError JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
socketTransport_getLastError(jdwpTransportEnv* env, char** msgP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    char *msg = (char *)dbgsysTlsGet(tlsIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    if (msg == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        return JDWPTRANSPORT_ERROR_MSG_NOT_AVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    }
896
5c02031316bf 6725543: Compiler warnings in serviceability native code
ohair
parents: 2
diff changeset
   683
    *msgP = (*callback->alloc)((int)strlen(msg)+1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    if (*msgP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        return JDWPTRANSPORT_ERROR_OUT_OF_MEMORY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    strcpy(*msgP, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    return JDWPTRANSPORT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
jdwpTransport_OnLoad(JavaVM *vm, jdwpTransportCallback* cbTablePtr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                     jint version, jdwpTransportEnv** result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    if (version != JDWPTRANSPORT_VERSION_1_0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        return JNI_EVERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    if (initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         * This library doesn't support multiple environments (yet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        return JNI_EEXIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    initialized = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    jvm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    callback = cbTablePtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /* initialize interface table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    interface.GetCapabilities = &socketTransport_getCapabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    interface.Attach = &socketTransport_attach;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    interface.StartListening = &socketTransport_startListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    interface.StopListening = &socketTransport_stopListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    interface.Accept = &socketTransport_accept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    interface.IsOpen = &socketTransport_isOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    interface.Close = &socketTransport_close;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    interface.ReadPacket = &socketTransport_readPacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    interface.WritePacket = &socketTransport_writePacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    interface.GetLastError = &socketTransport_getLastError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    *result = &single_env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    /* initialized TLS */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    tlsIndex = dbgsysTlsAlloc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    return JNI_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
}