Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/jcmd/JCmd.java
38918 views
1
/*
2
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
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
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;
32
import java.util.ArrayList;
33
import java.util.Comparator;
34
import java.net.URISyntaxException;
35
36
import com.sun.tools.attach.AttachOperationFailedException;
37
import com.sun.tools.attach.VirtualMachine;
38
import com.sun.tools.attach.VirtualMachineDescriptor;
39
import com.sun.tools.attach.AgentLoadException;
40
import com.sun.tools.attach.AttachNotSupportedException;
41
42
import sun.tools.attach.HotSpotVirtualMachine;
43
import sun.tools.jstat.JStatLogger;
44
import sun.jvmstat.monitor.Monitor;
45
import sun.jvmstat.monitor.MonitoredHost;
46
import sun.jvmstat.monitor.MonitoredVm;
47
import sun.jvmstat.monitor.MonitoredVmUtil;
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();
65
System.exit(1);
66
}
67
68
if (arg.isListProcesses()) {
69
List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
70
for (VirtualMachineDescriptor vmd : vmds) {
71
System.out.println(vmd.id() + " " + vmd.displayName());
72
}
73
System.exit(0);
74
}
75
76
List<String> pids = new ArrayList<String>();
77
if (arg.getPid() == 0) {
78
// find all VMs
79
List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
80
for (VirtualMachineDescriptor vmd : vmds) {
81
if (!isJCmdProcess(vmd)) {
82
pids.add(vmd.id());
83
}
84
}
85
} else if (arg.getProcessSubstring() != null) {
86
// use the partial class-name match
87
List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
88
for (VirtualMachineDescriptor vmd : vmds) {
89
if (isJCmdProcess(vmd)) {
90
continue;
91
}
92
try {
93
String mainClass = getMainClass(vmd);
94
if (mainClass != null
95
&& mainClass.indexOf(arg.getProcessSubstring()) != -1) {
96
pids.add(vmd.id());
97
}
98
} catch (MonitorException|URISyntaxException e) {
99
if (e.getMessage() != null) {
100
System.err.println(e.getMessage());
101
} else {
102
Throwable cause = e.getCause();
103
if ((cause != null) && (cause.getMessage() != null)) {
104
System.err.println(cause.getMessage());
105
} else {
106
e.printStackTrace();
107
}
108
}
109
}
110
}
111
if (pids.isEmpty()) {
112
System.err.println("Could not find any processes matching : '"
113
+ arg.getProcessSubstring() + "'");
114
System.exit(1);
115
}
116
} else if (arg.getPid() == -1) {
117
System.err.println("Invalid pid specified");
118
System.exit(1);
119
} else {
120
// Use the found pid
121
pids.add(arg.getPid() + "");
122
}
123
124
boolean success = true;
125
for (String pid : pids) {
126
System.out.println(pid + ":");
127
if (arg.isListCounters()) {
128
listCounters(pid);
129
} else {
130
try {
131
executeCommandForPid(pid, arg.getCommand());
132
} catch(AttachOperationFailedException ex) {
133
System.err.println(ex.getMessage());
134
success = false;
135
} catch(Exception ex) {
136
ex.printStackTrace();
137
success = false;
138
}
139
}
140
}
141
System.exit(success ? 0 : 1);
142
}
143
144
private static void executeCommandForPid(String pid, String command)
145
throws AttachNotSupportedException, IOException,
146
UnsupportedEncodingException {
147
VirtualMachine vm = VirtualMachine.attach(pid);
148
149
// Cast to HotSpotVirtualMachine as this is an
150
// implementation specific method.
151
HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
152
String lines[] = command.split("\\n");
153
for (String line : lines) {
154
if (line.trim().equals("stop")) {
155
break;
156
}
157
try (InputStream in = hvm.executeJCmd(line);) {
158
// read to EOF and just print output
159
byte b[] = new byte[256];
160
int n;
161
boolean messagePrinted = false;
162
do {
163
n = in.read(b);
164
if (n > 0) {
165
String s = new String(b, 0, n, "UTF-8");
166
System.out.print(s);
167
messagePrinted = true;
168
}
169
} while (n > 0);
170
if (!messagePrinted) {
171
System.out.println("Command executed successfully");
172
}
173
}
174
}
175
vm.detach();
176
}
177
178
private static void listCounters(String pid) {
179
// Code from JStat (can't call it directly since it does System.exit)
180
VmIdentifier vmId = null;
181
try {
182
vmId = new VmIdentifier(pid);
183
} catch (URISyntaxException e) {
184
System.err.println("Malformed VM Identifier: " + pid);
185
return;
186
}
187
try {
188
MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
189
MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, -1);
190
JStatLogger logger = new JStatLogger(monitoredVm);
191
logger.printSnapShot("\\w*", // all names
192
new AscendingMonitorComparator(), // comparator
193
false, // not verbose
194
true, // show unsupported
195
System.out);
196
monitoredHost.detach(monitoredVm);
197
} catch (MonitorException ex) {
198
ex.printStackTrace();
199
}
200
}
201
202
private static boolean isJCmdProcess(VirtualMachineDescriptor vmd) {
203
try {
204
String mainClass = getMainClass(vmd);
205
return mainClass != null && mainClass.equals(JCmd.class.getName());
206
} catch (URISyntaxException|MonitorException ex) {
207
return false;
208
}
209
}
210
211
private static String getMainClass(VirtualMachineDescriptor vmd)
212
throws URISyntaxException, MonitorException {
213
try {
214
String mainClass = null;
215
VmIdentifier vmId = new VmIdentifier(vmd.id());
216
MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
217
MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, -1);
218
mainClass = MonitoredVmUtil.mainClass(monitoredVm, true);
219
monitoredHost.detach(monitoredVm);
220
return mainClass;
221
} catch(NullPointerException e) {
222
// There is a potential race, where a running java app is being
223
// queried, unfortunately the java app has shutdown after this
224
// method is started but before getMonitoredVM is called.
225
// If this is the case, then the /tmp/hsperfdata_xxx/pid file
226
// will have disappeared and we will get a NullPointerException.
227
// Handle this gracefully....
228
return null;
229
}
230
}
231
232
/**
233
* Class to compare two Monitor objects by name in ascending order.
234
* (from jstat)
235
*/
236
static class AscendingMonitorComparator implements Comparator<Monitor> {
237
238
public int compare(Monitor m1, Monitor m2) {
239
String name1 = m1.getName();
240
String name2 = m2.getName();
241
return name1.compareTo(name2);
242
}
243
}
244
}
245
246