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