jdk/src/share/classes/sun/tools/attach/HotSpotAttachProvider.java
author herrick
Wed, 09 Feb 2011 09:19:33 -0500
changeset 8197 e45f21c2a40b
parent 5506 202f599c92aa
child 9035 1255eb81cc2f
permissions -rw-r--r--
7016724: Remove sun.jkernel.* classes in JDK 7 Summary: Remove sun.jkernel.* classes in JDK 7 Reviewed-by: ohair, alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.tools.attach;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import com.sun.tools.attach.VirtualMachineDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.attach.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.attach.AttachPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.attach.AttachNotSupportedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.attach.spi.AttachProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.URISyntaxException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.jvmstat.monitor.HostIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.jvmstat.monitor.Monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.jvmstat.monitor.MonitoredHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.jvmstat.monitor.MonitoredVm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.jvmstat.monitor.MonitoredVmUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.jvmstat.monitor.VmIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.jvmstat.monitor.MonitorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Platform specific provider implementations extend this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public abstract class HotSpotAttachProvider extends AttachProvider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // perf count name for the JVM version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final String JVM_VERSION = "java.property.java.vm.version";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public HotSpotAttachProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public void checkAttachPermission() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            sm.checkPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                new AttachPermission("attachVirtualMachine")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * This listVirtualMachines implementation is based on jvmstat. Can override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * this in platform implementations when there is a more efficient mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public List<VirtualMachineDescriptor> listVirtualMachines() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        ArrayList<VirtualMachineDescriptor> result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            new ArrayList<VirtualMachineDescriptor>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        MonitoredHost host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Set vms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            vms = host.activeVms();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (t instanceof ExceptionInInitializerError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                t = t.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (t instanceof ThreadDeath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                throw (ThreadDeath)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (t instanceof SecurityException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new InternalError();          // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        for (Object vmid: vms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (vmid instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                String pid = vmid.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                String name = pid;      // default to pid if name not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                boolean isAttachable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                MonitoredVm mvm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    mvm = host.getMonitoredVm(new VmIdentifier(pid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        isAttachable = MonitoredVmUtil.isAttachable(mvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        // use the command line as the display name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        name =  MonitoredVmUtil.commandLine(mvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    if (isAttachable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        result.add(new HotSpotVirtualMachineDescriptor(this, pid, name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    if (t instanceof ThreadDeath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        throw (ThreadDeath)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    if (mvm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        mvm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Test if a VM is attachable. If it's not attachable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * an AttachNotSupportedException will be thrown. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * 1.4.2 or 5.0 VM are not attachable. There are cases that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * we can't determine if a VM is attachable or not and this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * will just return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * This method uses the jvmstat counter to determine if a VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * is attachable. If the target VM does not have a jvmstat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * share memory buffer, this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @exception AttachNotSupportedException if it's not attachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    void testAttachable(String id) throws AttachNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        MonitoredVm mvm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            VmIdentifier vmid = new VmIdentifier(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            MonitoredHost host = MonitoredHost.getMonitoredHost(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            mvm = host.getMonitoredVm(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (MonitoredVmUtil.isAttachable(mvm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                // it's attachable; so return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (t instanceof ThreadDeath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                ThreadDeath td = (ThreadDeath)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                throw td;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            // we do not know what this id is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (mvm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                mvm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // we're sure it's not attachable; throw exception
8197
e45f21c2a40b 7016724: Remove sun.jkernel.* classes in JDK 7
herrick
parents: 5506
diff changeset
   164
        throw new AttachNotSupportedException(
e45f21c2a40b 7016724: Remove sun.jkernel.* classes in JDK 7
herrick
parents: 5506
diff changeset
   165
                  "The VM does not support the attach mechanism");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * A virtual machine descriptor to describe a HotSpot virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    static class HotSpotVirtualMachineDescriptor extends VirtualMachineDescriptor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        HotSpotVirtualMachineDescriptor(AttachProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                        String id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                        String displayName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            super(provider, id, displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        public boolean isAttachable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}