Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.attach/share/classes/sun/tools/attach/HotSpotAttachProvider.java
41161 views
1
/*
2
* Copyright (c) 2005, 2021, 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
package sun.tools.attach;
26
27
import com.sun.tools.attach.VirtualMachineDescriptor;
28
import com.sun.tools.attach.AttachPermission;
29
import com.sun.tools.attach.AttachNotSupportedException;
30
import com.sun.tools.attach.spi.AttachProvider;
31
32
import java.util.List;
33
import java.util.ArrayList;
34
import java.util.Set;
35
36
import sun.jvmstat.monitor.HostIdentifier;
37
import sun.jvmstat.monitor.MonitoredHost;
38
import sun.jvmstat.monitor.MonitoredVm;
39
import sun.jvmstat.monitor.MonitoredVmUtil;
40
import sun.jvmstat.monitor.VmIdentifier;
41
42
/*
43
* Platform specific provider implementations extend this
44
*/
45
public abstract class HotSpotAttachProvider extends AttachProvider {
46
47
public HotSpotAttachProvider() {
48
}
49
50
public void checkAttachPermission() {
51
@SuppressWarnings("removal")
52
SecurityManager sm = System.getSecurityManager();
53
if (sm != null) {
54
sm.checkPermission(
55
new AttachPermission("attachVirtualMachine")
56
);
57
}
58
}
59
60
/*
61
* This listVirtualMachines implementation is based on jvmstat. Can override
62
* this in platform implementations when there is a more efficient mechanism
63
* available.
64
*/
65
public List<VirtualMachineDescriptor> listVirtualMachines() {
66
ArrayList<VirtualMachineDescriptor> result =
67
new ArrayList<VirtualMachineDescriptor>();
68
69
MonitoredHost host;
70
Set<Integer> vms;
71
try {
72
host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));
73
vms = host.activeVms();
74
} catch (Throwable t) {
75
if (t instanceof ExceptionInInitializerError) {
76
t = t.getCause();
77
}
78
if (t instanceof ThreadDeath) {
79
throw (ThreadDeath)t;
80
}
81
if (t instanceof SecurityException) {
82
return result;
83
}
84
throw new InternalError(t); // shouldn't happen
85
}
86
87
for (Integer vmid: vms) {
88
String pid = vmid.toString();
89
String name = pid; // default to pid if name not available
90
boolean isAttachable = false;
91
MonitoredVm mvm = null;
92
try {
93
mvm = host.getMonitoredVm(new VmIdentifier(pid));
94
try {
95
isAttachable = MonitoredVmUtil.isAttachable(mvm);
96
// use the command line as the display name
97
name = MonitoredVmUtil.commandLine(mvm);
98
} catch (Exception e) {
99
}
100
if (isAttachable) {
101
result.add(new HotSpotVirtualMachineDescriptor(this, pid, name));
102
}
103
} catch (Throwable t) {
104
if (t instanceof ThreadDeath) {
105
throw (ThreadDeath)t;
106
}
107
} finally {
108
if (mvm != null) {
109
mvm.detach();
110
}
111
}
112
}
113
return result;
114
}
115
116
/**
117
* Test if a VM is attachable. If it's not attachable,
118
* an AttachNotSupportedException will be thrown. For example,
119
* 1.4.2 or 5.0 VM are not attachable. There are cases that
120
* we can't determine if a VM is attachable or not and this method
121
* will just return.
122
*
123
* This method uses the jvmstat counter to determine if a VM
124
* is attachable. If the target VM does not have a jvmstat
125
* share memory buffer, this method returns.
126
*
127
* @exception AttachNotSupportedException if it's not attachable
128
*/
129
void testAttachable(String id) throws AttachNotSupportedException {
130
MonitoredVm mvm = null;
131
try {
132
VmIdentifier vmid = new VmIdentifier(id);
133
MonitoredHost host = MonitoredHost.getMonitoredHost(vmid);
134
mvm = host.getMonitoredVm(vmid);
135
136
if (MonitoredVmUtil.isAttachable(mvm)) {
137
// it's attachable; so return false
138
return;
139
}
140
} catch (Throwable t) {
141
if (t instanceof ThreadDeath) {
142
ThreadDeath td = (ThreadDeath)t;
143
throw td;
144
}
145
// we do not know what this id is
146
return;
147
} finally {
148
if (mvm != null) {
149
mvm.detach();
150
}
151
}
152
153
// we're sure it's not attachable; throw exception
154
throw new AttachNotSupportedException(
155
"The VM does not support the attach mechanism");
156
}
157
158
159
/**
160
* A virtual machine descriptor to describe a HotSpot virtual machine.
161
*/
162
static class HotSpotVirtualMachineDescriptor extends VirtualMachineDescriptor {
163
HotSpotVirtualMachineDescriptor(AttachProvider provider,
164
String id,
165
String displayName) {
166
super(provider, id, displayName);
167
}
168
169
public boolean isAttachable() {
170
return true;
171
}
172
}
173
}
174
175