# HG changeset patch # User stuefe # Date 1553502940 -3600 # Mon Mar 25 09:35:40 2019 +0100 # Node ID a9503053fba111104bf40118983e3521cd26e5ba # Parent d25b24c70126cc55db57579f950d1f3e12b64b77 8220786: Create new switch to redirect error reporting output to stdout or stderr Reviewed-by: diff --git a/src/hotspot/share/runtime/arguments.cpp b/src/hotspot/share/runtime/arguments.cpp --- a/src/hotspot/share/runtime/arguments.cpp +++ b/src/hotspot/share/runtime/arguments.cpp @@ -2875,6 +2875,20 @@ if (FLAG_SET_CMDLINE(bool, DisplayVMOutputToStdout, true) != JVMFlag::SUCCESS) { return JNI_EINVAL; } + } else if (match_option(option, "-XX:+ErrorFileToStderr")) { + if (FLAG_SET_CMDLINE(bool, ErrorFileToStdout, false) != JVMFlag::SUCCESS) { + return JNI_EINVAL; + } + if (FLAG_SET_CMDLINE(bool, ErrorFileToStderr, true) != JVMFlag::SUCCESS) { + return JNI_EINVAL; + } + } else if (match_option(option, "-XX:+ErrorFileToStdout")) { + if (FLAG_SET_CMDLINE(bool, ErrorFileToStderr, false) != JVMFlag::SUCCESS) { + return JNI_EINVAL; + } + if (FLAG_SET_CMDLINE(bool, ErrorFileToStdout, true) != JVMFlag::SUCCESS) { + return JNI_EINVAL; + } } else if (match_option(option, "-XX:+ExtendedDTraceProbes")) { #if defined(DTRACE_ENABLED) if (FLAG_SET_CMDLINE(bool, ExtendedDTraceProbes, true) != JVMFlag::SUCCESS) { diff --git a/src/hotspot/share/runtime/globals.hpp b/src/hotspot/share/runtime/globals.hpp --- a/src/hotspot/share/runtime/globals.hpp +++ b/src/hotspot/share/runtime/globals.hpp @@ -1263,6 +1263,12 @@ product(bool, DisplayVMOutputToStdout, false, \ "If DisplayVMOutput is true, display all VM output to stdout") \ \ + product(bool, ErrorFileToStderr, false, \ + "If true, error data is printed to stderr instead of a file") \ + \ + product(bool, ErrorFileToStdout, false, \ + "If true, error data is printed to stdout instead of a file") \ + \ product(bool, UseHeavyMonitors, false, \ "use heavyweight instead of lightweight Java monitors") \ \ diff --git a/src/hotspot/share/utilities/vmError.cpp b/src/hotspot/share/utilities/vmError.cpp --- a/src/hotspot/share/utilities/vmError.cpp +++ b/src/hotspot/share/utilities/vmError.cpp @@ -1445,9 +1445,13 @@ } } - // print to screen + // Part 1: print an abbreviated version (the '#' section) to stdout. if (!out_done) { - report(&out, false); + // Suppress this output if we plan to print Part 2 to stdout too. + // No need to have the "#" section twice. + if (!(ErrorFileToStdout && out.fd() == 1)) { + report(&out, false); + } out_done = true; @@ -1455,21 +1459,27 @@ _current_step_info = ""; } + // Part 2: print a full error log file (optionally to stdout or stderr). // print to error log file if (!log_done) { // see if log file is already open if (!log.is_open()) { // open log file - fd_log = prepare_log_file(ErrorFile, "hs_err_pid%p.log", buffer, sizeof(buffer)); - if (fd_log != -1) { - out.print_raw("# An error report file with more information is saved as:\n# "); - out.print_raw_cr(buffer); - - log.set_fd(fd_log); + if (ErrorFileToStdout) { + fd_log = 1; + } else if (ErrorFileToStderr) { + fd_log = 2; } else { - out.print_raw_cr("# Can not save log file, dump to screen.."); - log.set_fd(fd_out); + fd_log = prepare_log_file(ErrorFile, "hs_err_pid%p.log", buffer, sizeof(buffer)); + if (fd_log != -1) { + out.print_raw("# An error report file with more information is saved as:\n# "); + out.print_raw_cr(buffer); + } else { + out.print_raw_cr("# Can not save log file, dump to screen.."); + fd_log = 1; + } } + log.set_fd(fd_log); } report(&log, true); diff --git a/test/hotspot/jtreg/runtime/ErrorHandling/ErrorFileRedirectTest.java b/test/hotspot/jtreg/runtime/ErrorHandling/ErrorFileRedirectTest.java new file mode 100644 --- /dev/null +++ b/test/hotspot/jtreg/runtime/ErrorHandling/ErrorFileRedirectTest.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, SAP. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + + +/* + * @test + * @bug 8220786 + * @summary Test ErrorFileToStderr and ErrorFileToStdout + * @library /test/lib + * @modules java.base/jdk.internal.misc + * @requires (vm.debug == true) + */ + +import jdk.test.lib.process.OutputAnalyzer; +import jdk.test.lib.process.ProcessTools; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.util.Map; +import java.util.regex.Pattern; + +public class ErrorFileRedirectTest { + + public static void do_test(boolean redirectStdout, boolean redirectStderr) throws Exception { + + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( + "-Xmx64M", + "-XX:-CreateCoredumpOnCrash", + "-XX:ErrorHandlerTest=14", + "-XX:" + (redirectStdout ? "+" : "-") + "ErrorFileToStdout", + "-XX:" + (redirectStderr ? "+" : "-") + "ErrorFileToStderr", + "-version"); + + OutputAnalyzer output_detail = new OutputAnalyzer(pb.start()); + + // we should have crashed with a SIGSEGV + output_detail.shouldMatch("# A fatal error has been detected by the Java Runtime Environment:.*"); + output_detail.shouldMatch("# +(?:SIGSEGV|EXCEPTION_ACCESS_VIOLATION).*"); + + // If no redirection happened, we should find a mention of the file in the output. + String hs_err_file = output_detail.firstMatch("# *(\\S*hs_err_pid\\d+\\.log)", 1); + if (redirectStdout == false && redirectStderr == false) { + if (hs_err_file == null) { + throw new RuntimeException("Expection mention of hs-err file but none fount."); + } else { + System.out.println("Found hs error file mentioned as expected: " + hs_err_file); + } + } else { + if (hs_err_file != null) { + throw new RuntimeException("Found unexpected mention of hs-err file (we did redirect the output so no file should have been written)."); + } else { + System.out.println("No mentioning an hs-err file - ok! "); + } + } + + // Check the output. Note that since stderr was specified last it has preference if both are set. + if (redirectStdout == true && redirectStderr == false) { + output_detail.stdoutShouldContain("--------------- S U M M A R Y ------------"); + output_detail.stderrShouldNotContain("--------------- S U M M A R Y ------------"); + System.out.println("Found report on stderr - ok! "); + } else if (redirectStderr == true) { + output_detail.stderrShouldContain("--------------- S U M M A R Y ------------"); + output_detail.stdoutShouldNotContain("--------------- S U M M A R Y ------------"); + System.out.println("Found report on stdout - ok! "); + } + + System.out.println("OK."); + + } + + public static void main(String[] args) throws Exception { + do_test(false, false); + do_test(false, true); + do_test(true, false); + do_test(true, true); + } + +} + +