1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. 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 it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 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 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.BufferedReader;
  25 import java.io.File;
  26 import java.io.FileInputStream;
  27 import java.io.InputStreamReader;
  28 import java.util.regex.Pattern;
  29 
  30 import jdk.test.lib.process.OutputAnalyzer;
  31 import jdk.test.lib.Platform;
  32 import jdk.test.lib.process.ProcessTools;
  33 
  34 /*
  35  * @test
  36  * @bug 8167108
  37  * @summary Nested ThreadsListHandle info should be in error handling output.
  38  * @modules java.base/jdk.internal.misc
  39  * @library /test/lib
  40  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+EnableThreadSMRStatistics NestedThreadsListHandleInErrorHandlingTest
  41  */
  42 
  43 /*
  44  * This test was created using SafeFetchInErrorHandlingTest.java
  45  * as a guide.
  46  */
  47 public class NestedThreadsListHandleInErrorHandlingTest {
  48   public static void main(String[] args) throws Exception {
  49 
  50     if (!Platform.isDebugBuild()) {
  51         // -XX:ErrorHandlerTest=N option requires debug bits.
  52         return;
  53     }
  54 
  55     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  56         "-XX:+UnlockDiagnosticVMOptions",
  57         "-Xmx100M",
  58         "-XX:ErrorHandlerTest=17",
  59         "-XX:-CreateCoredumpOnCrash",
  60         "-version");
  61 
  62     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  63 
  64     // We should have crashed with a specific fatal error:
  65     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  66     System.out.println("Found fatal error header.");
  67     output_detail.shouldMatch("# +fatal error: Force crash with a nested ThreadsListHandle.");
  68     System.out.println("Found specific fatal error.");
  69 
  70     // Extract hs_err_pid file.
  71     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  72     if (hs_err_file == null) {
  73         throw new RuntimeException("Did not find hs_err_pid file in output.\n");
  74     }
  75 
  76     File f = new File(hs_err_file);
  77     if (!f.exists()) {
  78         throw new RuntimeException("hs_err_pid file missing at "
  79                                    + f.getAbsolutePath() + ".\n");
  80     }
  81 
  82     System.out.println("Found hs_err_pid file. Scanning...");
  83 
  84     FileInputStream fis = new FileInputStream(f);
  85     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  86     String line = null;
  87 
  88     Pattern [] pattern = new Pattern[] {
  89         // The "Current thread" line should show a hazard ptr and
  90         // a nested hazard ptr:
  91         Pattern.compile("Current thread .* _threads_hazard_ptr=0x[0-9A-Fa-f][0-9A-Fa-f]*, _nested_threads_hazard_ptr_cnt=1, _nested_threads_hazard_ptrs=0x.*"),
  92         // We should have a section of Threads class SMR info:
  93         Pattern.compile("Threads class SMR info:"),
  94         // We should have one nested ThreadsListHandle:
  95         Pattern.compile(".*, _smr_nested_thread_list_max=1"),
  96         // The current thread (marked with '=>') in the threads list
  97         // should show a hazard ptr:
  98         Pattern.compile("=>.* JavaThread \"main\" .*_threads_hazard_ptr=0x[0-9A-Fa-f][0-9A-Fa-f]*, _nested_threads_hazard_ptr_cnt=1, _nested_threads_hazard_ptrs=0x.*"),
  99     };
 100     int currentPattern = 0;
 101 
 102     String lastLine = null;
 103     while ((line = br.readLine()) != null) {
 104         if (currentPattern < pattern.length) {
 105             if (pattern[currentPattern].matcher(line).matches()) {
 106                 System.out.println("Found: " + line + ".");
 107                 currentPattern++;
 108             }
 109         }
 110         lastLine = line;
 111     }
 112     br.close();
 113 
 114     if (currentPattern < pattern.length) {
 115         throw new RuntimeException("hs_err_pid file incomplete (first missing pattern: " +  currentPattern + ")");
 116     }
 117 
 118     if (!lastLine.equals("END.")) {
 119         throw new RuntimeException("hs-err file incomplete (missing END marker.)");
 120     } else {
 121         System.out.println("End marker found.");
 122     }
 123 
 124     System.out.println("Done scanning hs_err_pid_file.");
 125     System.out.println("PASSED.");
 126   }
 127 }