8220786: Create new switch to redirect error reporting output to stdout or stderr

Reviewed-by: dholmes, goetz
This commit is contained in:
Thomas Stuefe 2019-03-25 09:35:40 +01:00
parent 4969d23b89
commit dbe0da648a
4 changed files with 144 additions and 11 deletions

View File

@ -2875,6 +2875,20 @@ jint Arguments::parse_each_vm_init_arg(const JavaVMInitArgs* args, bool* patch_m
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) {

View File

@ -1267,6 +1267,12 @@ define_pd_global(uint64_t,MaxRAM, 1ULL*G);
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") \
\

View File

@ -1445,9 +1445,13 @@ void VMError::report_and_die(int id, const char* message, const char* detail_fmt
}
}
// 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 @@ void VMError::report_and_die(int id, const char* message, const char* detail_fmt
_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);
@ -1477,7 +1487,7 @@ void VMError::report_and_die(int id, const char* message, const char* detail_fmt
_current_step = 0;
_current_step_info = "";
if (fd_log != -1) {
if (fd_log > 3) {
close(fd_log);
fd_log = -1;
}

View File

@ -0,0 +1,103 @@
/*
* Copyright (c) 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("Expected hs-err file but none found.");
} 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 mention of 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);
}
}