1 /*
   2 * Copyright (c) 2013, 2016, 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 
  25 /*
  26  * @test
  27  * @bug 8185712
  28  * @summary Callstacks shall be printed correctly after a sigsegv.
  29  * @library /test/lib
  30  * @requires (vm.debug == true) & (os.family == "windows")
  31  * @author Thomas Stuefe (SAP)
  32  * @modules java.base/jdk.internal.misc
  33  *          java.management
  34  */
  35 
  36 import java.io.BufferedReader;
  37 import java.io.File;
  38 import java.io.FileInputStream;
  39 import java.io.InputStreamReader;
  40 import java.util.regex.Pattern;
  41 
  42 import jdk.test.lib.process.OutputAnalyzer;
  43 import jdk.test.lib.Platform;
  44 import jdk.test.lib.process.ProcessTools;
  45 
  46 public class CallstackAfterSegfault {
  47 
  48   // Whether callstack should show the name of the binary (shared lib/executable)
  49   final static boolean with_binary_name = true;
  50   // Whether callstack should show File information (source file, line)
  51   final static boolean with_source_file_info = Platform.isWindows();
  52 
  53   private static String buildFramePattern(String library, String functionName, String sourcefile) {
  54     StringBuilder bld = new StringBuilder();
  55     bld.append(".*");
  56     if (with_binary_name) {
  57         // We only append the raw library name without extensions or prefix, to match on all platforms.
  58         bld.append(library);
  59         bld.append(".*");
  60     }
  61     bld.append(functionName);
  62     bld.append(".*");
  63     if (with_source_file_info) {
  64         bld.append(sourcefile);
  65         bld.append(":\\d+");
  66         bld.append(".*");
  67     }
  68     return bld.toString();
  69   }
  70 
  71   public static void main(String[] args) throws Exception {
  72 
  73     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  74         "-XX:+UnlockDiagnosticVMOptions",
  75         "-Xmx32M",
  76         "-XX:-CreateCoredumpOnCrash",
  77         "-XX:ErrorHandlerTest=14",
  78         "-version");
  79 
  80     OutputAnalyzer output_detail = new OutputAnalyzer(pb.start());
  81 
  82     // we should have crashed with a SIGFPE
  83     output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*");
  84     output_detail.shouldMatch("# +EXCEPTION_ACCESS_VIOLATION.*");
  85 
  86     // extract hs-err file
  87     String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
  88     if (hs_err_file == null) {
  89       throw new RuntimeException("Did not find hs-err file in output.\n");
  90     }
  91 
  92     // scan hs-err file: File should contain the "[error occurred during error reporting..]"
  93     // markers which show that the secondary error handling kicked in and handled the
  94     // error successfully. As an added test, we check that the last line contains "END.",
  95     // which is an end marker written in the last step and proves that hs-err file was
  96     // completely written.
  97     File f = new File(hs_err_file);
  98     if (!f.exists()) {
  99       throw new RuntimeException("hs-err file missing at "
 100           + f.getAbsolutePath() + ".\n");
 101     }
 102 
 103     System.out.println("Found hs_err file at " +  f.getAbsolutePath() + ". Scanning...");
 104 
 105     FileInputStream fis = new FileInputStream(f);
 106     BufferedReader br = new BufferedReader(new InputStreamReader(fis));
 107     String line = null;
 108 
 109     Pattern [] pattern = new Pattern[] {
 110         /* Note: some platforms (e.g. AIX) have demangling off by default. Formulate the pattern such
 111          * that function names match for both mangled and unmangled case. */
 112         Pattern.compile(buildFramePattern("jvm", "crash_with_segfault", "vmerror.cpp")),
 113         Pattern.compile(buildFramePattern("jvm", "controlled_crash", "vmerror.cpp")),
 114         Pattern.compile(buildFramePattern("jvm", "JNI_CreateJavaVM", "jni.cpp"))
 115     };
 116     int currentPattern = 0;
 117 
 118     String lastLine = null;
 119     while ((line = br.readLine()) != null) {
 120       if (currentPattern < pattern.length) {
 121         if (pattern[currentPattern].matcher(line).matches()) {
 122           System.out.println("Found: " + line + ".");
 123           currentPattern ++;
 124         }
 125       }
 126       lastLine = line;
 127     }
 128     br.close();
 129 
 130     if (currentPattern < pattern.length) {
 131       throw new RuntimeException("Missing or incomplete callstack in hs-err file (first missing pattern: " + pattern[currentPattern] + ")");
 132     }
 133 
 134     System.out.println("OK.");
 135 
 136   }
 137 
 138 }
 139 
 140