Skip to content

Commit 7c31903

Browse files
committed
8267075: jcmd VM.cds should print directory of the output files
Reviewed-by: ccheung
1 parent e515873 commit 7c31903

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/java.base/share/classes/jdk/internal/misc/CDS.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ private static boolean containsExcludedFlags(String testStr) {
256256
* @param fileName user input archive name, can be null.
257257
*/
258258
private static void dumpSharedArchive(boolean isStatic, String fileName) throws Exception {
259+
String cwd = new File("").getAbsolutePath(); // current dir used for printing message.
259260
String currentPid = String.valueOf(ProcessHandle.current().pid());
260261
String archiveFileName = fileName != null ? fileName :
261262
"java_pid" + currentPid + (isStatic ? "_static.jsa" : "_dynamic.jsa");
@@ -299,8 +300,8 @@ private static void dumpSharedArchive(boolean isStatic, String fileName) throws
299300
Process proc = Runtime.getRuntime().exec(cmds.toArray(new String[0]));
300301

301302
// Drain stdout/stderr to files in new threads.
302-
String stdOutFile = drainOutput(proc.getInputStream(), proc.pid(), "stdout", cmds);
303-
String stdErrFile = drainOutput(proc.getErrorStream(), proc.pid(), "stderr", cmds);
303+
String stdOutFileName = drainOutput(proc.getInputStream(), proc.pid(), "stdout", cmds);
304+
String stdErrFileName = drainOutput(proc.getErrorStream(), proc.pid(), "stderr", cmds);
304305

305306
proc.waitFor();
306307
// done, delete classlist file.
@@ -311,14 +312,15 @@ private static void dumpSharedArchive(boolean isStatic, String fileName) throws
311312
if (!tempArchiveFile.exists()) {
312313
throw new RuntimeException("Archive file " + tempArchiveFileName +
313314
" is not created, please check stdout file " +
314-
stdOutFile + " or stderr file " +
315-
stdErrFile + " for more detail");
315+
cwd + File.separator + stdOutFileName + " or stderr file " +
316+
cwd + File.separator + stdErrFileName + " for more detail");
316317
}
317318
} else {
318319
dumpDynamicArchive(tempArchiveFileName);
319320
if (!tempArchiveFile.exists()) {
320321
throw new RuntimeException("Archive file " + tempArchiveFileName +
321-
" is not created, please check process " +
322+
" is not created, please check current working directory " +
323+
cwd + " for process " +
322324
currentPid + " output for more detail");
323325
}
324326
}
@@ -331,6 +333,6 @@ private static void dumpSharedArchive(boolean isStatic, String fileName) throws
331333
throw new RuntimeException("Cannot rename temp file " + tempArchiveFileName + " to archive file" + archiveFileName);
332334
}
333335
// Everyting goes well, print out the file name.
334-
System.out.println((isStatic ? "Static" : " Dynamic") + " dump to file " + archiveFileName);
336+
System.out.println((isStatic ? "Static" : " Dynamic") + " dump to file " + cwd + File.separator + archiveFileName);
335337
}
336338
}

test/hotspot/jtreg/runtime/cds/appcds/jcmd/JCmdTestDumpBase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private static void runWithArchiveFile(String archiveName, boolean useBoot, Str
172172
}
173173
}
174174

175-
protected static void test(String fileName, long pid,
175+
protected static OutputAnalyzer test(String fileName, long pid,
176176
boolean useBoot, boolean expectOK, String... messages) throws Exception {
177177
System.out.println("Expected: " + (expectOK ? "SUCCESS" : "FAIL"));
178178
String archiveFileName = fileName != null ? fileName :
@@ -200,6 +200,7 @@ protected static void test(String fileName, long pid,
200200
checkFileExistence(archiveFileName, false);
201201
}
202202
}
203+
return output;
203204
}
204205

205206
protected static void print2ln(String arg) {

test/hotspot/jtreg/runtime/cds/appcds/jcmd/JCmdTestFileSafety.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
/*
2626
* @test
27-
* @bug 8265465
27+
* @bug 8265465 8267075
2828
* @summary Test jcmd to dump static shared archive.
2929
* @requires vm.cds
3030
* @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
@@ -41,8 +41,25 @@
4141
import jdk.test.lib.cds.CDSTestUtils;
4242
import jdk.test.lib.apps.LingeredApp;
4343
import jdk.test.lib.Platform;
44+
import jdk.test.lib.process.OutputAnalyzer;
4445

4546
public class JCmdTestFileSafety extends JCmdTestDumpBase {
47+
static final String promptStdout = "please check stdout file";
48+
static final String promptStderr = "or stderr file";
49+
50+
static void checkContainAbsoluteLogPath(OutputAnalyzer output) throws Exception {
51+
String stdText = output.getOutput();
52+
if (stdText.contains(promptStdout) &&
53+
stdText.contains(promptStderr)) {
54+
int a = stdText.indexOf(promptStdout);
55+
int b = stdText.indexOf(promptStderr);
56+
String stdOutFileName = stdText.substring(a + promptStdout.length() + 1, b - 1).trim();
57+
File stdOutFile = new File(stdOutFileName);
58+
if (!stdOutFile.isAbsolute()) {
59+
throw new RuntimeException("Failed to set file name in absolute for prompting message");
60+
}
61+
}
62+
}
4663

4764
static void test() throws Exception {
4865
buildJars();
@@ -67,10 +84,16 @@ static void test() throws Exception {
6784
test(localFileName, pid, noBoot, EXPECT_PASS);
6885
outputDirFile.setWritable(false);
6986
test(localFileName, pid, noBoot, EXPECT_FAIL);
70-
app.stopApp();
7187
outputDirFile.setWritable(true);
7288
checkFileExistence(localFileName, true/*exist*/);
7389

90+
// Illegal character in file name
91+
localFileName = "mystatic:.jsa";
92+
OutputAnalyzer output = test(localFileName, pid, noBoot, EXPECT_FAIL);
93+
checkFileExistence(localFileName, false/*exist*/);
94+
checkContainAbsoluteLogPath(output);
95+
app.stopApp();
96+
7497
setIsStatic(false/*dynamic*/);
7598
// Set target dir not writable, do dynamic dump
7699
print2ln(test_count++ + " Set target dir not writable, do dynamic dump");

0 commit comments

Comments
 (0)