1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2019, SAP. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 26 /* 27 * @test 28 * @bug 8221738 29 * @summary Test that subsequent crashes will overwrite the file given to -XX:ErrorFile (unless %a is specified 30 * in the error file name) 31 * @library /test/lib 32 * @modules java.base/jdk.internal.misc 33 * @requires (vm.debug == true) 34 */ 35 36 import jdk.test.lib.process.OutputAnalyzer; 37 import jdk.test.lib.process.ProcessTools; 38 39 import java.io.*; 40 import java.util.regex.Pattern; 41 42 public class ErrorFileOverwriteTest { 43 44 private static File findHsErrorFileInOutput(OutputAnalyzer output) { 45 46 String hs_err_file = output.firstMatch("# *(\\S*hs_err_pid.*\\.log)", 1); 47 if(hs_err_file ==null) { 48 throw new RuntimeException("Did not find hs-err file in output.\n"); 49 } 50 51 File f = new File(hs_err_file); 52 if (!f.exists()) { 53 throw new RuntimeException("hs-err file missing at " 54 + f.getAbsolutePath() + ".\n"); 55 } 56 57 return f; 58 59 } 60 61 private static void scanHsErrorFileForContent(File f, Pattern[] pattern) throws IOException { 62 FileInputStream fis = new FileInputStream(f); 63 BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 64 String line = null; 65 66 int currentPattern = 0; 67 68 String lastLine = null; 69 while ((line = br.readLine()) != null && currentPattern < pattern.length) { 70 if (pattern[currentPattern].matcher(line).matches()) { 71 System.out.println("Found: " + line + "."); 72 currentPattern++; 73 } 74 lastLine = line; 75 } 76 br.close(); 77 78 if (currentPattern < pattern.length) { 79 throw new RuntimeException("hs-err file incomplete (first missing pattern: " + pattern[currentPattern] + ")"); 80 } 81 82 } 83 84 public static void do_test(boolean with_percent_p) throws Exception { 85 86 // Crash twice. 87 // 88 // Second crash should, given an error file Without %p, 89 // overwrite the first file. With %p it should not. 90 91 String errorFileStem = "hs_err_pid_test"; 92 String errorFileName = errorFileStem + (with_percent_p ? "%p" : "") + ".log"; 93 94 System.out.println("Testing with error file name " + errorFileName + "..."); 95 96 System.out.println("First crash..."); 97 98 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 99 "-Xmx64M", 100 "-XX:-CreateCoredumpOnCrash", 101 "-XX:ErrorHandlerTest=1", 102 "-XX:ErrorFile=" + errorFileName, 103 "-version"); 104 105 OutputAnalyzer output_detail = new OutputAnalyzer(pb.start()); 106 107 output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*"); 108 output_detail.shouldMatch("# An error report file with more information is saved as:.*"); 109 output_detail.shouldMatch("# " + errorFileStem + ".*"); 110 System.out.println("First crash: Found expected output on tty. Ok."); 111 112 File f = findHsErrorFileInOutput(output_detail); 113 System.out.println("First crash: Found hs error file at " + f.getAbsolutePath()); 114 115 scanHsErrorFileForContent(f, new Pattern[] { 116 Pattern.compile("# *Internal Error.*"), 117 Pattern.compile("Command Line:.*-XX:ErrorHandlerTest=1.*-XX:ErrorFile=" + errorFileStem + ".*") 118 }); 119 System.out.println("First crash: hs error content as expected. Ok."); 120 121 122 System.out.println("Second crash..."); 123 124 pb = ProcessTools.createJavaProcessBuilder( 125 "-Xmx64M", 126 "-XX:-CreateCoredumpOnCrash", 127 "-XX:ErrorHandlerTest=2", // << now 2 128 "-XX:ErrorFile=" + errorFileName, 129 "-version"); 130 131 output_detail = new OutputAnalyzer(pb.start()); 132 133 output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*"); 134 output_detail.shouldMatch("# An error report file with more information is saved as:.*"); 135 output_detail.shouldMatch("# " + errorFileStem + ".*"); 136 System.out.println("Second crash: Found expected output on tty. Ok."); 137 138 File f2 = findHsErrorFileInOutput(output_detail); 139 System.out.println("Second crash: Found hs error file at " + f2.getAbsolutePath()); 140 141 if (with_percent_p) { 142 if (f2.getAbsolutePath() == f.getAbsolutePath()) { 143 throw new RuntimeException("Unexpected overwriting of error file"); 144 } 145 } 146 147 scanHsErrorFileForContent(f2, new Pattern[] { 148 Pattern.compile("# *Internal Error.*"), 149 Pattern.compile("Command Line:.*-XX:ErrorHandlerTest=2.*-XX:ErrorFile=" + errorFileStem + ".*") 150 }); 151 System.out.println("Second crash: hs error content as expected. Ok."); 152 153 } 154 155 public static void main(String[] args) throws Exception { 156 do_test(false); 157 do_test(true); 158 } 159 160 } 161 162