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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
44 *
45 * @library /test/lib
46 * @modules java.base/jdk.internal.misc
47 * java.management
48 * jdk.jfr
49 *
50 * @run main/othervm --add-exports=java.base/jdk.internal.misc=ALL-UNNAMED jdk.jfr.jvm.TestDumpOnCrash
51 */
52 public class TestDumpOnCrash {
53
54 private static final CharSequence LOG_FILE_EXTENSION = ".log";
55 private static final CharSequence JFR_FILE_EXTENSION = ".jfr";
56
57 static class Crasher {
58 public static void main(String[] args) {
59 Unsafe.getUnsafe().putInt(0L, 0);
60 }
61 }
62
63 public static void main(String[] args) throws Exception {
64 processOutput(runProcess());
65 }
66
67 private static OutputAnalyzer runProcess() throws Exception {
68 return new OutputAnalyzer(
69 ProcessTools.createJavaProcessBuilder(true,
70 "-Xmx64m",
71 "-Xint",
72 "-XX:-CreateCoredumpOnCrash",
73 "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
74 "-XX:StartFlightRecording=dumponexit=true",
75 Crasher.class.getName()).start());
76 }
77
78 private static void processOutput(OutputAnalyzer output) throws Exception {
79 output.shouldContain("CreateCoredumpOnCrash turned off, no core file dumped");
80
81 final Path jfrEmergencyFilePath = getHsErrJfrPath(output);
82 Asserts.assertTrue(Files.exists(jfrEmergencyFilePath), "No emergency jfr recording file " + jfrEmergencyFilePath + " exists");
83 Asserts.assertNotEquals(Files.size(jfrEmergencyFilePath), 0L, "File length 0. Should at least be some bytes");
84 System.out.printf("File size=%d%n", Files.size(jfrEmergencyFilePath));
85
86 List<RecordedEvent> events = RecordingFile.readAllEvents(jfrEmergencyFilePath);
87 Asserts.assertFalse(events.isEmpty(), "No event found");
88 System.out.printf("Found event %s%n", events.get(0).getEventType().getName());
89 }
90
91 private static Path getHsErrJfrPath(OutputAnalyzer output) throws Exception {
92 // extract to find hs-err_pid log file location
93 final String hs_err_pid_log_file = output.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
94 if (hs_err_pid_log_file == null) {
|
1 /*
2 * Copyright (c) 2016, 2019, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
44 *
45 * @library /test/lib
46 * @modules java.base/jdk.internal.misc
47 * java.management
48 * jdk.jfr
49 *
50 * @run main/othervm --add-exports=java.base/jdk.internal.misc=ALL-UNNAMED jdk.jfr.jvm.TestDumpOnCrash
51 */
52 public class TestDumpOnCrash {
53
54 private static final CharSequence LOG_FILE_EXTENSION = ".log";
55 private static final CharSequence JFR_FILE_EXTENSION = ".jfr";
56
57 static class Crasher {
58 public static void main(String[] args) {
59 Unsafe.getUnsafe().putInt(0L, 0);
60 }
61 }
62
63 public static void main(String[] args) throws Exception {
64 processOutput(runProcess(true));
65 processOutput(runProcess(false));
66 }
67
68 private static OutputAnalyzer runProcess(boolean diskRepo) throws Exception {
69 String flightRecordingOptions = "dumponexit=true,disk=" + Boolean.toString(diskRepo);
70 return new OutputAnalyzer(
71 ProcessTools.createJavaProcessBuilder(true,
72 "-Xmx64m",
73 "-Xint",
74 "-XX:-CreateCoredumpOnCrash",
75 "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
76 "-XX:StartFlightRecording=" + flightRecordingOptions,
77 Crasher.class.getName()).start());
78 }
79
80 private static void processOutput(OutputAnalyzer output) throws Exception {
81 output.shouldContain("CreateCoredumpOnCrash turned off, no core file dumped");
82
83 final Path jfrEmergencyFilePath = getHsErrJfrPath(output);
84 Asserts.assertTrue(Files.exists(jfrEmergencyFilePath), "No emergency jfr recording file " + jfrEmergencyFilePath + " exists");
85 Asserts.assertNotEquals(Files.size(jfrEmergencyFilePath), 0L, "File length 0. Should at least be some bytes");
86 System.out.printf("File size=%d%n", Files.size(jfrEmergencyFilePath));
87
88 List<RecordedEvent> events = RecordingFile.readAllEvents(jfrEmergencyFilePath);
89 Asserts.assertFalse(events.isEmpty(), "No event found");
90 System.out.printf("Found event %s%n", events.get(0).getEventType().getName());
91 }
92
93 private static Path getHsErrJfrPath(OutputAnalyzer output) throws Exception {
94 // extract to find hs-err_pid log file location
95 final String hs_err_pid_log_file = output.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
96 if (hs_err_pid_log_file == null) {
|