1 /*
   2  * Copyright (c) 2019, 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  * @bug 8225715
  27  * @requires vm.hasSAandCanAttach
  28  * @library /test/lib
  29  * @compile -encoding utf8 JShellHeapDumpTest.java
  30  * @run main/timeout=240 JShellHeapDumpTest
  31  */
  32 
  33 import static jdk.test.lib.Asserts.assertTrue;
  34 
  35 import java.io.IOException;
  36 import java.io.File;
  37 import java.util.List;
  38 import java.util.Arrays;
  39 import java.util.Map;
  40 
  41 import jdk.test.lib.JDKToolLauncher;
  42 import jdk.test.lib.process.OutputAnalyzer;
  43 import jdk.test.lib.process.ProcessTools;
  44 import jdk.test.lib.hprof.parser.HprofReader;
  45 
  46 import jdk.jshell.JShell;
  47 
  48 public class JShellHeapDumpTest {
  49 
  50     protected static Process process;
  51 
  52     private static long pid;
  53 
  54     public static void launch(String expectedMessage, List<String> toolArgs)
  55         throws IOException {
  56 
  57         try {
  58             launchJshell();
  59 
  60             System.out.println("Starting " + toolArgs.get(0) + " against " + pid);
  61             JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
  62 
  63             for (String cmd : toolArgs) {
  64                 launcher.addToolArg(cmd);
  65             }
  66 
  67             launcher.addToolArg("--pid=" + Long.toString(pid));
  68 
  69             ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
  70             processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
  71             OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
  72             System.out.println("stdout:");
  73             System.out.println(output.getStdout());
  74             System.out.println("stderr:");
  75             System.out.println(output.getStderr());
  76             output.shouldNotContain("null");
  77             output.shouldHaveExitValue(0);
  78         } catch (Exception ex) {
  79             throw new RuntimeException("Test ERROR " + ex, ex);
  80         } finally {
  81            if (process.isAlive()) {
  82              process.destroy();
  83         } 
  84         }
  85     }
  86 
  87     public static void launch(String expectedMessage, String... toolArgs)
  88         throws IOException {
  89 
  90         launch(expectedMessage, Arrays.asList(toolArgs));
  91     }
  92 
  93     public static void printStackTraces(String file) throws IOException {
  94         try {
  95             String output = HprofReader.getStack(file, 0);
  96             if (!output.contains("JShellToolProvider")) {
  97                 throw new RuntimeException("'JShellToolProvider' missing from stdout/stderr");
  98             }
  99         } catch (Exception ex) {
 100             throw new RuntimeException("Test ERROR " + ex, ex);
 101         }
 102     }
 103 
 104     public static void testHeapDump() throws IOException {
 105         File dump = new File("jhsdb.jmap.heap." +
 106                              System.currentTimeMillis() + ".hprof");
 107         if (dump.exists()) {
 108             dump.delete();
 109         }
 110 
 111         launch("heap written to", "jmap",
 112                "--binaryheap", "--dumpfile=" + dump.getAbsolutePath());
 113 
 114         assertTrue(dump.exists() && dump.isFile(),
 115                    "Could not create dump file " + dump.getAbsolutePath());
 116 
 117         printStackTraces(dump.getAbsolutePath());
 118 
 119         dump.delete();
 120     }
 121 
 122     public static void launchJshell() throws IOException {
 123         System.out.println("Starting Jshell");
 124         String jdkPath = System.getProperty("test.jdk");
 125         if (jdkPath == null) {
 126             // we are not under jtreg, try env
 127             Map<String, String> env = System.getenv();
 128             jdkPath = env.get("TESTJAVA");
 129         }
 130  
 131         if (jdkPath == null) {
 132             throw new RuntimeException("Can't determine jdk path neither test.jdk property no TESTJAVA env are set");
 133         }
 134   
 135         String osname = System.getProperty("os.name");
 136         String jshell = jdkPath + ((osname.startsWith("window")) ? "/bin/jshell.exe" : "/bin/jshell");
 137  
 138         process = Runtime.getRuntime().exec(jshell);
 139         pid = process.pid();
 140     }
 141 
 142     public static void main(String[] args) throws Exception {
 143 
 144         testHeapDump();
 145 
 146         // The test throws RuntimeException on error.
 147         // IOException is thrown if Jshell can't start because of some bad
 148         // environment condition
 149         System.out.println("Test PASSED");
 150     }
 151 }