author | dtitov |
Mon, 08 Apr 2019 17:09:02 +0000 | |
changeset 54460 | 6733a9176cce |
parent 53707 | 67537bbafd7f |
child 55386 | 2f4e214781a1 |
permissions | -rw-r--r-- |
53707
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
1 |
/* |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
4 |
* |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
10 |
* |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
15 |
* accompanied this code). |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
16 |
* |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
20 |
* |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
23 |
* questions. |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
24 |
*/ |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
25 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
26 |
package sun.tools; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
27 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
28 |
import java.io.IOException; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
29 |
import java.io.UncheckedIOException; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
30 |
import java.nio.file.Files; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
31 |
import java.nio.file.Paths; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
32 |
import java.util.jar.Attributes; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
33 |
import java.util.jar.JarFile; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
34 |
import java.util.stream.Stream; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
35 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
36 |
/** |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
37 |
* A helper class that retrieves the main class name for |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
38 |
* a running Java process using the proc filesystem (procfs) |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
39 |
*/ |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
40 |
public class ProcessHelper implements sun.tools.common.ProcessHelper { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
41 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
42 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
43 |
private static final String CMD_PREFIX = "cmd:"; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
44 |
private static final ProcessHelper INSTANCE = new ProcessHelper(); |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
45 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
46 |
public static ProcessHelper getInstance() { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
47 |
return INSTANCE; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
48 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
49 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
50 |
/** |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
51 |
* Gets the main class name for the given Java process by parsing the |
54460 | 52 |
* process command line. If the application was started with the <em>-jar</em> |
53 |
* option this method returns the name of the jar file. If the application |
|
54 |
* was started with <em>-m</em> or <em>--module</em> option, the method returns |
|
55 |
* the module name and the main class name. |
|
53707
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
56 |
* @param pid - process ID (pid) |
54460 | 57 |
* @return the main class name or null if the process no longer exists or |
53707
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
58 |
* was started with a native launcher (e.g. jcmd etc) |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
59 |
*/ |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
60 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
61 |
public String getMainClass(String pid) { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
62 |
String cmdLine = getCommandLine(pid); |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
63 |
if (cmdLine == null) { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
64 |
return null; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
65 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
66 |
if (cmdLine.startsWith(CMD_PREFIX)) { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
67 |
cmdLine = cmdLine.substring(CMD_PREFIX.length()); |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
68 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
69 |
String[] parts = cmdLine.split(" "); |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
70 |
String mainClass = null; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
71 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
72 |
if(parts.length == 0) { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
73 |
return null; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
74 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
75 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
76 |
// Check the executable |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
77 |
String[] executablePath = parts[0].split("/"); |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
78 |
if (executablePath.length > 0) { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
79 |
String binaryName = executablePath[executablePath.length - 1]; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
80 |
if (!"java".equals(binaryName)) { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
81 |
// Skip the process if it is not started with java launcher |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
82 |
return null; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
83 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
84 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
85 |
|
54460 | 86 |
// To be consistent with the behavior on other platforms, if -jar, -m, or --module |
87 |
// options are used then just return the value (the path to the jar file or module |
|
88 |
// name with a main class). Otherwise, the main class name is the first part that |
|
89 |
// is not a Java option (doesn't start with '-' and is not a classpath or a module |
|
90 |
// path). |
|
53707
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
91 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
92 |
for (int i = 1; i < parts.length && mainClass == null; i++) { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
93 |
if (i < parts.length - 1) { |
54460 | 94 |
if (parts[i].equals("-m") || parts[i].equals("--module") || parts[i].equals("-jar")) { |
95 |
return parts[i + 1]; |
|
53707
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
96 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
97 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
98 |
// If this is a classpath or a module path option then skip the next part |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
99 |
// (the classpath or the module path itself) |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
100 |
if (parts[i].equals("-cp") || parts[i].equals("-classpath") || parts[i].equals("--class-path") || |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
101 |
parts[i].equals("-p") || parts[i].equals("--module-path")) { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
102 |
i++; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
103 |
continue; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
104 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
105 |
// Skip all other Java options |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
106 |
if (parts[i].startsWith("-")) { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
107 |
continue; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
108 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
109 |
mainClass = parts[i]; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
110 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
111 |
return mainClass; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
112 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
113 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
114 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
115 |
private static String getCommandLine(String pid) { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
116 |
try (Stream<String> lines = |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
117 |
Files.lines(Paths.get("/proc/" + pid + "/cmdline"))) { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
118 |
return lines.map(x -> x.replaceAll("\0", " ")).findFirst().orElse(null); |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
119 |
} catch (IOException | UncheckedIOException e) { |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
120 |
return null; |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
121 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
122 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
123 |
} |
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
124 |
|
67537bbafd7f
8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff
changeset
|
125 |