feat(jdk8): move files to new folder to avoid resources compiled.
This commit is contained in:
181
jdkSrc/jdk8/sun/tools/attach/HotSpotAttachProvider.java
Normal file
181
jdkSrc/jdk8/sun/tools/attach/HotSpotAttachProvider.java
Normal file
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package sun.tools.attach;
|
||||
|
||||
import com.sun.tools.attach.VirtualMachineDescriptor;
|
||||
import com.sun.tools.attach.VirtualMachine;
|
||||
import com.sun.tools.attach.AttachPermission;
|
||||
import com.sun.tools.attach.AttachNotSupportedException;
|
||||
import com.sun.tools.attach.spi.AttachProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Iterator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import sun.jvmstat.monitor.HostIdentifier;
|
||||
import sun.jvmstat.monitor.Monitor;
|
||||
import sun.jvmstat.monitor.MonitoredHost;
|
||||
import sun.jvmstat.monitor.MonitoredVm;
|
||||
import sun.jvmstat.monitor.MonitoredVmUtil;
|
||||
import sun.jvmstat.monitor.VmIdentifier;
|
||||
import sun.jvmstat.monitor.MonitorException;
|
||||
|
||||
/*
|
||||
* Platform specific provider implementations extend this
|
||||
*/
|
||||
public abstract class HotSpotAttachProvider extends AttachProvider {
|
||||
|
||||
// perf count name for the JVM version
|
||||
private static final String JVM_VERSION = "java.property.java.vm.version";
|
||||
|
||||
public HotSpotAttachProvider() {
|
||||
}
|
||||
|
||||
public void checkAttachPermission() {
|
||||
SecurityManager sm = System.getSecurityManager();
|
||||
if (sm != null) {
|
||||
sm.checkPermission(
|
||||
new AttachPermission("attachVirtualMachine")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This listVirtualMachines implementation is based on jvmstat. Can override
|
||||
* this in platform implementations when there is a more efficient mechanism
|
||||
* available.
|
||||
*/
|
||||
public List<VirtualMachineDescriptor> listVirtualMachines() {
|
||||
ArrayList<VirtualMachineDescriptor> result =
|
||||
new ArrayList<VirtualMachineDescriptor>();
|
||||
|
||||
MonitoredHost host;
|
||||
Set<Integer> vms;
|
||||
try {
|
||||
host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));
|
||||
vms = host.activeVms();
|
||||
} catch (Throwable t) {
|
||||
if (t instanceof ExceptionInInitializerError) {
|
||||
t = t.getCause();
|
||||
}
|
||||
if (t instanceof ThreadDeath) {
|
||||
throw (ThreadDeath)t;
|
||||
}
|
||||
if (t instanceof SecurityException) {
|
||||
return result;
|
||||
}
|
||||
throw new InternalError(t); // shouldn't happen
|
||||
}
|
||||
|
||||
for (Integer vmid: vms) {
|
||||
String pid = vmid.toString();
|
||||
String name = pid; // default to pid if name not available
|
||||
boolean isAttachable = false;
|
||||
MonitoredVm mvm = null;
|
||||
try {
|
||||
mvm = host.getMonitoredVm(new VmIdentifier(pid));
|
||||
try {
|
||||
isAttachable = MonitoredVmUtil.isAttachable(mvm);
|
||||
// use the command line as the display name
|
||||
name = MonitoredVmUtil.commandLine(mvm);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
if (isAttachable) {
|
||||
result.add(new HotSpotVirtualMachineDescriptor(this, pid, name));
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
if (t instanceof ThreadDeath) {
|
||||
throw (ThreadDeath)t;
|
||||
}
|
||||
} finally {
|
||||
if (mvm != null) {
|
||||
mvm.detach();
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a VM is attachable. If it's not attachable,
|
||||
* an AttachNotSupportedException will be thrown. For example,
|
||||
* 1.4.2 or 5.0 VM are not attachable. There are cases that
|
||||
* we can't determine if a VM is attachable or not and this method
|
||||
* will just return.
|
||||
*
|
||||
* This method uses the jvmstat counter to determine if a VM
|
||||
* is attachable. If the target VM does not have a jvmstat
|
||||
* share memory buffer, this method returns.
|
||||
*
|
||||
* @exception AttachNotSupportedException if it's not attachable
|
||||
*/
|
||||
void testAttachable(String id) throws AttachNotSupportedException {
|
||||
MonitoredVm mvm = null;
|
||||
try {
|
||||
VmIdentifier vmid = new VmIdentifier(id);
|
||||
MonitoredHost host = MonitoredHost.getMonitoredHost(vmid);
|
||||
mvm = host.getMonitoredVm(vmid);
|
||||
|
||||
if (MonitoredVmUtil.isAttachable(mvm)) {
|
||||
// it's attachable; so return false
|
||||
return;
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
if (t instanceof ThreadDeath) {
|
||||
ThreadDeath td = (ThreadDeath)t;
|
||||
throw td;
|
||||
}
|
||||
// we do not know what this id is
|
||||
return;
|
||||
} finally {
|
||||
if (mvm != null) {
|
||||
mvm.detach();
|
||||
}
|
||||
}
|
||||
|
||||
// we're sure it's not attachable; throw exception
|
||||
throw new AttachNotSupportedException(
|
||||
"The VM does not support the attach mechanism");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A virtual machine descriptor to describe a HotSpot virtual machine.
|
||||
*/
|
||||
static class HotSpotVirtualMachineDescriptor extends VirtualMachineDescriptor {
|
||||
HotSpotVirtualMachineDescriptor(AttachProvider provider,
|
||||
String id,
|
||||
String displayName) {
|
||||
super(provider, id, displayName);
|
||||
}
|
||||
|
||||
public boolean isAttachable() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
358
jdkSrc/jdk8/sun/tools/attach/HotSpotVirtualMachine.java
Normal file
358
jdkSrc/jdk8/sun/tools/attach/HotSpotVirtualMachine.java
Normal file
@@ -0,0 +1,358 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package sun.tools.attach;
|
||||
|
||||
import com.sun.tools.attach.VirtualMachine;
|
||||
import com.sun.tools.attach.AgentLoadException;
|
||||
import com.sun.tools.attach.AgentInitializationException;
|
||||
import com.sun.tools.attach.spi.AttachProvider;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/*
|
||||
* The HotSpot implementation of com.sun.tools.attach.VirtualMachine.
|
||||
*/
|
||||
|
||||
public abstract class HotSpotVirtualMachine extends VirtualMachine {
|
||||
|
||||
HotSpotVirtualMachine(AttachProvider provider, String id) {
|
||||
super(provider, id);
|
||||
}
|
||||
|
||||
/*
|
||||
* Load agent library
|
||||
* If isAbsolute is true then the agent library is the absolute path
|
||||
* to the library and thus will not be expanded in the target VM.
|
||||
* if isAbsolute is false then the agent library is just a library
|
||||
* name and it will be expended in the target VM.
|
||||
*/
|
||||
private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options)
|
||||
throws AgentLoadException, AgentInitializationException, IOException
|
||||
{
|
||||
InputStream in = execute("load",
|
||||
agentLibrary,
|
||||
isAbsolute ? "true" : "false",
|
||||
options);
|
||||
try {
|
||||
int result = readInt(in);
|
||||
if (result != 0) {
|
||||
throw new AgentInitializationException("Agent_OnAttach failed", result);
|
||||
}
|
||||
} finally {
|
||||
in.close();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Load agent library - library name will be expanded in target VM
|
||||
*/
|
||||
public void loadAgentLibrary(String agentLibrary, String options)
|
||||
throws AgentLoadException, AgentInitializationException, IOException
|
||||
{
|
||||
loadAgentLibrary(agentLibrary, false, options);
|
||||
}
|
||||
|
||||
/*
|
||||
* Load agent - absolute path of library provided to target VM
|
||||
*/
|
||||
public void loadAgentPath(String agentLibrary, String options)
|
||||
throws AgentLoadException, AgentInitializationException, IOException
|
||||
{
|
||||
loadAgentLibrary(agentLibrary, true, options);
|
||||
}
|
||||
|
||||
/*
|
||||
* Load JPLIS agent which will load the agent JAR file and invoke
|
||||
* the agentmain method.
|
||||
*/
|
||||
public void loadAgent(String agent, String options)
|
||||
throws AgentLoadException, AgentInitializationException, IOException
|
||||
{
|
||||
String args = agent;
|
||||
if (options != null) {
|
||||
args = args + "=" + options;
|
||||
}
|
||||
try {
|
||||
loadAgentLibrary("instrument", args);
|
||||
} catch (AgentLoadException x) {
|
||||
throw new InternalError("instrument library is missing in target VM", x);
|
||||
} catch (AgentInitializationException x) {
|
||||
/*
|
||||
* Translate interesting errors into the right exception and
|
||||
* message (FIXME: create a better interface to the instrument
|
||||
* implementation so this isn't necessary)
|
||||
*/
|
||||
int rc = x.returnValue();
|
||||
switch (rc) {
|
||||
case JNI_ENOMEM:
|
||||
throw new AgentLoadException("Insuffient memory");
|
||||
case ATTACH_ERROR_BADJAR:
|
||||
throw new AgentLoadException("Agent JAR not found or no Agent-Class attribute");
|
||||
case ATTACH_ERROR_NOTONCP:
|
||||
throw new AgentLoadException("Unable to add JAR file to system class path");
|
||||
case ATTACH_ERROR_STARTFAIL:
|
||||
throw new AgentInitializationException("Agent JAR loaded but agent failed to initialize");
|
||||
default :
|
||||
throw new AgentLoadException("Failed to load agent - unknown reason: " + rc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The possible errors returned by JPLIS's agentmain
|
||||
*/
|
||||
private static final int JNI_ENOMEM = -4;
|
||||
private static final int ATTACH_ERROR_BADJAR = 100;
|
||||
private static final int ATTACH_ERROR_NOTONCP = 101;
|
||||
private static final int ATTACH_ERROR_STARTFAIL = 102;
|
||||
|
||||
|
||||
/*
|
||||
* Send "properties" command to target VM
|
||||
*/
|
||||
public Properties getSystemProperties() throws IOException {
|
||||
InputStream in = null;
|
||||
Properties props = new Properties();
|
||||
try {
|
||||
in = executeCommand("properties");
|
||||
props.load(in);
|
||||
} finally {
|
||||
if (in != null) in.close();
|
||||
}
|
||||
return props;
|
||||
}
|
||||
|
||||
public Properties getAgentProperties() throws IOException {
|
||||
InputStream in = null;
|
||||
Properties props = new Properties();
|
||||
try {
|
||||
in = executeCommand("agentProperties");
|
||||
props.load(in);
|
||||
} finally {
|
||||
if (in != null) in.close();
|
||||
}
|
||||
return props;
|
||||
}
|
||||
|
||||
private static final String MANAGMENT_PREFIX = "com.sun.management.";
|
||||
|
||||
private static boolean checkedKeyName(Object key) {
|
||||
if (!(key instanceof String)) {
|
||||
throw new IllegalArgumentException("Invalid option (not a String): "+key);
|
||||
}
|
||||
if (!((String)key).startsWith(MANAGMENT_PREFIX)) {
|
||||
throw new IllegalArgumentException("Invalid option: "+key);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static String stripKeyName(Object key) {
|
||||
return ((String)key).substring(MANAGMENT_PREFIX.length());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startManagementAgent(Properties agentProperties) throws IOException {
|
||||
if (agentProperties == null) {
|
||||
throw new NullPointerException("agentProperties cannot be null");
|
||||
}
|
||||
// Convert the arguments into arguments suitable for the Diagnostic Command:
|
||||
// "ManagementAgent.start jmxremote.port=5555 jmxremote.authenticate=false"
|
||||
String args = agentProperties.entrySet().stream()
|
||||
.filter(entry -> checkedKeyName(entry.getKey()))
|
||||
.map(entry -> stripKeyName(entry.getKey()) + "=" + escape(entry.getValue()))
|
||||
.collect(Collectors.joining(" "));
|
||||
executeJCmd("ManagementAgent.start " + args);
|
||||
}
|
||||
|
||||
private String escape(Object arg) {
|
||||
String value = arg.toString();
|
||||
if (value.contains(" ")) {
|
||||
return "'" + value + "'";
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String startLocalManagementAgent() throws IOException {
|
||||
executeJCmd("ManagementAgent.start_local");
|
||||
return getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
|
||||
}
|
||||
|
||||
// --- HotSpot specific methods ---
|
||||
|
||||
// same as SIGQUIT
|
||||
public void localDataDump() throws IOException {
|
||||
executeCommand("datadump").close();
|
||||
}
|
||||
|
||||
// Remote ctrl-break. The output of the ctrl-break actions can
|
||||
// be read from the input stream.
|
||||
public InputStream remoteDataDump(Object ... args) throws IOException {
|
||||
return executeCommand("threaddump", args);
|
||||
}
|
||||
|
||||
// Remote heap dump. The output (error message) can be read from the
|
||||
// returned input stream.
|
||||
public InputStream dumpHeap(Object ... args) throws IOException {
|
||||
return executeCommand("dumpheap", args);
|
||||
}
|
||||
|
||||
// Heap histogram (heap inspection in HotSpot)
|
||||
public InputStream heapHisto(Object ... args) throws IOException {
|
||||
return executeCommand("inspectheap", args);
|
||||
}
|
||||
|
||||
// set JVM command line flag
|
||||
public InputStream setFlag(String name, String value) throws IOException {
|
||||
return executeCommand("setflag", name, value);
|
||||
}
|
||||
|
||||
// print command line flag
|
||||
public InputStream printFlag(String name) throws IOException {
|
||||
return executeCommand("printflag", name);
|
||||
}
|
||||
|
||||
public InputStream executeJCmd(String command) throws IOException {
|
||||
return executeCommand("jcmd", command);
|
||||
}
|
||||
|
||||
// -- Supporting methods
|
||||
|
||||
|
||||
/*
|
||||
* Execute the given command in the target VM - specific platform
|
||||
* implementation must implement this.
|
||||
*/
|
||||
abstract InputStream execute(String cmd, Object ... args)
|
||||
throws AgentLoadException, IOException;
|
||||
|
||||
/*
|
||||
* Convenience method for simple commands
|
||||
*/
|
||||
private InputStream executeCommand(String cmd, Object ... args) throws IOException {
|
||||
try {
|
||||
return execute(cmd, args);
|
||||
} catch (AgentLoadException x) {
|
||||
throw new InternalError("Should not get here", x);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Utility method to read an 'int' from the input stream. Ideally
|
||||
* we should be using java.util.Scanner here but this implementation
|
||||
* guarantees not to read ahead.
|
||||
*/
|
||||
int readInt(InputStream in) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
// read to \n or EOF
|
||||
int n;
|
||||
byte buf[] = new byte[1];
|
||||
do {
|
||||
n = in.read(buf, 0, 1);
|
||||
if (n > 0) {
|
||||
char c = (char)buf[0];
|
||||
if (c == '\n') {
|
||||
break; // EOL found
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
} while (n > 0);
|
||||
|
||||
if (sb.length() == 0) {
|
||||
throw new IOException("Premature EOF");
|
||||
}
|
||||
|
||||
int value;
|
||||
try {
|
||||
value = Integer.parseInt(sb.toString());
|
||||
} catch (NumberFormatException x) {
|
||||
throw new IOException("Non-numeric value found - int expected");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/*
|
||||
* Utility method to read data into a String.
|
||||
*/
|
||||
String readErrorMessage(InputStream sis) throws IOException {
|
||||
byte b[] = new byte[1024];
|
||||
int n;
|
||||
StringBuffer message = new StringBuffer();
|
||||
while ((n = sis.read(b)) != -1) {
|
||||
message.append(new String(b, 0, n, "UTF-8"));
|
||||
}
|
||||
return message.toString();
|
||||
}
|
||||
|
||||
|
||||
// -- attach timeout support
|
||||
|
||||
private static long defaultAttachTimeout = 5000;
|
||||
private volatile long attachTimeout;
|
||||
|
||||
/*
|
||||
* Return attach timeout based on the value of the sun.tools.attach.attachTimeout
|
||||
* property, or the default timeout if the property is not set to a positive
|
||||
* value.
|
||||
*/
|
||||
long attachTimeout() {
|
||||
if (attachTimeout == 0) {
|
||||
synchronized(this) {
|
||||
if (attachTimeout == 0) {
|
||||
try {
|
||||
String s =
|
||||
System.getProperty("sun.tools.attach.attachTimeout");
|
||||
attachTimeout = Long.parseLong(s);
|
||||
} catch (SecurityException se) {
|
||||
} catch (NumberFormatException ne) {
|
||||
}
|
||||
if (attachTimeout <= 0) {
|
||||
attachTimeout = defaultAttachTimeout;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return attachTimeout;
|
||||
}
|
||||
|
||||
protected static void checkNulls(Object... args) {
|
||||
for (Object arg : args) {
|
||||
if (arg instanceof String) {
|
||||
String s = (String)arg;
|
||||
if (s.indexOf(0) >= 0) {
|
||||
throw new IllegalArgumentException("illegal null character in command");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
178
jdkSrc/jdk8/sun/tools/attach/WindowsAttachProvider.java
Normal file
178
jdkSrc/jdk8/sun/tools/attach/WindowsAttachProvider.java
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package sun.tools.attach;
|
||||
|
||||
import com.sun.tools.attach.VirtualMachine;
|
||||
import com.sun.tools.attach.VirtualMachineDescriptor;
|
||||
import com.sun.tools.attach.AttachNotSupportedException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class WindowsAttachProvider extends HotSpotAttachProvider {
|
||||
|
||||
public WindowsAttachProvider() {
|
||||
String os = System.getProperty("os.name");
|
||||
if (os.startsWith("Windows 9") || os.equals("Windows Me")) {
|
||||
throw new RuntimeException(
|
||||
"This provider is not supported on this version of Windows");
|
||||
}
|
||||
String arch = System.getProperty("os.arch");
|
||||
if (!arch.equals("x86") && !arch.equals("amd64")) {
|
||||
throw new RuntimeException(
|
||||
"This provider is not supported on this processor architecture");
|
||||
}
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return "sun";
|
||||
}
|
||||
|
||||
public String type() {
|
||||
return "windows";
|
||||
}
|
||||
|
||||
public VirtualMachine attachVirtualMachine(String vmid)
|
||||
throws AttachNotSupportedException, IOException
|
||||
{
|
||||
checkAttachPermission();
|
||||
|
||||
// AttachNotSupportedException will be thrown if the target VM can be determined
|
||||
// to be not attachable.
|
||||
testAttachable(vmid);
|
||||
|
||||
return new WindowsVirtualMachine(this, vmid);
|
||||
}
|
||||
|
||||
public List<VirtualMachineDescriptor> listVirtualMachines() {
|
||||
// If the temporary file system is secure then we use the default
|
||||
// implementation, otherwise we create a list of Windows processes.
|
||||
if (isTempPathSecure()) {
|
||||
return super.listVirtualMachines();
|
||||
} else {
|
||||
return listJavaProcesses();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the temporary file system supports security
|
||||
*/
|
||||
private static boolean isTempPathSecure() {
|
||||
if (!wasTempPathChecked) {
|
||||
synchronized (WindowsAttachProvider.class) {
|
||||
if (!wasTempPathChecked) {
|
||||
// get the value of TMP/TEMP, ignoring UNC, and paths that
|
||||
// aren't absolute
|
||||
String temp = tempPath();
|
||||
if ((temp != null) && (temp.length() >= 3) &&
|
||||
(temp.charAt(1) == ':') && (temp.charAt(2) == '\\'))
|
||||
{
|
||||
// check if the volume supports security
|
||||
long flags = volumeFlags(temp.substring(0, 3));
|
||||
isTempPathSecure = ((flags & FS_PERSISTENT_ACLS) != 0);
|
||||
}
|
||||
wasTempPathChecked = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return isTempPathSecure;
|
||||
}
|
||||
|
||||
// flag to indicate persistent ACLs are supported
|
||||
private static final long FS_PERSISTENT_ACLS = 0x8L;
|
||||
|
||||
// indicates if we've checked the temporary file system
|
||||
private static volatile boolean wasTempPathChecked;
|
||||
|
||||
// indicates if the temporary file system is secure (only valid when
|
||||
// wasTempPathChecked is true)
|
||||
private static boolean isTempPathSecure;
|
||||
|
||||
// returns the value of TMP/TEMP
|
||||
private static native String tempPath();
|
||||
|
||||
// returns the flags for the given volume
|
||||
private static native long volumeFlags(String volume);
|
||||
|
||||
|
||||
/**
|
||||
* Returns a list of virtual machine descriptors derived from an enumeration
|
||||
* of the process list.
|
||||
*/
|
||||
private List<VirtualMachineDescriptor> listJavaProcesses() {
|
||||
ArrayList<VirtualMachineDescriptor> list =
|
||||
new ArrayList<VirtualMachineDescriptor>();
|
||||
|
||||
// Use localhost in the display name
|
||||
String host = "localhost";
|
||||
try {
|
||||
host = InetAddress.getLocalHost().getHostName();
|
||||
} catch (UnknownHostException uhe) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
// Enumerate all processes.
|
||||
// For those processes that have loaded a library named "jvm.dll"
|
||||
// then we attempt to attach. If we succeed then we have a 6.0+ VM.
|
||||
int processes[] = new int[1024];
|
||||
int count = enumProcesses(processes, processes.length);
|
||||
for (int i=0; i<count; i++) {
|
||||
if (isLibraryLoadedByProcess("jvm.dll", processes[i])) {
|
||||
String pid = Integer.toString(processes[i]);
|
||||
try {
|
||||
new WindowsVirtualMachine(this, pid).detach();
|
||||
|
||||
// FIXME - for now we don't have an appropriate display
|
||||
// name so we use pid@hostname
|
||||
String name = pid + "@" + host;
|
||||
|
||||
list.add(new HotSpotVirtualMachineDescriptor(this, pid, name));
|
||||
} catch (AttachNotSupportedException x) {
|
||||
} catch (IOException ioe) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
// enumerates processes using psapi's EnumProcesses
|
||||
private static native int enumProcesses(int[] processes, int max);
|
||||
|
||||
// indicates if a library of a given name has been loaded by a process
|
||||
private static native boolean isLibraryLoadedByProcess(String library,
|
||||
int processId);
|
||||
|
||||
|
||||
// native functions in this library
|
||||
static {
|
||||
System.loadLibrary("attach");
|
||||
}
|
||||
|
||||
}
|
||||
196
jdkSrc/jdk8/sun/tools/attach/WindowsVirtualMachine.java
Normal file
196
jdkSrc/jdk8/sun/tools/attach/WindowsVirtualMachine.java
Normal file
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package sun.tools.attach;
|
||||
|
||||
import com.sun.tools.attach.AttachOperationFailedException;
|
||||
import com.sun.tools.attach.AgentLoadException;
|
||||
import com.sun.tools.attach.AttachNotSupportedException;
|
||||
import com.sun.tools.attach.spi.AttachProvider;
|
||||
|
||||
import sun.tools.attach.HotSpotVirtualMachine;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Random;
|
||||
|
||||
public class WindowsVirtualMachine extends HotSpotVirtualMachine {
|
||||
|
||||
// the enqueue code stub (copied into each target VM)
|
||||
private static byte[] stub;
|
||||
|
||||
private volatile long hProcess; // handle to the process
|
||||
|
||||
WindowsVirtualMachine(AttachProvider provider, String id)
|
||||
throws AttachNotSupportedException, IOException
|
||||
{
|
||||
super(provider, id);
|
||||
|
||||
int pid;
|
||||
try {
|
||||
pid = Integer.parseInt(id);
|
||||
} catch (NumberFormatException x) {
|
||||
throw new AttachNotSupportedException("Invalid process identifier");
|
||||
}
|
||||
hProcess = openProcess(pid);
|
||||
|
||||
// The target VM might be a pre-6.0 VM so we enqueue a "null" command
|
||||
// which minimally tests that the enqueue function exists in the target
|
||||
// VM.
|
||||
try {
|
||||
enqueue(hProcess, stub, null, null);
|
||||
} catch (IOException x) {
|
||||
throw new AttachNotSupportedException(x.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void detach() throws IOException {
|
||||
synchronized (this) {
|
||||
if (hProcess != -1) {
|
||||
closeProcess(hProcess);
|
||||
hProcess = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
InputStream execute(String cmd, Object ... args)
|
||||
throws AgentLoadException, IOException
|
||||
{
|
||||
assert args.length <= 3; // includes null
|
||||
checkNulls(args);
|
||||
|
||||
// create a pipe using a random name
|
||||
int r = (new Random()).nextInt();
|
||||
String pipename = "\\\\.\\pipe\\javatool" + r;
|
||||
long hPipe = createPipe(pipename);
|
||||
|
||||
// check if we are detached - in theory it's possible that detach is invoked
|
||||
// after this check but before we enqueue the command.
|
||||
if (hProcess == -1) {
|
||||
closePipe(hPipe);
|
||||
throw new IOException("Detached from target VM");
|
||||
}
|
||||
|
||||
try {
|
||||
// enqueue the command to the process
|
||||
enqueue(hProcess, stub, cmd, pipename, args);
|
||||
|
||||
// wait for command to complete - process will connect with the
|
||||
// completion status
|
||||
connectPipe(hPipe);
|
||||
|
||||
// create an input stream for the pipe
|
||||
PipedInputStream is = new PipedInputStream(hPipe);
|
||||
|
||||
// read completion status
|
||||
int status = readInt(is);
|
||||
if (status != 0) {
|
||||
// read from the stream and use that as the error message
|
||||
String message = readErrorMessage(is);
|
||||
// special case the load command so that the right exception is thrown
|
||||
if (cmd.equals("load")) {
|
||||
throw new AgentLoadException("Failed to load agent library");
|
||||
} else {
|
||||
if (message == null) {
|
||||
throw new AttachOperationFailedException("Command failed in target VM");
|
||||
} else {
|
||||
throw new AttachOperationFailedException(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return the input stream
|
||||
return is;
|
||||
|
||||
} catch (IOException ioe) {
|
||||
closePipe(hPipe);
|
||||
throw ioe;
|
||||
}
|
||||
}
|
||||
|
||||
// An InputStream based on a pipe to the target VM
|
||||
private class PipedInputStream extends InputStream {
|
||||
|
||||
private long hPipe;
|
||||
|
||||
public PipedInputStream(long hPipe) {
|
||||
this.hPipe = hPipe;
|
||||
}
|
||||
|
||||
public synchronized int read() throws IOException {
|
||||
byte b[] = new byte[1];
|
||||
int n = this.read(b, 0, 1);
|
||||
if (n == 1) {
|
||||
return b[0] & 0xff;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized int read(byte[] bs, int off, int len) throws IOException {
|
||||
if ((off < 0) || (off > bs.length) || (len < 0) ||
|
||||
((off + len) > bs.length) || ((off + len) < 0)) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
} else if (len == 0)
|
||||
return 0;
|
||||
|
||||
return WindowsVirtualMachine.readPipe(hPipe, bs, off, len);
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
if (hPipe != -1) {
|
||||
WindowsVirtualMachine.closePipe(hPipe);
|
||||
hPipe = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-- native methods
|
||||
|
||||
static native void init();
|
||||
|
||||
static native byte[] generateStub();
|
||||
|
||||
static native long openProcess(int pid) throws IOException;
|
||||
|
||||
static native void closeProcess(long hProcess) throws IOException;
|
||||
|
||||
static native long createPipe(String name) throws IOException;
|
||||
|
||||
static native void closePipe(long hPipe) throws IOException;
|
||||
|
||||
static native void connectPipe(long hPipe) throws IOException;
|
||||
|
||||
static native int readPipe(long hPipe, byte buf[], int off, int buflen) throws IOException;
|
||||
|
||||
static native void enqueue(long hProcess, byte[] stub,
|
||||
String cmd, String pipename, Object ... args) throws IOException;
|
||||
|
||||
static {
|
||||
System.loadLibrary("attach");
|
||||
init(); // native initialization
|
||||
stub = generateStub(); // generate stub to copy into target process
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user