jdk/test/com/sun/jdi/VMConnection.java
author jjg
Thu, 22 May 2008 15:51:41 -0700
changeset 655 1ebc7ce89018
parent 2 90ce3da70b43
child 838 d01176a115ba
permissions -rw-r--r--
6705945: com.sun.tools.javac.zip files do not have valid copyright Reviewed-by: mcimadamore
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 1999-2006 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
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        File myFile = new File(testClasses, "@debuggeeVMOptions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (!myFile.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            // Not there - look in parent (in case we are in a subdir)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            myFile = new File(testClasses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            String parentDir = myFile.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if (parentDir != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                myFile = new File(parentDir, "@debuggeeVMOptions");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                if (!myFile.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        String wholePath = myFile.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            reader = new BufferedReader(new FileReader(myFile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        } catch (FileNotFoundException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            System.out.println("-- Error 2 trying to access file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                               wholePath + ": " + ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                line = reader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                System.out.println("-- Error reading options from file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                   wholePath + ": " + ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                System.out.println("-- No debuggee VM options found in file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                   wholePath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            line = line.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (line.length() != 0 && !line.startsWith("#")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                System.out.println("-- Added debuggeeVM options from file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                   wholePath + ": " + line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                retVal = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // Else, read he next line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            reader.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static public String[] insertDebuggeeVMOptions(String[] cmdLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        String opts = getDebuggeeVMOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (opts.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return cmdLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // Insert the options at position 1.  Blanks in args are not allowed!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        String[] v1 = opts.split(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        String[] retVal = new String[cmdLine.length + v1.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        retVal[0] = cmdLine[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        System.arraycopy(v1, 0, retVal, 1, v1.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        System.arraycopy(cmdLine, 1, retVal, v1.length + 1, cmdLine.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private Connector findConnector(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        List connectors = Bootstrap.virtualMachineManager().allConnectors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Iterator iter = connectors.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            Connector connector = (Connector)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (connector.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                return connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private Map parseConnectorArgs(Connector connector, String argString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        StringTokenizer tokenizer = new StringTokenizer(argString, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Map arguments = connector.defaultArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        while (tokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            String token = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            int index = token.indexOf('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                throw new IllegalArgumentException("Illegal connector argument: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                                   token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            String name = token.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            String value = token.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            Connector.Argument argument = (Connector.Argument)arguments.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (argument == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                throw new IllegalArgumentException("Argument " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                               "is not defined for connector: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                               connector.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            argument.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return arguments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    VMConnection(String connectSpec, int traceFlags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        String nameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        String argString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int index = connectSpec.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            nameString = connectSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            argString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            nameString = connectSpec.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            argString = connectSpec.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        connector = findConnector(nameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (connector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throw new IllegalArgumentException("No connector named: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                               nameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        connectorArgs = parseConnectorArgs(connector, argString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        this.traceFlags = traceFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    synchronized VirtualMachine open() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (connector instanceof LaunchingConnector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            vm = launchTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } else if (connector instanceof AttachingConnector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            vm = attachTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } else if (connector instanceof ListeningConnector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            vm = listenTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new InternalError("Invalid connect type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        vm.setDebugTraceMode(traceFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        System.out.println("JVM version:" + vm.version());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        System.out.println("JDI version: " + Bootstrap.virtualMachineManager().majorInterfaceVersion() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                           "." + Bootstrap.virtualMachineManager().minorInterfaceVersion());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        System.out.println("JVM description: " + vm.description());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    boolean setConnectorArg(String name, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         * Too late if the connection already made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (vm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        Connector.Argument argument = (Connector.Argument)connectorArgs.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (argument == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        argument.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    String connectorArg(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        Connector.Argument argument = (Connector.Argument)connectorArgs.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (argument == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return argument.value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public synchronized VirtualMachine vm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (vm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new InternalError("VM not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    boolean isOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return (vm != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    boolean isLaunch() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return (connector instanceof LaunchingConnector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    Connector connector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    boolean isListen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return (connector instanceof ListeningConnector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    boolean isAttach() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return (connector instanceof AttachingConnector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private synchronized void notifyOutputComplete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        outputCompleteCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private synchronized void waitOutputComplete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // Wait for stderr and stdout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            while (outputCompleteCount < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                try {wait();} catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
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
    public void disposeVM() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (vm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                vm.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                process.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                process = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            waitOutputComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private void dumpStream(InputStream stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                PrintStream outStream = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                BufferedReader in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        new BufferedReader(new InputStreamReader(stream));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                while(true){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                      try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                          line = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                          if( line == null ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                              break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                          outStream.println(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                      catch(IOException ieo){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                           /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                            * IOException with "Bad file number..." can happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            * when the debuggee process is destroyed. Ignore such exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                           String s = ieo.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                           if( s.startsWith("Bad file number") ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                               break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                           throw ieo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                      catch(NullPointerException npe){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                          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
   312
                      }
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
     *  Create a Thread that will retrieve and display any output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *  Needs to be high priority, else debugger may exit before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *  it can be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private void displayRemoteOutput(final InputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        Thread thr = new Thread("output reader") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    dumpStream(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    System.err.println("IOException reading output of child java interpreter:"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                       + ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    notifyOutputComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        thr.setPriority(Thread.MAX_PRIORITY-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private void dumpFailedLaunchInfo(Process process) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            dumpStream(process.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            dumpStream(process.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            System.err.println("Unable to display process output: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                               e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /* launch child target vm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private VirtualMachine launchTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        LaunchingConnector launcher = (LaunchingConnector)connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            VirtualMachine vm = launcher.launch(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            process = vm.process();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            displayRemoteOutput(process.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            displayRemoteOutput(process.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            System.err.println("\n Unable to launch target VM.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        } catch (IllegalConnectorArgumentsException icae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            icae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            System.err.println("\n Internal debugger error.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } catch (VMStartException vmse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            System.err.println(vmse.getMessage() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            dumpFailedLaunchInfo(vmse.process());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            System.err.println("\n Target VM failed to initialize.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return null; // Shuts up the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /* attach to running target vm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private VirtualMachine attachTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        AttachingConnector attacher = (AttachingConnector)connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return attacher.attach(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            System.err.println("\n Unable to attach to target VM.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        } catch (IllegalConnectorArgumentsException icae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            icae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            System.err.println("\n Internal debugger error.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return null; // Shuts up the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /* listen for connection from target vm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private VirtualMachine listenTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        ListeningConnector listener = (ListeningConnector)connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            String retAddress = listener.startListening(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            System.out.println("Listening at address: " + retAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            vm = listener.accept(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            listener.stopListening(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            System.err.println("\n Unable to attach to target VM.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        } catch (IllegalConnectorArgumentsException icae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            icae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            System.err.println("\n Internal debugger error.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return null; // Shuts up the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
}