本文整理匯總了Java中com.sun.tools.attach.VirtualMachine.detach方法的典型用法代碼示例。如果您正苦於以下問題:Java VirtualMachine.detach方法的具體用法?Java VirtualMachine.detach怎麽用?Java VirtualMachine.detach使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.tools.attach.VirtualMachine
的用法示例。
在下文中一共展示了VirtualMachine.detach方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: attach
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
/**
* Spawn recaf in the JVM by the given descriptor.
*
* @param vm
*/
public static void attach(VirtualMachineDescriptor vm) {
try {
// Run attach
VirtualMachine target = VirtualMachine.attach(vm);
String agentPath = getAgentJar();
if (!agentPath.endsWith(".jar")) {
Recaf.INSTANCE.logging.error(new RuntimeException("Recaf could not resolve a path to itself."));
return;
}
File agent = new File(agentPath);
if (agent.exists()) {
Recaf.INSTANCE.logging.info("Attempting to attach to '" + vm.displayName() + "' with agent '" + agent.getAbsolutePath() + "'.");
target.loadAgent(agent.getAbsolutePath());
target.detach();
} else {
Recaf.INSTANCE.logging.error(new RuntimeException("Recaf could not resolve a path to itself, attempt gave: " + agent.getAbsolutePath()));
}
} catch (Exception e) {
Recaf.INSTANCE.logging.error(e);
}
}
示例2: loadManagementAgent
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
private void loadManagementAgent() throws IOException {
VirtualMachine vm = null;
String name = String.valueOf(vmid);
try {
vm = VirtualMachine.attach(name);
} catch (AttachNotSupportedException x) {
IOException ioe = new IOException(x.getMessage());
ioe.initCause(x);
throw ioe;
}
vm.startLocalManagementAgent();
// get the connector address
Properties agentProps = vm.getAgentProperties();
address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
vm.detach();
}
示例3: main
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
String value = System.getProperty("jdk.attach.allowAttachSelf");
boolean canAttachSelf = (value != null) && !value.equals("false");
String vmid = "" + ProcessHandle.current().pid();
VirtualMachine vm = null;
try {
vm = VirtualMachine.attach(vmid);
if (!canAttachSelf)
throw new RuntimeException("Attached to self not expected");
} catch (IOException ioe) {
if (canAttachSelf)
throw ioe;
} finally {
if (vm != null) vm.detach();
}
}
示例4: executeCommandForPid
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
private static void executeCommandForPid(String pid, String command, Object ... args)
throws AttachNotSupportedException, IOException,
UnsupportedEncodingException {
VirtualMachine vm = VirtualMachine.attach(pid);
// Cast to HotSpotVirtualMachine as this is an
// implementation specific method.
HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
try (InputStream in = hvm.executeCommand(command, args)) {
// read to EOF and just print output
byte b[] = new byte[256];
int n;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
}
} while (n > 0);
}
vm.detach();
}
示例5: attachAgent
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
static void attachAgent(File agentFile, Class<?>[] transformers) {
try {
String pid = ManagementFactory.getRuntimeMXBean().getName();
VirtualMachine vm = VirtualMachine.attach(pid.substring(0, pid.indexOf('@')));
vm.loadAgent(agentFile.getAbsolutePath());
vm.detach();
Agent.getInstance().process(transformers);
} catch (Exception e) {
e.printStackTrace();
}
}
示例6: runTests
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
public static void runTests(long pid) throws Exception {
VirtualMachine vm = VirtualMachine.attach(""+pid);
try {
basicTests(vm);
testLocalAgent(vm);
// we retry the remote case several times in case the error
// was caused by a port conflict
int i = 0;
boolean success = false;
do {
try {
System.err.println("Trying remote agent. Try #" + i);
testRemoteAgent(vm);
System.err.println("Successfully connected to remote agent");
success = true;
} catch(Exception ex) {
System.err.println("testRemoteAgent failed with exception:");
ex.printStackTrace();
System.err.println("Retrying.");
}
i++;
} while(!success && i < MAX_RETRIES);
if (!success) {
throw new Exception("testRemoteAgent failed after " + MAX_RETRIES + " tries");
}
} finally {
System.err.println("Detaching from VM ...");
vm.detach();
System.err.println("Detached");
}
}
示例7: executeCommandForPid
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
private static void executeCommandForPid(String pid, String command)
throws AttachNotSupportedException, IOException,
UnsupportedEncodingException {
VirtualMachine vm = VirtualMachine.attach(pid);
// Cast to HotSpotVirtualMachine as this is an
// implementation specific method.
HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
String lines[] = command.split("\\n");
for (String line : lines) {
if (line.trim().equals("stop")) {
break;
}
try (InputStream in = hvm.executeJCmd(line);) {
// read to EOF and just print output
byte b[] = new byte[256];
int n;
boolean messagePrinted = false;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
messagePrinted = true;
}
} while (n > 0);
if (!messagePrinted) {
System.out.println("Command executed successfully");
}
}
}
vm.detach();
}
示例8: drain
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
private static void drain(VirtualMachine vm, InputStream in) throws Exception {
byte b[] = new byte[256];
int n;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
}
} while (n > 0);
in.close();
vm.detach();
}
示例9: main
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
if (args.length < 2 || args.length > 3) {
System.err.println("Usage: java -cp dir/agent.jar:/.../jvm/java-8-jdk/lib/tools.jar " +
"io.prometheus.jmx.shaded.io.prometheus.jmx.Attach pid [host:]<port>:<yaml configuration file>");
System.exit(1);
}
List<VirtualMachineDescriptor> vms = VirtualMachine.list();
VirtualMachineDescriptor selectedVM = null;
for (VirtualMachineDescriptor vmd : vms) {
if (vmd.id().equals(args[0])) {
selectedVM = vmd;
}
}
if (selectedVM == null) {
System.err.println("No such java process with pid=" + args[0]);
System.exit(-1);
}
VirtualMachine attachedVm = VirtualMachine.attach(selectedVM);
File currentJarFile = new File(Attach.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
attachedVm.loadAgent(currentJarFile.getAbsolutePath(), args[1]);
attachedVm.detach();
}
示例10: runThreadDump
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
private static void runThreadDump(String pid, String args[]) throws Exception {
VirtualMachine vm = null;
try {
vm = VirtualMachine.attach(pid);
} catch (Exception x) {
String msg = x.getMessage();
if (msg != null) {
System.err.println(pid + ": " + msg);
} else {
x.printStackTrace();
}
System.exit(1);
}
// Cast to HotSpotVirtualMachine as this is implementation specific
// method.
InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args);
// read to EOF and just print output
byte b[] = new byte[256];
int n;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
}
} while (n > 0);
in.close();
vm.detach();
}
示例11: runThreadDump
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
private static void runThreadDump(String pid, String args[]) throws Exception {
VirtualMachine vm = null;
try {
vm = VirtualMachine.attach(pid);
} catch (Exception x) {
String msg = x.getMessage();
if (msg != null) {
System.err.println(pid + ": " + msg);
} else {
x.printStackTrace();
}
if ((x instanceof AttachNotSupportedException) &&
(loadSAClass() != null)) {
System.err.println("The -F option can be used when the target " +
"process is not responding");
}
System.exit(1);
}
// Cast to HotSpotVirtualMachine as this is implementation specific
// method.
InputStream in = ((HotSpotVirtualMachine)vm).remoteDataDump((Object[])args);
// read to EOF and just print output
byte b[] = new byte[256];
int n;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
}
} while (n > 0);
in.close();
vm.detach();
}
示例12: drain
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
private static void drain(VirtualMachine vm, InputStream in) throws IOException {
// read to EOF and just print output
byte b[] = new byte[256];
int n;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
System.out.print(s);
}
} while (n > 0);
in.close();
vm.detach();
}
示例13: main
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
static public void main(String[] args) throws Exception {
// Create speculative trap entries
Test.m();
String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
int p = nameOfRunningVM.indexOf('@');
String pid = nameOfRunningVM.substring(0, p);
// Make the nmethod go away
for (int i = 0; i < 10; i++) {
System.gc();
}
// Redefine class
try {
VirtualMachine vm = VirtualMachine.attach(pid);
vm.loadAgent(AGENT_JAR, "");
vm.detach();
} catch (Exception e) {
throw new RuntimeException(e);
}
Test.m();
// GC will hit dead method pointer
for (int i = 0; i < 10; i++) {
System.gc();
}
}
示例14: main
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
if (args.length < 2)
{
logOut("Not enough arguments given. This code requires, in this order:");
logOut("1) The PID of JVM to attach to.");
logOut("1) The absolute location of the agent jar to be attached.");
logOut("3) (optional) Any arguments to be passed to the java agent.");
System.exit(1);
}
String vmPidArg = args[0];
Integer.parseInt(vmPidArg); //Just to make sure it's a number.
logOut("Will attempt to attach to JVM with pid " + vmPidArg);
String agentPathString = args[1];
logOut("Attach will be done using agent jar file: " + agentPathString);
// This may cause a compiler error in eclipse, as it is not a java.* class
// It can be fixed by editing the access rules for the JRE system library
// attach to target VM
//First we wait for the target VM to become available
boolean foundTarget = false;
while (!foundTarget) {
List<VirtualMachineDescriptor> allVMs = VirtualMachine.list();
for (VirtualMachineDescriptor oneVM : allVMs) {
if (oneVM.id().equals(vmPidArg)) {
foundTarget = true;
}
}
if (!foundTarget) {
Thread.sleep(1000);
}
}
//Then we attach to it.
VirtualMachine vm = VirtualMachine.attach(vmPidArg);
// load agent into target VM
try {
if (args.length > 2) {
vm.loadAgent(agentPathString, args[2]);
} else {
vm.loadAgent(agentPathString);
}
} catch (java.io.IOException e) {
//This exception is thrown if the test process terminates before the agent does.
//We don't mind if this happens.
}
// detach
vm.detach();
}
示例15: main
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
static public void main(String[] args) throws Exception {
// loader2 must be first on the list so loader 1 must be used first
ClassLoader loader1 = newClassLoader();
String packageName = Agent.class.getPackage().getName();
Class dummy = loader1.loadClass(packageName + ".Test");
ClassLoader loader2 = newClassLoader();
Test_class = loader2.loadClass(packageName + ".Test");
Method m3 = Test_class.getMethod("m3", ClassLoader.class);
// Add speculative trap in m2() (loaded by loader1) that
// references m4() (loaded by loader2).
m3.invoke(Test_class.newInstance(), loader1);
String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
int p = nameOfRunningVM.indexOf('@');
String pid = nameOfRunningVM.substring(0, p);
// Make the nmethod go away
for (int i = 0; i < 10; i++) {
System.gc();
}
// Redefine class Test loaded by loader2
for (int i = 0; i < 2; i++) {
try {
VirtualMachine vm = VirtualMachine.attach(pid);
vm.loadAgent(AGENT_JAR, "");
vm.detach();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// Will process loader2 first, find m4() is redefined and
// needs to be freed then process loader1, check the
// speculative trap in m2() and try to access m4() which was
// freed already.
for (int i = 0; i < 10; i++) {
System.gc();
}
}