1 /*
   2  * Copyright (c) 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 TestJmapCore
  26  * @summary Test verifies that jhsdb jmap could generate heap dump from core when heap is full
  27  * @modules java.base/jdk.internal.misc
  28  * @library /test/lib
  29  * @run driver/timeout=240 TestJmapCore run heap
  30  */
  31 
  32 import jdk.test.lib.Asserts;
  33 import jdk.test.lib.JDKToolFinder;
  34 import jdk.test.lib.JDKToolLauncher;
  35 import jdk.test.lib.Platform;
  36 import jdk.test.lib.classloader.GeneratingClassLoader;
  37 import jdk.test.lib.hprof.HprofParser;
  38 import jdk.test.lib.process.ProcessTools;
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 
  41 import java.io.File;
  42 
  43 public class TestJmapCore {
  44     static final String pidSeparator = ":KILLED_PID";
  45 
  46     public static String HEAP_OOME = "heap";
  47     public static String METASPACE_OOME = "metaspace";
  48 
  49 
  50     public static void main(String[] args) throws Throwable {
  51         if (args.length == 1) {
  52             // If 1 argument is set prints pid so main process could find corefile
  53             System.out.println(ProcessHandle.current().pid() + pidSeparator);
  54             try {
  55                 if (args[0].equals(HEAP_OOME)) {
  56                     Object[] oa = new Object[Integer.MAX_VALUE / 2];
  57                     for(int i = 0; i < oa.length; i++) {
  58                         oa[i] = new Object[Integer.MAX_VALUE / 2];
  59                     }
  60                 } else {
  61                     GeneratingClassLoader loader = new GeneratingClassLoader();
  62                     for (int i = 0; ; i++) {
  63                         loader.loadClass(loader.getClassName(i));
  64                     }
  65                 }
  66                 throw new Error("OOME not triggered");
  67             } catch (OutOfMemoryError err) {
  68                 return;
  69             }
  70         }
  71         test(args[1]);
  72     }
  73 
  74     // Test tries to run java with ulimit unlimited if it is possible
  75     static boolean useDefaultUlimit() {
  76         if (Platform.isWindows()) {
  77             return true;
  78         }
  79         try {
  80             OutputAnalyzer output = ProcessTools.executeProcess("sh", "-c", "ulimit -c unlimited && ulimit -c");
  81             return !(output.getExitValue() == 0 && output.getStdout().contains("unlimited"));
  82         } catch (Throwable t) {
  83             return true;
  84         }
  85     }
  86 
  87     static void test(String type) throws Throwable {
  88         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "-XX:+CreateCoredumpOnCrash",
  89                 "-XX:MaxMetaspaceSize=64m", "-XX:+CrashOnOutOfMemoryError", "-XX:-TransmitErrorReport",
  90                 TestJmapCore.class.getName(), type);
  91 
  92         boolean useDefaultUlimit = useDefaultUlimit();
  93         System.out.println("Run test with ulimit: " + (useDefaultUlimit ? "default" : "unlimited"));
  94         OutputAnalyzer output = useDefaultUlimit
  95             ? ProcessTools.executeProcess(pb)
  96             : ProcessTools.executeProcess("sh", "-c", "ulimit -c unlimited && "
  97                     + ProcessTools.getCommandLine(pb));
  98         File core;
  99         String pattern = Platform.isWindows() ? "mdmp" : "core";
 100         File[] cores = new File(".").listFiles((dir, name) -> name.contains(pattern));
 101         if (cores.length == 0) {
 102             // /cores/core.$pid might be generated on macosx by default
 103             String pid = output.firstMatch("^(\\d+)" + pidSeparator, 1);
 104             core = new File("cores/core." + pid);
 105             if (!core.exists()) {
 106                 System.out.println("Has not been able to find coredump. Test skipped.");
 107                 return;
 108             }
 109         } else {
 110             Asserts.assertTrue(cores.length == 1,
 111                     "There are unexpected files containing core "
 112                     + ": " + String.join(",", new File(".").list()) + ".");
 113             core = cores[0];
 114         }
 115         System.out.println("Found corefile: " + core.getAbsolutePath());
 116 
 117         File dumpFile = new File("heap.hprof");
 118         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
 119         launcher.addToolArg("jmap");
 120         launcher.addToolArg("--binaryheap");
 121         launcher.addToolArg("--dumpfile=" + dumpFile);
 122         launcher.addToolArg("--exe");
 123         launcher.addToolArg(JDKToolFinder.getTestJDKTool("java"));
 124         launcher.addToolArg("--core");
 125         launcher.addToolArg(core.getPath());
 126 
 127         ProcessBuilder jhsdpb = new ProcessBuilder();
 128         jhsdpb.command(launcher.getCommand());
 129         Process jhsdb = jhsdpb.start();
 130         OutputAnalyzer out = new OutputAnalyzer(jhsdb);
 131 
 132         jhsdb.waitFor();
 133 
 134         System.out.println(out.getStdout());
 135         System.err.println(out.getStderr());
 136 
 137         Asserts.assertTrue(dumpFile.exists() && dumpFile.isFile(),
 138                 "Could not find dump file " + dumpFile.getAbsolutePath());
 139 
 140         HprofParser.parse(dumpFile);
 141         System.out.println("PASSED");
 142     }
 143 }