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