1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2019, SAP. 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 26 /* 27 * @test 28 * @bug 8220786 29 * @summary Test ErrorFileToStderr and ErrorFileToStdout 30 * @library /test/lib 31 * @modules java.base/jdk.internal.misc 32 * @requires (vm.debug == true) 33 * @run driver ErrorFileRedirectTest 34 */ 35 36 import jdk.test.lib.process.OutputAnalyzer; 37 import jdk.test.lib.process.ProcessTools; 38 39 import java.io.BufferedReader; 40 import java.io.File; 41 import java.io.FileInputStream; 42 import java.io.InputStreamReader; 43 import java.util.Map; 44 import java.util.regex.Pattern; 45 46 public class ErrorFileRedirectTest { 47 48 public static void do_test(boolean redirectStdout, boolean redirectStderr) throws Exception { 49 50 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 51 "-Xmx64M", 52 "-XX:-CreateCoredumpOnCrash", 53 "-XX:ErrorHandlerTest=14", 54 "-XX:" + (redirectStdout ? "+" : "-") + "ErrorFileToStdout", 55 "-XX:" + (redirectStderr ? "+" : "-") + "ErrorFileToStderr", 56 "-version"); 57 58 OutputAnalyzer output_detail = new OutputAnalyzer(pb.start()); 59 60 // we should have crashed with a SIGSEGV 61 output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*"); 62 output_detail.shouldMatch("# +(?:SIGSEGV|SIGBUS|EXCEPTION_ACCESS_VIOLATION).*"); 63 64 // If no redirection happened, we should find a mention of the file in the output. 65 String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1); 66 if (redirectStdout == false && redirectStderr == false) { 67 if (hs_err_file == null) { 68 throw new RuntimeException("Expected hs-err file but none found."); 69 } else { 70 System.out.println("Found hs error file mentioned as expected: " + hs_err_file); 71 } 72 } else { 73 if (hs_err_file != null) { 74 throw new RuntimeException("Found unexpected mention of hs-err file (we did redirect the output so no file should have been written)."); 75 } else { 76 System.out.println("No mention of an hs-err file - ok! "); 77 } 78 } 79 80 // Check the output. Note that since stderr was specified last it has preference if both are set. 81 if (redirectStdout == true && redirectStderr == false) { 82 output_detail.stdoutShouldContain("--------------- S U M M A R Y ------------"); 83 output_detail.stderrShouldNotContain("--------------- S U M M A R Y ------------"); 84 System.out.println("Found report on stderr - ok! "); 85 } else if (redirectStderr == true) { 86 output_detail.stderrShouldContain("--------------- S U M M A R Y ------------"); 87 output_detail.stdoutShouldNotContain("--------------- S U M M A R Y ------------"); 88 System.out.println("Found report on stdout - ok! "); 89 } 90 91 System.out.println("OK."); 92 93 } 94 95 public static void main(String[] args) throws Exception { 96 do_test(false, false); 97 do_test(false, true); 98 do_test(true, false); 99 do_test(true, true); 100 } 101 102 } 103 104