1 /*
   2  * Copyright (c) 2017, 2018, 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  * @requires (vm.debug == true)
  37  * @bug 8167108
  38  * @summary Nested ThreadsListHandle info should be in error handling output.
  39  * @modules java.base/jdk.internal.misc
  40  * @library /test/lib
  41  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+EnableThreadSMRStatistics NestedThreadsListHandleInErrorHandlingTest
  42  */
  43 
  44 /*
  45  * This test was created using SafeFetchInErrorHandlingTest.java
  46  * as a guide.
  47  */
  48 public class NestedThreadsListHandleInErrorHandlingTest {
  49   public static void main(String[] args) throws Exception {
  50 
  51     // The -XX:ErrorHandlerTest=N option requires debug bits.
  52     // Need to disable ShowRegistersOnAssert: that flag causes registers to be shown, which calls os::print_location,
  53     //  which - as part of its checks - will iterate the threads list under a ThreadListHandle, changing the max nesting
  54     // counters and confusing this test.
  55     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  56         "-XX:+UnlockDiagnosticVMOptions",
  57         "-Xmx100M",
  58         "-XX:ErrorHandlerTest=17",
  59         "-XX:-CreateCoredumpOnCrash",
  60         "-XX:-ShowRegistersOnAssert",
  61         "-version");
  62 
  63     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  64 
  65     // We should have crashed with a specific fatal error:
  66     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  67     System.out.println("Found fatal error header.");
  68     output_detail.shouldMatch("# +fatal error: Force crash with a nested ThreadsListHandle.");
  69     System.out.println("Found specific fatal error.");
  70 
  71     // Extract hs_err_pid file.
  72     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  73     if (hs_err_file == null) {
  74         throw new RuntimeException("Did not find hs_err_pid file in output.\n");
  75     }
  76 
  77     File f = new File(hs_err_file);
  78     if (!f.exists()) {
  79         throw new RuntimeException("hs_err_pid file missing at "
  80                                    + f.getAbsolutePath() + ".\n");
  81     }
  82 
  83     System.out.println("Found hs_err_pid file. Scanning...");
  84 
  85     FileInputStream fis = new FileInputStream(f);
  86     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  87     String line = null;
  88 
  89     Pattern [] pattern = new Pattern[] {
  90         // The "Current thread" line should show a hazard ptr and
  91         // a nested hazard ptr:
  92         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_ptr=0x[0-9A-Fa-f][0-9A-Fa-f]*.*"),
  93         // We should have a section of Threads class SMR info:
  94         Pattern.compile("Threads class SMR info:"),
  95         // We should have one nested ThreadsListHandle:
  96         Pattern.compile(".*, _nested_thread_list_max=1"),
  97         // The current thread (marked with '=>') in the threads list
  98         // should show a hazard ptr and a nested hazard ptr:
  99         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_ptr=0x[0-9A-Fa-f][0-9A-Fa-f]*.*"),
 100     };
 101     int currentPattern = 0;
 102 
 103     String lastLine = null;
 104     while ((line = br.readLine()) != null) {
 105         if (currentPattern < pattern.length) {
 106             if (pattern[currentPattern].matcher(line).matches()) {
 107                 System.out.println("Found: " + line + ".");
 108                 currentPattern++;
 109             }
 110         }
 111         lastLine = line;
 112     }
 113     br.close();
 114 
 115     if (currentPattern < pattern.length) {
 116         throw new RuntimeException("hs_err_pid file incomplete (first missing pattern: " +  currentPattern + ")");
 117     }
 118 
 119     if (!lastLine.equals("END.")) {
 120         throw new RuntimeException("hs-err file incomplete (missing END marker.)");
 121     } else {
 122         System.out.println("End marker found.");
 123     }
 124 
 125     System.out.println("Done scanning hs_err_pid_file.");
 126     System.out.println("PASSED.");
 127   }
 128 }