Path: blob/master/test/hotspot/jtreg/vmTestbase/ExecDriver.java
40930 views
/*1* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.io.IOException;25import java.io.InputStream;26import java.io.OutputStream;27import java.nio.file.Path;28import java.nio.file.Paths;29import java.util.ArrayList;30import java.util.Arrays;31import java.util.Collections;32import java.util.List;3334/**35* Starts a new process to execute a command.36* <p>Usage: --java|--cmd|--launcher <arg>+37* <p>If {@code --cmd} flag is specified, the arguments are treated as38* a program to run and its arguments. Non-zero exit code of the created process39* will be reported as an {@link AssertionError}.40* <p>If {@code --java} flag is specified, the arguments are passed to {@code java}41* from JDK under test. If exit code doesn't equal to 0 or 95, {@link AssertionError}42* will be thrown.43* <p>If {@code --launcher} flag is specified, the arguments treated similar as44* for {@code --cmd}, but the started process will have the directory which45* contains {@code jvm.so} in dynamic library path, and {@code test.class.path}46* as CLASSPATH environment variable. Exit codes are checked as in47* {@code --java}, i.e. 0 or 95 means pass.48*/49public class ExecDriver {50// copied from jdk.test.lib.Utils.TEST_CLASS_PATH51private static final String TEST_CLASS_PATH = System.getProperty("test.class.path", ".");52// copied from jdk.test.lib.Utils.TEST_CLASS_PATH53private static final String TEST_JDK = System.getProperty("test.jdk");54public static void main(String[] args) throws IOException, InterruptedException {55boolean java = false;56boolean launcher = false;5758String type = args[0];59switch (type) {60case "--java":61String[] oldArgs = args;62int count;63String libraryPath = System.getProperty("test.nativepath");64if (libraryPath != null && !libraryPath.isEmpty()) {65count = 4;66args = new String[args.length + 3];67args[3] = "-Djava.library.path=" + libraryPath;68} else {69count = 3;70args = new String[args.length + 2];71}72args[0] = javaBin();73args[1] = "-cp";74args[2] = TEST_CLASS_PATH;75System.arraycopy(oldArgs, 1, args, count, oldArgs.length - 1);76java = true;77break;78case "--launcher":79java = true;80launcher = true;81case "--cmd":82args = Arrays.copyOfRange(args, 1, args.length);83break;84default:85throw new Error("unknown type: " + type);86}87// adding 'test.vm.opts' and 'test.java.opts'88if (java) {89String[] oldArgs = args;90String[] testJavaOpts = getTestJavaOpts();91if (testJavaOpts.length > 0) {92args = new String[args.length + testJavaOpts.length];93// bin/java goes before options94args[0] = oldArgs[0];95// then external java options96System.arraycopy(testJavaOpts, 0, args, 1, testJavaOpts.length);97// and then options and args from a test98System.arraycopy(oldArgs, 1, args, 1 + testJavaOpts.length, oldArgs.length - 1);99}100}101String command = Arrays.toString(args);102System.out.println("exec " + command);103104ProcessBuilder pb = new ProcessBuilder(args);105// adding jvm.so to library path106if (launcher) {107Path dir = Paths.get(TEST_JDK);108String value;109String name = sharedLibraryPathVariableName();110// if (jdk.test.lib.Platform.isWindows()) {111if (System.getProperty("os.name").toLowerCase().startsWith("win")) {112value = dir.resolve("bin")113.resolve(variant())114.toAbsolutePath()115.toString();116value += File.pathSeparator;117value += dir.resolve("bin")118.toAbsolutePath()119.toString();120} else {121value = dir.resolve("lib")122.resolve(variant())123.toAbsolutePath()124.toString();125}126127System.out.println(" with " + name + " = " +128pb.environment()129.merge(name, value, (x, y) -> y + File.pathSeparator + x));130System.out.println(" with CLASSPATH = " +131pb.environment()132.put("CLASSPATH", TEST_CLASS_PATH));133}134Process p = pb.start();135// inheritIO does not work as expected for @run driver136new Thread(() -> copy(p.getInputStream(), System.out)).start();137new Thread(() -> copy(p.getErrorStream(), System.out)).start();138int exitCode = p.waitFor();139140if (exitCode != 0 && (!java || exitCode != 95)) {141throw new AssertionError(command + " exit code is " + exitCode);142}143}144145// copied from jdk.test.lib.Platform::sharedLibraryPathVariableName146private static String sharedLibraryPathVariableName() {147String osName = System.getProperty("os.name").toLowerCase();148if (osName.startsWith("win")) {149return "PATH";150} else if (osName.startsWith("mac")) {151return "DYLD_LIBRARY_PATH";152} else if (osName.startsWith("aix")) {153return "LIBPATH";154} else {155return "LD_LIBRARY_PATH";156}157}158159// copied from jdk.test.lib.Utils::getTestJavaOpts()160private static String[] getTestJavaOpts() {161List<String> opts = new ArrayList<String>();162{163String v = System.getProperty("test.vm.opts", "").trim();164if (!v.isEmpty()) {165Collections.addAll(opts, v.split("\\s+"));166}167}168{169String v = System.getProperty("test.java.opts", "").trim();170if (!v.isEmpty()) {171Collections.addAll(opts, v.split("\\s+"));172}173}174return opts.toArray(new String[0]);175}176177// copied jdk.test.lib.Platform::variant178private static String variant() {179String vmName = System.getProperty("java.vm.name");180if (vmName.endsWith(" Server VM")) {181return "server";182} else if (vmName.endsWith(" Client VM")) {183return "client";184} else if (vmName.endsWith(" Minimal VM")) {185return "minimal";186} else {187throw new Error("TESTBUG: unsuppported vm variant");188}189}190191private static void copy(InputStream is, OutputStream os) {192byte[] buffer = new byte[1024];193int n;194try (InputStream close = is) {195while ((n = is.read(buffer)) != -1) {196os.write(buffer, 0, n);197}198os.flush();199} catch (IOException e) {200e.printStackTrace();201}202}203204private static String javaBin() {205return Paths.get(TEST_JDK)206.resolve("bin")207.resolve("java")208.toAbsolutePath()209.toString();210}211}212213214215