java agent-03-Java Instrumentation 结合 bytekit 实战笔记 agent attach
2023年7月19日大约 3 分钟
java agent 系列
java agent-02-Java Instrumentation API
java agent-03-Java Instrumentation 结合 bytekit 实战笔记 agent attach
java agent-03-Java Instrumentation 结合 bytekit 实战笔记 agent premain
java agent-04-统一管理众多的Java Agent
拓展阅读
前面几篇文档,我们简单介绍了一下 java Instrumentation。
本篇我们结合一下 bytekit 进行实际的文件修改。
测试代码
整体目录
│ │ └─com
│ │ └─github
│ │ └─houbb
│ │ └─bytekit
│ │ └─learn
│ │ └─agentattach
│ │ │ AgentAttachMain.java
│ │ │ MyAttachMain.java
│ │ │ MyClassFileTransformer.java
│ │ │ package-info.java
│ │ │
│ │ └─interceptor
│ │ SampleInterceptor.java
│ │
│ └─resources
│ └─META-INF
│ MANIFEST.MF
│
└─test
└─java
└─com.github.houbb.bytekit.learn.agentattach
Sample.java
TestMain.java
MANIFEST.MF
Manifest-Version: 1.0
Agent-Class: com.github.houbb.bytekit.learn.agentattach.AgentAttachMain
Can-Redefine-Classes: true
Can-Retransform-Classes: true
AgentAttachMain-核心入口
这里指定了核心入口 AgentAttachMain
- AgentAttachMain.java
动态 Attach 的 agent 与通过 JVM 启动 javaagent 参数指定的 agent jar 包的方式有所不同,动态 Attach 的 agent 会执行 agentmain 方法,而不是 premain 方法。
package com.github.houbb.bytekit.learn.agentattach;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
public class AgentAttachMain {
/**
* 动态 Attach 的 agent 会执行 agentmain 方法,而不是 premain 方法。
*
* @param agentArgs
* @param inst
* @throws ClassNotFoundException
* @throws UnmodifiableClassException
*/
public static void agentmain(String agentArgs, Instrumentation inst) throws ClassNotFoundException, UnmodifiableClassException {
System.out.println("agentmain called");
Class classes[] = inst.getAllLoadedClasses();
for (int i = 0; i clazz = Sample.class;
Sample sample = this;
System.out.println("atEnter, args[0]: " + objectArray[0]);
}
catch (RuntimeException runtimeException) {
Class clazz = Sample.class;
RuntimeException runtimeException2 = runtimeException;
System.out.println("exception handler: " + clazz);
runtimeException2.printStackTrace();
}
if (exception != false) {
++this.exceptionCount;
throw new RuntimeException("test exception, str: " + (String)str);
}
String string5 = string2 = "hello " + (String)str;
System.out.println("atExit, returnObject: " + string5);
return string2;
}
catch (RuntimeException runtimeException) {
int n = this.exceptionCount;
RuntimeException runtimeException3 = runtimeException;
System.out.println("atExceptionExit, ex: " + runtimeException3.getMessage() + ", field exceptionCount: " + n);
throw runtimeException;
}
}
}
end transform name=== com/github/houbb/bytekit/learn/agentattach/Sample
Reloading done: com.github.houbb.bytekit.learn.agentattach.Sample
atEnter, args[0]: 123
atExit, returnObject: hello 123
hello 123
atEnter, args[0]: 123
atExit, returnObject: hello 123
hello 123
atEnter, args[0]: 123
atExit, returnObject: hello 123
hello 123
atEnter, args[0]: 123
atExit, returnObject: hello 123
hello 123
拓展阅读
VirtualMachine 类不存在
添加jdk tools.jar解决com.sun.tools.attach.VirtualMachine 类找不到的问题
发现配置了 java_home 及相关信息还是不行,可以手动在项目中引入。
idea 就是 libs 种添加依赖。
参考资料
贡献者
binbin.hou