Skip to content

Commit 10b4cc9

Browse files
author
Roger Riggs
committed
8301627: System.exit and Runtime.exit debug logging
Reviewed-by: alanb, chegar
1 parent db483a3 commit 10b4cc9

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

src/java.base/share/classes/java/lang/Runtime.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ private Runtime() {}
156156
* <p> The {@link System#exit(int) System.exit} method is the
157157
* conventional and convenient means of invoking this method.
158158
*
159+
* @implNote
160+
* If the {@linkplain System#getLogger(String) system logger} for {@code java.lang.Runtime}
161+
* is enabled with logging level {@link System.Logger.Level#DEBUG Level.DEBUG} the stack trace
162+
* of the call to {@code Runtime.exit()} is logged.
163+
*
159164
* @param status
160165
* Termination status. By convention, a nonzero status code
161166
* indicates abnormal termination.

src/java.base/share/classes/java/lang/Shutdown.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,36 @@ static void halt(int status) {
157157
* which should pass a nonzero status code.
158158
*/
159159
static void exit(int status) {
160+
System.Logger log = getRuntimeExitLogger(); // Locate the logger without holding the lock;
160161
synchronized (Shutdown.class) {
161162
/* Synchronize on the class object, causing any other thread
162163
* that attempts to initiate shutdown to stall indefinitely
163164
*/
165+
if (log != null) {
166+
Throwable throwable = new Throwable("Runtime.exit(" + status + ")");
167+
log.log(System.Logger.Level.DEBUG, "Runtime.exit() called with status: " + status,
168+
throwable);
169+
}
164170
beforeHalt();
165171
runHooks();
166172
halt(status);
167173
}
168174
}
169175

176+
/* Locate and return the logger for Shutdown.exit, if it is functional and DEBUG enabled.
177+
* Exceptions should not prevent System.exit; the exception is printed and otherwise ignored.
178+
*/
179+
private static System.Logger getRuntimeExitLogger() {
180+
try {
181+
System.Logger log = System.getLogger("java.lang.Runtime");
182+
return (log.isLoggable(System.Logger.Level.DEBUG)) ? log : null;
183+
} catch (Throwable throwable) {
184+
// Exceptions from locating the Logger are printed but do not prevent exit
185+
System.err.println("Runtime.exit() log finder failed with: " + throwable.getMessage());
186+
}
187+
return null;
188+
}
189+
170190

171191
/* Invoked by the JNI DestroyJavaVM procedure when the last non-daemon
172192
* thread has finished. Unlike the exit method, this method does not

src/java.base/share/classes/java/lang/System.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,9 @@ public static Logger getLogger(String name, ResourceBundle bundle) {
19021902
* Runtime.getRuntime().exit(n)
19031903
* </pre></blockquote>
19041904
*
1905+
* @implNote
1906+
* The initiation of the shutdown sequence is logged by {@link Runtime#exit(int)}.
1907+
*
19051908
* @param status exit status.
19061909
* @throws SecurityException
19071910
* if a security manager exists and its {@code checkExit} method
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
############################################################
2+
# Enable logging java.lang.Runtime to the console
3+
############################################################
4+
5+
handlers= java.util.logging.ConsoleHandler
6+
7+
java.util.logging.ConsoleHandler.level = ALL
8+
java.lang.Runtime.level = FINE
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
############################################################
2+
# Enable logging java.lang.Runtime to the console
3+
############################################################
4+
5+
handlers= java.util.logging.ConsoleHandler
6+
7+
java.util.logging.ConsoleHandler.level = ALL
8+
java.lang.Runtime.level = INFO
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2023, 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+
import java.io.BufferedReader;
26+
import java.io.IOException;
27+
import java.nio.file.Path;
28+
import java.util.List;
29+
import java.util.Optional;
30+
import java.util.stream.Stream;
31+
32+
33+
import org.junit.jupiter.params.provider.Arguments;
34+
import org.junit.jupiter.params.provider.MethodSource;
35+
import org.junit.jupiter.params.ParameterizedTest;
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.junit.jupiter.api.Assertions.fail;
38+
39+
/*
40+
* @test
41+
* @summary verify logging of call to System.exit or Runtime.exit.
42+
* @run junit/othervm RuntimeExitLogTest
43+
*/
44+
45+
public class RuntimeExitLogTest {
46+
47+
private static final String TEST_JDK = System.getProperty("test.jdk");
48+
private static final String TEST_SRC = System.getProperty("test.src");
49+
50+
/**
51+
* Call System.exit() with the parameter (or zero if not supplied).
52+
* @param args zero or 1 argument, an exit status
53+
*/
54+
public static void main(String[] args) throws InterruptedException {
55+
int status = args.length > 0 ? Integer.parseInt(args[0]) : 0;
56+
System.exit(status);
57+
}
58+
59+
/**
60+
* Test various log level settings, and none.
61+
* @return a stream of arguments for parameterized test
62+
*/
63+
private static Stream<Arguments> logParamProvider() {
64+
return Stream.of(
65+
// Logging enabled with level DEBUG
66+
Arguments.of(List.of("-Djava.util.logging.config.file=" +
67+
Path.of(TEST_SRC, "ExitLogging-FINE.properties").toString()), 1, true),
68+
// Logging disabled due to level
69+
Arguments.of(List.of("-Djava.util.logging.config.file=" +
70+
Path.of(TEST_SRC, "ExitLogging-INFO.properties").toString()), 2, false),
71+
// Console logger
72+
Arguments.of(List.of("--limit-modules", "java.base",
73+
"-Djdk.system.logger.level=DEBUG"), 3, true),
74+
// Console logger
75+
Arguments.of(List.of(), 4, false)
76+
);
77+
}
78+
79+
/**
80+
* Check that the logger output of a launched process contains the expected message.
81+
* @param logProps The name of the log.properties file to set on the command line
82+
* @param status the expected exit status of the process
83+
* @param shouldLog true if the log should contain the message expected from Runtime.exit(status)
84+
*/
85+
@ParameterizedTest
86+
@MethodSource("logParamProvider")
87+
public void checkLogger(List<String> logProps, int status, boolean shouldLog) {
88+
ProcessBuilder pb = new ProcessBuilder();
89+
pb.redirectErrorStream(true);
90+
91+
List<String> cmd = pb.command();
92+
cmd.add(Path.of(TEST_JDK,"bin", "java").toString());
93+
cmd.addAll(logProps);
94+
cmd.add(this.getClass().getName());
95+
cmd.add(Integer.toString(status));
96+
97+
try {
98+
Process process = pb.start();
99+
try (BufferedReader reader = process.inputReader()) {
100+
List<String> lines = reader.lines().toList();
101+
final String expected = "Runtime.exit() called with status: " + status;
102+
Optional<String> found = lines.stream().filter(s -> s.contains(expected)).findFirst();
103+
if (found.isPresent() != shouldLog) {
104+
System.err.println("---- Process output begin");
105+
lines.forEach(l -> System.err.println(l));
106+
System.err.println("---- Process output end");
107+
fail("Unexpected log contents");
108+
}
109+
}
110+
int result = process.waitFor();
111+
assertEquals(status, result, "Exit status");
112+
} catch (IOException | InterruptedException ex) {
113+
fail(ex);
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)