1 /*
  2  * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2022 SAP SE. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 import jdk.test.lib.process.OutputAnalyzer;
 26 
 27 import java.io.*;
 28 import java.util.*;
 29 import java.util.regex.Pattern;
 30 
 31 public class HsErrFileUtils {
 32 
 33     /**
 34      * Given the output of a java VM that crashed, extract the name of the hs-err file from the output
 35      */
 36     public static String extractHsErrFileNameFromOutput(OutputAnalyzer output) {
 37         output.shouldMatch("# A fatal error has been detected.*");
 38 
 39         // extract hs-err file
 40         String hs_err_file = output.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1);
 41         if (hs_err_file == null) {
 42             throw new RuntimeException("Did not find hs-err file in output.\n");
 43         }
 44 
 45         return hs_err_file;
 46     }
 47 
 48     /**
 49      * Given the output of a java VM that crashed, extract the name of the hs-err file from the output,
 50      * open that file and return its File.
 51      */
 52     public static File openHsErrFileFromOutput(OutputAnalyzer output) {
 53         String name = extractHsErrFileNameFromOutput(output);
 54         File f = new File(name);
 55         if (!f.exists()) {
 56             throw new RuntimeException("Cannot find hs-err file at " + f.getAbsolutePath());
 57         }
 58         return f;
 59     }
 60 
 61     /**
 62      * Given an open hs-err file, read it line by line and check for existence of a set of patterns. Will fail
 63      * if patterns are missing, or if the END marker is missing.
 64      * @param f Input file
 65      * @param patterns An array of patterns that need to match, in that order
 66      * @param verbose If true, the content of the hs-err file is printed while matching. If false, only the matched patterns
 67      *                are printed.
 68      * @throws RuntimeException, {@link IOException}
 69      */
 70     public static void checkHsErrFileContent(File f, Pattern[] patterns, boolean verbose) throws IOException {
 71         checkHsErrFileContent(f, patterns, null, true, verbose);
 72     }
 73 
 74     /**
 75      * Given an open hs-err file, read it line by line and check for various conditions.
 76      * @param f input file
 77      * @param positivePatterns Optional array of patterns that need to appear, in given order, in the file. Missing
 78      *                        patterns cause the test to fail.
 79      * @param negativePatterns Optional array of patterns that must not appear in the file; test fails if they do.
 80      *                        Order is irrelevant.
 81      * @param checkEndMarker If true, we check for the final "END" in an hs-err file; if it is missing it indicates
 82      *                        that hs-err file printing did not complete successfully.
 83      * @param verbose If true, the content of the hs-err file is printed while matching. If false, only important
 84      *               information are printed.
 85      * @throws RuntimeException, {@link IOException}
 86      */
 87     public static void checkHsErrFileContent(File f, Pattern[] positivePatterns, Pattern[] negativePatterns, boolean checkEndMarker, boolean verbose) throws IOException {
 88         try (
 89                 FileInputStream fis = new FileInputStream(f);
 90                 BufferedReader br = new BufferedReader(new InputStreamReader(fis));
 91         ) {
 92             String line = null;
 93             String lastLine = null;
 94             int lineNo = 0;
 95 
 96             Deque<Pattern> positivePatternStack = new ArrayDeque<Pattern>();
 97             if (positivePatterns != null) {
 98                 Collections.addAll(positivePatternStack, positivePatterns);
 99             }
100             Pattern currentPositivePattern = positivePatternStack.pollFirst();
101 
102             while ((line = br.readLine()) != null) {
103                 if (verbose) {
104                     System.out.println(line);
105                 }
106                 if (currentPositivePattern != null) {
107                     if (currentPositivePattern.matcher(line).matches()) {
108                         if (!verbose) {
109                             System.out.println(line);
110                         }
111                         System.out.println("^^^ Matches " + currentPositivePattern + " at line " + lineNo + "^^^");
112                         currentPositivePattern = positivePatternStack.pollFirst();
113                         if (currentPositivePattern == null && negativePatterns == null && checkEndMarker == false) {
114                             System.out.println("Lazily skipping the rest of the hs-err file...");
115                             break; // Shortcut. Nothing to do.
116                         }
117                     }
118                 }
119                 if (negativePatterns != null) {
120                     for (Pattern negativePattern : negativePatterns) {
121                         if (negativePattern.matcher(line).matches()) {
122                             if (!verbose) {
123                                 System.out.println(line);
124                             }
125                             System.out.println("^^^ Forbidden pattern found at line " + lineNo + ": " + negativePattern + "^^^");
126                             throw new RuntimeException("Forbidden pattern found at line " + lineNo + ": " + negativePattern);
127                         }
128                     }
129                 }
130                 lastLine = line;
131                 lineNo++;
132             }
133             // If the current pattern is not null then it didn't match
134             if (currentPositivePattern != null) {
135                 throw new RuntimeException("hs-err file incomplete (first missing pattern: " + currentPositivePattern.pattern() + ")");
136             }
137             if (checkEndMarker && !lastLine.equals("END.")) {
138                 throw new RuntimeException("hs-err file incomplete (missing END marker.)");
139             }
140             System.out.println("hs-err file " + f.getAbsolutePath() + " scanned successfully.");
141         }
142     }
143 
144 }