Skip to content

Commit 42f48a3

Browse files
committed
8350689: Turn on timestamp and thread metadata by default for java.security.debug
Reviewed-by: mullan
1 parent cd16b68 commit 42f48a3

File tree

5 files changed

+54
-169
lines changed

5 files changed

+54
-169
lines changed

src/java.base/share/classes/java/security/doc-files/debug-system-property.html

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,10 @@ <h1><B>{@systemProperty java.security.debug}</B></h1>
5252
<p> To monitor security access, you can set the <code>java.security.debug</code>
5353
system property, which determines what trace messages are printed during
5454
execution. The value of the property is one or more options separated by a
55-
comma.
55+
comma. Each trace message includes the thread id, caller information, and
56+
timestamp.
5657
</p>
5758

58-
<h2>Printing Thread and Timestamp Information</h2>
59-
<p>
60-
You can append the following strings to any option specified in the
61-
<code>java.security.debug</code> system property to print additional
62-
information:
63-
<ul>
64-
<li><code>+thread</code>: Print thread and caller information</li>
65-
<li><code>+timestamp</code>: Print timestamp information</li>
66-
</ul>
67-
<p>
68-
For example, to add thread, caller, and timestamp information to all
69-
debugging output, set the <code>java.security.debug</code> system property
70-
on the command line as follows:
71-
<pre><code>java -Djava.security.debug=all+thread+timestamp MyApp</code></pre>
72-
7359
<p>The following table lists the <code>java.security.debug</code> options:</p>
7460

7561
<table id="debug">

src/java.base/share/classes/sun/security/util/Debug.java

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,7 @@
4141
public class Debug {
4242

4343
private String prefix;
44-
private boolean printDateTime;
45-
private boolean printThreadDetails;
46-
4744
private static String args;
48-
private static boolean threadInfoAll;
49-
private static boolean timeStampInfoAll;
50-
private static final String TIMESTAMP_OPTION = "+timestamp";
51-
private static final String THREAD_OPTION = "+thread";
5245

5346
static {
5447
args = System.getProperty("java.security.debug");
@@ -66,16 +59,6 @@ public class Debug {
6659
args = args.toLowerCase(Locale.ENGLISH);
6760
if (args.equals("help")) {
6861
Help();
69-
} else if (args.contains("all")) {
70-
// "all" option has special handling for decorator options
71-
// If the thread or timestamp decorator option is detected
72-
// with the "all" option, then it impacts decorator options
73-
// for other categories
74-
int beginIndex = args.lastIndexOf("all") + "all".length();
75-
int commaIndex = args.indexOf(',', beginIndex);
76-
if (commaIndex == -1) commaIndex = args.length();
77-
threadInfoAll = args.substring(beginIndex, commaIndex).contains(THREAD_OPTION);
78-
timeStampInfoAll = args.substring(beginIndex, commaIndex).contains(TIMESTAMP_OPTION);
7962
}
8063
}
8164
}
@@ -106,11 +89,6 @@ public static void Help() {
10689
System.err.println("ts timestamping");
10790
System.err.println("x509 X.509 certificate debugging");
10891
System.err.println();
109-
System.err.println("+timestamp can be appended to any of above options to print");
110-
System.err.println(" a timestamp for that debug option");
111-
System.err.println("+thread can be appended to any of above options to print");
112-
System.err.println(" thread and caller information for that debug option");
113-
System.err.println();
11492
System.err.println("The following can be used with provider:");
11593
System.err.println();
11694
System.err.println("engine=<engines>");
@@ -151,7 +129,6 @@ public static Debug getInstance(String option, String prefix) {
151129
if (isOn(option)) {
152130
Debug d = new Debug();
153131
d.prefix = prefix;
154-
d.configureExtras(option);
155132
return d;
156133
} else {
157134
return null;
@@ -166,32 +143,6 @@ private static String formatCaller() {
166143
.findFirst().orElse("unknown caller"));
167144
}
168145

169-
// parse an option string to determine if extra details,
170-
// like thread and timestamp, should be printed
171-
private void configureExtras(String option) {
172-
// treat "all" as special case, only used for java.security.debug property
173-
this.printDateTime = timeStampInfoAll;
174-
this.printThreadDetails = threadInfoAll;
175-
176-
if (printDateTime && printThreadDetails) {
177-
// nothing left to configure
178-
return;
179-
}
180-
181-
// args is converted to lower case for the most part via marshal method
182-
int optionIndex = args.lastIndexOf(option);
183-
if (optionIndex == -1) {
184-
// option not in args list. Only here since "all" was present
185-
// in debug property argument. "all" option already parsed
186-
return;
187-
}
188-
int beginIndex = optionIndex + option.length();
189-
int commaIndex = args.indexOf(',', beginIndex);
190-
if (commaIndex == -1) commaIndex = args.length();
191-
String subOpt = args.substring(beginIndex, commaIndex);
192-
printDateTime = printDateTime || subOpt.contains(TIMESTAMP_OPTION);
193-
printThreadDetails = printThreadDetails || subOpt.contains(THREAD_OPTION);
194-
}
195146

196147
/**
197148
* Get a Debug object corresponding to the given option on the given
@@ -208,11 +159,6 @@ private void configureExtras(String option) {
208159
* Debug debug = Debug.of("login", property);
209160
* }
210161
*
211-
* +timestamp string can be appended to property value
212-
* to print timestamp information. (e.g. true+timestamp)
213-
* +thread string can be appended to property value
214-
* to print thread and caller information. (e.g. true+thread)
215-
*
216162
* @param prefix the debug option name
217163
* @param property debug setting for this option
218164
* @return a new Debug object if the property is true
@@ -221,8 +167,6 @@ public static Debug of(String prefix, String property) {
221167
if (property != null && property.toLowerCase(Locale.ROOT).startsWith("true")) {
222168
Debug d = new Debug();
223169
d.prefix = prefix;
224-
d.printThreadDetails = property.contains(THREAD_OPTION);
225-
d.printDateTime = property.contains(TIMESTAMP_OPTION);
226170
return d;
227171
}
228172
return null;
@@ -285,23 +229,18 @@ public void println(String prefix, String message) {
285229
}
286230

287231
/**
288-
* If thread debug option enabled, include information containing
289-
* hex value of threadId and the current thread name
290-
* If timestamp debug option enabled, include timestamp string
291-
* @return extra info if debug option enabled.
232+
* Include information containing:
233+
* - hex value of threadId
234+
* - the current thread name
235+
* - timestamp string
236+
* @return String with above metadata
292237
*/
293238
private String extraInfo() {
294-
String retString = "";
295-
if (printThreadDetails) {
296-
retString = "0x" + Long.toHexString(
297-
Thread.currentThread().threadId()).toUpperCase(Locale.ROOT) +
298-
"|" + Thread.currentThread().getName() + "|" + formatCaller();
299-
}
300-
if (printDateTime) {
301-
retString += (retString.isEmpty() ? "" : "|")
302-
+ FormatHolder.DATE_TIME_FORMATTER.format(Instant.now());
303-
}
304-
return retString.isEmpty() ? "" : "[" + retString + "]";
239+
return String.format("[0x%s|%s|%s|%s]",
240+
Long.toHexString(Thread.currentThread().threadId()).toUpperCase(Locale.ROOT),
241+
Thread.currentThread().getName(),
242+
formatCaller(),
243+
FormatHolder.DATE_TIME_FORMATTER.format(Instant.now()));
305244
}
306245

307246
/**

test/jdk/sun/security/krb5/auto/LoginModuleDebug.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -47,8 +47,8 @@ private static Stream<Arguments> patternMatches() {
4747
// debug option set to true - no extra info
4848
Arguments.of("debug",
4949
"true",
50-
"krb5loginmodule:",
51-
"krb5loginmodule\\["),
50+
"krb5loginmodule\\[.*\\|main|" + DATE_REGEX + ".*\\]:",
51+
"krb5loginmodule:"),
5252
// debug option set to false
5353
Arguments.of("debug",
5454
"false",
@@ -59,19 +59,9 @@ private static Stream<Arguments> patternMatches() {
5959
"bar",
6060
"",
6161
"krb5loginmodule"),
62-
// thread info only
62+
// test for thread and timestamp info
6363
Arguments.of("debug",
6464
"true+thread",
65-
"krb5loginmodule\\[.*\\|main|\\.*java.*]:",
66-
"\\|" + DATE_REGEX + ".*\\]:"),
67-
// timestamp info only
68-
Arguments.of("debug",
69-
"true+timestamp",
70-
"krb5loginmodule\\[" + DATE_REGEX + ".*\\]",
71-
"\\|main\\]:"),
72-
// both thread and timestamp
73-
Arguments.of("debug",
74-
"true+timestamp+thread",
7565
"krb5loginmodule\\[.*\\|main|" + DATE_REGEX + ".*\\]:",
7666
"krb5loginmodule:")
7767
);
@@ -104,4 +94,4 @@ public static void main(String[] args) throws Exception {
10494
new Subject(), null, Map.of(), Map.of(args[0], args[1]));
10595
}
10696
}
107-
}
97+
}

test/jdk/sun/security/ssl/SSLLogger/DebugPropertyValuesTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ public class DebugPropertyValuesTest extends SSLSocketTemplate {
5151

5252
private static final Path LOG_FILE = Path.of("logging.conf");
5353
private static final HashMap<String, List<String>> debugMessages = new HashMap<>();
54+
private static final String DATE_REGEX = "\\d{4}-\\d{2}-\\d{2}";
5455

5556
static {
57+
58+
5659
debugMessages.put("handshake",
5760
List.of("Produced ClientHello handshake message",
5861
"supported_versions"));
@@ -74,10 +77,10 @@ public class DebugPropertyValuesTest extends SSLSocketTemplate {
7477
debugMessages.put("help",
7578
List.of("print the help messages",
7679
"debugging can be widened with:"));
77-
debugMessages.put("javax.net.debug",
78-
List.of("properties: Initial security property:",
79-
"certpath: Cert path validation succeeded"));
80-
debugMessages.put("logger",
80+
debugMessages.put("java.security.debug",
81+
List.of("properties\\[.*\\|main\\|.*" + DATE_REGEX + ".*\\]:",
82+
"certpath\\[.*\\|main\\|.*" + DATE_REGEX + ".*\\]:"));
83+
debugMessages.put("javax.net.debug.logger",
8184
List.of("FINE: adding as trusted certificates",
8285
"FINE: WRITE: TLSv1.3 application_data"));
8386
}
@@ -151,14 +154,15 @@ private static Stream<Arguments> patternMatches() {
151154
// add in javax.net.debug sanity test
152155
Arguments.of(List.of("-Djavax.net.debug=ssl:trustmanager",
153156
"-Djava.security.debug=all"),
154-
List.of("handshake", "javax.net.debug", "keymanager",
157+
List.of("handshake", "java.security.debug", "keymanager",
155158
"record", "session", "ssl", "sslctx",
156159
"trustmanager", "verbose")),
157160
// empty invokes System.Logger use
158161
Arguments.of(List.of("-Djavax.net.debug",
159162
"-Djava.util.logging.config.file=" + LOG_FILE),
160-
List.of("handshake", "keymanager", "logger", "packet",
161-
"plaintext", "record", "session", "ssl",
163+
List.of("handshake", "javax.net.debug.logger",
164+
"keymanager", "packet", "plaintext",
165+
"record", "session", "ssl",
162166
"sslctx", "trustmanager", "verbose"))
163167
);
164168
}

test/jdk/sun/security/util/Debug/DebugOptions.java

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8051959
26+
* @bug 8051959 8350689
2727
* @summary Option to print extra information in java.security.debug output
2828
* @library /test/lib
2929
* @run junit DebugOptions
@@ -43,75 +43,41 @@
4343
public class DebugOptions {
4444

4545
static final String DATE_REGEX = "\\d{4}-\\d{2}-\\d{2}";
46+
static final String EXPECTED_PROP_REGEX =
47+
"properties\\[.*\\|main|" + DATE_REGEX + ".*\\]:";
48+
static final String EXPECTED_PROP_KEYSTORE_REGEX =
49+
"properties\\[.*\\|main|" + DATE_REGEX +
50+
".*\\Rkeystore\\[.*\\|main|" + DATE_REGEX + ".*\\]:";
51+
static final String EXPECTED_ALL_REGEX =
52+
"properties\\[.*\\|main.*\\|" + DATE_REGEX +
53+
".*\\]((.*\\R)*)keystore\\[.*\\|main.*\\|"
54+
+ DATE_REGEX + ".*\\]:";
4655

4756
private static Stream<Arguments> patternMatches() {
4857
return Stream.of(
49-
// no extra info present
58+
// test for thread and timestamp info
5059
Arguments.of("properties",
51-
"properties: Initial",
52-
"properties\\["),
53-
// thread info only
60+
EXPECTED_PROP_REGEX,
61+
"properties:"),
62+
// test for thread and timestamp info
5463
Arguments.of("properties+thread",
55-
"properties\\[.*\\|main\\|.*java.*]:",
56-
"properties\\[" + DATE_REGEX),
57-
// timestamp info only
58-
Arguments.of("properties+timestamp",
59-
"properties\\[" + DATE_REGEX + ".*\\]",
60-
"\\|main\\]:"),
61-
// both thread and timestamp
62-
Arguments.of("properties+timestamp+thread",
63-
"properties\\[.*\\|main|" + DATE_REGEX + ".*\\]:",
64+
EXPECTED_PROP_REGEX,
6465
"properties:"),
6566
// flip the arguments of previous test
6667
Arguments.of("properties+thread+timestamp",
67-
"properties\\[.*\\|main|" + DATE_REGEX + ".*\\]:",
68+
EXPECTED_PROP_REGEX,
6869
"properties:"),
69-
// comma not valid separator, ignore extra info printing request
70-
Arguments.of("properties,thread,timestamp",
71-
"properties:",
72-
"properties\\[.*\\|main|" + DATE_REGEX + ".*\\]:"),
73-
// no extra info for keystore debug prints
74-
Arguments.of("properties+thread+timestamp,keystore",
75-
"properties\\[.*\\|main|" + DATE_REGEX + ".*\\]:",
76-
"keystore\\["),
77-
// flip arguments around in last test - same outcome expected
78-
Arguments.of("keystore,properties+thread+timestamp",
79-
"properties\\[.*\\|main|" + DATE_REGEX + ".*\\]:",
80-
"keystore\\["),
81-
// turn on thread info for both keystore and properties components
82-
Arguments.of("keystore+thread,properties+thread",
83-
"properties\\[.*\\|main|.*\\Rkeystore\\[.*\\|main|.*\\]:",
84-
"\\|" + DATE_REGEX + ".*\\]:"),
85-
// same as above with erroneous comma at end of string. same output expected
86-
Arguments.of("keystore+thread,properties+thread,",
87-
"properties\\[.*\\|main|.*\\Rkeystore\\[.*\\|main|.*\\]:",
88-
"\\|" + DATE_REGEX + ".*\\]:"),
89-
// turn on thread info for properties and timestamp for keystore
90-
Arguments.of("keystore+timestamp,properties+thread",
91-
"properties\\[.*\\|main|.*\\Rkeystore\\[" + DATE_REGEX + ".*\\]:",
92-
"properties\\[.*\\|" + DATE_REGEX + ".*\\]:"),
93-
// turn on thread info for all components
94-
Arguments.of("all+thread",
95-
"properties\\[.*\\|main.*((.*\\R)*)keystore\\[.*\\|main.*java.*\\]:",
96-
"properties\\[" + DATE_REGEX + ".*\\]:"),
97-
// turn on thread info and timestamp for all components
98-
Arguments.of("all+thread+timestamp",
99-
"properties\\[.*\\|main.*\\|" + DATE_REGEX +
100-
".*\\]((.*\\R)*)keystore\\[.*\\|main.*\\|" + DATE_REGEX + ".*\\]:",
70+
// regular keystore,properties component string
71+
Arguments.of("keystore,properties",
72+
EXPECTED_PROP_KEYSTORE_REGEX,
10173
"properties:"),
102-
// all decorator option should override other component options
103-
Arguments.of("all+thread+timestamp,properties",
104-
"properties\\[.*\\|main.*\\|" + DATE_REGEX +
105-
".*\\]((.*\\R)*)keystore\\[.*\\|main.*\\|" + DATE_REGEX + ".*\\]:",
74+
// turn on all
75+
Arguments.of("all",
76+
EXPECTED_ALL_REGEX,
10677
"properties:"),
107-
// thread details should only be printed for properties option
108-
Arguments.of("properties+thread,all",
109-
"properties\\[.*\\|main\\|.*\\]:",
110-
"keystore\\[.*\\|main\\|.*\\]:"),
111-
// thread details should be printed for all statements
112-
Arguments.of("properties,all+thread",
113-
"properties\\[.*\\|main.*java" +
114-
".*\\]((.*\\R)*)keystore\\[.*\\|main.*java.*\\]:",
78+
// expect thread and timestamp info
79+
Arguments.of("all+thread",
80+
EXPECTED_ALL_REGEX,
11581
"properties:")
11682
);
11783
}

0 commit comments

Comments
 (0)