jdk/test/com/sun/jdi/connect/spi/SimpleLaunchingConnector.java
author ohair
Wed, 25 Nov 2009 08:24:58 -0800
changeset 4335 365eb4449319
parent 2 90ce3da70b43
child 4342 9f3aa1172803
permissions -rw-r--r--
6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm Reviewed-by: dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * A Simple LaunchingConnector used by unit test :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * test/com/sun/jdi/connect/spi/DebugUsingCustomConnector.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jdi.Bootstrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jdi.connect.Connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jdi.connect.LaunchingConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.jdi.connect.Transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.jdi.connect.IllegalConnectorArgumentsException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.jdi.connect.VMStartException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.jdi.connect.spi.TransportService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.jdi.connect.spi.Connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class SimpleLaunchingConnector implements LaunchingConnector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    TransportService ts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    String ARG_NAME = "class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Simple implementation of Connector.StringArgument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static class StringArgumentImpl implements Connector.StringArgument {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        String label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        String description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        String value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        StringArgumentImpl(String name, String label, String description, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        public String label() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        public String description() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public String value() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public void setValue(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        public boolean isValid(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (value.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public boolean mustSpecify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public SimpleLaunchingConnector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            Class c = Class.forName("com.sun.tools.jdi.SocketTransportService");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            ts = (TransportService)c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new Error(x);
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
    public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return "SimpleLaunchingConnector";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public String description() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return "SimpleLaunchingConnector";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public Transport transport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return new Transport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                return ts.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public Map defaultArguments() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        HashMap map = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        map.put(ARG_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                new StringArgumentImpl(ARG_NAME, "class name", "class name", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public VirtualMachine launch(Map<String, ? extends Connector.Argument> arguments) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                              IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                              IllegalConnectorArgumentsException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                              VMStartException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * Get the class name that we are to execute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        String className = ((StringArgumentImpl)arguments.get(ARG_NAME)).value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (className.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new IllegalConnectorArgumentsException("class name missing", ARG_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         * Listen on an emperical port; launch the debuggee; wait for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         * for the debuggee to connect; stop listening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        TransportService.ListenKey key = ts.startListening();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        String exe = System.getProperty("java.home") + File.separator + "bin" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        String arch = System.getProperty("os.arch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (arch.equals("sparcv9")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            exe += "sparcv9/java";
4335
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 2
diff changeset
   150
        } else if (arch.equals("amd64")) {
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 2
diff changeset
   151
            exe += "amd64/java";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            exe += "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        String cmd = exe + " -Xdebug -Xrunjdwp:transport=dt_socket,timeout=15000,address=" +
4335
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 2
diff changeset
   156
            key.address() +
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 2
diff changeset
   157
            " -classpath " + System.getProperty("test.classes") +
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 2
diff changeset
   158
            " " + className;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        Process process = Runtime.getRuntime().exec(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        Connection conn = ts.accept(key, 30*1000, 9*1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        ts.stopListening(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * Debugee is connected - return the virtual machine mirror
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return Bootstrap.virtualMachineManager().createVirtualMachine(conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}