jdk/test/com/sun/jdi/ExclusiveBind.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 20201 50cc2d25a60b
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
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: 4342
diff changeset
     2
 * Copyright (c) 2003, 2006, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4342
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4342
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4342
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4531526
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test that more than one debuggee cannot bind to same port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          at the same time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @build VMConnection ExclusiveBind HelloWorld
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main ExclusiveBind
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.Bootstrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.jdi.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.connect.Connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.jdi.connect.AttachingConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class ExclusiveBind {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Helper class to direct process output to the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * System.out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static class IOHandler implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        IOHandler(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            this.in = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        static void handle(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            IOHandler handler = new IOHandler(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            Thread thr = new Thread(handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            thr.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    int n = in.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    if (n < 0) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    for (int i=0; i<n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                        System.out.print((char)b[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Find a connector by name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static Connector findConnector(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        List connectors = Bootstrap.virtualMachineManager().allConnectors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        Iterator iter = connectors.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            Connector connector = (Connector)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (connector.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                return connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Launch (in server mode) a debuggee with the given address and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * suspend mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static Process launch(String address, boolean suspend, String class_name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        String exe = System.getProperty("java.home") + File.separator + "bin" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String arch = System.getProperty("os.arch");
4342
9f3aa1172803 6905705: Fix broken exit code values in jdk/test/Makefile
ohair
parents: 4335
diff changeset
   102
        String osname = System.getProperty("os.name");
9f3aa1172803 6905705: Fix broken exit code values in jdk/test/Makefile
ohair
parents: 4335
diff changeset
   103
        if (osname.equals("SunOS") && arch.equals("sparcv9")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            exe += "sparcv9/java";
4342
9f3aa1172803 6905705: Fix broken exit code values in jdk/test/Makefile
ohair
parents: 4335
diff changeset
   105
        } else if (osname.equals("SunOS") && arch.equals("amd64")) {
4335
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 2
diff changeset
   106
            exe += "amd64/java";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            exe += "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        String cmd = exe + " " + VMConnection.getDebuggeeVMOptions() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            " -agentlib:jdwp=transport=dt_socket,server=y,suspend=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (suspend) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            cmd += "y";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            cmd += "n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        cmd += ",address=" + address + " " + class_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        System.out.println("Starting: " + cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Process p = Runtime.getRuntime().exec(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        IOHandler.handle(p.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        IOHandler.handle(p.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * - pick a TCP port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * - Launch a debuggee in server=y,suspend=y,address=${port}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * - Launch a second debuggee in server=y,suspend=n with the same port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * - Second debuggee should fail with an error (address already in use)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * - For clean-up we attach to the first debuggee and resume it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // find a free port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        ServerSocket ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int port = ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        String address = String.valueOf(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // launch the first debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        Process process1 = launch(address, true, "HelloWorld");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // give first debuggee time to suspend
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        Thread.currentThread().sleep(5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // launch a second debuggee with the same address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        Process process2 = launch(address, false, "HelloWorld");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // get exit status from second debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int exitCode = process2.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // clean-up - attach to first debuggee and resume it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        Map conn_args = conn.defaultArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        Connector.IntegerArgument port_arg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            (Connector.IntegerArgument)conn_args.get("port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        port_arg.setValue(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        VirtualMachine vm = conn.attach(conn_args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        vm.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // if the second debuggee ran to completion then we've got a problem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (exitCode == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new RuntimeException("Test failed - second debuggee didn't fail to bind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            System.out.println("Test passed - second debuggee correctly failed to bind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}