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  * @run driver ErrorFileOverwriteTest
  35  */
  36 
  37 import jdk.test.lib.process.OutputAnalyzer;
  38 import jdk.test.lib.process.ProcessTools;
  39 
  40 import java.io.*;
  41 import java.util.regex.Pattern;
  42 
  43 public class ErrorFileOverwriteTest {
  44 
  45   private static File findHsErrorFileInOutput(OutputAnalyzer output) {
  46 
  47     String hs_err_file = output.firstMatch("# *(\\S*hs_err_pid.*\\.log)", 1);
  48     if(hs_err_file ==null) {
  49       throw new RuntimeException("Did not find hs-err file in output.\n");
  50     }
  51 
  52     File f = new File(hs_err_file);
  53     if (!f.exists()) {
  54       throw new RuntimeException("hs-err file missing at "
  55               + f.getAbsolutePath() + ".\n");
  56     }
  57 
  58     return f;
  59 
  60   }
  61 
  62   private static void scanHsErrorFileForContent(File f, Pattern[] pattern) throws IOException {
  63     FileInputStream fis = new FileInputStream(f);
  64     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  65     String line = null;
  66 
  67     int currentPattern = 0;
  68 
  69     String lastLine = null;
  70     while ((line = br.readLine()) != null && currentPattern < pattern.length) {
  71       if (pattern[currentPattern].matcher(line).matches()) {
  72         System.out.println("Found: " + line + ".");
  73         currentPattern++;
  74       }
  75       lastLine = line;
  76     }
  77     br.close();
  78 
  79     if (currentPattern < pattern.length) {
  80       throw new RuntimeException("hs-err file incomplete (first missing pattern: " +  pattern[currentPattern] + ")");
  81     }
  82 
  83   }
  84 
  85   public static void do_test(boolean with_percent_p) throws Exception {
  86 
  87     // Crash twice.
  88     //
  89     // Second crash should, given an error file Without %p,
  90     // overwrite the first file. With %p it should not.
  91 
  92     String errorFileStem = "hs_err_pid_test";
  93     String errorFileName = errorFileStem + (with_percent_p ? "%p" : "") + ".log";
  94 
  95     System.out.println("Testing with error file name " + errorFileName + "...");
  96 
  97     System.out.println("First crash...");
  98 
  99     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 100             "-Xmx64M",
 101             "-XX:-CreateCoredumpOnCrash",
 102             "-XX:ErrorHandlerTest=1",
 103             "-XX:ErrorFile=" + errorFileName,
 104             "-version");
 105 
 106     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
 107 
 108     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
 109     output_detail.shouldMatch("# An error report file with more information is saved as:.*");
 110     output_detail.shouldMatch("# " + errorFileStem + ".*");
 111     System.out.println("First crash: Found expected output on tty. Ok.");
 112 
 113     File f = findHsErrorFileInOutput(output_detail);
 114     System.out.println("First crash: Found hs error file at " + f.getAbsolutePath());
 115 
 116     scanHsErrorFileForContent(f, new Pattern[] {
 117             Pattern.compile("# *Internal Error.*"),
 118             Pattern.compile("Command Line:.*-XX:ErrorHandlerTest=1.*-XX:ErrorFile=" + errorFileStem + ".*")
 119     });
 120     System.out.println("First crash: hs error content as expected. Ok.");
 121 
 122 
 123     System.out.println("Second crash...");
 124 
 125     pb = ProcessTools.createJavaProcessBuilder(
 126             "-Xmx64M",
 127             "-XX:-CreateCoredumpOnCrash",
 128             "-XX:ErrorHandlerTest=2", // << now 2
 129             "-XX:ErrorFile=" + errorFileName,
 130             "-version");
 131 
 132     output_detail = new OutputAnalyzer(pb.start());
 133 
 134     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
 135     output_detail.shouldMatch("# An error report file with more information is saved as:.*");
 136     output_detail.shouldMatch("# " + errorFileStem + ".*");
 137     System.out.println("Second crash: Found expected output on tty. Ok.");
 138 
 139     File f2 = findHsErrorFileInOutput(output_detail);
 140     System.out.println("Second crash: Found hs error file at " + f2.getAbsolutePath());
 141 
 142     if (with_percent_p) {
 143       if (f2.getAbsolutePath() == f.getAbsolutePath()) {
 144         throw new RuntimeException("Unexpected overwriting of error file");
 145       }
 146     }
 147 
 148     scanHsErrorFileForContent(f2, new Pattern[] {
 149             Pattern.compile("# *Internal Error.*"),
 150             Pattern.compile("Command Line:.*-XX:ErrorHandlerTest=2.*-XX:ErrorFile=" + errorFileStem + ".*")
 151     });
 152     System.out.println("Second crash: hs error content as expected. Ok.");
 153 
 154   }
 155 
 156   public static void main(String[] args) throws Exception {
 157     do_test(false);
 158     do_test(true);
 159   }
 160 
 161 }
 162 
 163