jdk/test/com/sun/jdi/VMConnection.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 24973 8c4bc3fa4c4e
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: 4335
diff changeset
     2
 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
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: 4335
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4335
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4335
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
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import com.sun.jdi.connect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import com.sun.jdi.request.EventRequestManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Manages a VM conection for the JDI test framework.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
class VMConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private VirtualMachine vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private Process process = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private int outputCompleteCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private final Connector connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private final Map connectorArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private final int traceFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Return a String containing VM Options to pass to the debugee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * or an empty string if there are none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * These are read from the first non-comment line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * in file @debuggeeVMOptions in the test.classes dir
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static public String getDebuggeeVMOptions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        String retVal = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // When we run under jtreg, test.classes contains the pathname of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // the dir in which the .class files will be placed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        BufferedReader reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        String testClasses = System.getProperty("test.classes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (testClasses == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
4335
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 1247
diff changeset
    60
        retVal += "-classpath " + testClasses + " ";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        File myFile = new File(testClasses, "@debuggeeVMOptions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (!myFile.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            // Not there - look in parent (in case we are in a subdir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            myFile = new File(testClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            String parentDir = myFile.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if (parentDir != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                myFile = new File(parentDir, "@debuggeeVMOptions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                if (!myFile.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        String wholePath = myFile.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            reader = new BufferedReader(new FileReader(myFile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        } catch (FileNotFoundException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.out.println("-- Error 2 trying to access file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                               wholePath + ": " + ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                line = reader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                System.out.println("-- Error reading options from file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                   wholePath + ": " + ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                System.out.println("-- No debuggee VM options found in file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                   wholePath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            line = line.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (line.length() != 0 && !line.startsWith("#")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                System.out.println("-- Added debuggeeVM options from file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                   wholePath + ": " + line);
4335
365eb4449319 6904183: Fix jdk/test/com/sun/jdi tests to run with -samevm
ohair
parents: 1247
diff changeset
   101
                retVal += line;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // Else, read he next line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            reader.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static public String[] insertDebuggeeVMOptions(String[] cmdLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        String opts = getDebuggeeVMOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (opts.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return cmdLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // Insert the options at position 1.  Blanks in args are not allowed!
838
d01176a115ba 6725192: Some JDI Regression tests fail if @debuggeeVMOptions contain multiple blanks
jjh
parents: 2
diff changeset
   119
        String[] v1 = opts.split(" +");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String[] retVal = new String[cmdLine.length + v1.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        retVal[0] = cmdLine[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        System.arraycopy(v1, 0, retVal, 1, v1.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.arraycopy(cmdLine, 1, retVal, v1.length + 1, cmdLine.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private Connector findConnector(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        List connectors = Bootstrap.virtualMachineManager().allConnectors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Iterator iter = connectors.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            Connector connector = (Connector)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (connector.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                return connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private Map parseConnectorArgs(Connector connector, String argString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        StringTokenizer tokenizer = new StringTokenizer(argString, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Map arguments = connector.defaultArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        while (tokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            String token = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            int index = token.indexOf('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                throw new IllegalArgumentException("Illegal connector argument: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                                   token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            String name = token.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            String value = token.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            Connector.Argument argument = (Connector.Argument)arguments.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (argument == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                throw new IllegalArgumentException("Argument " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                               "is not defined for connector: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                               connector.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            argument.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return arguments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    VMConnection(String connectSpec, int traceFlags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        String nameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        String argString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        int index = connectSpec.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            nameString = connectSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            argString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            nameString = connectSpec.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            argString = connectSpec.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        connector = findConnector(nameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (connector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            throw new IllegalArgumentException("No connector named: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                               nameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        connectorArgs = parseConnectorArgs(connector, argString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        this.traceFlags = traceFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    synchronized VirtualMachine open() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (connector instanceof LaunchingConnector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            vm = launchTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } else if (connector instanceof AttachingConnector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            vm = attachTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } else if (connector instanceof ListeningConnector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            vm = listenTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new InternalError("Invalid connect type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        vm.setDebugTraceMode(traceFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        System.out.println("JVM version:" + vm.version());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        System.out.println("JDI version: " + Bootstrap.virtualMachineManager().majorInterfaceVersion() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                           "." + Bootstrap.virtualMachineManager().minorInterfaceVersion());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        System.out.println("JVM description: " + vm.description());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    boolean setConnectorArg(String name, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * Too late if the connection already made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (vm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        Connector.Argument argument = (Connector.Argument)connectorArgs.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (argument == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        argument.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    String connectorArg(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        Connector.Argument argument = (Connector.Argument)connectorArgs.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (argument == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return argument.value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public synchronized VirtualMachine vm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (vm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            throw new InternalError("VM not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    boolean isOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return (vm != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    boolean isLaunch() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return (connector instanceof LaunchingConnector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    Connector connector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    boolean isListen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return (connector instanceof ListeningConnector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    boolean isAttach() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return (connector instanceof AttachingConnector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private synchronized void notifyOutputComplete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        outputCompleteCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private synchronized void waitOutputComplete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // Wait for stderr and stdout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            while (outputCompleteCount < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                try {wait();} catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public void disposeVM() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (vm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                vm.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                process.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                process = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            waitOutputComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private void dumpStream(InputStream stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                PrintStream outStream = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                BufferedReader in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        new BufferedReader(new InputStreamReader(stream));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                while(true){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                      try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                          line = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                          if( line == null ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                          outStream.println(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                      catch(IOException ieo){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                           /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            * IOException with "Bad file number..." can happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            * when the debuggee process is destroyed. Ignore such exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                            *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                           String s = ieo.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                           if( s.startsWith("Bad file number") ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                               break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                           throw ieo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                      catch(NullPointerException npe){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                          throw new IOException("Bug 4728096 in Java io may cause in.readLine() to throw a NULL pointer exception");
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *  Create a Thread that will retrieve and display any output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *  Needs to be high priority, else debugger may exit before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *  it can be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    private void displayRemoteOutput(final InputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        Thread thr = new Thread("output reader") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    dumpStream(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    System.err.println("IOException reading output of child java interpreter:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                       + ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    notifyOutputComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        thr.setPriority(Thread.MAX_PRIORITY-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private void dumpFailedLaunchInfo(Process process) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            dumpStream(process.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            dumpStream(process.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            System.err.println("Unable to display process output: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                               e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /* launch child target vm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private VirtualMachine launchTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        LaunchingConnector launcher = (LaunchingConnector)connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            VirtualMachine vm = launcher.launch(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            process = vm.process();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            displayRemoteOutput(process.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            displayRemoteOutput(process.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            System.err.println("\n Unable to launch target VM.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } catch (IllegalConnectorArgumentsException icae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            icae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            System.err.println("\n Internal debugger error.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        } catch (VMStartException vmse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            System.err.println(vmse.getMessage() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            dumpFailedLaunchInfo(vmse.process());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            System.err.println("\n Target VM failed to initialize.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return null; // Shuts up the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /* attach to running target vm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private VirtualMachine attachTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        AttachingConnector attacher = (AttachingConnector)connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return attacher.attach(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            System.err.println("\n Unable to attach to target VM.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } catch (IllegalConnectorArgumentsException icae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            icae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            System.err.println("\n Internal debugger error.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return null; // Shuts up the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /* listen for connection from target vm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    private VirtualMachine listenTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        ListeningConnector listener = (ListeningConnector)connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            String retAddress = listener.startListening(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            System.out.println("Listening at address: " + retAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            vm = listener.accept(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            listener.stopListening(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            System.err.println("\n Unable to attach to target VM.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        } catch (IllegalConnectorArgumentsException icae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            icae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            System.err.println("\n Internal debugger error.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return null; // Shuts up the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
}