hotspot/agent/test/jdi/VMConnection.java
author sundar
Wed, 09 Jan 2013 22:32:40 +0530
changeset 16163 71f4cff209a9
parent 5547 f4b087cbb361
permissions -rw-r--r--
8005940: provide ant targets to get and update external test scripts Reviewed-by: jlaskey, lagergren
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
     2
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
import com.sun.jdi.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
import com.sun.jdi.connect.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import com.sun.jdi.request.EventRequestManager;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
/**
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
 * Manages a VM conection for the JDI test framework.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
class VMConnection {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    private VirtualMachine vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    private Process process = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
    private int outputCompleteCount = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    private final Connector connector;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    private final Map connectorArgs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    private final int traceFlags;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    /**
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
     * Return a String containing VM Options to pass to the debugee
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
     * or an empty string if there are none.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
     * These are read from the first non-comment line
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
     * in file test/com/sun/jdi/@debuggeeVMOptions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    static public String getDebuggeeVMOptions() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
        // When we run under jtreg, test.src contains the pathname of
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
        // the test/com/sun/jdi dir.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
        BufferedReader reader;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
        final String filename = "@debuggeeVMOptions";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
        String srcDir = System.getProperty("test.src");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
        if (srcDir == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
          srcDir = System.getProperty("user.dir");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
        srcDir = srcDir + File.separator;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
        File myDir = new File(srcDir);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
        File myFile = new File(myDir, filename);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
        if (!myFile.canRead()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
            try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
                // We have some subdirs of test/com/sun/jdi so in case we
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
                // are in one of them, look in our parent dir for the file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
                myFile = new File(myDir.getCanonicalFile().getParent(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
                                  filename);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
                if (!myFile.canRead()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
                    return "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
            } catch (IOException ee) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
                System.out.println("-- Error 1 trying to access file " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
                                   myFile.getPath() + ": " + ee);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
                return "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
        String wholePath = myFile.getPath();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
            reader = new BufferedReader(new FileReader(myFile));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
        } catch (FileNotFoundException ee) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
            System.out.println("-- Error 2 trying to access file " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
                               wholePath + ": " + ee);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
            return "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
        String line;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
        String retVal = "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
        while (true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
            try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
                line = reader.readLine();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
            } catch (IOException ee) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
                System.out.println("-- Error reading options from file " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
                                   wholePath + ": " + ee);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
            if (line == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
                System.out.println("-- No debuggee VM options found in file " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
                                   wholePath);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
            line = line.trim();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
            if (line.length() != 0 && !line.startsWith("#")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
                System.out.println("-- Added debuggeeVM options from file " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
                                   wholePath + ": " + line);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
                retVal = line;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
            // Else, read he next line.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
            reader.close();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
        } catch (IOException ee) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
        return retVal;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    private Connector findConnector(String name) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
        List connectors = Bootstrap.virtualMachineManager().allConnectors();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
        Iterator iter = connectors.iterator();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
        while (iter.hasNext()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
            Connector connector = (Connector)iter.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
            if (connector.name().equals(name)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
                return connector;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
        return null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    private Map parseConnectorArgs(Connector connector, String argString) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
        StringTokenizer tokenizer = new StringTokenizer(argString, ",");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
        Map arguments = connector.defaultArguments();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
        while (tokenizer.hasMoreTokens()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
            String token = tokenizer.nextToken();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
            int index = token.indexOf('=');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
            if (index == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
                throw new IllegalArgumentException("Illegal connector argument: " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
                                                   token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
            String name = token.substring(0, index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
            String value = token.substring(index + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
            Connector.Argument argument = (Connector.Argument)arguments.get(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
            if (argument == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
                throw new IllegalArgumentException("Argument " + name +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
                                               "is not defined for connector: " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
                                               connector.name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
            argument.setValue(value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
        return arguments;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    VMConnection(String connectSpec, int traceFlags) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
        String nameString;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
        String argString;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
        int index = connectSpec.indexOf(':');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
        if (index == -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
            nameString = connectSpec;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
            argString = "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
            nameString = connectSpec.substring(0, index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
            argString = connectSpec.substring(index + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
        connector = findConnector(nameString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
        if (connector == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
            throw new IllegalArgumentException("No connector named: " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
                                               nameString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
        connectorArgs = parseConnectorArgs(connector, argString);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
        this.traceFlags = traceFlags;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    synchronized VirtualMachine open() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
        if (connector instanceof LaunchingConnector) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
            vm = launchTarget();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
        } else if (connector instanceof AttachingConnector) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
            vm = attachTarget();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
        } else if (connector instanceof ListeningConnector) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
            vm = listenTarget();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
            throw new InternalError("Invalid connect type");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
        vm.setDebugTraceMode(traceFlags);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
        System.out.println("JVM version:" + vm.version());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
        System.out.println("JDI version: " + Bootstrap.virtualMachineManager().majorInterfaceVersion() +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
                           "." + Bootstrap.virtualMachineManager().minorInterfaceVersion());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
        System.out.println("JVM description: " + vm.description());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
        return vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    boolean setConnectorArg(String name, String value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
        /*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
         * Too late if the connection already made
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
         */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
        if (vm != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
        Connector.Argument argument = (Connector.Argument)connectorArgs.get(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
        if (argument == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
        argument.setValue(value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
        return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
    String connectorArg(String name) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
        Connector.Argument argument = (Connector.Argument)connectorArgs.get(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
        if (argument == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
            return "";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
        return argument.value();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
    public synchronized VirtualMachine vm() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
        if (vm == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
            throw new InternalError("VM not connected");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
            return vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    boolean isOpen() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
        return (vm != null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    boolean isLaunch() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
        return (connector instanceof LaunchingConnector);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    Connector connector() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
        return connector;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
    boolean isListen() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
        return (connector instanceof ListeningConnector);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
    boolean isAttach() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
        return (connector instanceof AttachingConnector);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
    private synchronized void notifyOutputComplete() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
        outputCompleteCount++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
        notifyAll();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
    private synchronized void waitOutputComplete() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
        // Wait for stderr and stdout
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
        if (process != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
            while (outputCompleteCount < 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
                try {wait();} catch (InterruptedException e) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    public void disposeVM() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
            if (vm != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
                vm.dispose();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
                vm = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
        } finally {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
            if (process != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
                process.destroy();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
                process = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
            waitOutputComplete();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
    private void dumpStream(InputStream stream) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
        PrintStream outStream = System.out;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
        BufferedReader in =
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
            new BufferedReader(new InputStreamReader(stream));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
        String line;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
        while ((line = in.readLine()) != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
            outStream.println(line);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
    /**
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
     *  Create a Thread that will retrieve and display any output.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
     *  Needs to be high priority, else debugger may exit before
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
     *  it can be displayed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
    private void displayRemoteOutput(final InputStream stream) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
        Thread thr = new Thread("output reader") {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
            public void run() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
                try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
                    dumpStream(stream);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
                } catch (IOException ex) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
                    System.err.println("IOException reading output of child java interpreter:"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
                                       + ex.getMessage());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
                } finally {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
                    notifyOutputComplete();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
        };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
        thr.setPriority(Thread.MAX_PRIORITY-1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
        thr.start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    private void dumpFailedLaunchInfo(Process process) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
            dumpStream(process.getErrorStream());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
            dumpStream(process.getInputStream());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
        } catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
            System.err.println("Unable to display process output: " +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
                               e.getMessage());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
    /* launch child target vm */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
    private VirtualMachine launchTarget() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
        LaunchingConnector launcher = (LaunchingConnector)connector;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
            VirtualMachine vm = launcher.launch(connectorArgs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
            process = vm.process();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
            displayRemoteOutput(process.getErrorStream());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
            displayRemoteOutput(process.getInputStream());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
            return vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
        } catch (IOException ioe) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
            ioe.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
            System.err.println("\n Unable to launch target VM.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
        } catch (IllegalConnectorArgumentsException icae) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
            icae.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
            System.err.println("\n Internal debugger error.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
        } catch (VMStartException vmse) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
            System.err.println(vmse.getMessage() + "\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
            dumpFailedLaunchInfo(vmse.process());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
            System.err.println("\n Target VM failed to initialize.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
        return null; // Shuts up the compiler
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
    /* attach to running target vm */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
    private VirtualMachine attachTarget() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
        AttachingConnector attacher = (AttachingConnector)connector;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
            return attacher.attach(connectorArgs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
        } catch (IOException ioe) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
            ioe.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
            System.err.println("\n Unable to attach to target VM.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
        } catch (IllegalConnectorArgumentsException icae) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
            icae.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
            System.err.println("\n Internal debugger error.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
        return null; // Shuts up the compiler
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    /* listen for connection from target vm */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
    private VirtualMachine listenTarget() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
        ListeningConnector listener = (ListeningConnector)connector;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
            String retAddress = listener.startListening(connectorArgs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
            System.out.println("Listening at address: " + retAddress);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
            vm = listener.accept(connectorArgs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
            listener.stopListening(connectorArgs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
            return vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
        } catch (IOException ioe) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
            ioe.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
            System.err.println("\n Unable to attach to target VM.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
        } catch (IllegalConnectorArgumentsException icae) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
            icae.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
            System.err.println("\n Internal debugger error.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
        return null; // Shuts up the compiler
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
}