langtools/src/jdk.jshell/share/classes/jdk/jshell/ExecutionControl.java
author alanb
Thu, 17 Mar 2016 19:04:28 +0000
changeset 36526 3b41f1c69604
parent 36499 9d823cc0fe98
child 37644 33cf53901cac
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: jjg, jlahoda, vromero, mcimadamore, bpatel, ksrini, darcy, anazarov, dfuchs Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, jan.lahoda@oracle.com, vicente.romero@oracle.com, andreas.lundblad@oracle.com, andrey.x.nazarov@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, kumar.x.srinivasan@oracle.com, sundararajan.athijegannathan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     4
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    10
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    15
 * accompanied this code).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    16
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    20
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    23
 * questions.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    24
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    25
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    26
package jdk.jshell;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    27
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    28
import static jdk.internal.jshell.remote.RemoteCodes.*;
35002
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
    29
import java.io.DataInputStream;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
    30
import java.io.InputStream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
import java.io.IOException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    32
import java.io.ObjectInputStream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    33
import java.io.ObjectOutputStream;
35002
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
    34
import java.io.PrintStream;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    35
import java.net.ServerSocket;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
import java.net.Socket;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
import com.sun.jdi.*;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
import java.io.EOFException;
34755
135cde5b66cf 8143952: JShell: space in class path causes remote launch failure
jlahoda
parents: 33362
diff changeset
    39
import java.util.HashMap;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import java.util.List;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import java.util.Map;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import jdk.jshell.ClassTracker.ClassInfo;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    43
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
/**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
 * Controls the remote execution environment.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
 * @author Robert Field
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
class ExecutionControl {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
    private final JDIEnv env;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
    private final SnippetMaps maps;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
    private JDIEventHandler handler;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    55
    private Socket socket;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
    private ObjectInputStream in;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
    private ObjectOutputStream out;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
    private final JShell proc;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
    ExecutionControl(JDIEnv env, SnippetMaps maps, JShell proc) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    61
        this.env = env;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
        this.maps = maps;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
        this.proc = proc;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    64
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    66
    void launch() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    67
        try (ServerSocket listener = new ServerSocket(0)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    68
            // timeout after 60 seconds
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    69
            listener.setSoTimeout(60000);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    70
            int port = listener.getLocalPort();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    71
            jdiGo(port);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    72
            socket = listener.accept();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    73
            // out before in -- match remote creation so we don't hang
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    74
            out = new ObjectOutputStream(socket.getOutputStream());
35002
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
    75
            PipeInputStream commandIn = new PipeInputStream();
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
    76
            new DemultiplexInput(socket.getInputStream(), commandIn, proc.out, proc.err).start();
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
    77
            in = new ObjectInputStream(commandIn);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    80
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    81
    void commandExit() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    82
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    83
            if (out != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    84
                out.writeInt(CMD_EXIT);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    85
                out.flush();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    86
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    87
            JDIConnection c = env.connection();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    88
            if (c != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    89
                c.disposeVM();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    90
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    91
        } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
            proc.debug(DBG_GEN, "Exception on JDI exit: %s\n", ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    93
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    94
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    95
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    96
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    97
    boolean commandLoad(List<ClassInfo> cil) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    98
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    99
            out.writeInt(CMD_LOAD);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   100
            out.writeInt(cil.size());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   101
            for (ClassInfo ci : cil) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   102
                out.writeUTF(ci.getClassName());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   103
                out.writeObject(ci.getBytes());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   104
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   105
            out.flush();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   106
            return readAndReportResult();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   107
        } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   108
            proc.debug(DBG_GEN, "IOException on remote load operation: %s\n", ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   109
            return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   110
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   111
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   112
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   113
    String commandInvoke(String classname) throws EvalException, UnresolvedReferenceException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   114
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   115
            synchronized (STOP_LOCK) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   116
                userCodeRunning = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   117
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   118
            out.writeInt(CMD_INVOKE);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   119
            out.writeUTF(classname);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   120
            out.flush();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   121
            if (readAndReportExecutionResult()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   122
                String result = in.readUTF();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   123
                return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   124
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   125
        } catch (IOException | ClassNotFoundException ex) {
35002
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   126
            if (!env.connection().isRunning()) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   127
                env.shutdown();
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   128
            } else {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   129
                proc.debug(DBG_GEN, "Exception on remote invoke: %s\n", ex);
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   130
                return "Execution failure: " + ex.getMessage();
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   131
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   132
        } finally {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   133
            synchronized (STOP_LOCK) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   134
                userCodeRunning = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   135
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   136
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   137
        return "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   138
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   139
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   140
    String commandVarValue(String classname, String varname) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   141
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   142
            out.writeInt(CMD_VARVALUE);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   143
            out.writeUTF(classname);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   144
            out.writeUTF(varname);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   145
            out.flush();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   146
            if (readAndReportResult()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   147
                String result = in.readUTF();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   148
                return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   149
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   150
        } catch (EOFException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   151
            env.shutdown();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   152
        } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   153
            proc.debug(DBG_GEN, "Exception on remote var value: %s\n", ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   154
            return "Execution failure: " + ex.getMessage();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   155
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   156
        return "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   157
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   158
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   159
    boolean commandAddToClasspath(String cp) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   160
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   161
            out.writeInt(CMD_CLASSPATH);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   162
            out.writeUTF(cp);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   163
            out.flush();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   164
            return readAndReportResult();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   165
        } catch (IOException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   166
            throw new InternalError("Classpath addition failed: " + cp, ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   167
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   168
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   169
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   170
    boolean commandRedefine(Map<ReferenceType, byte[]> mp) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   171
        try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   172
            env.vm().redefineClasses(mp);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   173
            return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   174
        } catch (UnsupportedOperationException ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   175
            return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   176
        } catch (Exception ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   177
            proc.debug(DBG_GEN, "Exception on JDI redefine: %s\n", ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   178
            return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   179
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   180
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   181
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   182
    ReferenceType nameToRef(String name) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
        List<ReferenceType> rtl = env.vm().classesByName(name);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
        if (rtl.size() != 1) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   185
            return null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   187
        return rtl.get(0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   188
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   189
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   190
    private boolean readAndReportResult() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   191
        int ok = in.readInt();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   192
        switch (ok) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   193
            case RESULT_SUCCESS:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   194
                return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
            case RESULT_FAIL: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
                String ex = in.readUTF();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   197
                proc.debug(DBG_GEN, "Exception on remote operation: %s\n", ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   198
                return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   199
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   200
            default: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   201
                proc.debug(DBG_GEN, "Bad remote result code: %s\n", ok);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   202
                return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   203
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   204
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   205
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   206
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   207
    private boolean readAndReportExecutionResult() throws IOException, ClassNotFoundException, EvalException, UnresolvedReferenceException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   208
        int ok = in.readInt();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   209
        switch (ok) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   210
            case RESULT_SUCCESS:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   211
                return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   212
            case RESULT_FAIL: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   213
                String ex = in.readUTF();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   214
                proc.debug(DBG_GEN, "Exception on remote operation: %s\n", ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   215
                return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   216
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   217
            case RESULT_EXCEPTION: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   218
                String exceptionClassName = in.readUTF();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   219
                String message = in.readUTF();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   220
                StackTraceElement[] elems = readStackTrace();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   221
                EvalException ee = new EvalException(message, exceptionClassName, elems);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   222
                throw ee;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   223
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   224
            case RESULT_CORRALLED: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   225
                int id = in.readInt();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   226
                StackTraceElement[] elems = readStackTrace();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   227
                Snippet si = maps.getSnippet(id);
36499
9d823cc0fe98 8080069: JShell: Support for corralled classes
rfield
parents: 35002
diff changeset
   228
                throw new UnresolvedReferenceException((DeclarationSnippet) si, elems);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   229
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   230
            case RESULT_KILLED: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   231
                proc.out.println("Killed.");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   232
                return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   233
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   234
            default: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   235
                proc.debug(DBG_GEN, "Bad remote result code: %s\n", ok);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   236
                return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   237
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   238
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   239
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   240
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   241
    private StackTraceElement[] readStackTrace() throws IOException {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   242
        int elemCount = in.readInt();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   243
        StackTraceElement[] elems = new StackTraceElement[elemCount];
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   244
        for (int i = 0; i < elemCount; ++i) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   245
            String className = in.readUTF();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   246
            String methodName = in.readUTF();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   247
            String fileName = in.readUTF();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   248
            int line = in.readInt();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   249
            elems[i] = new StackTraceElement(className, methodName, fileName, line);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   250
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   251
        return elems;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   252
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   253
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   254
    private void jdiGo(int port) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   255
        //MessageOutput.textResources = ResourceBundle.getBundle("impl.TTYResources",
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   256
        //        Locale.getDefault());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   257
34755
135cde5b66cf 8143952: JShell: space in class path causes remote launch failure
jlahoda
parents: 33362
diff changeset
   258
        String connectorName = "com.sun.jdi.CommandLineLaunch";
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   259
        String classPath = System.getProperty("java.class.path");
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36499
diff changeset
   260
        String javaArgs = "-classpath " + classPath;
34755
135cde5b66cf 8143952: JShell: space in class path causes remote launch failure
jlahoda
parents: 33362
diff changeset
   261
        Map<String, String> argumentName2Value = new HashMap<>();
135cde5b66cf 8143952: JShell: space in class path causes remote launch failure
jlahoda
parents: 33362
diff changeset
   262
        argumentName2Value.put("main", "jdk.internal.jshell.remote.RemoteAgent " + port);
135cde5b66cf 8143952: JShell: space in class path causes remote launch failure
jlahoda
parents: 33362
diff changeset
   263
        argumentName2Value.put("options", javaArgs);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   264
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   265
        boolean launchImmediately = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   266
        int traceFlags = 0;// VirtualMachine.TRACE_SENDS | VirtualMachine.TRACE_EVENTS;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   267
34755
135cde5b66cf 8143952: JShell: space in class path causes remote launch failure
jlahoda
parents: 33362
diff changeset
   268
        env.init(connectorName, argumentName2Value, launchImmediately, traceFlags);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   269
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   270
        if (env.connection().isOpen() && env.vm().canBeModified()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   271
            /*
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   272
             * Connection opened on startup. Start event handler
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   273
             * immediately, telling it (through arg 2) to stop on the
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   274
             * VM start event.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   275
             */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   276
            handler = new JDIEventHandler(env);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   277
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   278
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   279
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   280
    private final Object STOP_LOCK = new Object();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   281
    private boolean userCodeRunning = false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   282
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   283
    void commandStop() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   284
        synchronized (STOP_LOCK) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   285
            if (!userCodeRunning)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   286
                return ;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   287
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   288
            VirtualMachine vm = handler.env.vm();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   289
            vm.suspend();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   290
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   291
                OUTER: for (ThreadReference thread : vm.allThreads()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   292
                    // could also tag the thread (e.g. using name), to find it easier
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   293
                    for (StackFrame frame : thread.frames()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   294
                        String remoteAgentName = "jdk.internal.jshell.remote.RemoteAgent";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   295
                        if (remoteAgentName.equals(frame.location().declaringType().name()) &&
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   296
                            "commandLoop".equals(frame.location().method().name())) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   297
                            ObjectReference thiz = frame.thisObject();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   298
                            if (((BooleanValue) thiz.getValue(thiz.referenceType().fieldByName("inClientCode"))).value()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   299
                                thiz.setValue(thiz.referenceType().fieldByName("expectingStop"), vm.mirrorOf(true));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   300
                                ObjectReference stopInstance = (ObjectReference) thiz.getValue(thiz.referenceType().fieldByName("stopException"));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   301
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   302
                                vm.resume();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   303
                                proc.debug(DBG_GEN, "Attempting to stop the client code...\n");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   304
                                thread.stop(stopInstance);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   305
                                thiz.setValue(thiz.referenceType().fieldByName("expectingStop"), vm.mirrorOf(false));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   306
                            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   307
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   308
                            break OUTER;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   309
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   310
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   311
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   312
            } catch (ClassNotLoadedException | IncompatibleThreadStateException | InvalidTypeException  ex) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   313
                proc.debug(DBG_GEN, "Exception on remote stop: %s\n", ex);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   314
            } finally {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   315
                vm.resume();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   316
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   317
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   318
    }
35002
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   319
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   320
    private final class DemultiplexInput extends Thread {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   321
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   322
        private final DataInputStream delegate;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   323
        private final PipeInputStream command;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   324
        private final PrintStream out;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   325
        private final PrintStream err;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   326
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   327
        public DemultiplexInput(InputStream input,
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   328
                                         PipeInputStream command,
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   329
                                         PrintStream out,
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   330
                                         PrintStream err) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   331
            super("output reader");
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   332
            this.delegate = new DataInputStream(input);
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   333
            this.command = command;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   334
            this.out = out;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   335
            this.err = err;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   336
        }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   337
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   338
        public void run() {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   339
            try {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   340
                while (true) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   341
                    int nameLen = delegate.read();
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   342
                    if (nameLen == (-1))
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   343
                        break;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   344
                    byte[] name = new byte[nameLen];
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   345
                    DemultiplexInput.this.delegate.readFully(name);
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   346
                    int dataLen = delegate.read();
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   347
                    byte[] data = new byte[dataLen];
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   348
                    DemultiplexInput.this.delegate.readFully(data);
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   349
                    switch (new String(name, "UTF-8")) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   350
                        case "err":
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   351
                            err.write(data);
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   352
                            break;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   353
                        case "out":
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   354
                            out.write(data);
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   355
                            break;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   356
                        case "command":
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   357
                            for (byte b : data) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   358
                                command.write(Byte.toUnsignedInt(b));
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   359
                            }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   360
                            break;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   361
                    }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   362
                }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   363
            } catch (IOException ex) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   364
                proc.debug(ex, "Failed reading output");
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   365
            } finally {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   366
                command.close();
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   367
            }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   368
        }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   369
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   370
    }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   371
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   372
    public static final class PipeInputStream extends InputStream {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   373
        public static final int INITIAL_SIZE = 128;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   374
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   375
        private int[] buffer = new int[INITIAL_SIZE];
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   376
        private int start;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   377
        private int end;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   378
        private boolean closed;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   379
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   380
        @Override
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   381
        public synchronized int read() {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   382
            while (start == end) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   383
                if (closed) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   384
                    return -1;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   385
                }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   386
                try {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   387
                    wait();
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   388
                } catch (InterruptedException ex) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   389
                    //ignore
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   390
                }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   391
            }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   392
            try {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   393
                return buffer[start];
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   394
            } finally {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   395
                start = (start + 1) % buffer.length;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   396
            }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   397
        }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   398
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   399
        public synchronized void write(int b) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   400
            if (closed)
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   401
                throw new IllegalStateException("Already closed.");
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   402
            int newEnd = (end + 1) % buffer.length;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   403
            if (newEnd == start) {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   404
                //overflow:
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   405
                int[] newBuffer = new int[buffer.length * 2];
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   406
                int rightPart = (end > start ? end : buffer.length) - start;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   407
                int leftPart = end > start ? 0 : start - 1;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   408
                System.arraycopy(buffer, start, newBuffer, 0, rightPart);
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   409
                System.arraycopy(buffer, 0, newBuffer, rightPart, leftPart);
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   410
                buffer = newBuffer;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   411
                start = 0;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   412
                end = rightPart + leftPart;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   413
                newEnd = end + 1;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   414
            }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   415
            buffer[end] = b;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   416
            end = newEnd;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   417
            notifyAll();
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   418
        }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   419
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   420
        @Override
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   421
        public synchronized void close() {
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   422
            closed = true;
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   423
            notifyAll();
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   424
        }
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   425
209d72239196 8144906: Fix jshell's ToolBasicTest
jlahoda
parents: 34755
diff changeset
   426
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   427
}