1 /*
   2  * Copyright (c) 2016, 2018, 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 8163346
  27  * @summary Test hashing of extended characters in Serviceability Agent.
  28  * @requires vm.hasSAandCanAttach
  29  * @library /test/lib
  30  * @library /lib/testlibrary
  31  * @compile -encoding utf8 HeapDumpTest.java
  32  * @run main/timeout=240 HeapDumpTest
  33  */
  34 
  35 import static jdk.testlibrary.Asserts.assertTrue;
  36 
  37 import java.io.IOException;
  38 import java.io.File;
  39 import java.util.List;
  40 import java.util.Arrays;
  41 
  42 import jdk.test.lib.process.OutputAnalyzer;
  43 import jdk.test.lib.process.ProcessTools;
  44 import jdk.test.lib.apps.LingeredApp;
  45 import jdk.test.lib.Platform;
  46 import jdk.test.lib.hprof.parser.HprofReader;
  47 import jdk.testlibrary.JDKToolLauncher;
  48 
  49 public class HeapDumpTest {
  50 
  51     private static LingeredAppWithExtendedChars theApp = null;
  52 
  53     /**
  54      *
  55      * @param vmArgs  - tool arguments to launch jhsdb
  56      * @return exit code of tool
  57      */
  58     public static void launch(String expectedMessage, List<String> toolArgs)
  59         throws IOException {
  60 
  61         System.out.println("Starting LingeredApp");
  62         try {
  63             theApp = new LingeredAppWithExtendedChars();
  64             LingeredApp.startApp(Arrays.asList("-Xmx256m"), theApp);
  65 
  66             System.out.println(theApp.\u00CB);
  67             System.out.println("Starting " + toolArgs.get(0) + " against " + theApp.getPid());
  68             JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
  69 
  70             for (String cmd : toolArgs) {
  71                 launcher.addToolArg(cmd);
  72             }
  73 
  74             launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));
  75 
  76             ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
  77             processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
  78             OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
  79             System.out.println("stdout:");
  80             System.out.println(output.getStdout());
  81             System.out.println("stderr:");
  82             System.out.println(output.getStderr());
  83             output.shouldContain(expectedMessage);
  84             output.shouldHaveExitValue(0);
  85 
  86         } catch (Exception ex) {
  87             throw new RuntimeException("Test ERROR " + ex, ex);
  88         } finally {
  89             LingeredApp.stopApp(theApp);
  90         }
  91     }
  92 
  93     public static void launch(String expectedMessage, String... toolArgs)
  94         throws IOException {
  95 
  96         launch(expectedMessage, Arrays.asList(toolArgs));
  97     }
  98 
  99     public static void printStackTraces(String file) throws IOException {
 100         try {
 101             String output = HprofReader.getStack(file, 0);
 102             if (!output.contains("LingeredAppWithExtendedChars.main")) {
 103                 throw new RuntimeException("'LingeredAppWithExtendedChars.main' missing from stdout/stderr");
 104             }
 105         } catch (Exception ex) {
 106             throw new RuntimeException("Test ERROR " + ex, ex);
 107         }
 108     }
 109 
 110     public static void testHeapDump() throws IOException {
 111         File dump = new File("jhsdb.jmap.heap." +
 112                              System.currentTimeMillis() + ".hprof");
 113         if (dump.exists()) {
 114             dump.delete();
 115         }
 116 
 117         launch("heap written to", "jmap",
 118                "--binaryheap", "--dumpfile=" + dump.getAbsolutePath());
 119 
 120         assertTrue(dump.exists() && dump.isFile(),
 121                    "Could not create dump file " + dump.getAbsolutePath());
 122 
 123         printStackTraces(dump.getAbsolutePath());
 124 
 125         dump.delete();
 126     }
 127 
 128     public static void main(String[] args) throws Exception {
 129 
 130         testHeapDump();
 131 
 132         // The test throws RuntimeException on error.
 133         // IOException is thrown if LingeredApp can't start because of some bad
 134         // environment condition
 135         System.out.println("Test PASSED");
 136     }
 137 }