Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/tools/attach/WindowsAttachProvider.java
32288 views
/*1* Copyright (c) 2005, 2011, 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.VirtualMachine;27import com.sun.tools.attach.VirtualMachineDescriptor;28import com.sun.tools.attach.AttachNotSupportedException;2930import java.util.ArrayList;31import java.util.List;32import java.io.IOException;33import java.net.InetAddress;34import java.net.UnknownHostException;3536public class WindowsAttachProvider extends HotSpotAttachProvider {3738public WindowsAttachProvider() {39String os = System.getProperty("os.name");40if (os.startsWith("Windows 9") || os.equals("Windows Me")) {41throw new RuntimeException(42"This provider is not supported on this version of Windows");43}44String arch = System.getProperty("os.arch");45if (!arch.equals("x86") && !arch.equals("amd64")) {46throw new RuntimeException(47"This provider is not supported on this processor architecture");48}49}5051public String name() {52return "sun";53}5455public String type() {56return "windows";57}5859public VirtualMachine attachVirtualMachine(String vmid)60throws AttachNotSupportedException, IOException61{62checkAttachPermission();6364// AttachNotSupportedException will be thrown if the target VM can be determined65// to be not attachable.66testAttachable(vmid);6768return new WindowsVirtualMachine(this, vmid);69}7071public List<VirtualMachineDescriptor> listVirtualMachines() {72// If the temporary file system is secure then we use the default73// implementation, otherwise we create a list of Windows processes.74if (isTempPathSecure()) {75return super.listVirtualMachines();76} else {77return listJavaProcesses();78}79}8081/**82* Returns true if the temporary file system supports security83*/84private static boolean isTempPathSecure() {85if (!wasTempPathChecked) {86synchronized (WindowsAttachProvider.class) {87if (!wasTempPathChecked) {88// get the value of TMP/TEMP, ignoring UNC, and paths that89// aren't absolute90String temp = tempPath();91if ((temp != null) && (temp.length() >= 3) &&92(temp.charAt(1) == ':') && (temp.charAt(2) == '\\'))93{94// check if the volume supports security95long flags = volumeFlags(temp.substring(0, 3));96isTempPathSecure = ((flags & FS_PERSISTENT_ACLS) != 0);97}98wasTempPathChecked = true;99}100}101}102103return isTempPathSecure;104}105106// flag to indicate persistent ACLs are supported107private static final long FS_PERSISTENT_ACLS = 0x8L;108109// indicates if we've checked the temporary file system110private static volatile boolean wasTempPathChecked;111112// indicates if the temporary file system is secure (only valid when113// wasTempPathChecked is true)114private static boolean isTempPathSecure;115116// returns the value of TMP/TEMP117private static native String tempPath();118119// returns the flags for the given volume120private static native long volumeFlags(String volume);121122123/**124* Returns a list of virtual machine descriptors derived from an enumeration125* of the process list.126*/127private List<VirtualMachineDescriptor> listJavaProcesses() {128ArrayList<VirtualMachineDescriptor> list =129new ArrayList<VirtualMachineDescriptor>();130131// Use localhost in the display name132String host = "localhost";133try {134host = InetAddress.getLocalHost().getHostName();135} catch (UnknownHostException uhe) {136// ignore137}138139// Enumerate all processes.140// For those processes that have loaded a library named "jvm.dll"141// then we attempt to attach. If we succeed then we have a 6.0+ VM.142int processes[] = new int[1024];143int count = enumProcesses(processes, processes.length);144for (int i=0; i<count; i++) {145if (isLibraryLoadedByProcess("jvm.dll", processes[i])) {146String pid = Integer.toString(processes[i]);147try {148new WindowsVirtualMachine(this, pid).detach();149150// FIXME - for now we don't have an appropriate display151// name so we use pid@hostname152String name = pid + "@" + host;153154list.add(new HotSpotVirtualMachineDescriptor(this, pid, name));155} catch (AttachNotSupportedException x) {156} catch (IOException ioe) {157}158}159}160161return list;162}163164// enumerates processes using psapi's EnumProcesses165private static native int enumProcesses(int[] processes, int max);166167// indicates if a library of a given name has been loaded by a process168private static native boolean isLibraryLoadedByProcess(String library,169int processId);170171172// native functions in this library173static {174System.loadLibrary("attach");175}176177}178179180