34
34
import lib .jdb .JdbCommand ;
35
35
import jdk .test .lib .process .OutputAnalyzer ;
36
36
37
+ import java .io .IOException ;
38
+ import java .io .PrintStream ;
37
39
import java .lang .management .ManagementFactory ;
40
+ import java .nio .charset .StandardCharsets ;
41
+ import java .nio .file .Files ;
42
+ import java .nio .file .Path ;
43
+ import java .nio .file .Paths ;
38
44
import java .util .Arrays ;
39
45
import java .util .List ;
46
+ import java .util .UUID ;
47
+ import java .util .stream .Collectors ;
40
48
41
49
class JbdOptionsTarg {
42
50
static final String OK_MSG = "JbdOptionsTarg: OK" ;
@@ -49,47 +57,62 @@ static String propString(String name, String value) {
49
57
return "prop[" + name + "] = >" + value + "<" ;
50
58
}
51
59
52
- public static void main (String [] args ) {
53
- System .out .println (OK_MSG );
54
- // print all args
55
- List <String > vmArgs = ManagementFactory .getRuntimeMXBean ().getInputArguments ();
56
- for (String s : vmArgs ) {
57
- System .out .println (argString (s ));
58
- }
59
- // print requested sys.props
60
- for (String p : args ) {
61
- System .out .println (propString (p , System .getProperty (p )));
60
+ /**
61
+ * 1st argument is a filename to redirect application output,
62
+ * the rest are names of the properties to dump.
63
+ */
64
+ public static void main (String [] args ) throws IOException {
65
+ String outFile = args [0 ];
66
+ try (PrintStream out = new PrintStream (outFile , StandardCharsets .UTF_8 )) {
67
+ out .println (OK_MSG );
68
+ // print all args
69
+ List <String > vmArgs = ManagementFactory .getRuntimeMXBean ().getInputArguments ();
70
+ for (String s : vmArgs ) {
71
+ out .println (argString (s ));
72
+ }
73
+ // print requested sys.props (skip 1st arg which is output filename)
74
+ for (int i =1 ; i < args .length ; i ++) {
75
+ String p = args [i ];
76
+ out .println (propString (p , System .getProperty (p )));
77
+ }
62
78
}
63
79
}
64
80
}
65
81
66
82
public class JdbOptions {
83
+ private static final String outFilename = UUID .randomUUID ().toString () + ".out" ;
84
+ private static final Path outPath = Paths .get (outFilename );
67
85
private static final String targ = JbdOptionsTarg .class .getName ();
68
86
69
87
public static void main (String [] args ) throws Exception {
70
88
// the simplest case
71
89
test ("-connect" ,
72
- "com.sun.jdi.CommandLineLaunch:vmexec=java,options=-client -XX:+PrintVMOptions,main=" + targ )
90
+ "com.sun.jdi.CommandLineLaunch:vmexec=java,options=-client -XX:+PrintVMOptions"
91
+ + ",main=" + targ + " " + outFilename )
73
92
.expectedArg ("-XX:+PrintVMOptions" );
74
93
75
94
// pass property through 'options'
76
95
test ("-connect" ,
77
- "com.sun.jdi.CommandLineLaunch:vmexec=java,options='-Dboo=foo',main=" + targ + " boo" )
96
+ "com.sun.jdi.CommandLineLaunch:vmexec=java,options='-Dboo=foo'"
97
+ + ",main=" + targ + " " + outFilename + " boo" )
78
98
.expectedProp ("boo" , "foo" );
79
99
80
100
// property with spaces
81
101
test ("-connect" ,
82
- "com.sun.jdi.CommandLineLaunch:vmexec=java,options=\" -Dboo=foo 2\" ,main=" + targ + " boo" )
102
+ "com.sun.jdi.CommandLineLaunch:vmexec=java,options=\" -Dboo=foo 2\" "
103
+ + ",main=" + targ + " " + outFilename + " boo" )
83
104
.expectedProp ("boo" , "foo 2" );
84
105
85
106
// property with spaces (with single quotes)
86
107
test ("-connect" ,
87
- "com.sun.jdi.CommandLineLaunch:vmexec=java,options='-Dboo=foo 2',main=" + targ + " boo" )
108
+ "com.sun.jdi.CommandLineLaunch:vmexec=java,options='-Dboo=foo 2'"
109
+ + ",main=" + targ + " " + outFilename + " boo" )
88
110
.expectedProp ("boo" , "foo 2" );
89
111
90
112
// properties with spaces (with single quotes)
91
113
test ("-connect" ,
92
- "com.sun.jdi.CommandLineLaunch:vmexec=java,options=-Dboo=foo '-Dboo2=foo 2',main=" + targ + " boo boo2" )
114
+ "com.sun.jdi.CommandLineLaunch:vmexec=java,options=-Dboo=foo '-Dboo2=foo 2'"
115
+ + ",main=" + targ + " " + outFilename + " boo boo2" )
93
116
.expectedProp ("boo" , "foo" )
94
117
.expectedProp ("boo2" , "foo 2" );
95
118
@@ -98,7 +121,7 @@ public static void main(String[] args) throws Exception {
98
121
"com.sun.jdi.CommandLineLaunch:vmexec=java,options=\" -client\" \" -XX:+PrintVMOptions\" "
99
122
+ " -XX:+IgnoreUnrecognizedVMOptions"
100
123
+ " \" -XX:StartFlightRecording=dumponexit=true,maxsize=500M\" \" -XX:FlightRecorderOptions=repository=jfrrep\" "
101
- + ",main=" + targ )
124
+ + ",main=" + targ + " " + outFilename )
102
125
.expectedArg ("-XX:StartFlightRecording=dumponexit=true,maxsize=500M" )
103
126
.expectedArg ("-XX:FlightRecorderOptions=repository=jfrrep" );
104
127
@@ -107,7 +130,7 @@ public static void main(String[] args) throws Exception {
107
130
"com.sun.jdi.CommandLineLaunch:vmexec=java,options='-client' '-XX:+PrintVMOptions'"
108
131
+ " -XX:+IgnoreUnrecognizedVMOptions"
109
132
+ " '-XX:StartFlightRecording=dumponexit=true,maxsize=500M' '-XX:FlightRecorderOptions=repository=jfrrep'"
110
- + ",main=" + targ )
133
+ + ",main=" + targ + " " + outFilename )
111
134
.expectedArg ("-XX:StartFlightRecording=dumponexit=true,maxsize=500M" )
112
135
.expectedArg ("-XX:FlightRecorderOptions=repository=jfrrep" );
113
136
@@ -120,7 +143,7 @@ public static void main(String[] args) throws Exception {
120
143
+ " -XX:+IgnoreUnrecognizedVMOptions"
121
144
+ " \" -XX:StartFlightRecording=dumponexit=true,maxsize=500M\" "
122
145
+ " '-XX:FlightRecorderOptions=repository=jfrrep'"
123
- + ",main=" + targ + " prop1 prop2 prop3 prop4" )
146
+ + ",main=" + targ + " " + outFilename + " prop1 prop2 prop3 prop4" )
124
147
.expectedProp ("prop1" , "val1" )
125
148
.expectedProp ("prop2" , "val 2" )
126
149
.expectedProp ("prop3" , "val3" )
@@ -154,12 +177,18 @@ private static TestResult test(String... args) throws Exception {
154
177
.map (s -> s .replace ("\" " , "\\ \" " ))
155
178
.toArray (String []::new );
156
179
}
180
+
157
181
try (Jdb jdb = new Jdb (args )) {
158
182
jdb .waitForSimplePrompt (1024 , true ); // 1024 lines should be enough
159
183
jdb .command (JdbCommand .run ().allowExit ());
160
- OutputAnalyzer out = new OutputAnalyzer (jdb .getJdbOutput ());
161
- out .shouldContain (JbdOptionsTarg .OK_MSG );
162
- return new TestResult (out );
163
184
}
185
+ String output = Files .readAllLines (outPath , StandardCharsets .UTF_8 ).stream ()
186
+ .collect (Collectors .joining (System .getProperty ("line.separator" )));
187
+ Files .deleteIfExists (outPath );
188
+ System .out .println ("Debuggee output: [" );
189
+ System .out .println (output );
190
+ System .out .println ("]" );
191
+ OutputAnalyzer out = new OutputAnalyzer (output );
192
+ return new TestResult (out );
164
193
}
165
194
}
0 commit comments