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 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(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) {
  97             throw new RuntimeException("Did not find hs_err_pid.log file in output.\n");
  98         }
  99         // the dumped out jfr file should have the same name and location but with a .jfr extension
 100         final String hs_err_pid_jfr_file = hs_err_pid_log_file.replace(LOG_FILE_EXTENSION, JFR_FILE_EXTENSION);
 101         return Paths.get(hs_err_pid_jfr_file);
 102     }
 103 }