//具体示例查看jdk中的dome文件编写例如: heapTracker文件编写
//使用java字节码技术修改内存中的java代码,采用动态的方式加载外部类
//开启调试端口
public class Test {
//public static void main(String[] args) throws AgentLoadException, AgentInitializationException, IOException, AttachNotSupportedException {
//attach();
//}
//public static void attach() throws AgentLoadException, AgentInitializationException, IOException, AttachNotSupportedException{
//String pid = "10748";//java进程号
//String agentPath = "C:\\Users\\DanteJ\\source\\repos\\agent\\x64\\Debug\\agent.dll";//本地库路径
//System.out.println("attaching....pid="+pid);
//VirtualMachine virtualMachine = VirtualMachine.attach(pid);//attach JVM
//virtualMachine.loadAgentPath(agentPath);//加载本地库
//virtualMachine.detach();//断开
//}
}
// Dll.cpp : 定义 DLL 应用程序的导出函数。
/*
* java agent有2个启动函数分别为Agent_OnLoad和Agent_OnAttach
* Agent_OnLoad在onload阶段被调用
* Agent_OnAttach在live阶段被调用
* 但是每个agent只有一个启动函数会被调用
*/
/*
* 此阶段JVM还没有初始化,所以能做的操作比较受限制
* JVM参数都无法获取
* The return value from Agent_OnLoad is used to indicate an error.
* Any value other than zero indicates an error and causes termination of the VM.
* 任何非零的返回值都会导致JVM终止。
*/
//JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {}
/*
* Any value other than zero indicates an error.
* An error does not cause the VM to terminate.
* Instead the VM ignores the error, or takes some implementation specific action -- for example it might print an error to standard error, or record the error in a system log.
*
*/
//JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {}
/*
* This function can be used to clean-up resources allocated by the agent.
*/
//JNIEXPORT void JNICALL Agent_OnUnload(JavaVM *vm) {}