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 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 jdk.jfr.consumer.RecordedEvent; 34 import jdk.jfr.consumer.RecordingFile; 35 import jdk.test.lib.Asserts; 36 import jdk.test.lib.process.OutputAnalyzer; 37 import jdk.test.lib.process.ProcessTools; 38 39 /** 40 * @test 41 * @key jfr 42 * @summary Verifies that data associated with a running recording can be evacuated to an hs_err_pidXXX.jfr when the VM crashes 43 * @requires vm.hasJFR 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) { 95 throw new RuntimeException("Did not find hs_err_pid.log file in output.\n"); 96 } 97 // the dumped out jfr file should have the same name and location but with a .jfr extension 98 final String hs_err_pid_jfr_file = hs_err_pid_log_file.replace(LOG_FILE_EXTENSION, JFR_FILE_EXTENSION); 99 return Paths.get(hs_err_pid_jfr_file); 100 } 101 }