1 import java.io.BufferedReader;
   2 import java.io.File;
   3 import java.io.FileInputStream;
   4 import java.io.InputStreamReader;
   5 import java.util.regex.Pattern;
   6 
   7 import com.oracle.java.testlibrary.OutputAnalyzer;
   8 import com.oracle.java.testlibrary.Platform;
   9 import com.oracle.java.testlibrary.ProcessTools;
  10 
  11 /*
  12  * @test
  13  * @bug 8065896
  14  * @summary Synchronous signals during error reporting may terminate or hang VM process
  15  * @library /testlibrary
  16  * @author Thomas Stuefe (SAP)
  17  */
  18 
  19 public class SecondaryErrorTest {
  20 
  21 
  22   public static void main(String[] args) throws Exception {
  23 
  24     // Do not execute for windows, nor for non-debug builds
  25     if (Platform.isWindows()) {
  26       return;
  27     }
  28 
  29     if (!Platform.isDebugBuild()) {
  30       return;
  31     }
  32 
  33     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  34         "-XX:+UnlockDiagnosticVMOptions",
  35         "-Xmx100M",
  36         "-XX:ErrorHandlerTest=15",
  37         "-XX:TestCrashInErrorHandler=14",
  38         "-version");
  39 
  40     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  41 
  42     // we should have crashed with a SIGFPE
  43     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  44     output_detail.shouldMatch("# +SIGFPE.*");
  45 
  46     // extract hs-err file
  47     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  48     if (hs_err_file == null) {
  49       throw new RuntimeException("Did not find hs-err file in output.\n");
  50     }
  51 
  52     // scan hs-err file: File should contain the "[error occurred during error reporting..]"
  53     // markers which show that the secondary error handling kicked in and handled the
  54     // error successfully. As an added test, we check that the last line contains "END.",
  55     // which is an end marker written in the last step and proves that hs-err file was
  56     // completely written.
  57     File f = new File(hs_err_file);
  58     if (!f.exists()) {
  59       throw new RuntimeException("hs-err file missing at "
  60           + f.getAbsolutePath() + ".\n");
  61     }
  62 
  63     System.out.println("Found hs_err file. Scanning...");
  64 
  65     FileInputStream fis = new FileInputStream(f);
  66     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  67     String line = null;
  68 
  69     Pattern [] pattern = new Pattern[] {
  70         Pattern.compile("Will crash now \\(TestCrashInErrorHandler=14\\)..."),
  71         Pattern.compile("\\[error occurred during error reporting \\(test secondary crash 1\\).*\\]"),
  72         Pattern.compile("Will crash now \\(TestCrashInErrorHandler=14\\)..."),
  73         Pattern.compile("\\[error occurred during error reporting \\(test secondary crash 2\\).*\\]"),
  74     };
  75     int currentPattern = 0;
  76 
  77     String lastLine = null;
  78     while ((line = br.readLine()) != null) {
  79       if (currentPattern < pattern.length) {
  80         if (pattern[currentPattern].matcher(line).matches()) {
  81           System.out.println("Found: " + line + ".");
  82           currentPattern ++;
  83         }
  84       }
  85       lastLine = line;
  86     }
  87     br.close();
  88 
  89     if (currentPattern < pattern.length) {
  90       throw new RuntimeException("hs-err file incomplete (first missing pattern: " +  currentPattern + ")");
  91     }
  92 
  93     if (!lastLine.equals("END.")) {
  94       throw new RuntimeException("hs-err file incomplete (missing END marker.)");
  95     } else {
  96       System.out.println("End marker found.");
  97     }
  98 
  99     System.out.println("OK.");
 100 
 101   }
 102 
 103 }
 104 
 105