src/jdk.jdi/share/classes/com/sun/tools/jdi/SocketTransportService.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 46091 jdk/src/jdk.jdi/share/classes/com/sun/tools/jdi/SocketTransportService.java@a6f2dc91ba56
child 54884 8a6093c186a6
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
     2
 * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.jdi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    28
import java.io.IOException;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    29
import java.net.Inet6Address;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    30
import java.net.InetAddress;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    31
import java.net.InetSocketAddress;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    32
import java.net.ServerSocket;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    33
import java.net.Socket;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    34
import java.net.SocketTimeoutException;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    35
import java.net.UnknownHostException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    38
import com.sun.jdi.connect.TransportTimeoutException;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    39
import com.sun.jdi.connect.spi.Connection;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    40
import com.sun.jdi.connect.spi.TransportService;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A transport service based on a TCP connection between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * debugger and debugee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class SocketTransportService extends TransportService {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private ResourceBundle messages = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * The listener returned by startListening encapsulates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * the ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static class SocketListenKey extends ListenKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        SocketListenKey(ServerSocket ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            this.ss = ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        ServerSocket socket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            return ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         * Returns the string representation of the address that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         * listen key represents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public String address() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            InetAddress address = ss.getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
             * If bound to the wildcard address then use current local
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
             * hostname. In the event that we don't know our own hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
             * then assume that host supports IPv4 and return something to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
             * represent the loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (address.isAnyLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    address = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                } catch (UnknownHostException uhe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    byte[] loopback = {0x7f,0x00,0x00,0x01};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        address = InetAddress.getByAddress("127.0.0.1", loopback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    } catch (UnknownHostException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        throw new InternalError("unable to get local hostname");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
             * Now decide if we return a hostname or IP address. Where possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
             * return a hostname but in the case that we are bound to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
             * address that isn't registered in the name service then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
             * return an address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            String result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            String hostname = address.getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            String hostaddr = address.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (hostname.equals(hostaddr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                if (address instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    result = "[" + hostaddr + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    result = hostaddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                result = hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
             * Finally return "hostname:port", "ipv4-address:port" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
             * "[ipv6-address]:port".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return result + ":" + ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Handshake with the debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    void handshake(Socket s, long timeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        s.setSoTimeout((int)timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        byte[] hello = "JDWP-Handshake".getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        s.getOutputStream().write(hello);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        byte[] b = new byte[hello.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int received = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        while (received < hello.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                n = s.getInputStream().read(b, received, hello.length-received);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } catch (SocketTimeoutException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                throw new IOException("handshake timeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                throw new IOException("handshake failed - connection prematurally closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            received += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        for (int i=0; i<hello.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (b[i] != hello[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                throw new IOException("handshake failed - unrecognized message from target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // disable read timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        s.setSoTimeout(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * No-arg constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public SocketTransportService() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * The name of this transport service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return "Socket";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Return localized description of this transport service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public String description() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (messages == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                messages = ResourceBundle.getBundle("com.sun.tools.jdi.resources.jdi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return messages.getString("socket_transportservice.description");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Return the capabilities of this transport service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public Capabilities capabilities() {
45714
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   185
        return new TransportService.Capabilities() {
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   186
            public boolean supportsMultipleConnections() {
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   187
                return true;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   188
            }
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   189
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   190
            public boolean supportsAttachTimeout() {
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   191
                return true;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   192
            }
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   193
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   194
            public boolean supportsAcceptTimeout() {
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   195
                return true;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   196
            }
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   197
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   198
            public boolean supportsHandshakeTimeout() {
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   199
                return true;
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   200
            }
1820d351198d 8183012: Code cleanup in com.sun.tools.jdi
clanger
parents: 25859
diff changeset
   201
        };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Attach to the specified address with optional attach and handshake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public Connection attach(String address, long attachTimeout, long handshakeTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (address == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw new NullPointerException("address is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (attachTimeout < 0 || handshakeTimeout < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throw new IllegalArgumentException("timeout is negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int splitIndex = address.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        String portStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (splitIndex < 0) {
24514
2440b44952d7 8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents: 5506
diff changeset
   222
            host = "localhost";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            portStr = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            host = address.substring(0, splitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            portStr = address.substring(splitIndex+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
24514
2440b44952d7 8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents: 5506
diff changeset
   229
        if (host.equals("*")) {
2440b44952d7 8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents: 5506
diff changeset
   230
            host = InetAddress.getLocalHost().getHostName();
2440b44952d7 8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents: 5506
diff changeset
   231
        }
2440b44952d7 8043716: JDI test com/sun/jdi/ProcessAttachTest.sh and other 3 jdi tests failed in nightly
dsamersoff
parents: 5506
diff changeset
   232
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            port = Integer.decode(portStr).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                "unable to parse port number in address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // open TCP connection to VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        InetSocketAddress sa = new InetSocketAddress(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        Socket s = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            s.connect(sa, (int)attachTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } catch (SocketTimeoutException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            } catch (IOException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new TransportTimeoutException("timed out trying to establish connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // handshake with the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            handshake(s, handshakeTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } catch (IOException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            } catch (IOException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return new SocketConnection(s);
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
     * Listen on the specified address and port. Return a listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * that encapsulates the ServerSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    ListenKey startListening(String localaddress, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        InetSocketAddress sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (localaddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            sa = new InetSocketAddress(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            sa = new InetSocketAddress(localaddress, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        ServerSocket ss = new ServerSocket();
46091
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 45714
diff changeset
   278
        if (port == 0) {
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 45714
diff changeset
   279
            // Only need SO_REUSEADDR if we're using a fixed port. If we
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 45714
diff changeset
   280
            // start seeing EADDRINUSE due to collisions in free ports
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 45714
diff changeset
   281
            // then we should retry the bind() a few times.
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 45714
diff changeset
   282
            ss.setReuseAddress(false);
a6f2dc91ba56 8182757: JDWP: Socket Transport handshake hangs on Solaris
gthornbr
parents: 45714
diff changeset
   283
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        ss.bind(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return new SocketListenKey(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Listen on the specified address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public ListenKey startListening(String address) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // use ephemeral port if address isn't specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (address == null || address.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            address = "0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        int splitIndex = address.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        String localaddr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (splitIndex >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            localaddr = address.substring(0, splitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            address = address.substring(splitIndex+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            port = Integer.decode(address).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    "unable to parse port number in address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return startListening(localaddr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Listen on the default address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public ListenKey startListening() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return startListening(null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Stop the listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public void stopListening(ListenKey listener) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (!(listener instanceof SocketListenKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            throw new IllegalArgumentException("Invalid listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        synchronized (listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            ServerSocket ss = ((SocketListenKey)listener).socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            // if the ServerSocket has been closed it means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // the listener is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (ss.isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                throw new IllegalArgumentException("Invalid listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Accept a connection from a debuggee and handshake with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public Connection accept(ListenKey listener, long acceptTimeout, long handshakeTimeout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (acceptTimeout < 0 || handshakeTimeout < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            throw new IllegalArgumentException("timeout is negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (!(listener instanceof SocketListenKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            throw new IllegalArgumentException("Invalid listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        // obtain the ServerSocket from the listener - if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // socket is closed it means the listener is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        synchronized (listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            ss = ((SocketListenKey)listener).socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (ss.isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
               throw new IllegalArgumentException("Invalid listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        // from here onwards it's possible that the ServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // may be closed by a call to stopListening - that's okay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        // because the ServerSocket methods will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // IOException indicating the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        // Additionally, it's possible that another thread calls accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        // with a different accept timeout - that creates a same race
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        // condition between setting the timeout and calling accept.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        // As it is such an unlikely scenario (requires both threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // to be using the same listener we've chosen to ignore the issue).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        ss.setSoTimeout((int)acceptTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } catch (SocketTimeoutException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            throw new TransportTimeoutException("timeout waiting for connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        // handshake here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        handshake(s, handshakeTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return new SocketConnection(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
       return name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
}