jdk/src/share/classes/sun/tools/attach/HotSpotAttachProvider.java
author alanb
Fri, 02 Dec 2011 17:37:30 +0000
changeset 11125 99b115114fa3
parent 10419 12c063b39232
permissions -rw-r--r--
7117357: Warnings in sun.instrument, tools and other sun.* classes Reviewed-by: lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
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;
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
    78
        Set<Integer> vms;
2
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
            }
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
    92
            throw new InternalError(t);          // shouldn't happen
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
    95
        for (Integer vmid: vms) {
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
    96
            String pid = vmid.toString();
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
    97
            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
    98
            boolean isAttachable = false;
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
    99
            MonitoredVm mvm = null;
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   100
            try {
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   101
                mvm = host.getMonitoredVm(new VmIdentifier(pid));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                try {
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   103
                    isAttachable = MonitoredVmUtil.isAttachable(mvm);
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   104
                    // use the command line as the display name
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   105
                    name =  MonitoredVmUtil.commandLine(mvm);
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   106
                } catch (Exception e) {
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   107
                }
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   108
                if (isAttachable) {
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   109
                    result.add(new HotSpotVirtualMachineDescriptor(this, pid, name));
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   110
                }
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   111
            } catch (Throwable t) {
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   112
                if (t instanceof ThreadDeath) {
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   113
                    throw (ThreadDeath)t;
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   114
                }
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   115
            } finally {
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   116
                if (mvm != null) {
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 10419
diff changeset
   117
                    mvm.detach();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Test if a VM is attachable. If it's not attachable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * an AttachNotSupportedException will be thrown. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * 1.4.2 or 5.0 VM are not attachable. There are cases that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * we can't determine if a VM is attachable or not and this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * will just return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * This method uses the jvmstat counter to determine if a VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * is attachable. If the target VM does not have a jvmstat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * share memory buffer, this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @exception AttachNotSupportedException if it's not attachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    void testAttachable(String id) throws AttachNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        MonitoredVm mvm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            VmIdentifier vmid = new VmIdentifier(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            MonitoredHost host = MonitoredHost.getMonitoredHost(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            mvm = host.getMonitoredVm(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (MonitoredVmUtil.isAttachable(mvm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // it's attachable; so return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (t instanceof ThreadDeath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                ThreadDeath td = (ThreadDeath)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                throw td;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // we do not know what this id is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (mvm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                mvm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // we're sure it's not attachable; throw exception
8197
e45f21c2a40b 7016724: Remove sun.jkernel.* classes in JDK 7
herrick
parents: 5506
diff changeset
   162
        throw new AttachNotSupportedException(
e45f21c2a40b 7016724: Remove sun.jkernel.* classes in JDK 7
herrick
parents: 5506
diff changeset
   163
                  "The VM does not support the attach mechanism");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * A virtual machine descriptor to describe a HotSpot virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    static class HotSpotVirtualMachineDescriptor extends VirtualMachineDescriptor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        HotSpotVirtualMachineDescriptor(AttachProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                        String id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                        String displayName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            super(provider, id, displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        public boolean isAttachable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}