1/* 2 * Copyright (c) 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 24import java.util.ArrayList; 25import java.util.List; 26import java.io.File; 27import java.nio.file.Files; 28import java.io.IOException; 29import java.io.BufferedInputStream; 30import java.util.stream.Collectors; 31import java.io.FileInputStream; 32 33import sun.jvm.hotspot.HotSpotAgent; 34import sun.jvm.hotspot.debugger.*; 35 36import jdk.test.lib.apps.LingeredApp; 37import jdk.test.lib.JDKToolLauncher; 38import jdk.test.lib.JDKToolFinder; 39import jdk.test.lib.Platform; 40import jdk.test.lib.process.ProcessTools; 41import jdk.test.lib.process.OutputAnalyzer; 42import jdk.test.lib.Utils; 43import jdk.test.lib.Asserts; 44import jdk.test.lib.hprof.HprofParser; 45import jdk.test.lib.hprof.parser.HprofReader; 46import jdk.test.lib.hprof.parser.PositionDataInputStream; 47import jdk.test.lib.hprof.model.Snapshot; 48 49/* 50 * @test 51 * @library /test/lib 52 * @requires os.family != "mac" 53 * @modules java.base/jdk.internal.misc 54 * jdk.hotspot.agent/sun.jvm.hotspot 55 * jdk.hotspot.agent/sun.jvm.hotspot.utilities 56 * jdk.hotspot.agent/sun.jvm.hotspot.oops 57 * jdk.hotspot.agent/sun.jvm.hotspot.debugger 58 * @run main/othervm TestHeapDumpForInvokeDynamic 59 */ 60 61public class TestHeapDumpForInvokeDynamic { 62 63 private static LingeredAppWithInvokeDynamic theApp = null; 64 65 private static void verifyHeapDump(String heapFile) { 66 67 File heapDumpFile = new File(heapFile); 68 Asserts.assertTrue(heapDumpFile.exists() && heapDumpFile.isFile(), 69 "Could not create dump file " + heapDumpFile.getAbsolutePath()); 70 try (PositionDataInputStream in = new PositionDataInputStream( 71 new BufferedInputStream(new FileInputStream(heapFile)))) { 72 int i = in.readInt(); 73 if (HprofReader.verifyMagicNumber(i)) { 74 Snapshot sshot; 75 HprofReader r = new HprofReader(heapFile, in, 0, 76 false, 0); 77 sshot = r.read(); 78 } else { 79 throw new IOException("Unrecognized magic number: " + i); 80 } 81 } catch (Exception e) { 82 e.printStackTrace(); 83 Asserts.fail("Could not read dump file " + heapFile); 84 } finally { 85 heapDumpFile.delete(); 86 } 87 } 88 89 private static void attachDumpAndVerify(String heapDumpFileName, 90 long lingeredAppPid) throws Exception { 91 92 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb"); 93 launcher.addToolArg("jmap"); 94 launcher.addToolArg("--binaryheap"); 95 launcher.addToolArg("--dumpfile"); 96 launcher.addToolArg(heapDumpFileName); 97 launcher.addToolArg("--pid"); 98 launcher.addToolArg(Long.toString(lingeredAppPid)); 99 100 ProcessBuilder processBuilder = new ProcessBuilder(); 101 processBuilder.command(launcher.getCommand()); 102 System.out.println( 103 processBuilder.command().stream().collect(Collectors.joining(" "))); 104 105 OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder); 106 SAOutput.shouldHaveExitValue(0); 107 SAOutput.shouldContain("heap written to"); 108 SAOutput.shouldContain(heapDumpFileName); 109 System.out.println(SAOutput.getOutput()); 110 111 verifyHeapDump(heapDumpFileName); 112 } 113 114 public static void main (String... args) throws Exception { 115 116 String heapDumpFileName = "lambdaHeapDump.bin"; 117 118 if (!Platform.shouldSAAttach()) { 119 System.out.println( 120 "SA attach not expected to work - test skipped."); 121 return; 122 } 123 124 File heapDumpFile = new File(heapDumpFileName); 125 if (heapDumpFile.exists()) { 126 heapDumpFile.delete(); 127 } 128 129 try { 130 List<String> vmArgs = new ArrayList<String>(); 131 vmArgs.add("-XX:+UsePerfData"); 132 vmArgs.addAll(Utils.getVmOptions()); 133 134 theApp = new LingeredAppWithInvokeDynamic(); 135 LingeredApp.startApp(vmArgs, theApp); 136 attachDumpAndVerify(heapDumpFileName, theApp.getPid()); 137 } finally { 138 LingeredApp.stopApp(theApp); 139 } 140 } 141} 142