本文整理汇总了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();
}
}