jdk/src/share/classes/com/sun/tools/example/debug/tty/VMConnection.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1998-2008 Sun Microsystems, Inc.  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.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.example.debug.tty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jdi.connect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jdi.request.EventRequestManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jdi.request.ExceptionRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.jdi.request.ThreadStartRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.jdi.request.ThreadDeathRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
class VMConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private VirtualMachine vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private Process process = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private int outputCompleteCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private final Connector connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final Map<String, com.sun.jdi.connect.Connector.Argument> connectorArgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final int traceFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    synchronized void notifyOutputComplete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        outputCompleteCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    synchronized void waitOutputComplete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // Wait for stderr and stdout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            while (outputCompleteCount < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                try {wait();} catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Connector findConnector(String name) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    64
        for (Connector connector :
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    65
                 Bootstrap.virtualMachineManager().allConnectors()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if (connector.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                return connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private Map <String, com.sun.jdi.connect.Connector.Argument> parseConnectorArgs(Connector connector, String argString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Map<String, com.sun.jdi.connect.Connector.Argument> arguments = connector.defaultArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         * We are parsing strings of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         *    name1=value1,[name2=value2,...]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
         * However, the value1...valuen substrings may contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         * embedded comma(s), so make provision for quoting inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         * the value substrings. (Bug ID 4285874)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        String regexPattern =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            "(quote=[^,]+,)|" +           // special case for quote=.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            "(\\w+=)" +                   // name=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            "(((\"[^\"]*\")|" +           //   ( "l , ue"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            "('[^']*')|" +                //     'l , ue'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            "([^,'\"]+))+,)";             //     v a l u e )+ ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Pattern p = Pattern.compile(regexPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Matcher m = p.matcher(argString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        while (m.find()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            int startPosition = m.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int endPosition = m.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (startPosition > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                 * It is an error if parsing skips over any part of argString.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    (MessageOutput.format("Illegal connector argument",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                          argString));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            String token = argString.substring(startPosition, endPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            int index = token.indexOf('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            String name = token.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            String value = token.substring(index + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                           token.length() - 1); // Remove comma delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   109
            Connector.Argument argument = arguments.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (argument == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    (MessageOutput.format("Argument is not defined for connector:",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                          new Object [] {name, connector.name()}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            argument.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            argString = argString.substring(endPosition); // Remove what was just parsed...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            m = p.matcher(argString);                     //    and parse again on what is left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if ((! argString.equals(",")) && (argString.length() > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
             * It is an error if any part of argString is left over,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
             * unless it was empty to begin with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                (MessageOutput.format("Illegal connector argument", argString));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return arguments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    VMConnection(String connectSpec, int traceFlags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        String nameString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        String argString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int index = connectSpec.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            nameString = connectSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            argString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            nameString = connectSpec.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            argString = connectSpec.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        connector = findConnector(nameString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (connector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                (MessageOutput.format("No connector named:", nameString));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        connectorArgs = parseConnectorArgs(connector, argString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.traceFlags = traceFlags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    synchronized VirtualMachine open() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (connector instanceof LaunchingConnector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            vm = launchTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } else if (connector instanceof AttachingConnector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            vm = attachTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } else if (connector instanceof ListeningConnector) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            vm = listenTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new InternalError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                (MessageOutput.format("Invalid connect type"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        vm.setDebugTraceMode(traceFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (vm.canBeModified()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            setEventRequests(vm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            resolveEventRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         * Now that the vm connection is open, fetch the debugee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * classpath and set up a default sourcepath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * (Unless user supplied a sourcepath on the command line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * (Bug ID 4186582)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (Env.getSourcePath().length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (vm instanceof PathSearchingVirtualMachine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                PathSearchingVirtualMachine psvm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    (PathSearchingVirtualMachine) vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                Env.setSourcePath(psvm.classPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                Env.setSourcePath(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    boolean setConnectorArg(String name, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         * Too late if the connection already made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (vm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   196
        Connector.Argument argument = connectorArgs.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (argument == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        argument.setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    String connectorArg(String name) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   205
        Connector.Argument argument = connectorArgs.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (argument == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return argument.value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public synchronized VirtualMachine vm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (vm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new VMNotConnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    boolean isOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return (vm != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    boolean isLaunch() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return (connector instanceof LaunchingConnector);
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 void disposeVM() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (vm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                vm.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (process != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                process.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                process = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            waitOutputComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private void setEventRequests(VirtualMachine vm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        EventRequestManager erm = vm.eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // Normally, we want all uncaught exceptions.  We request them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // via the same mechanism as Commands.commandCatchException()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // so the user can ignore them later if they are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // interested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        // FIXME: this works but generates spurious messages on stdout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        //        during startup:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        //          Set uncaught java.lang.Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        //          Set deferred uncaught java.lang.Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        Commands evaluator = new Commands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        evaluator.commandCatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            (new StringTokenizer("uncaught java.lang.Throwable"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        ThreadStartRequest tsr = erm.createThreadStartRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        tsr.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        ThreadDeathRequest tdr = erm.createThreadDeathRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        tdr.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private void resolveEventRequests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        Env.specList.resolveAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private void dumpStream(InputStream stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        BufferedReader in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            new BufferedReader(new InputStreamReader(stream));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            while ((i = in.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                   MessageOutput.printDirect((char)i);// Special case: use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                                      //   printDirect()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            String s = ex.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (!s.startsWith("Bad file number")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                  throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // else we got a Bad file number IOException which just means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // that the debuggee has gone away.  We'll just treat it the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // same as if we got an EOF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *  Create a Thread that will retrieve and display any output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *  Needs to be high priority, else debugger may exit before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *  it can be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private void displayRemoteOutput(final InputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        Thread thr = new Thread("output reader") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    dumpStream(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    MessageOutput.fatalError("Failed reading output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    notifyOutputComplete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        thr.setPriority(Thread.MAX_PRIORITY-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    private void dumpFailedLaunchInfo(Process process) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            dumpStream(process.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            dumpStream(process.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            MessageOutput.println("Unable to display process output:",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                  e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /* launch child target vm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private VirtualMachine launchTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        LaunchingConnector launcher = (LaunchingConnector)connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            VirtualMachine vm = launcher.launch(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            process = vm.process();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            displayRemoteOutput(process.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            displayRemoteOutput(process.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            MessageOutput.fatalError("Unable to launch target VM.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } catch (IllegalConnectorArgumentsException icae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            icae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            MessageOutput.fatalError("Internal debugger error.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } catch (VMStartException vmse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            MessageOutput.println("vmstartexception", vmse.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            MessageOutput.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            dumpFailedLaunchInfo(vmse.process());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            MessageOutput.fatalError("Target VM failed to initialize.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return null; // Shuts up the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /* attach to running target vm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private VirtualMachine attachTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        AttachingConnector attacher = (AttachingConnector)connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            return attacher.attach(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            MessageOutput.fatalError("Unable to attach to target VM.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } catch (IllegalConnectorArgumentsException icae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            icae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            MessageOutput.fatalError("Internal debugger error.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return null; // Shuts up the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /* listen for connection from target vm */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    private VirtualMachine listenTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        ListeningConnector listener = (ListeningConnector)connector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            String retAddress = listener.startListening(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            MessageOutput.println("Listening at address:", retAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            vm = listener.accept(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            listener.stopListening(connectorArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            ioe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            MessageOutput.fatalError("Unable to attach to target VM.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } catch (IllegalConnectorArgumentsException icae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            icae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            MessageOutput.fatalError("Internal debugger error.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        return null; // Shuts up the compiler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
}