Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/attach/HotSpotAttachProvider.java
38918 views
/*1* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.tools.attach;2526import com.sun.tools.attach.VirtualMachineDescriptor;27import com.sun.tools.attach.VirtualMachine;28import com.sun.tools.attach.AttachPermission;29import com.sun.tools.attach.AttachNotSupportedException;30import com.sun.tools.attach.spi.AttachProvider;3132import java.io.IOException;33import java.util.List;34import java.util.Iterator;35import java.util.ArrayList;36import java.util.Set;37import java.net.URISyntaxException;3839import sun.jvmstat.monitor.HostIdentifier;40import sun.jvmstat.monitor.Monitor;41import sun.jvmstat.monitor.MonitoredHost;42import sun.jvmstat.monitor.MonitoredVm;43import sun.jvmstat.monitor.MonitoredVmUtil;44import sun.jvmstat.monitor.VmIdentifier;45import sun.jvmstat.monitor.MonitorException;4647/*48* Platform specific provider implementations extend this49*/50public abstract class HotSpotAttachProvider extends AttachProvider {5152// perf count name for the JVM version53private static final String JVM_VERSION = "java.property.java.vm.version";5455public HotSpotAttachProvider() {56}5758public void checkAttachPermission() {59SecurityManager sm = System.getSecurityManager();60if (sm != null) {61sm.checkPermission(62new AttachPermission("attachVirtualMachine")63);64}65}6667/*68* This listVirtualMachines implementation is based on jvmstat. Can override69* this in platform implementations when there is a more efficient mechanism70* available.71*/72public List<VirtualMachineDescriptor> listVirtualMachines() {73ArrayList<VirtualMachineDescriptor> result =74new ArrayList<VirtualMachineDescriptor>();7576MonitoredHost host;77Set<Integer> vms;78try {79host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));80vms = host.activeVms();81} catch (Throwable t) {82if (t instanceof ExceptionInInitializerError) {83t = t.getCause();84}85if (t instanceof ThreadDeath) {86throw (ThreadDeath)t;87}88if (t instanceof SecurityException) {89return result;90}91throw new InternalError(t); // shouldn't happen92}9394for (Integer vmid: vms) {95String pid = vmid.toString();96String name = pid; // default to pid if name not available97boolean isAttachable = false;98MonitoredVm mvm = null;99try {100mvm = host.getMonitoredVm(new VmIdentifier(pid));101try {102isAttachable = MonitoredVmUtil.isAttachable(mvm);103// use the command line as the display name104name = MonitoredVmUtil.commandLine(mvm);105} catch (Exception e) {106}107if (isAttachable) {108result.add(new HotSpotVirtualMachineDescriptor(this, pid, name));109}110} catch (Throwable t) {111if (t instanceof ThreadDeath) {112throw (ThreadDeath)t;113}114} finally {115if (mvm != null) {116mvm.detach();117}118}119}120return result;121}122123/**124* Test if a VM is attachable. If it's not attachable,125* an AttachNotSupportedException will be thrown. For example,126* 1.4.2 or 5.0 VM are not attachable. There are cases that127* we can't determine if a VM is attachable or not and this method128* will just return.129*130* This method uses the jvmstat counter to determine if a VM131* is attachable. If the target VM does not have a jvmstat132* share memory buffer, this method returns.133*134* @exception AttachNotSupportedException if it's not attachable135*/136void testAttachable(String id) throws AttachNotSupportedException {137MonitoredVm mvm = null;138try {139VmIdentifier vmid = new VmIdentifier(id);140MonitoredHost host = MonitoredHost.getMonitoredHost(vmid);141mvm = host.getMonitoredVm(vmid);142143if (MonitoredVmUtil.isAttachable(mvm)) {144// it's attachable; so return false145return;146}147} catch (Throwable t) {148if (t instanceof ThreadDeath) {149ThreadDeath td = (ThreadDeath)t;150throw td;151}152// we do not know what this id is153return;154} finally {155if (mvm != null) {156mvm.detach();157}158}159160// we're sure it's not attachable; throw exception161throw new AttachNotSupportedException(162"The VM does not support the attach mechanism");163}164165166/**167* A virtual machine descriptor to describe a HotSpot virtual machine.168*/169static class HotSpotVirtualMachineDescriptor extends VirtualMachineDescriptor {170HotSpotVirtualMachineDescriptor(AttachProvider provider,171String id,172String displayName) {173super(provider, id, displayName);174}175176public boolean isAttachable() {177return true;178}179}180}181182183