jdk/src/jdk.attach/share/classes/sun/tools/attach/HotSpotAttachProvider.java
author sla
Mon, 08 Sep 2014 14:46:51 +0200
changeset 26453 eb89fa61bc70
parent 25859 3317bb8137f4
permissions -rw-r--r--
8057776: Misc cleanups of the attach code Reviewed-by: alanb, dfuchs, jbachorik
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.AttachPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.attach.AttachNotSupportedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.attach.spi.AttachProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.jvmstat.monitor.HostIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.jvmstat.monitor.MonitoredHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.jvmstat.monitor.MonitoredVm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.jvmstat.monitor.MonitoredVmUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.jvmstat.monitor.VmIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Platform specific provider implementations extend this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public abstract class HotSpotAttachProvider extends AttachProvider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public HotSpotAttachProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public void checkAttachPermission() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            sm.checkPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                new AttachPermission("attachVirtualMachine")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * This listVirtualMachines implementation is based on jvmstat. Can override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * this in platform implementations when there is a more efficient mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public List<VirtualMachineDescriptor> listVirtualMachines() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        ArrayList<VirtualMachineDescriptor> result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            new ArrayList<VirtualMachineDescriptor>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            vms = host.activeVms();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (t instanceof ExceptionInInitializerError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                t = t.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (t instanceof ThreadDeath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                throw (ThreadDeath)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (t instanceof SecurityException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Test if a VM is attachable. If it's not attachable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * an AttachNotSupportedException will be thrown. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * 1.4.2 or 5.0 VM are not attachable. There are cases that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * we can't determine if a VM is attachable or not and this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * will just return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * This method uses the jvmstat counter to determine if a VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * is attachable. If the target VM does not have a jvmstat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * share memory buffer, this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @exception AttachNotSupportedException if it's not attachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    void testAttachable(String id) throws AttachNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        MonitoredVm mvm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            VmIdentifier vmid = new VmIdentifier(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            MonitoredHost host = MonitoredHost.getMonitoredHost(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            mvm = host.getMonitoredVm(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (MonitoredVmUtil.isAttachable(mvm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                // it's attachable; so return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (t instanceof ThreadDeath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                ThreadDeath td = (ThreadDeath)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                throw td;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            // we do not know what this id is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (mvm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                mvm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // we're sure it's not attachable; throw exception
8197
e45f21c2a40b 7016724: Remove sun.jkernel.* classes in JDK 7
herrick
parents: 5506
diff changeset
   153
        throw new AttachNotSupportedException(
e45f21c2a40b 7016724: Remove sun.jkernel.* classes in JDK 7
herrick
parents: 5506
diff changeset
   154
                  "The VM does not support the attach mechanism");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * A virtual machine descriptor to describe a HotSpot virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    static class HotSpotVirtualMachineDescriptor extends VirtualMachineDescriptor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        HotSpotVirtualMachineDescriptor(AttachProvider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                        String id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                        String displayName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            super(provider, id, displayName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        public boolean isAttachable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}