author | jdv |
Wed, 18 Apr 2018 13:22:53 +0530 | |
changeset 49996 | 39dc39093c5e |
parent 48543 | 7067fe4e054e |
child 54566 | 4224f26b2e7f |
permissions | -rw-r--r-- |
11365 | 1 |
/* |
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. |
11365 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
14856 | 4 |
* |
11365 | 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 |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
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 |
* |
|
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. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.tools.jcmd; |
|
27 |
||
28 |
import java.io.InputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.UnsupportedEncodingException; |
|
31 |
import java.util.List; |
|
38361
8ea2d56bfdf3
8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
25859
diff
changeset
|
32 |
import java.util.Collection; |
8ea2d56bfdf3
8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
25859
diff
changeset
|
33 |
import java.util.Collections; |
11365 | 34 |
import java.util.Comparator; |
35 |
import java.net.URISyntaxException; |
|
36 |
||
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
37 |
import com.sun.tools.attach.AttachOperationFailedException; |
11365 | 38 |
import com.sun.tools.attach.VirtualMachine; |
39 |
import com.sun.tools.attach.VirtualMachineDescriptor; |
|
40 |
import com.sun.tools.attach.AttachNotSupportedException; |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
41 |
|
11365 | 42 |
import sun.tools.attach.HotSpotVirtualMachine; |
38361
8ea2d56bfdf3
8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
25859
diff
changeset
|
43 |
import sun.tools.common.ProcessArgumentMatcher; |
11365 | 44 |
import sun.tools.jstat.JStatLogger; |
45 |
import sun.jvmstat.monitor.Monitor; |
|
46 |
import sun.jvmstat.monitor.MonitoredHost; |
|
47 |
import sun.jvmstat.monitor.MonitoredVm; |
|
48 |
import sun.jvmstat.monitor.MonitorException; |
|
49 |
import sun.jvmstat.monitor.VmIdentifier; |
|
50 |
||
51 |
public class JCmd { |
|
52 |
public static void main(String[] args) { |
|
53 |
Arguments arg = null; |
|
54 |
try { |
|
55 |
arg = new Arguments(args); |
|
56 |
} catch (IllegalArgumentException ex) { |
|
57 |
System.err.println("Error parsing arguments: " + ex.getMessage() |
|
58 |
+ "\n"); |
|
59 |
Arguments.usage(); |
|
60 |
System.exit(1); |
|
61 |
} |
|
62 |
||
63 |
if (arg.isShowUsage()) { |
|
64 |
Arguments.usage(); |
|
48543
7067fe4e054e
8189102: All tools should support -?, -h and --help
goetz
parents:
47216
diff
changeset
|
65 |
System.exit(0); |
11365 | 66 |
} |
67 |
||
38956
cf2c4bfba78a
8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents:
38361
diff
changeset
|
68 |
ProcessArgumentMatcher ap = null; |
cf2c4bfba78a
8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents:
38361
diff
changeset
|
69 |
try { |
cf2c4bfba78a
8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents:
38361
diff
changeset
|
70 |
ap = new ProcessArgumentMatcher(arg.getProcessString()); |
cf2c4bfba78a
8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents:
38361
diff
changeset
|
71 |
} catch (IllegalArgumentException iae) { |
cf2c4bfba78a
8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents:
38361
diff
changeset
|
72 |
System.err.println("Invalid pid '" + arg.getProcessString() + "' specified"); |
cf2c4bfba78a
8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents:
38361
diff
changeset
|
73 |
System.exit(1); |
cf2c4bfba78a
8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents:
38361
diff
changeset
|
74 |
} |
cf2c4bfba78a
8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents:
38361
diff
changeset
|
75 |
|
11365 | 76 |
if (arg.isListProcesses()) { |
38956
cf2c4bfba78a
8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents:
38361
diff
changeset
|
77 |
for (VirtualMachineDescriptor vmd : ap.getVirtualMachineDescriptors(/* include jcmd in listing */)) { |
11365 | 78 |
System.out.println(vmd.id() + " " + vmd.displayName()); |
79 |
} |
|
80 |
System.exit(0); |
|
81 |
} |
|
82 |
||
44422
14ab3266fe62
8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents:
38956
diff
changeset
|
83 |
Collection<String> pids = ap.getVirtualMachinePids(JCmd.class); |
38956
cf2c4bfba78a
8156537: Tools using MonitoredVmUtil do not parse module in cmdline correctly
rehn
parents:
38361
diff
changeset
|
84 |
|
44422
14ab3266fe62
8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents:
38956
diff
changeset
|
85 |
if (pids.isEmpty()) { |
38361
8ea2d56bfdf3
8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
25859
diff
changeset
|
86 |
System.err.println("Could not find any processes matching : '" |
8ea2d56bfdf3
8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
25859
diff
changeset
|
87 |
+ arg.getProcessString() + "'"); |
8ea2d56bfdf3
8154985: Add the ability to use main class as lookup (as jcmd) to jinfo, jmap, jstack
rehn
parents:
25859
diff
changeset
|
88 |
System.exit(1); |
11365 | 89 |
} |
90 |
||
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
91 |
boolean success = true; |
44422
14ab3266fe62
8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents:
38956
diff
changeset
|
92 |
for (String pid : pids) { |
14ab3266fe62
8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents:
38956
diff
changeset
|
93 |
System.out.println(pid + ":"); |
11365 | 94 |
if (arg.isListCounters()) { |
44422
14ab3266fe62
8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents:
38956
diff
changeset
|
95 |
listCounters(pid); |
11365 | 96 |
} else { |
97 |
try { |
|
44422
14ab3266fe62
8176533: REGRESSION: a java process is not recognized by jcmd/jinfo/jstack/jmap tool
rehn
parents:
38956
diff
changeset
|
98 |
executeCommandForPid(pid, arg.getCommand()); |
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
99 |
} catch(AttachOperationFailedException ex) { |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
100 |
System.err.println(ex.getMessage()); |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
101 |
success = false; |
11365 | 102 |
} catch(Exception ex) { |
103 |
ex.printStackTrace(); |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
104 |
success = false; |
11365 | 105 |
} |
106 |
} |
|
107 |
} |
|
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
108 |
System.exit(success ? 0 : 1); |
11365 | 109 |
} |
110 |
||
111 |
private static void executeCommandForPid(String pid, String command) |
|
112 |
throws AttachNotSupportedException, IOException, |
|
113 |
UnsupportedEncodingException { |
|
114 |
VirtualMachine vm = VirtualMachine.attach(pid); |
|
115 |
||
116 |
// Cast to HotSpotVirtualMachine as this is an |
|
117 |
// implementation specific method. |
|
118 |
HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm; |
|
12704
c516c24fa010
7167157: jcmd command file parsing does not respect the "stop" command
sla
parents:
12390
diff
changeset
|
119 |
String lines[] = command.split("\\n"); |
12390
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
120 |
for (String line : lines) { |
12704
c516c24fa010
7167157: jcmd command file parsing does not respect the "stop" command
sla
parents:
12390
diff
changeset
|
121 |
if (line.trim().equals("stop")) { |
c516c24fa010
7167157: jcmd command file parsing does not respect the "stop" command
sla
parents:
12390
diff
changeset
|
122 |
break; |
c516c24fa010
7167157: jcmd command file parsing does not respect the "stop" command
sla
parents:
12390
diff
changeset
|
123 |
} |
12390
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
124 |
try (InputStream in = hvm.executeJCmd(line);) { |
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
125 |
// read to EOF and just print output |
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
126 |
byte b[] = new byte[256]; |
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
127 |
int n; |
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
128 |
boolean messagePrinted = false; |
12390
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
129 |
do { |
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
130 |
n = in.read(b); |
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
131 |
if (n > 0) { |
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
132 |
String s = new String(b, 0, n, "UTF-8"); |
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
133 |
System.out.print(s); |
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
134 |
messagePrinted = true; |
12390
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
135 |
} |
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
136 |
} while (n > 0); |
24363
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
137 |
if (!messagePrinted) { |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
138 |
System.out.println("Command executed successfully"); |
33b869a8806b
8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents:
14856
diff
changeset
|
139 |
} |
12390
b77511960916
7154822: forward port fix for Bug 13645891 to JDK8 jcmd (1024 byte file size limit issue)
dbuck
parents:
11365
diff
changeset
|
140 |
} |
11365 | 141 |
} |
142 |
vm.detach(); |
|
143 |
} |
|
144 |
||
145 |
private static void listCounters(String pid) { |
|
146 |
// Code from JStat (can't call it directly since it does System.exit) |
|
147 |
VmIdentifier vmId = null; |
|
148 |
try { |
|
149 |
vmId = new VmIdentifier(pid); |
|
150 |
} catch (URISyntaxException e) { |
|
151 |
System.err.println("Malformed VM Identifier: " + pid); |
|
152 |
return; |
|
153 |
} |
|
154 |
try { |
|
155 |
MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId); |
|
156 |
MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, -1); |
|
157 |
JStatLogger logger = new JStatLogger(monitoredVm); |
|
158 |
logger.printSnapShot("\\w*", // all names |
|
159 |
new AscendingMonitorComparator(), // comparator |
|
160 |
false, // not verbose |
|
161 |
true, // show unsupported |
|
162 |
System.out); |
|
163 |
monitoredHost.detach(monitoredVm); |
|
164 |
} catch (MonitorException ex) { |
|
165 |
ex.printStackTrace(); |
|
166 |
} |
|
167 |
} |
|
168 |
||
169 |
/** |
|
170 |
* Class to compare two Monitor objects by name in ascending order. |
|
171 |
* (from jstat) |
|
172 |
*/ |
|
173 |
static class AscendingMonitorComparator implements Comparator<Monitor> { |
|
174 |
||
175 |
public int compare(Monitor m1, Monitor m2) { |
|
176 |
String name1 = m1.getName(); |
|
177 |
String name2 = m2.getName(); |
|
178 |
return name1.compareTo(name2); |
|
179 |
} |
|
180 |
} |
|
181 |
} |