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 23 * questions. 24 */ 25 package jdk.jfr.jvm; 26 27 import java.nio.file.Files; 28 import java.nio.file.Path; 29 import java.nio.file.Paths; 30 import java.util.List; 31 32 // import jdk.internal.misc.Unsafe; 33 import sun.misc.Unsafe; 34 import jdk.jfr.consumer.RecordedEvent; 35 import jdk.jfr.consumer.RecordingFile; 36 import jdk.testlibrary.Asserts; 37 import jdk.testlibrary.OutputAnalyzer; 38 import jdk.testlibrary.ProcessTools; 39 40 /* 41 * @test 42 * @key jfr 43 * @summary Verifies that data associated with a running recording can be evacuated to an hs_err_pidXXX.jfr when the VM crashes 44 * 45 * @library /lib/testlibrary 46 * @modules java.base/jdk.internal.misc 47 * java.management 48 * jdk.jfr 49 * 50 * @run main/othervm 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 //-XX:StartFlightRecording=dumponexit=true doesn't support 65 //processOutput(runProcess()); 66 } 67 68 private static OutputAnalyzer runProcess() throws Exception { 69 return new OutputAnalyzer( 70 ProcessTools.createJavaProcessBuilder(true, 71 "-Xmx64m", 72 "-Xint", 73 "-XX:-TransmitErrorReport", 74 "-XX:-CreateCoredumpOnCrash", 75 "-XX:StartFlightRecording=dumponexit=true", 76 Crasher.class.getName()).start()); 77 } 78 79 private static void processOutput(OutputAnalyzer output) throws Exception { 80 //output.shouldContain("CreateCoredumpOnCrash turned off, no core file dumped"); 81 82 final Path jfrEmergencyFilePath = getHsErrJfrPath(output); 83 Asserts.assertTrue(Files.exists(jfrEmergencyFilePath), "No emergency jfr recording file " + jfrEmergencyFilePath + " exists"); 84 Asserts.assertNotEquals(Files.size(jfrEmergencyFilePath), 0L, "File length 0. Should at least be some bytes"); 85 System.out.printf("File size=%d%n", Files.size(jfrEmergencyFilePath)); 86 87 List<RecordedEvent> events = RecordingFile.readAllEvents(jfrEmergencyFilePath); 88 Asserts.assertFalse(events.isEmpty(), "No event found"); 89 System.out.printf("Found event %s%n", events.get(0).getEventType().getName()); 90 } 91 92 private static Path getHsErrJfrPath(OutputAnalyzer output) throws Exception { 93 // extract to find hs-err_pid log file location 94 final String hs_err_pid_log_file = output.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1); 95 if (hs_err_pid_log_file == null) { 96 throw new RuntimeException("Did not find hs_err_pid.log file in output.\n"); 97 } 98 // the dumped out jfr file should have the same name and location but with a .jfr extension 99 final String hs_err_pid_jfr_file = hs_err_pid_log_file.replace(LOG_FILE_EXTENSION, JFR_FILE_EXTENSION); 100 return Paths.get(hs_err_pid_jfr_file); 101 } 102 }