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