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