author | gromero |
Tue, 04 Sep 2018 11:46:23 -0400 | |
changeset 51629 | 78c7f0c7827d |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8197
diff
changeset
|
2 |
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
SecurityManager sm = System.getSecurityManager(); |
|
52 |
if (sm != null) { |
|
53 |
sm.checkPermission( |
|
54 |
new AttachPermission("attachVirtualMachine") |
|
55 |
); |
|
56 |
} |
|
57 |
} |
|
58 |
||
59 |
/* |
|
60 |
* This listVirtualMachines implementation is based on jvmstat. Can override |
|
61 |
* this in platform implementations when there is a more efficient mechanism |
|
62 |
* available. |
|
63 |
*/ |
|
64 |
public List<VirtualMachineDescriptor> listVirtualMachines() { |
|
65 |
ArrayList<VirtualMachineDescriptor> result = |
|
66 |
new ArrayList<VirtualMachineDescriptor>(); |
|
67 |
||
68 |
MonitoredHost host; |
|
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
69 |
Set<Integer> vms; |
2 | 70 |
try { |
71 |
host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null)); |
|
72 |
vms = host.activeVms(); |
|
73 |
} catch (Throwable t) { |
|
74 |
if (t instanceof ExceptionInInitializerError) { |
|
75 |
t = t.getCause(); |
|
76 |
} |
|
77 |
if (t instanceof ThreadDeath) { |
|
78 |
throw (ThreadDeath)t; |
|
79 |
} |
|
80 |
if (t instanceof SecurityException) { |
|
81 |
return result; |
|
82 |
} |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
9035
diff
changeset
|
83 |
throw new InternalError(t); // shouldn't happen |
2 | 84 |
} |
85 |
||
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
86 |
for (Integer vmid: vms) { |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
87 |
String pid = vmid.toString(); |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
88 |
String name = pid; // default to pid if name not available |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
89 |
boolean isAttachable = false; |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
90 |
MonitoredVm mvm = null; |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
91 |
try { |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
92 |
mvm = host.getMonitoredVm(new VmIdentifier(pid)); |
2 | 93 |
try { |
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
94 |
isAttachable = MonitoredVmUtil.isAttachable(mvm); |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
95 |
// use the command line as the display name |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
96 |
name = MonitoredVmUtil.commandLine(mvm); |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
97 |
} catch (Exception e) { |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
98 |
} |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
99 |
if (isAttachable) { |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
100 |
result.add(new HotSpotVirtualMachineDescriptor(this, pid, name)); |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
101 |
} |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
102 |
} catch (Throwable t) { |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
103 |
if (t instanceof ThreadDeath) { |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
104 |
throw (ThreadDeath)t; |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
105 |
} |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
106 |
} finally { |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
107 |
if (mvm != null) { |
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
10419
diff
changeset
|
108 |
mvm.detach(); |
2 | 109 |
} |
110 |
} |
|
111 |
} |
|
112 |
return result; |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Test if a VM is attachable. If it's not attachable, |
|
117 |
* an AttachNotSupportedException will be thrown. For example, |
|
118 |
* 1.4.2 or 5.0 VM are not attachable. There are cases that |
|
119 |
* we can't determine if a VM is attachable or not and this method |
|
120 |
* will just return. |
|
121 |
* |
|
122 |
* This method uses the jvmstat counter to determine if a VM |
|
123 |
* is attachable. If the target VM does not have a jvmstat |
|
124 |
* share memory buffer, this method returns. |
|
125 |
* |
|
126 |
* @exception AttachNotSupportedException if it's not attachable |
|
127 |
*/ |
|
128 |
void testAttachable(String id) throws AttachNotSupportedException { |
|
129 |
MonitoredVm mvm = null; |
|
130 |
try { |
|
131 |
VmIdentifier vmid = new VmIdentifier(id); |
|
132 |
MonitoredHost host = MonitoredHost.getMonitoredHost(vmid); |
|
133 |
mvm = host.getMonitoredVm(vmid); |
|
134 |
||
135 |
if (MonitoredVmUtil.isAttachable(mvm)) { |
|
136 |
// it's attachable; so return false |
|
137 |
return; |
|
138 |
} |
|
139 |
} catch (Throwable t) { |
|
140 |
if (t instanceof ThreadDeath) { |
|
141 |
ThreadDeath td = (ThreadDeath)t; |
|
142 |
throw td; |
|
143 |
} |
|
144 |
// we do not know what this id is |
|
145 |
return; |
|
146 |
} finally { |
|
147 |
if (mvm != null) { |
|
148 |
mvm.detach(); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
// we're sure it's not attachable; throw exception |
|
8197 | 153 |
throw new AttachNotSupportedException( |
154 |
"The VM does not support the attach mechanism"); |
|
2 | 155 |
} |
156 |
||
157 |
||
158 |
/** |
|
159 |
* A virtual machine descriptor to describe a HotSpot virtual machine. |
|
160 |
*/ |
|
161 |
static class HotSpotVirtualMachineDescriptor extends VirtualMachineDescriptor { |
|
162 |
HotSpotVirtualMachineDescriptor(AttachProvider provider, |
|
163 |
String id, |
|
164 |
String displayName) { |
|
165 |
super(provider, id, displayName); |
|
166 |
} |
|
167 |
||
168 |
public boolean isAttachable() { |
|
169 |
return true; |
|
170 |
} |
|
171 |
} |
|
172 |
} |