TestDumpOnCrash.java 6.5 KB
Newer Older
A
apetushkov 已提交
1
/*
2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
A
apetushkov 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package jdk.jfr.jvm;

27 28 29 30 31
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
A
apetushkov 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import sun.misc.Unsafe;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.consumer.RecordingFile;
import jdk.test.lib.Asserts;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;

/**
 * @test
 * @key jfr
 * @summary Verifies that data associated with a running recording can be evacuated to an hs_err_pidXXX.jfr when the VM crashes
 *
 *
 * @library /lib /
 *

 *
 * @run main/othervm jdk.jfr.jvm.TestDumpOnCrash
 */
public class TestDumpOnCrash {

    private static final CharSequence LOG_FILE_EXTENSION = ".log";
    private static final CharSequence JFR_FILE_EXTENSION = ".jfr";

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    private static String readString(Path path) throws IOException {
        try (InputStream in = new FileInputStream(path.toFile())) {
            byte[] bytes = new byte[32];
            int length = in.read(bytes);
            Asserts.assertTrue(length < bytes.length, "bytes array to small");
            if (length == -1) {
                return null;
            }
            return new String(bytes, 0, length);
        }
    }

    private static void writeString(Path path, String content) throws IOException {
        try (OutputStream out = new FileOutputStream(path.toFile())) {
            out.write(content.getBytes());
        }
    }

    static class CrasherIllegalAccess {
A
apetushkov 已提交
81 82
        public static void main(String[] args) {
          try {
83 84 85
            Path pidPath = Paths.get(args[1]);
            writeString(pidPath, ProcessTools.getProcessId() + "@");

A
apetushkov 已提交
86 87 88 89 90 91 92 93 94 95
            Field theUnsafeRefLocation = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafeRefLocation.setAccessible(true);
            ((Unsafe)theUnsafeRefLocation.get(null)).putInt(0L, 0);
          }
          catch(Exception ex) {
            Asserts.fail("cannot execute");
          }
        }
    }

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    static class CrasherHalt {
        public static void main(String[] args) throws Exception {
            Path pidPath = Paths.get(args[1]);
            writeString(pidPath, ProcessTools.getProcessId() + "@");

            System.out.println("Running Runtime.getRuntime.halt");
            Runtime.getRuntime().halt(17);
        }
    }

    static class CrasherSig {
        public static void main(String[] args) throws Exception {
            long pid = Long.valueOf(ProcessTools.getProcessId());
            Path pidPath = Paths.get(args[1]);
            writeString(pidPath, pid + "@");

            String signalName = args[0];
            System.out.println("Sending SIG" + signalName + " to process " + pid);
            Runtime.getRuntime().exec("kill -" + signalName + " " + pid).waitFor();
        }
    }

A
apetushkov 已提交
118
    public static void main(String[] args) throws Exception {
119 120 121 122
        verify(runProcess(CrasherIllegalAccess.class.getName(), "", true));
        verify(runProcess(CrasherIllegalAccess.class.getName(), "", false));
        verify(runProcess(CrasherHalt.class.getName(), "", true));
        verify(runProcess(CrasherHalt.class.getName(), "", false));
123 124

        // Verification is excluded for the test case below until 8219680 is fixed
125
        long pid = runProcess(CrasherSig.class.getName(), "FPE", true);
126 127
        // @ignore 8219680
        // verify(pid);
A
apetushkov 已提交
128 129
    }

130
    private static long runProcess(String crasher, String signal, boolean disk) throws Exception {
131
        System.out.println("Test case for crasher " + crasher);
132
        final String flightRecordingOptions = "dumponexit=true,disk=" + Boolean.toString(disk);
133 134
        Path pidPath = Paths.get("pid-" + System.currentTimeMillis()).toAbsolutePath();
        Process p = ProcessTools.createJavaProcessBuilder(true,
A
apetushkov 已提交
135 136 137 138
                "-Xmx64m",
                "-XX:-TransmitErrorReport",
                "-XX:-CreateMinidumpOnCrash",
                /*"--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",*/
139
                "-XX:StartFlightRecording=" + flightRecordingOptions,
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                crasher,
                signal,
                pidPath.toString())
            .start();

        OutputAnalyzer output = new OutputAnalyzer(p);
        System.out.println("========== Crasher process output:");
        System.out.println(output.getOutput());
        System.out.println("==================================");

        String pidStr;
        do {
            pidStr = readString(pidPath);
        } while (pidStr == null || !pidStr.endsWith("@"));

        return Long.valueOf(pidStr.substring(0, pidStr.length() - 1));
A
apetushkov 已提交
156 157
    }

158 159 160
    private static void verify(long pid) throws IOException {
        String fileName = "hs_err_pid" + pid + ".jfr";
        Path file = Paths.get(fileName).toAbsolutePath().normalize();
A
apetushkov 已提交
161

162 163 164
        Asserts.assertTrue(Files.exists(file), "No emergency jfr recording file " + file + " exists");
        Asserts.assertNotEquals(Files.size(file), 0L, "File length 0. Should at least be some bytes");
        System.out.printf("File size=%d%n", Files.size(file));
A
apetushkov 已提交
165

166
        List<RecordedEvent> events = RecordingFile.readAllEvents(file);
A
apetushkov 已提交
167 168 169 170 171
        Asserts.assertFalse(events.isEmpty(), "No event found");
        System.out.printf("Found event %s%n", events.get(0).getEventType().getName());
    }
}