author | jdv |
Wed, 18 Apr 2018 13:22:53 +0530 | |
changeset 49996 | 39dc39093c5e |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
32474
5b41b53a4997
8134420: sun/tools/jps/TestJpsClass fails with java.lang.RuntimeException: The line 'line 2' does not match pattern '^\\d+\\s+.*': expected true, was false
jbachorik
parents:
27957
diff
changeset
|
2 |
* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.tools.jps; |
|
27 |
||
28 |
import java.util.*; |
|
29 |
import java.net.*; |
|
30 |
import sun.jvmstat.monitor.*; |
|
31 |
||
32 |
/** |
|
33 |
* Application to provide a listing of monitorable java processes. |
|
34 |
* |
|
35 |
* @author Brian Doherty |
|
36 |
* @since 1.5 |
|
37 |
*/ |
|
38 |
public class Jps { |
|
39 |
||
40 |
private static Arguments arguments; |
|
41 |
||
42 |
public static void main(String[] args) { |
|
43 |
try { |
|
44 |
arguments = new Arguments(args); |
|
45 |
} catch (IllegalArgumentException e) { |
|
46 |
System.err.println(e.getMessage()); |
|
47 |
Arguments.printUsage(System.err); |
|
21673
c5d341bc60dd
8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents:
11125
diff
changeset
|
48 |
System.exit(1); |
2 | 49 |
} |
50 |
||
51 |
if (arguments.isHelp()) { |
|
21673
c5d341bc60dd
8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents:
11125
diff
changeset
|
52 |
Arguments.printUsage(System.err); |
2 | 53 |
System.exit(0); |
54 |
} |
|
55 |
||
56 |
try { |
|
57 |
HostIdentifier hostId = arguments.hostId(); |
|
58 |
MonitoredHost monitoredHost = |
|
59 |
MonitoredHost.getMonitoredHost(hostId); |
|
60 |
||
61 |
// get the set active JVMs on the specified host. |
|
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
8390
diff
changeset
|
62 |
Set<Integer> jvms = monitoredHost.activeVms(); |
2 | 63 |
|
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
8390
diff
changeset
|
64 |
for (Integer jvm: jvms) { |
2 | 65 |
StringBuilder output = new StringBuilder(); |
66 |
Throwable lastError = null; |
|
67 |
||
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
8390
diff
changeset
|
68 |
int lvmid = jvm; |
2 | 69 |
|
70 |
output.append(String.valueOf(lvmid)); |
|
71 |
||
72 |
if (arguments.isQuiet()) { |
|
73 |
System.out.println(output); |
|
74 |
continue; |
|
75 |
} |
|
76 |
||
77 |
MonitoredVm vm = null; |
|
78 |
String vmidString = "//" + lvmid + "?mode=r"; |
|
79 |
||
8390
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
80 |
String errorString = null; |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
81 |
|
2 | 82 |
try { |
8390
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
83 |
// Note: The VM associated with the current VM id may |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
84 |
// no longer be running so these queries may fail. We |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
85 |
// already added the VM id to the output stream above. |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
86 |
// If one of the queries fails, then we try to add a |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
87 |
// reasonable message to indicate that the requested |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
88 |
// info is not available. |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
89 |
|
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
90 |
errorString = " -- process information unavailable"; |
2 | 91 |
VmIdentifier id = new VmIdentifier(vmidString); |
92 |
vm = monitoredHost.getMonitoredVm(id, 0); |
|
8390
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
93 |
|
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
94 |
errorString = " -- main class information unavailable"; |
27957
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
95 |
output.append(' ').append(MonitoredVmUtil.mainClass(vm, |
8390
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
96 |
arguments.showLongPaths())); |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
97 |
|
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
98 |
if (arguments.showMainArgs()) { |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
99 |
errorString = " -- main args information unavailable"; |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
100 |
String mainArgs = MonitoredVmUtil.mainArgs(vm); |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
101 |
if (mainArgs != null && mainArgs.length() > 0) { |
27957
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
102 |
output.append(' ').append(mainArgs); |
8390
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
103 |
} |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
104 |
} |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
105 |
if (arguments.showVmArgs()) { |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
106 |
errorString = " -- jvm args information unavailable"; |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
107 |
String jvmArgs = MonitoredVmUtil.jvmArgs(vm); |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
108 |
if (jvmArgs != null && jvmArgs.length() > 0) { |
32474
5b41b53a4997
8134420: sun/tools/jps/TestJpsClass fails with java.lang.RuntimeException: The line 'line 2' does not match pattern '^\\d+\\s+.*': expected true, was false
jbachorik
parents:
27957
diff
changeset
|
109 |
output.append(' ') |
5b41b53a4997
8134420: sun/tools/jps/TestJpsClass fails with java.lang.RuntimeException: The line 'line 2' does not match pattern '^\\d+\\s+.*': expected true, was false
jbachorik
parents:
27957
diff
changeset
|
110 |
.append( |
5b41b53a4997
8134420: sun/tools/jps/TestJpsClass fails with java.lang.RuntimeException: The line 'line 2' does not match pattern '^\\d+\\s+.*': expected true, was false
jbachorik
parents:
27957
diff
changeset
|
111 |
// multi-line args are permitted |
5b41b53a4997
8134420: sun/tools/jps/TestJpsClass fails with java.lang.RuntimeException: The line 'line 2' does not match pattern '^\\d+\\s+.*': expected true, was false
jbachorik
parents:
27957
diff
changeset
|
112 |
jvmArgs.replace("\n", "\\n").replace("\r", "\\r") |
5b41b53a4997
8134420: sun/tools/jps/TestJpsClass fails with java.lang.RuntimeException: The line 'line 2' does not match pattern '^\\d+\\s+.*': expected true, was false
jbachorik
parents:
27957
diff
changeset
|
113 |
); |
8390
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
114 |
} |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
115 |
} |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
116 |
if (arguments.showVmFlags()) { |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
117 |
errorString = " -- jvm flags information unavailable"; |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
118 |
String jvmFlags = MonitoredVmUtil.jvmFlags(vm); |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
119 |
if (jvmFlags != null && jvmFlags.length() > 0) { |
27957
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
120 |
output.append(' ').append(jvmFlags); |
8390
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
121 |
} |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
122 |
} |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
123 |
|
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
124 |
errorString = " -- detach failed"; |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
125 |
monitoredHost.detach(vm); |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
126 |
|
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
127 |
System.out.println(output); |
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
128 |
|
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
129 |
errorString = null; |
2 | 130 |
} catch (URISyntaxException e) { |
131 |
// unexpected as vmidString is based on a validated hostid |
|
132 |
lastError = e; |
|
133 |
assert false; |
|
134 |
} catch (Exception e) { |
|
135 |
lastError = e; |
|
136 |
} finally { |
|
8390
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
137 |
if (errorString != null) { |
2 | 138 |
/* |
139 |
* we ignore most exceptions, as there are race |
|
140 |
* conditions where a JVM in 'jvms' may terminate |
|
141 |
* before we get a chance to list its information. |
|
142 |
* Other errors, such as access and I/O exceptions |
|
143 |
* should stop us from iterating over the complete set. |
|
144 |
*/ |
|
8390
f9ed509a2634
6637230: 2/3 jps doesn't work for application waiting for direct attach
dcubed
parents:
5506
diff
changeset
|
145 |
output.append(errorString); |
2 | 146 |
if (arguments.isDebug()) { |
147 |
if ((lastError != null) |
|
148 |
&& (lastError.getMessage() != null)) { |
|
149 |
output.append("\n\t"); |
|
150 |
output.append(lastError.getMessage()); |
|
151 |
} |
|
152 |
} |
|
153 |
System.out.println(output); |
|
154 |
if (arguments.printStackTrace()) { |
|
155 |
lastError.printStackTrace(); |
|
156 |
} |
|
157 |
continue; |
|
158 |
} |
|
159 |
} |
|
160 |
} |
|
161 |
} catch (MonitorException e) { |
|
162 |
if (e.getMessage() != null) { |
|
163 |
System.err.println(e.getMessage()); |
|
164 |
} else { |
|
165 |
Throwable cause = e.getCause(); |
|
166 |
if ((cause != null) && (cause.getMessage() != null)) { |
|
167 |
System.err.println(cause.getMessage()); |
|
168 |
} else { |
|
169 |
e.printStackTrace(); |
|
170 |
} |
|
171 |
} |
|
21673
c5d341bc60dd
8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
sla
parents:
11125
diff
changeset
|
172 |
System.exit(1); |
2 | 173 |
} |
174 |
} |
|
175 |
} |