8301627: System.exit and Runtime.exit debug logging

Reviewed-by: alanb, chegar
This commit is contained in:
Roger Riggs 2023-02-21 15:37:08 +00:00
parent db483a38a8
commit 10b4cc9eb4
6 changed files with 160 additions and 0 deletions

View file

@ -156,6 +156,11 @@ public class Runtime {
* <p> The {@link System#exit(int) System.exit} method is the
* conventional and convenient means of invoking this method.
*
* @implNote
* If the {@linkplain System#getLogger(String) system logger} for {@code java.lang.Runtime}
* is enabled with logging level {@link System.Logger.Level#DEBUG Level.DEBUG} the stack trace
* of the call to {@code Runtime.exit()} is logged.
*
* @param status
* Termination status. By convention, a nonzero status code
* indicates abnormal termination.

View file

@ -157,16 +157,36 @@ class Shutdown {
* which should pass a nonzero status code.
*/
static void exit(int status) {
System.Logger log = getRuntimeExitLogger(); // Locate the logger without holding the lock;
synchronized (Shutdown.class) {
/* Synchronize on the class object, causing any other thread
* that attempts to initiate shutdown to stall indefinitely
*/
if (log != null) {
Throwable throwable = new Throwable("Runtime.exit(" + status + ")");
log.log(System.Logger.Level.DEBUG, "Runtime.exit() called with status: " + status,
throwable);
}
beforeHalt();
runHooks();
halt(status);
}
}
/* Locate and return the logger for Shutdown.exit, if it is functional and DEBUG enabled.
* Exceptions should not prevent System.exit; the exception is printed and otherwise ignored.
*/
private static System.Logger getRuntimeExitLogger() {
try {
System.Logger log = System.getLogger("java.lang.Runtime");
return (log.isLoggable(System.Logger.Level.DEBUG)) ? log : null;
} catch (Throwable throwable) {
// Exceptions from locating the Logger are printed but do not prevent exit
System.err.println("Runtime.exit() log finder failed with: " + throwable.getMessage());
}
return null;
}
/* Invoked by the JNI DestroyJavaVM procedure when the last non-daemon
* thread has finished. Unlike the exit method, this method does not

View file

@ -1902,6 +1902,9 @@ public final class System {
* Runtime.getRuntime().exit(n)
* </pre></blockquote>
*
* @implNote
* The initiation of the shutdown sequence is logged by {@link Runtime#exit(int)}.
*
* @param status exit status.
* @throws SecurityException
* if a security manager exists and its {@code checkExit} method