Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/common/ProcessArgumentMatcher.java
41159 views
1
/*
2
* Copyright (c) 2016, 2019, 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.common;
27
28
import java.net.URISyntaxException;
29
import java.util.ArrayList;
30
import java.util.Collection;
31
import java.util.List;
32
import java.util.stream.Collectors;
33
34
import com.sun.tools.attach.VirtualMachine;
35
import com.sun.tools.attach.VirtualMachineDescriptor;
36
37
import sun.jvmstat.monitor.MonitorException;
38
import sun.jvmstat.monitor.MonitoredHost;
39
import sun.jvmstat.monitor.MonitoredVm;
40
import sun.jvmstat.monitor.MonitoredVmUtil;
41
import sun.jvmstat.monitor.VmIdentifier;
42
43
/**
44
* Class for finding process matching a process argument,
45
* excluding tool it self and returning a list containing
46
* the process identifiers.
47
*/
48
public class ProcessArgumentMatcher {
49
private String matchClass;
50
private String singlePid;
51
52
public ProcessArgumentMatcher(String pidArg) {
53
if (pidArg == null || pidArg.isEmpty()) {
54
throw new IllegalArgumentException("Pid string is invalid");
55
}
56
if (pidArg.charAt(0) == '-') {
57
throw new IllegalArgumentException("Unrecognized " + pidArg);
58
}
59
try {
60
long pid = Long.parseLong(pidArg);
61
if (pid != 0) {
62
singlePid = String.valueOf(pid);
63
}
64
} catch (NumberFormatException nfe) {
65
matchClass = pidArg;
66
}
67
}
68
69
private static String getExcludeStringFrom(Class<?> excludeClass) {
70
if (excludeClass == null) {
71
return "";
72
}
73
Module m = excludeClass.getModule();
74
if (m.isNamed()) {
75
return m.getName() + "/" + excludeClass.getName();
76
}
77
return excludeClass.getName();
78
}
79
80
private static boolean check(VirtualMachineDescriptor vmd, String excludeClass, String partialMatch) {
81
82
// Try to get the main class name using (platform specific) ProcessHelper
83
String mainClass = ProcessHelper.getMainClass(vmd.id());
84
85
// If the main class name could not be retrieved by ProcessHelper, get it with the attach mechanism
86
if (mainClass == null) {
87
try {
88
VmIdentifier vmId = new VmIdentifier(vmd.id());
89
MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
90
MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, -1);
91
mainClass = MonitoredVmUtil.mainClass(monitoredVm, true);
92
monitoredHost.detach(monitoredVm);
93
} catch (NullPointerException npe) {
94
// There is a potential race, where a running java app is being
95
// queried, unfortunately the java app has shutdown after this
96
// method is started but before getMonitoredVM is called.
97
// If this is the case, then the /tmp/hsperfdata_xxx/pid file
98
// will have disappeared and we will get a NullPointerException.
99
// Handle this gracefully....
100
return false;
101
} catch (MonitorException | URISyntaxException e) {
102
return false;
103
}
104
}
105
106
107
if (excludeClass != null && mainClass.equals(excludeClass)) {
108
return false;
109
}
110
111
if (partialMatch != null && mainClass.indexOf(partialMatch) == -1) {
112
return false;
113
}
114
115
return true;
116
}
117
118
private static Collection<VirtualMachineDescriptor> getSingleVMD(String pid) {
119
Collection<VirtualMachineDescriptor> vids = new ArrayList<>();
120
List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
121
for (VirtualMachineDescriptor vmd : vmds) {
122
if (check(vmd, null, null)) {
123
if (pid.equals(vmd.id())) {
124
vids.add(vmd);
125
}
126
}
127
}
128
return vids;
129
}
130
131
private static Collection<VirtualMachineDescriptor> getVMDs(Class<?> excludeClass, String partialMatch) {
132
String excludeCls = getExcludeStringFrom(excludeClass);
133
Collection<VirtualMachineDescriptor> vids = new ArrayList<>();
134
List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
135
for (VirtualMachineDescriptor vmd : vmds) {
136
if (check(vmd, excludeCls, partialMatch)) {
137
vids.add(vmd);
138
}
139
}
140
return vids;
141
}
142
143
public Collection<VirtualMachineDescriptor> getVirtualMachineDescriptors() {
144
if (singlePid != null) {
145
return getSingleVMD(singlePid);
146
} else {
147
return getVMDs(null, matchClass);
148
}
149
}
150
151
public Collection<String> getVirtualMachinePids(Class<?> excludeClass) {
152
if (singlePid != null) {
153
// There is a bug in AttachProvider, when VM is debuggee-suspended it's not listed by the AttachProvider.
154
// If we are talking about a specific pid, just return it.
155
return List.of(singlePid);
156
} else {
157
return getVMDs(excludeClass, matchClass).stream().map(x -> {return x.id();}).collect(Collectors.toList());
158
}
159
}
160
161
}
162
163