langtools/src/jdk.jshell/share/classes/jdk/jshell/execution/JdiDefaultExecutionControl.java
changeset 41941 a935ac3f5274
parent 40767 c7908e8c786b
child 41994 e43f670394ca
equal deleted inserted replaced
41940:048d559e9da7 41941:a935ac3f5274
       
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package jdk.jshell.execution;
       
    26 
       
    27 import java.io.IOException;
       
    28 import java.io.InputStream;
       
    29 import java.io.ObjectInput;
       
    30 import java.io.ObjectOutput;
       
    31 import java.io.OutputStream;
       
    32 import java.net.ServerSocket;
       
    33 import java.net.Socket;
       
    34 import java.util.ArrayList;
       
    35 import java.util.HashMap;
       
    36 import java.util.List;
       
    37 import java.util.Map;
       
    38 import java.util.function.Consumer;
       
    39 import com.sun.jdi.BooleanValue;
       
    40 import com.sun.jdi.ClassNotLoadedException;
       
    41 import com.sun.jdi.Field;
       
    42 import com.sun.jdi.IncompatibleThreadStateException;
       
    43 import com.sun.jdi.InvalidTypeException;
       
    44 import com.sun.jdi.ObjectReference;
       
    45 import com.sun.jdi.StackFrame;
       
    46 import com.sun.jdi.ThreadReference;
       
    47 import com.sun.jdi.VMDisconnectedException;
       
    48 import com.sun.jdi.VirtualMachine;
       
    49 import jdk.jshell.spi.ExecutionControl;
       
    50 import jdk.jshell.spi.ExecutionEnv;
       
    51 import static jdk.jshell.execution.Util.remoteInputOutput;
       
    52 
       
    53 /**
       
    54  * The implementation of {@link jdk.jshell.spi.ExecutionControl} that the
       
    55  * JShell-core uses by default.
       
    56  * Launches a remote process -- the "remote agent".
       
    57  * Interfaces to the remote agent over a socket and via JDI.
       
    58  * Designed to work with {@link RemoteExecutionControl}.
       
    59  *
       
    60  * @author Robert Field
       
    61  * @author Jan Lahoda
       
    62  */
       
    63 public class JdiDefaultExecutionControl extends JdiExecutionControl {
       
    64 
       
    65     private static final String REMOTE_AGENT = RemoteExecutionControl.class.getName();
       
    66 
       
    67     private VirtualMachine vm;
       
    68     private Process process;
       
    69 
       
    70     private final Object STOP_LOCK = new Object();
       
    71     private boolean userCodeRunning = false;
       
    72 
       
    73     /**
       
    74      * Creates an ExecutionControl instance based on a JDI
       
    75      * {@code LaunchingConnector}.
       
    76      *
       
    77      * @return the generator
       
    78      */
       
    79     public static ExecutionControl.Generator launch() {
       
    80         return env -> create(env, true, null);
       
    81     }
       
    82 
       
    83     /**
       
    84      * Creates an ExecutionControl instance based on a JDI
       
    85      * {@code ListeningConnector}.
       
    86      *
       
    87      * @param host explicit hostname to use, if null use discovered
       
    88      * hostname, applies to listening only (!isLaunch)
       
    89      * @return the generator
       
    90      */
       
    91     public static ExecutionControl.Generator listen(String host) {
       
    92         return env -> create(env, false, host);
       
    93     }
       
    94 
       
    95     /**
       
    96      * Creates an ExecutionControl instance based on a JDI
       
    97      * {@code ListeningConnector} or {@code LaunchingConnector}.
       
    98      *
       
    99      * Initialize JDI and use it to launch the remote JVM. Set-up a socket for
       
   100      * commands and results. This socket also transports the user
       
   101      * input/output/error.
       
   102      *
       
   103      * @param env the context passed by
       
   104      * {@link jdk.jshell.spi.ExecutionControl#start(jdk.jshell.spi.ExecutionEnv) }
       
   105      * @param isLaunch does JDI do the launch? That is, LaunchingConnector,
       
   106      * otherwise we start explicitly and use ListeningConnector
       
   107      * @param host explicit hostname to use, if null use discovered
       
   108      * hostname, applies to listening only (!isLaunch)
       
   109      * @return the channel
       
   110      * @throws IOException if there are errors in set-up
       
   111      */
       
   112     private static ExecutionControl create(ExecutionEnv env,
       
   113             boolean isLaunch, String host) throws IOException {
       
   114         try (final ServerSocket listener = new ServerSocket(0)) {
       
   115             // timeout after 60 seconds
       
   116             listener.setSoTimeout(60000);
       
   117             int port = listener.getLocalPort();
       
   118 
       
   119             // Set-up the JDI connection
       
   120             JdiInitiator jdii = new JdiInitiator(port,
       
   121                     env.extraRemoteVMOptions(), REMOTE_AGENT, isLaunch, host);
       
   122             VirtualMachine vm = jdii.vm();
       
   123             Process process = jdii.process();
       
   124 
       
   125             List<Consumer<String>> deathListeners = new ArrayList<>();
       
   126             deathListeners.add(s -> env.closeDown());
       
   127             Util.detectJdiExitEvent(vm, s -> {
       
   128                 for (Consumer<String> h : deathListeners) {
       
   129                     h.accept(s);
       
   130                 }
       
   131             });
       
   132 
       
   133             // Set-up the commands/reslts on the socket.  Piggy-back snippet
       
   134             // output.
       
   135             Socket socket = listener.accept();
       
   136             // out before in -- match remote creation so we don't hang
       
   137             OutputStream out = socket.getOutputStream();
       
   138             Map<String, OutputStream> outputs = new HashMap<>();
       
   139             outputs.put("out", env.userOut());
       
   140             outputs.put("err", env.userErr());
       
   141             Map<String, InputStream> input = new HashMap<>();
       
   142             input.put("in", env.userIn());
       
   143             return remoteInputOutput(socket.getInputStream(), out, outputs, input, (objIn, objOut) -> new JdiDefaultExecutionControl(objOut, objIn, vm, process, deathListeners));
       
   144         }
       
   145     }
       
   146 
       
   147     /**
       
   148      * Create an instance.
       
   149      *
       
   150      * @param cmdout the output for commands
       
   151      * @param cmdin the input for responses
       
   152      */
       
   153     private JdiDefaultExecutionControl(ObjectOutput cmdout, ObjectInput cmdin,
       
   154             VirtualMachine vm, Process process, List<Consumer<String>> deathListeners) {
       
   155         super(cmdout, cmdin);
       
   156         this.vm = vm;
       
   157         this.process = process;
       
   158         deathListeners.add(s -> disposeVM());
       
   159     }
       
   160 
       
   161     @Override
       
   162     public String invoke(String classname, String methodname)
       
   163             throws RunException,
       
   164             EngineTerminationException, InternalException {
       
   165         String res;
       
   166         synchronized (STOP_LOCK) {
       
   167             userCodeRunning = true;
       
   168         }
       
   169         try {
       
   170             res = super.invoke(classname, methodname);
       
   171         } finally {
       
   172             synchronized (STOP_LOCK) {
       
   173                 userCodeRunning = false;
       
   174             }
       
   175         }
       
   176         return res;
       
   177     }
       
   178 
       
   179     /**
       
   180      * Interrupts a running remote invoke by manipulating remote variables
       
   181      * and sending a stop via JDI.
       
   182      *
       
   183      * @throws EngineTerminationException the execution engine has terminated
       
   184      * @throws InternalException an internal problem occurred
       
   185      */
       
   186     @Override
       
   187     public void stop() throws EngineTerminationException, InternalException {
       
   188         synchronized (STOP_LOCK) {
       
   189             if (!userCodeRunning) {
       
   190                 return;
       
   191             }
       
   192 
       
   193             vm().suspend();
       
   194             try {
       
   195                 OUTER:
       
   196                 for (ThreadReference thread : vm().allThreads()) {
       
   197                     // could also tag the thread (e.g. using name), to find it easier
       
   198                     for (StackFrame frame : thread.frames()) {
       
   199                         if (REMOTE_AGENT.equals(frame.location().declaringType().name()) &&
       
   200                                 (    "invoke".equals(frame.location().method().name())
       
   201                                 || "varValue".equals(frame.location().method().name()))) {
       
   202                             ObjectReference thiz = frame.thisObject();
       
   203                             Field inClientCode = thiz.referenceType().fieldByName("inClientCode");
       
   204                             Field expectingStop = thiz.referenceType().fieldByName("expectingStop");
       
   205                             Field stopException = thiz.referenceType().fieldByName("stopException");
       
   206                             if (((BooleanValue) thiz.getValue(inClientCode)).value()) {
       
   207                                 thiz.setValue(expectingStop, vm().mirrorOf(true));
       
   208                                 ObjectReference stopInstance = (ObjectReference) thiz.getValue(stopException);
       
   209 
       
   210                                 vm().resume();
       
   211                                 debug("Attempting to stop the client code...\n");
       
   212                                 thread.stop(stopInstance);
       
   213                                 thiz.setValue(expectingStop, vm().mirrorOf(false));
       
   214                             }
       
   215 
       
   216                             break OUTER;
       
   217                         }
       
   218                     }
       
   219                 }
       
   220             } catch (ClassNotLoadedException | IncompatibleThreadStateException | InvalidTypeException ex) {
       
   221                 throw new InternalException("Exception on remote stop: " + ex);
       
   222             } finally {
       
   223                 vm().resume();
       
   224             }
       
   225         }
       
   226     }
       
   227 
       
   228     @Override
       
   229     public void close() {
       
   230         super.close();
       
   231         disposeVM();
       
   232     }
       
   233 
       
   234     private synchronized void disposeVM() {
       
   235         try {
       
   236             if (vm != null) {
       
   237                 vm.dispose(); // This could NPE, so it is caught below
       
   238                 vm = null;
       
   239             }
       
   240         } catch (VMDisconnectedException ex) {
       
   241             // Ignore if already closed
       
   242         } catch (Throwable ex) {
       
   243             debug(ex, "disposeVM");
       
   244         } finally {
       
   245             if (process != null) {
       
   246                 process.destroy();
       
   247                 process = null;
       
   248             }
       
   249         }
       
   250     }
       
   251 
       
   252     @Override
       
   253     protected synchronized VirtualMachine vm() throws EngineTerminationException {
       
   254         if (vm == null) {
       
   255             throw new EngineTerminationException("VM closed");
       
   256         } else {
       
   257             return vm;
       
   258         }
       
   259     }
       
   260 
       
   261     /**
       
   262      * Log debugging information. Arguments as for {@code printf}.
       
   263      *
       
   264      * @param format a format string as described in Format string syntax
       
   265      * @param args arguments referenced by the format specifiers in the format
       
   266      * string.
       
   267      */
       
   268     private static void debug(String format, Object... args) {
       
   269         // Reserved for future logging
       
   270     }
       
   271 
       
   272     /**
       
   273      * Log a serious unexpected internal exception.
       
   274      *
       
   275      * @param ex the exception
       
   276      * @param where a description of the context of the exception
       
   277      */
       
   278     private static void debug(Throwable ex, String where) {
       
   279         // Reserved for future logging
       
   280     }
       
   281 
       
   282 }