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:-CreateCoredumpOnCrash",
  37         "-XX:ErrorHandlerTest=15",
  38         "-XX:TestCrashInErrorHandler=14",
  39         "-version");
  40 
  41     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  42 
  43     // we should have crashed with a SIGFPE
  44     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  45     output_detail.shouldMatch("# +SIGFPE.*");
  46 
  47     // extract hs-err file
  48     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  49     if (hs_err_file == null) {
  50       throw new RuntimeException("Did not find hs-err file in output.\n");
  51     }
  52 
  53     // scan hs-err file: File should contain the "[error occurred during error reporting..]"
  54     // markers which show that the secondary error handling kicked in and handled the
  55     // error successfully. As an added test, we check that the last line contains "END.",
  56     // which is an end marker written in the last step and proves that hs-err file was
  57     // completely written.
  58     File f = new File(hs_err_file);
  59     if (!f.exists()) {
  60       throw new RuntimeException("hs-err file missing at "
  61           + f.getAbsolutePath() + ".\n");
  62     }
  63 
  64     System.out.println("Found hs_err file. Scanning...");
  65 
  66     FileInputStream fis = new FileInputStream(f);
  67     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  68     String line = null;
  69 
  70     Pattern [] pattern = new Pattern[] {
  71         Pattern.compile("Will crash now \\(TestCrashInErrorHandler=14\\)..."),
  72         Pattern.compile("\\[error occurred during error reporting \\(test secondary crash 1\\).*\\]"),
  73         Pattern.compile("Will crash now \\(TestCrashInErrorHandler=14\\)..."),
  74         Pattern.compile("\\[error occurred during error reporting \\(test secondary crash 2\\).*\\]"),
  75     };
  76     int currentPattern = 0;
  77 
  78     String lastLine = null;
  79     while ((line = br.readLine()) != null) {
  80       if (currentPattern < pattern.length) {
  81         if (pattern[currentPattern].matcher(line).matches()) {
  82           System.out.println("Found: " + line + ".");
  83           currentPattern ++;
  84         }
  85       }
  86       lastLine = line;
  87     }
  88     br.close();
  89 
  90     if (currentPattern < pattern.length) {
  91       throw new RuntimeException("hs-err file incomplete (first missing pattern: " +  currentPattern + ")");
  92     }
  93 
  94     if (!lastLine.equals("END.")) {
  95       throw new RuntimeException("hs-err file incomplete (missing END marker.)");
  96     } else {
  97       System.out.println("End marker found.");
  98     }
  99 
 100     System.out.println("OK.");
 101 
 102   }
 103 
 104 }
 105 
 106