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.lang.reflect.Field; 28 import java.nio.file.Files; 29 import java.nio.file.Path; 30 import java.nio.file.Paths; 31 import java.util.List; 32 33 import sun.misc.Unsafe; 34 import jdk.jfr.consumer.RecordedEvent; 35 import jdk.jfr.consumer.RecordingFile; 36 import jdk.test.lib.Asserts; 37 import jdk.test.lib.process.OutputAnalyzer; 38 import jdk.test.lib.process.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 * 46 * @library /lib / 47 * 48 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 try { 60 Field theUnsafeRefLocation = Unsafe.class.getDeclaredField("theUnsafe"); 61 theUnsafeRefLocation.setAccessible(true); 62 ((Unsafe)theUnsafeRefLocation.get(null)).putInt(0L, 0); 63 } 64 catch(Exception ex) { 65 Asserts.fail("cannot execute"); 66 } 67 } 68 } 69 70 public static void main(String[] args) throws Exception { 71 processOutput(runProcess()); 72 } 73 74 private static OutputAnalyzer runProcess() throws Exception { 75 return new OutputAnalyzer( 76 ProcessTools.createJavaProcessBuilder(true, 77 "-Xmx64m", 78 "-Xint", 79 "-XX:-TransmitErrorReport", 80 "-XX:-CreateMinidumpOnCrash", 81 /*"--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",*/ 82 "-XX:StartFlightRecording=dumponexit=true", 83 Crasher.class.getName()).start()); 84 } 85 86 private static void processOutput(OutputAnalyzer output) throws Exception { 87 //output.shouldContain("CreateCoredumpOnCrash turned off, no core file dumped"); 88 89 final Path jfrEmergencyFilePath = getHsErrJfrPath(output); 90 Asserts.assertTrue(Files.exists(jfrEmergencyFilePath), "No emergency jfr recording file " + jfrEmergencyFilePath + " exists"); 91 Asserts.assertNotEquals(Files.size(jfrEmergencyFilePath), 0L, "File length 0. Should at least be some bytes"); 92 System.out.printf("File size=%d%n", Files.size(jfrEmergencyFilePath)); 93 94 List<RecordedEvent> events = RecordingFile.readAllEvents(jfrEmergencyFilePath); 95 Asserts.assertFalse(events.isEmpty(), "No event found"); 96 System.out.printf("Found event %s%n", events.get(0).getEventType().getName()); 97 } 98 99 private static Path getHsErrJfrPath(OutputAnalyzer output) throws Exception { 100 // extract to find hs-err_pid log file location 101 final String hs_err_pid_log_file = output.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1); 102 if (hs_err_pid_log_file == null) { 103 throw new RuntimeException("Did not find hs_err_pid.log file in output.\n"); 104 } 105 // the dumped out jfr file should have the same name and location but with a .jfr extension 106 final String hs_err_pid_jfr_file = hs_err_pid_log_file.replace(LOG_FILE_EXTENSION, JFR_FILE_EXTENSION); 107 return Paths.get(hs_err_pid_jfr_file); 108 } 109 } 110