Path: blob/master/test/hotspot/jtreg/runtime/ErrorHandling/ErrorFileOverwriteTest.java
40942 views
/*1* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2019, SAP. 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/232425/*26* @test27* @bug 822173828* @summary Test that subsequent crashes will overwrite the file given to -XX:ErrorFile (unless %a is specified29* in the error file name)30* @library /test/lib31* @modules java.base/jdk.internal.misc32* @requires (vm.debug == true)33* @run driver ErrorFileOverwriteTest34*/3536import jdk.test.lib.process.OutputAnalyzer;37import jdk.test.lib.process.ProcessTools;3839import java.io.*;40import java.util.regex.Pattern;4142public class ErrorFileOverwriteTest {4344private static File findHsErrorFileInOutput(OutputAnalyzer output) {4546String hs_err_file = output.firstMatch("# *(\\S*hs_err_pid.*\\.log)", 1);47if(hs_err_file ==null) {48throw new RuntimeException("Did not find hs-err file in output.\n");49}5051File f = new File(hs_err_file);52if (!f.exists()) {53throw new RuntimeException("hs-err file missing at "54+ f.getAbsolutePath() + ".\n");55}5657return f;5859}6061private static void scanHsErrorFileForContent(File f, Pattern[] pattern) throws IOException {62FileInputStream fis = new FileInputStream(f);63BufferedReader br = new BufferedReader(new InputStreamReader(fis));64String line = null;6566int currentPattern = 0;6768String lastLine = null;69while ((line = br.readLine()) != null && currentPattern < pattern.length) {70if (pattern[currentPattern].matcher(line).matches()) {71System.out.println("Found: " + line + ".");72currentPattern++;73}74lastLine = line;75}76br.close();7778if (currentPattern < pattern.length) {79throw new RuntimeException("hs-err file incomplete (first missing pattern: " + pattern[currentPattern] + ")");80}8182}8384public static void do_test(boolean with_percent_p) throws Exception {8586// Crash twice.87//88// Second crash should, given an error file Without %p,89// overwrite the first file. With %p it should not.9091String errorFileStem = "hs_err_pid_test";92String errorFileName = errorFileStem + (with_percent_p ? "%p" : "") + ".log";9394System.out.println("Testing with error file name " + errorFileName + "...");9596System.out.println("First crash...");9798ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(99"-Xmx64M",100"-XX:-CreateCoredumpOnCrash",101"-XX:ErrorHandlerTest=1",102"-XX:ErrorFile=" + errorFileName,103"-version");104105OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());106107output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");108output_detail.shouldMatch("# An error report file with more information is saved as:.*");109output_detail.shouldMatch("# " + errorFileStem + ".*");110System.out.println("First crash: Found expected output on tty. Ok.");111112File f = findHsErrorFileInOutput(output_detail);113System.out.println("First crash: Found hs error file at " + f.getAbsolutePath());114115scanHsErrorFileForContent(f, new Pattern[] {116Pattern.compile("# *Internal Error.*"),117Pattern.compile("Command Line:.*-XX:ErrorHandlerTest=1.*-XX:ErrorFile=" + errorFileStem + ".*")118});119System.out.println("First crash: hs error content as expected. Ok.");120121122System.out.println("Second crash...");123124pb = ProcessTools.createJavaProcessBuilder(125"-Xmx64M",126"-XX:-CreateCoredumpOnCrash",127"-XX:ErrorHandlerTest=2", // << now 2128"-XX:ErrorFile=" + errorFileName,129"-version");130131output_detail = new OutputAnalyzer(pb.start());132133output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");134output_detail.shouldMatch("# An error report file with more information is saved as:.*");135output_detail.shouldMatch("# " + errorFileStem + ".*");136System.out.println("Second crash: Found expected output on tty. Ok.");137138File f2 = findHsErrorFileInOutput(output_detail);139System.out.println("Second crash: Found hs error file at " + f2.getAbsolutePath());140141if (with_percent_p) {142if (f2.getAbsolutePath() == f.getAbsolutePath()) {143throw new RuntimeException("Unexpected overwriting of error file");144}145}146147scanHsErrorFileForContent(f2, new Pattern[] {148Pattern.compile("# *Internal Error.*"),149Pattern.compile("Command Line:.*-XX:ErrorHandlerTest=2.*-XX:ErrorFile=" + errorFileStem + ".*")150});151System.out.println("Second crash: hs error content as expected. Ok.");152153}154155public static void main(String[] args) throws Exception {156do_test(false);157do_test(true);158}159160}161162163164165