本文整理匯總了Java中com.sun.tools.attach.VirtualMachine.loadAgent方法的典型用法代碼示例。如果您正苦於以下問題:Java VirtualMachine.loadAgent方法的具體用法?Java VirtualMachine.loadAgent怎麽用?Java VirtualMachine.loadAgent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.tools.attach.VirtualMachine
的用法示例。
在下文中一共展示了VirtualMachine.loadAgent方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: main
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException,
AttachNotSupportedException, AgentLoadException,
AgentInitializationException, InterruptedException {
// 獲取當前jvm的進程pid
String pid = ManagementFactory.getRuntimeMXBean().getName();
int indexOf = pid.indexOf('@');
if (indexOf > 0) {
pid = pid.substring(0, indexOf);
}
System.out.println("當前JVM Process ID: " + pid);
// 獲取當前jvm
VirtualMachine vm = VirtualMachine.attach(pid);
// 當前jvm加載代理jar包,參數1是jar包路徑地址,參數2是給jar包代理類傳遞的參數
vm.loadAgent("D:/123.jar", "my agent:123.jar");
Thread.sleep(1000);
vm.detach();
}
示例3: main
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
public static void main(String[] args) throws AttachNotSupportedException,
IOException, AgentLoadException, AgentInitializationException,
InterruptedException {
VirtualMachine vm=AgentUtil.getCurrentVm();
//先載入agent
vm.loadAgent("d:/springloaded-1.2.1.RELEASE.jar","myagent");
System.out.println("加載agent成功");
vm.detach();
TestSpringLoacedClass t1=new TestSpringLoacedClass();
for(int i=0;i<10000;i++)
{
TestSpringLoacedClass t2=new TestSpringLoacedClass();
t1.say2();
t2.say2();
Thread.sleep(1000);
}
}
示例4: load
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
private static void load(VirtualMachine vm, Agent agent, List<Class<?>> classes, File... libs) throws AgentLoadException, AgentInitializationException, NotFoundException, CannotCompileException, IOException {
vm.loadAgent(AgentTools.createAgentJar(agent, classes, libs).getAbsolutePath());
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: main
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
public static void main(String[] args) throws AgentLoadException, AgentInitializationException, IOException, AttachNotSupportedException {
VirtualMachine vm=VirtualMachine.attach("7876");
vm.loadAgent("d:/springloaded-1.2.1.RELEASE.jar");
// vm.loadAgent("d:/myagent.jar","myagent");
System.out.println(vm.getAgentProperties().toString());
System.in.read();
}
示例7: 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();
}
示例8: loadInstrumentationAgent
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
private static void loadInstrumentationAgent(String myName, byte[] buf) throws Exception {
// Create agent jar file on the fly
Manifest m = new Manifest();
m.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
m.getMainAttributes().put(new Attributes.Name("Agent-Class"), myName);
m.getMainAttributes().put(new Attributes.Name("Can-Redefine-Classes"), "true");
File jarFile = File.createTempFile("agent", ".jar");
jarFile.deleteOnExit();
JarOutputStream jar = new JarOutputStream(new FileOutputStream(jarFile), m);
jar.putNextEntry(new JarEntry(myName.replace('.', '/') + ".class"));
jar.write(buf);
jar.close();
String pid = Long.toString(ProcessTools.getProcessId());
System.out.println("Our pid is = " + pid);
VirtualMachine vm = VirtualMachine.attach(pid);
vm.loadAgent(jarFile.getAbsolutePath());
}
示例9: 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();
}
}
示例10: startJavaAgent
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
/**
* Loads a java agent into the VM and checks that java.instrument is loaded.
*/
static void startJavaAgent(VirtualMachine vm, Path agent) throws Exception {
System.out.println("Load java agent ...");
vm.loadAgent(agent.toString());
// the Agent class should be visible
Class.forName("Agent");
// types in java.instrument should be visible
Class.forName("java.lang.instrument.Instrumentation");
}
示例11: loadAgentIntoTargetJVM
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
private static void loadAgentIntoTargetJVM(final String jar, final String options, final String pid)
throws AttachNotSupportedException, IOException, AgentLoadException, AgentInitializationException {
VirtualMachine virtualMachine = VirtualMachine.attach(pid);
virtualMachine.loadAgent(jar,options);
}
示例12: 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();
}
示例13: attach
import com.sun.tools.attach.VirtualMachine; //導入方法依賴的package包/類
public static void attach(String pid, Class<?> agent, Class<?>... resources) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
VirtualMachine vm = VirtualMachine.attach(pid);
vm.loadAgent(createAgent(agent, resources).getAbsolutePath());
vm.detach();
}
示例14: 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();
}
}