1 /*
   2  * Copyright (c) 2015, 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.
   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 
  24 /*
  25  * @test
  26  * @summary Basic test for jhsdb launcher
  27  * @library /test/lib/share/classes
  28  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.*
  30  * @build jdk.test.lib.apps.*
  31  * @run main BasicLauncherTest
  32  */
  33 
  34 import static jdk.testlibrary.Asserts.assertTrue;
  35 
  36 import java.io.BufferedReader;
  37 import java.io.IOException;
  38 import java.io.InputStreamReader;
  39 import java.io.File;
  40 import java.util.ArrayList;
  41 import java.util.List;
  42 import java.util.Arrays;
  43 import java.util.Optional;
  44 import jdk.testlibrary.JDKToolLauncher;
  45 import jdk.testlibrary.Utils;
  46 import jdk.testlibrary.OutputAnalyzer;
  47 import jdk.testlibrary.ProcessTools;
  48 import jdk.test.lib.apps.LingeredApp;
  49 import jdk.testlibrary.Platform;
  50 
  51 public class BasicLauncherTest {
  52 
  53     private static LingeredApp theApp = null;
  54     private static boolean useJavaLauncher = false;
  55 
  56     private static JDKToolLauncher createSALauncher() {
  57         JDKToolLauncher launcher = null;
  58         if (useJavaLauncher) {
  59             // Use java launcher if we need to pass additional parameters to VM
  60             // for debugging purpose
  61             // e.g. -Xlog:class+load=info:file=/tmp/BasicLauncherTest.log
  62             launcher = JDKToolLauncher.createUsingTestJDK("java");
  63             launcher.addToolArg("sun.jvm.hotspot.SALauncher");
  64         }
  65         else {
  66             launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
  67         }
  68 
  69         return launcher;
  70     }
  71 
  72     public static void launchCLHSDB()
  73         throws IOException {
  74 
  75         System.out.println("Starting LingeredApp");
  76         try {
  77             theApp = LingeredApp.startApp();
  78 
  79             System.out.println("Starting clhsdb against " + theApp.getPid());
  80             JDKToolLauncher launcher = createSALauncher();
  81             launcher.addToolArg("clhsdb");
  82             launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));
  83 
  84             ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
  85             processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
  86             Process toolProcess = processBuilder.start();
  87             toolProcess.getOutputStream().write("quit\n".getBytes());
  88             toolProcess.getOutputStream().close();
  89 
  90             // By default child process output stream redirected to pipe, so we are reading it in foreground.
  91             BufferedReader reader = new BufferedReader(new InputStreamReader(toolProcess.getInputStream()));
  92 
  93             String line;
  94             while ((line = reader.readLine()) != null) {
  95                 System.out.println(line.trim());
  96             }
  97 
  98             toolProcess.waitFor();
  99 
 100             if (toolProcess.exitValue() != 0) {
 101                 throw new RuntimeException("FAILED CLHSDB terminated with non-zero exit code " + toolProcess.exitValue());
 102             }
 103         } catch (Exception ex) {
 104             throw new RuntimeException("Test ERROR " + ex, ex);
 105         } finally {
 106             LingeredApp.stopApp(theApp);
 107         }
 108     }
 109 
 110     /**
 111      *
 112      * @param vmArgs  - vm and java arguments to launch test app
 113      * @return exit code of tool
 114      */
 115     public static void launch(String expectedMessage,
 116                  Optional<String> unexpectedMessage, List<String> toolArgs)
 117         throws IOException {
 118 
 119         System.out.println("Starting LingeredApp");
 120         try {
 121             theApp = LingeredApp.startApp(Arrays.asList("-Xmx256m"));
 122 
 123             System.out.println("Starting " + toolArgs.get(0) + " against " + theApp.getPid());
 124             JDKToolLauncher launcher = createSALauncher();
 125 
 126             for (String cmd : toolArgs) {
 127                 launcher.addToolArg(cmd);
 128             }
 129 
 130             launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));
 131 
 132             ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
 133             processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
 134             OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);;
 135             output.shouldContain(expectedMessage);
 136             unexpectedMessage.ifPresent(output::shouldNotContain);
 137             output.shouldHaveExitValue(0);
 138 
 139         } catch (Exception ex) {
 140             throw new RuntimeException("Test ERROR " + ex, ex);
 141         } finally {
 142             LingeredApp.stopApp(theApp);
 143         }
 144     }
 145 
 146     public static void launch(String expectedMessage,
 147                               String unexpectedMessage, String... toolArgs)
 148         throws IOException {
 149 
 150         launch(expectedMessage, Optional.ofNullable(unexpectedMessage),
 151                                                        Arrays.asList(toolArgs));
 152     }
 153 
 154     public static void launchNotOSX(String expectedMessage,
 155                               String unexpectedMessage, String... toolArgs)
 156         throws IOException {
 157 
 158         if (Platform.isOSX()) {
 159             // Coredump stackwalking is not implemented for Darwin
 160             System.out.println("This test is not expected to work on OS X. Skipping");
 161             return;
 162         }
 163 
 164         launch(expectedMessage, unexpectedMessage, toolArgs);
 165     }
 166 
 167     public static void testHeapDump() throws IOException {
 168         File dump = new File("jhsdb.jmap.dump." +
 169                              System.currentTimeMillis() + ".hprof");
 170         if (dump.exists()) {
 171             dump.delete();
 172         }
 173         dump.deleteOnExit();
 174 
 175         launch("heap written to", null, "jmap",
 176                "--binaryheap", "--dumpfile=" + dump.getAbsolutePath());
 177 
 178         assertTrue(dump.exists() && dump.isFile(),
 179                    "Could not create dump file " + dump.getAbsolutePath());
 180     }
 181 
 182     public static void main(String[] args)
 183         throws IOException {
 184 
 185         if (!Platform.shouldSAAttach()) {
 186             // Silently skip the test if we don't have enough permissions to attach
 187             System.err.println("Error! Insufficient permissions to attach.");
 188             return;
 189         }
 190 
 191         launchCLHSDB();
 192 
 193         launch("compiler detected", null, "jmap", "--clstats");
 194         launchNotOSX("No deadlocks found", null, "jstack");
 195         launch("compiler detected", null, "jmap");
 196         launch("Java System Properties",
 197                "System Properties info not available", "jinfo");
 198         launch("java.threads", null, "jsnap");
 199 
 200         testHeapDump();
 201 
 202         // The test throws RuntimeException on error.
 203         // IOException is thrown if LingeredApp can't start because of some bad
 204         // environment condition
 205         System.out.println("Test PASSED");
 206     }
 207 }