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 * @requires vm.hasSA 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 import jtreg.SkippedException; 41 42 import java.io.File; 43 44 public class TestJmapCore { 45 static final String pidSeparator = ":KILLED_PID"; 46 47 public static final String HEAP_OOME = "heap"; 48 public static final String METASPACE_OOME = "metaspace"; 49 50 51 public static void main(String[] args) throws Throwable { 52 if (args.length == 1) { 53 // If 1 argument is set prints pid so main process could find corefile 54 System.out.println(ProcessHandle.current().pid() + pidSeparator); 55 try { 56 if (args[0].equals(HEAP_OOME)) { 57 Object[] oa = new Object[Integer.MAX_VALUE / 2]; 58 for(int i = 0; i < oa.length; i++) { 59 oa[i] = new Object[Integer.MAX_VALUE / 2]; 60 } 61 } else { 62 GeneratingClassLoader loader = new GeneratingClassLoader(); 63 for (int i = 0; ; i++) { 64 loader.loadClass(loader.getClassName(i)); 65 } 66 } 67 throw new Error("OOME not triggered"); 68 } catch (OutOfMemoryError err) { 69 return; 70 } 71 } 72 test(args[1]); 73 } 74 75 // Test tries to run java with ulimit unlimited if it is possible 76 static boolean useDefaultUlimit() { 77 if (Platform.isWindows()) { 78 return true; 79 } 80 try { 81 OutputAnalyzer output = ProcessTools.executeProcess("sh", "-c", "ulimit -c unlimited && ulimit -c"); 82 return !(output.getExitValue() == 0 && output.getStdout().contains("unlimited")); 83 } catch (Throwable t) { 84 return true; 85 } 86 } 87 88 static void test(String type) throws Throwable { 89 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "-XX:+CreateCoredumpOnCrash", 90 "-Xmx512m", "-XX:MaxMetaspaceSize=64m", "-XX:+CrashOnOutOfMemoryError", 91 TestJmapCore.class.getName(), type); 92 93 boolean useDefaultUlimit = useDefaultUlimit(); 94 System.out.println("Run test with ulimit: " + (useDefaultUlimit ? "default" : "unlimited")); 95 OutputAnalyzer output = useDefaultUlimit 96 ? ProcessTools.executeProcess(pb) 97 : ProcessTools.executeProcess("sh", "-c", "ulimit -c unlimited && " 98 + ProcessTools.getCommandLine(pb)); 99 File core; 100 String pattern = Platform.isWindows() ? "mdmp" : "core"; 101 File[] cores = new File(".").listFiles((dir, name) -> name.contains(pattern)); 102 if (cores.length == 0) { 103 // /cores/core.$pid might be generated on macosx by default 104 String pid = output.firstMatch("^(\\d+)" + pidSeparator, 1); 105 core = new File("cores/core." + pid); 106 if (!core.exists()) { 107 throw new SkippedException("Has not been able to find coredump"); 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 }