sun.tools.attach.HotSpotVirtualMachine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for automated developer testing.
It contains mocking/faking APIs and a code coverage tool, supporting both JUnit and TestNG.
The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested
in isolation from selected dependencies.
/*
* Copyright (c) 2005, 2014, 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.Map.Entry;
/*
* 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"
StringBuilder args = new StringBuilder();
String sep = "";
for (Entry