1 /* 2 * Copyright (c) 2015, 2015, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.graalvm.compiler.test; 24 25 import java.io.File; 26 import java.io.IOException; 27 import java.nio.file.Files; 28 import java.util.Arrays; 29 import java.util.List; 30 31 /** 32 * Utility methods for spawning a VM in a subprocess during unit tests. 33 */ 34 public final class SubprocessUtil { 35 36 private SubprocessUtil() { 37 } 38 39 /** 40 * Gets the command line for the current process. 41 * 42 * @return the command line arguments for the current process or {@code null} if they are not 43 * available 44 */ 45 public static List<String> getProcessCommandLine() { 46 String processArgsFile = System.getenv().get("MX_SUBPROCESS_COMMAND_FILE"); 47 if (processArgsFile != null) { 48 try { 49 return Files.readAllLines(new File(processArgsFile).toPath()); 50 } catch (IOException e) { 51 } 52 } 53 return null; 54 } 55 56 /** 57 * Gets the command line used to start the current Java VM, including all VM arguments, but not 58 * including the main class or any Java arguments. This can be used to spawn an identical VM, 59 * but running different Java code. 60 */ 61 public static List<String> getVMCommandLine() { 62 List<String> args = getProcessCommandLine(); 63 if (args == null) { 64 return null; 65 } else { 66 int index = findMainClassIndex(args); 67 return args.subList(0, index); 68 } 69 } 70 71 public static final List<String> JVM_OPTIONS_WITH_ONE_ARG = System.getProperty("java.specification.version").compareTo("1.9") < 0 ? // 72 Arrays.asList("-cp", "-classpath") : // 73 Arrays.asList("-cp", "-classpath", "-mp", "-modulepath", "-upgrademodulepath", "-addmods", "-m", "-limitmods"); 74 75 private static int findMainClassIndex(List<String> commandLine) { 76 int i = 1; // Skip the java executable 77 78 while (i < commandLine.size()) { 79 String s = commandLine.get(i); 80 if (s.charAt(0) != '-') { 81 return i; 82 } else if (JVM_OPTIONS_WITH_ONE_ARG.contains(s)) { 83 i += 2; 84 } else { 85 i++; 86 } 87 } 88 throw new InternalError(); 89 } 90 91 }