Path: blob/master/src/jdk.attach/share/classes/sun/tools/attach/HotSpotAttachProvider.java
41161 views
/*1* Copyright (c) 2005, 2021, 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.AttachPermission;28import com.sun.tools.attach.AttachNotSupportedException;29import com.sun.tools.attach.spi.AttachProvider;3031import java.util.List;32import java.util.ArrayList;33import java.util.Set;3435import sun.jvmstat.monitor.HostIdentifier;36import sun.jvmstat.monitor.MonitoredHost;37import sun.jvmstat.monitor.MonitoredVm;38import sun.jvmstat.monitor.MonitoredVmUtil;39import sun.jvmstat.monitor.VmIdentifier;4041/*42* Platform specific provider implementations extend this43*/44public abstract class HotSpotAttachProvider extends AttachProvider {4546public HotSpotAttachProvider() {47}4849public void checkAttachPermission() {50@SuppressWarnings("removal")51SecurityManager sm = System.getSecurityManager();52if (sm != null) {53sm.checkPermission(54new AttachPermission("attachVirtualMachine")55);56}57}5859/*60* This listVirtualMachines implementation is based on jvmstat. Can override61* this in platform implementations when there is a more efficient mechanism62* available.63*/64public List<VirtualMachineDescriptor> listVirtualMachines() {65ArrayList<VirtualMachineDescriptor> result =66new ArrayList<VirtualMachineDescriptor>();6768MonitoredHost host;69Set<Integer> vms;70try {71host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));72vms = host.activeVms();73} catch (Throwable t) {74if (t instanceof ExceptionInInitializerError) {75t = t.getCause();76}77if (t instanceof ThreadDeath) {78throw (ThreadDeath)t;79}80if (t instanceof SecurityException) {81return result;82}83throw new InternalError(t); // shouldn't happen84}8586for (Integer vmid: vms) {87String pid = vmid.toString();88String name = pid; // default to pid if name not available89boolean isAttachable = false;90MonitoredVm mvm = null;91try {92mvm = host.getMonitoredVm(new VmIdentifier(pid));93try {94isAttachable = MonitoredVmUtil.isAttachable(mvm);95// use the command line as the display name96name = MonitoredVmUtil.commandLine(mvm);97} catch (Exception e) {98}99if (isAttachable) {100result.add(new HotSpotVirtualMachineDescriptor(this, pid, name));101}102} catch (Throwable t) {103if (t instanceof ThreadDeath) {104throw (ThreadDeath)t;105}106} finally {107if (mvm != null) {108mvm.detach();109}110}111}112return result;113}114115/**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 that119* we can't determine if a VM is attachable or not and this method120* will just return.121*122* This method uses the jvmstat counter to determine if a VM123* is attachable. If the target VM does not have a jvmstat124* share memory buffer, this method returns.125*126* @exception AttachNotSupportedException if it's not attachable127*/128void testAttachable(String id) throws AttachNotSupportedException {129MonitoredVm mvm = null;130try {131VmIdentifier vmid = new VmIdentifier(id);132MonitoredHost host = MonitoredHost.getMonitoredHost(vmid);133mvm = host.getMonitoredVm(vmid);134135if (MonitoredVmUtil.isAttachable(mvm)) {136// it's attachable; so return false137return;138}139} catch (Throwable t) {140if (t instanceof ThreadDeath) {141ThreadDeath td = (ThreadDeath)t;142throw td;143}144// we do not know what this id is145return;146} finally {147if (mvm != null) {148mvm.detach();149}150}151152// we're sure it's not attachable; throw exception153throw new AttachNotSupportedException(154"The VM does not support the attach mechanism");155}156157158/**159* A virtual machine descriptor to describe a HotSpot virtual machine.160*/161static class HotSpotVirtualMachineDescriptor extends VirtualMachineDescriptor {162HotSpotVirtualMachineDescriptor(AttachProvider provider,163String id,164String displayName) {165super(provider, id, displayName);166}167168public boolean isAttachable() {169return true;170}171}172}173174175