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