jdk/src/windows/classes/sun/tools/attach/WindowsAttachProvider.java
author trims
Thu, 11 Nov 2010 23:30:49 -0800
changeset 7128 a683e80b9ca8
parent 5506 202f599c92aa
child 9840 376425f4ed3d
permissions -rw-r--r--
6997698: Bump the HS20 build number to 03 Summary: Update the HS20 build number to 03 Reviewed-by: jcoomes
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.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.attach.VirtualMachineDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.attach.AttachNotSupportedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class WindowsAttachProvider extends HotSpotAttachProvider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public WindowsAttachProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        String os = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        if (os.startsWith("Windows 9") || os.equals("Windows Me")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                "This provider is not supported on this version of Windows");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        String arch = System.getProperty("os.arch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        if (!arch.equals("x86") && !arch.equals("amd64")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                "This provider is not supported on this processor architecture");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public String name() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return "sun";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public String type() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return "windows";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public VirtualMachine attachVirtualMachine(String vmid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        throws AttachNotSupportedException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        checkAttachPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // AttachNotSupportedException will be thrown if the target VM can be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // to be not attachable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        testAttachable(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return new WindowsVirtualMachine(this, vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public List<VirtualMachineDescriptor> listVirtualMachines() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // If the temporary file system is secure then we use the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // implementation, otherwise we create a list of Windows processes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (isTempPathSecure()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            return super.listVirtualMachines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return listJavaProcesses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Returns true if the temporary file system supports security
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static boolean isTempPathSecure() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (!wasTempPathChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            synchronized (WindowsAttachProvider.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                if (!wasTempPathChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    // get the value of TMP/TEMP, ignoring UNC, and paths that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    // aren't absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    String temp = tempPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    if ((temp != null) && (temp.length() >= 3) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        (temp.charAt(1) == ':') && (temp.charAt(2) == '\\'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        // check if the volume supports security
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        long flags = volumeFlags(temp.substring(0, 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        isTempPathSecure = ((flags & FS_PERSISTENT_ACLS) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    wasTempPathChecked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return isTempPathSecure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // flag to indicate persistent ACLs are supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static final long FS_PERSISTENT_ACLS = 0x8L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // indicates if we've checked the temporary file system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static volatile boolean wasTempPathChecked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // indicates if the temporary file system is secure (only valid when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // wasTempPathChecked is true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static boolean isTempPathSecure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // returns the value of TMP/TEMP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static native String tempPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // returns the flags for the given volume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static native long volumeFlags(String volume);
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
     * Returns a list of virtual machine descriptors derived from an enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * of the process list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private List<VirtualMachineDescriptor> listJavaProcesses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // ensure that process status helper is loaded (psapi.dll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (!isProcessStatusHelperInitialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            synchronized (WindowsAttachProvider.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (!isProcessStatusHelperInitialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    initializeProcessStatusHelper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    isProcessStatusHelperInitialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        ArrayList<VirtualMachineDescriptor> list =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            new ArrayList<VirtualMachineDescriptor>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // Use localhost in the display name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        String host = "localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            host = InetAddress.getLocalHost().getHostName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } catch (UnknownHostException uhe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // Enumerate all processes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // For those processes that have loaded a library named "jvm.dll"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // then we attempt to attach. If we succeed then we have a 6.0+ VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int processes[] = new int[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int count = enumProcesses(processes, processes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        for (int i=0; i<count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (isLibraryLoadedByProcess("jvm.dll", processes[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                String pid = Integer.toString(processes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    new WindowsVirtualMachine(this, pid).detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    // FIXME - for now we don't have an appropriate display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    // name so we use pid@hostname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    String name = pid + "@" + host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    list.add(new HotSpotVirtualMachineDescriptor(this, pid, name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                } catch (AttachNotSupportedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // indicates if psapi.dll has been initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static volatile boolean isProcessStatusHelperInitialized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    // loads psapi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private static native void initializeProcessStatusHelper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // enumerates processes using psapi's EnumProcesses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private static native int enumProcesses(int[] processes, int max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    // indicates if a library of a given name has been loaded by a process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private static native boolean isLibraryLoadedByProcess(String library,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                                           int processId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // native functions in this library
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        System.loadLibrary("attach");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}