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