1/* 2 * Copyright (c) 2016, 2017, 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 */ 25package jdk.jshell.execution; 26 27import java.io.File; 28import java.io.IOException; 29import java.nio.charset.StandardCharsets; 30import java.nio.file.Files; 31import java.util.ArrayList; 32import java.util.HashMap; 33import java.util.List; 34import java.util.Map; 35import java.util.Map.Entry; 36import com.sun.jdi.Bootstrap; 37import com.sun.jdi.VirtualMachine; 38import com.sun.jdi.connect.Connector; 39import com.sun.jdi.connect.LaunchingConnector; 40import com.sun.jdi.connect.ListeningConnector; 41import java.util.concurrent.Callable; 42import java.util.concurrent.ExecutorService; 43import java.util.concurrent.Executors; 44import java.util.concurrent.Future; 45import java.util.concurrent.TimeUnit; 46import java.util.concurrent.TimeoutException; 47import com.sun.jdi.connect.IllegalConnectorArgumentsException; 48 49/** 50 * Sets up a JDI connection, providing the resulting JDI {@link VirtualMachine} 51 * and the {@link Process} the remote agent is running in. 52 * 53 * @since 9 54 */ 55public class JdiInitiator { 56 57 // factor for the timeout on all of connect 58 private static final double CONNECT_TIMEOUT_FACTOR = 1.5; 59 60 // Over-all connect time-out 61 private final int connectTimeout; 62 63 private VirtualMachine vm; 64 private Process process = null; 65 private final Connector connector; 66 private final String remoteAgent; 67 private final Map<String, com.sun.jdi.connect.Connector.Argument> connectorArgs; 68 69 /** 70 * Start the remote agent and establish a JDI connection to it. 71 * 72 * @param port the socket port for (non-JDI) commands 73 * @param remoteVMOptions any user requested VM command-line options 74 * @param remoteAgent full class name of remote agent to launch 75 * @param isLaunch does JDI do the launch? That is, LaunchingConnector, 76 * otherwise we start explicitly and use ListeningConnector 77 * @param host explicit hostname to use, if null use discovered 78 * hostname, applies to listening only (!isLaunch) 79 * @param timeout the start-up time-out in milliseconds. If zero or negative, 80 * will not wait thus will timeout immediately if not already started. 81 * @param customConnectorArgs custom arguments passed to the connector. 82 * These are JDI com.sun.jdi.connect.Connector arguments. 83 */ 84 public JdiInitiator(int port, List<String> remoteVMOptions, String remoteAgent, 85 boolean isLaunch, String host, int timeout, 86 Map<String, String> customConnectorArgs) { 87 this.remoteAgent = remoteAgent; 88 this.connectTimeout = (int) (timeout * CONNECT_TIMEOUT_FACTOR); 89 String connectorName 90 = isLaunch 91 ? "com.sun.jdi.CommandLineLaunch" 92 : "com.sun.jdi.SocketListen"; 93 this.connector = findConnector(connectorName); 94 if (connector == null) { 95 throw new IllegalArgumentException("No connector named: " + connectorName); 96 } 97 Map<String, String> argumentName2Value 98 = isLaunch 99 ? launchArgs(port, String.join(" ", remoteVMOptions)) 100 : new HashMap<>(); 101 if (!isLaunch) { 102 argumentName2Value.put("timeout", ""+timeout); 103 if (host != null && !isLaunch) { 104 argumentName2Value.put("localAddress", host); 105 } 106 } 107 argumentName2Value.putAll(customConnectorArgs); 108 this.connectorArgs = mergeConnectorArgs(connector, argumentName2Value); 109 this.vm = isLaunch 110 ? launchTarget() 111 : listenTarget(port, remoteVMOptions); 112 113 } 114 115 /** 116 * Returns the resulting {@code VirtualMachine} instance. 117 * 118 * @return the virtual machine 119 */ 120 public VirtualMachine vm() { 121 return vm; 122 } 123 124 /** 125 * Returns the launched process. 126 * 127 * @return the remote agent process 128 */ 129 public Process process() { 130 return process; 131 } 132 133 /* launch child target vm */ 134 private VirtualMachine launchTarget() { 135 LaunchingConnector launcher = (LaunchingConnector) connector; 136 try { 137 VirtualMachine new_vm = timedVirtualMachineCreation(() -> launcher.launch(connectorArgs), null); 138 process = new_vm.process(); 139 return new_vm; 140 } catch (Throwable ex) { 141 throw reportLaunchFail(ex, "launch"); 142 } 143 } 144 145 /** 146 * Directly launch the remote agent and connect JDI to it with a 147 * ListeningConnector. 148 */ 149 private VirtualMachine listenTarget(int port, List<String> remoteVMOptions) { 150 ListeningConnector listener = (ListeningConnector) connector; 151 // Files to collection to output of a start-up failure 152 File crashErrorFile = createTempFile("error"); 153 File crashOutputFile = createTempFile("output"); 154 try { 155 // Start listening, get the JDI connection address 156 String addr = listener.startListening(connectorArgs); 157 debug("Listening at address: " + addr); 158 159 // Launch the RemoteAgent requesting a connection on that address 160 String javaHome = System.getProperty("java.home"); 161 List<String> args = new ArrayList<>(); 162 args.add(javaHome == null 163 ? "java" 164 : javaHome + File.separator + "bin" + File.separator + "java"); 165 args.add("-agentlib:jdwp=transport=" + connector.transport().name() + 166 ",address=" + addr); 167 args.addAll(remoteVMOptions); 168 args.add(remoteAgent); 169 args.add("" + port); 170 ProcessBuilder pb = new ProcessBuilder(args); 171 pb.redirectError(crashErrorFile); 172 pb.redirectOutput(crashOutputFile); 173 process = pb.start(); 174 175 // Accept the connection from the remote agent 176 vm = timedVirtualMachineCreation(() -> listener.accept(connectorArgs), 177 () -> process.waitFor()); 178 try { 179 listener.stopListening(connectorArgs); 180 } catch (IOException | IllegalConnectorArgumentsException ex) { 181 // ignore 182 } 183 crashErrorFile.delete(); 184 crashOutputFile.delete(); 185 return vm; 186 } catch (Throwable ex) { 187 if (process != null) { 188 process.destroyForcibly(); 189 } 190 try { 191 listener.stopListening(connectorArgs); 192 } catch (IOException | IllegalConnectorArgumentsException iex) { 193 // ignore 194 } 195 String text = readFile(crashErrorFile) + readFile(crashOutputFile); 196 crashErrorFile.delete(); 197 crashOutputFile.delete(); 198 if (text.isEmpty()) { 199 throw reportLaunchFail(ex, "listen"); 200 } else { 201 throw new IllegalArgumentException(text); 202 } 203 } 204 } 205 206 private File createTempFile(String label) { 207 try { 208 File f = File.createTempFile("remote", label); 209 f.deleteOnExit(); 210 return f; 211 } catch (IOException ex) { 212 throw new InternalError("Failed create temp ", ex); 213 } 214 } 215 216 private String readFile(File f) { 217 try { 218 return new String(Files.readAllBytes(f.toPath()), 219 StandardCharsets.UTF_8); 220 } catch (IOException ex) { 221 return "error reading " + f + " : " + ex.toString(); 222 } 223 } 224 225 VirtualMachine timedVirtualMachineCreation(Callable<VirtualMachine> creator, 226 Callable<Integer> processComplete) throws Exception { 227 VirtualMachine result; 228 ExecutorService executor = Executors.newCachedThreadPool(runnable -> { 229 Thread thread = Executors.defaultThreadFactory().newThread(runnable); 230 thread.setDaemon(true); 231 return thread; 232 }); 233 try { 234 Future<VirtualMachine> future = executor.submit(creator); 235 if (processComplete != null) { 236 executor.submit(() -> { 237 Integer i = processComplete.call(); 238 future.cancel(true); 239 return i; 240 }); 241 } 242 243 try { 244 result = future.get(connectTimeout, TimeUnit.MILLISECONDS); 245 } catch (TimeoutException ex) { 246 future.cancel(true); 247 throw ex; 248 } 249 } finally { 250 executor.shutdownNow(); 251 } 252 return result; 253 } 254 255 private Connector findConnector(String name) { 256 for (Connector cntor 257 : Bootstrap.virtualMachineManager().allConnectors()) { 258 if (cntor.name().equals(name)) { 259 return cntor; 260 } 261 } 262 return null; 263 } 264 265 private Map<String, Connector.Argument> mergeConnectorArgs(Connector connector, Map<String, String> argumentName2Value) { 266 Map<String, Connector.Argument> arguments = connector.defaultArguments(); 267 268 for (Entry<String, String> argumentEntry : argumentName2Value.entrySet()) { 269 String name = argumentEntry.getKey(); 270 String value = argumentEntry.getValue(); 271 Connector.Argument argument = arguments.get(name); 272 273 if (argument == null) { 274 throw new IllegalArgumentException("Argument is not defined for connector:" + 275 name + " -- " + connector.name()); 276 } 277 278 argument.setValue(value); 279 } 280 281 return arguments; 282 } 283 284 /** 285 * The JShell specific Connector args for the LaunchingConnector. 286 * 287 * @param portthe socket port for (non-JDI) commands 288 * @param remoteVMOptions any user requested VM options 289 * @return the argument map 290 */ 291 private Map<String, String> launchArgs(int port, String remoteVMOptions) { 292 Map<String, String> argumentName2Value = new HashMap<>(); 293 argumentName2Value.put("main", remoteAgent + " " + port); 294 argumentName2Value.put("options", remoteVMOptions); 295 return argumentName2Value; 296 } 297 298 private InternalError reportLaunchFail(Throwable ex, String context) { 299 return new InternalError("Failed remote " + context + ": " 300 + ex.toString() 301 + " @ " + connector + 302 " -- " + connectorArgs, ex); 303 } 304 305 /** 306 * Log debugging information. Arguments as for {@code printf}. 307 * 308 * @param format a format string as described in Format string syntax 309 * @param args arguments referenced by the format specifiers in the format 310 * string. 311 */ 312 private void debug(String format, Object... args) { 313 // Reserved for future logging 314 } 315 316 /** 317 * Log a serious unexpected internal exception. 318 * 319 * @param ex the exception 320 * @param where a description of the context of the exception 321 */ 322 private void debug(Throwable ex, String where) { 323 // Reserved for future logging 324 } 325 326} 327