Path: blob/master/test/jdk/sun/tools/jhsdb/BasicLauncherTest.java
41152 views
/*1* Copyright (c) 2015, 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*/2223/**24* @test25* @summary Basic test for jhsdb launcher26* @library /test/lib27* @requires vm.hasSA28* @build jdk.test.lib.apps.*29* @run main BasicLauncherTest30*/3132import java.io.BufferedReader;33import java.io.IOException;34import java.io.OutputStream;35import java.io.InputStreamReader;36import java.util.ArrayList;37import java.util.List;38import java.util.Arrays;39import java.util.Optional;40import jdk.test.lib.process.OutputAnalyzer;41import jdk.test.lib.process.ProcessTools;42import jdk.test.lib.apps.LingeredApp;43import jdk.test.lib.Platform;44import jdk.test.lib.JDKToolLauncher;45import jdk.test.lib.SA.SATestUtils;46import jdk.test.lib.Utils;4748public class BasicLauncherTest {4950private static LingeredApp theApp = null;51private static boolean useJavaLauncher = false;5253private static JDKToolLauncher createSALauncher() {54JDKToolLauncher launcher = null;55if (useJavaLauncher) {56// Use java launcher if we need to pass additional parameters to VM57// for debugging purpose58// e.g. -Xlog:class+load=info:file=/tmp/BasicLauncherTest.log59launcher = JDKToolLauncher.createUsingTestJDK("java");60launcher.addToolArg("sun.jvm.hotspot.SALauncher");61}62else {63launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");64}65launcher.addVMArgs(Utils.getFilteredTestJavaOpts("-Xcomp"));66return launcher;67}6869public static void launchCLHSDB()70throws IOException {7172System.out.println("Starting LingeredApp");73try {74theApp = LingeredApp.startApp();7576System.out.println("Starting clhsdb against " + theApp.getPid());77JDKToolLauncher launcher = createSALauncher();78launcher.addToolArg("clhsdb");79launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));8081ProcessBuilder processBuilder = SATestUtils.createProcessBuilder(launcher);82processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);83Process toolProcess = processBuilder.start();8485try (OutputStream out = toolProcess.getOutputStream()) {86out.write("universe\n".getBytes());87out.write("quit\n".getBytes());88}8990// By default child process output stream redirected to pipe, so we are reading it in foreground.91Exception unexpected = null;92try (BufferedReader reader =93new BufferedReader(new InputStreamReader(toolProcess.getInputStream()))) {94String line;9596while ((line = reader.readLine()) != null) {97line = line.trim();98System.out.println(line);99100if (line.contains("unknown subtype of CollectedHeap")) {101unexpected = new RuntimeException("CollectedHeap type should be known.");102break;103}104}105}106107toolProcess.waitFor();108109if (toolProcess.exitValue() != 0) {110throw new RuntimeException("FAILED CLHSDB terminated with non-zero exit code " + toolProcess.exitValue());111}112113if (unexpected != null) {114throw unexpected;115}116117} catch (Exception ex) {118throw new RuntimeException("Test ERROR " + ex, ex);119} finally {120LingeredApp.stopApp(theApp);121}122}123124public static void launchJStack() throws IOException {125System.out.println("Starting LingeredApp");126try {127theApp = LingeredApp.startApp("-Xmx256m");128129System.out.println("Starting jstack against " + theApp.getPid());130JDKToolLauncher launcher = createSALauncher();131132launcher.addToolArg("jstack");133launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));134135ProcessBuilder processBuilder = SATestUtils.createProcessBuilder(launcher);136OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);;137output.shouldContain("No deadlocks found");138output.shouldNotContain("illegal bci");139output.shouldNotContain("AssertionFailure");140output.shouldHaveExitValue(0);141142} catch (Exception ex) {143throw new RuntimeException("Test ERROR " + ex, ex);144} finally {145LingeredApp.stopApp(theApp);146}147}148149/**150*151* @param vmArgs - vm and java arguments to launch test app152* @return exit code of tool153*/154public static void launch(String expectedMessage,155Optional<String> unexpectedMessage, List<String> toolArgs)156throws IOException {157158System.out.println("Starting LingeredApp");159try {160theApp = LingeredApp.startApp("-Xmx256m");161162System.out.println("Starting " + toolArgs.get(0) + " against " + theApp.getPid());163JDKToolLauncher launcher = createSALauncher();164165for (String cmd : toolArgs) {166launcher.addToolArg(cmd);167}168169launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));170171ProcessBuilder processBuilder = SATestUtils.createProcessBuilder(launcher);172processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);173OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);;174output.shouldContain(expectedMessage);175unexpectedMessage.ifPresent(output::shouldNotContain);176output.shouldHaveExitValue(0);177178} catch (Exception ex) {179throw new RuntimeException("Test ERROR " + ex, ex);180} finally {181LingeredApp.stopApp(theApp);182}183}184185public static void launch(String expectedMessage,186String unexpectedMessage, String... toolArgs)187throws IOException {188189launch(expectedMessage, Optional.ofNullable(unexpectedMessage),190Arrays.asList(toolArgs));191}192193public static void main(String[] args) throws Exception {194SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.195196launchCLHSDB();197198launch("compiler detected", null, "jmap", "--clstats");199launchJStack();200launch("compiler detected", null, "jmap");201launch("Java System Properties",202"System Properties info not available", "jinfo");203launch("java.threads", null, "jsnap");204205// The test throws RuntimeException on error.206// IOException is thrown if LingeredApp can't start because of some bad207// environment condition208System.out.println("Test PASSED");209}210}211212213