• Java Agent入门教程


    1. Java Agent简介

    • Java Agent是什么?
      Java Agent 又叫做 Java 探针,是在 JDK1.5 引入的一种可以动态修改 Java 字节码的技术。Java 类编译之后形成字节码被 JVM 执行,在 JVM 在执行这些字节码之前获取这些字节码信息,并且通过字节码转换器对这些字节码进行修改,来完成一些额外的功能。java agent本质上可以理解为一个jar包插件,这个jar包通过JVMTI(JVM Tool Interface,是Java虚拟机对外提供的Native编程接口,外部进程可以获取到运行时JVM的很多信息,比如线程、GC等)完成加载,利用JVM提供的Instrumentation API可以在 JVM 执行字节码之前或在JVM 加载字节码之后对字节码进行修改。-javaagent是java命令的一个参数,应用启动时可以用这个参数指定一个jar包,去实现我们想做事情。

    • Java Agent能做什么?
      可以在加载class文件之前做拦截,对字节码做修改
      可以在运行期将已经加载的类的字节码做变更
      获取所有已经被加载过的类
      获取所有已经被初始化过了的类
      获取某个对象的大小
      将某个jar加入到bootstrapclasspath里作为高优先级被bootstrapClassloader加载
      将某个jar加入到classpath里供AppClassloard去加载
      设置某些native方法的前缀,主要在查找native方法的时候做规则匹配
      链路追踪系统:skywalking
      诊断工具:Arthas

    • 如果要使用agent,我们需要定义两个方法

    premain: 将在JVM启动时使用-javaagent参数静态地加载agent
    agentmain: 将使用Java Attach API动态加载agent到JVM中
    
    • 1
    • 2

    2. premain方式

    • 以jvm参数形式调用premain方法,在java程序的main方法执行之前执行

    2.1 新建工程

    • 先创建一个maven工程

    2.2 创建agent类

    package org.example.agent;
    
    import java.lang.instrument.Instrumentation;
    
    public class MyAgent {
    
        /**
         * jvm参数形式调用premain方法,在java程序的main方法执行之前执行
         */
        public static void premain(String agentArgs, Instrumentation inst) {
            System.out.println("[MyAgent][premain]: " + agentArgs);
            System.out.println("Premain-Class: " + MyAgent.class.getName());
            System.out.println("Can-Redefine-Classes: " + inst.isRedefineClassesSupported());
            System.out.println("Can-Retransform-Classes: " + inst.isRetransformClassesSupported());
            System.out.println("Can-Set-Native-Method-Prefix: " + inst.isNativeMethodPrefixSupported());
            System.out.println("========= ========= =========");
        }
    
        /**
         * attach方式调用agentmain方法,在java程序启动后执行
         */
        public static void agentmain(String agentArgs, Instrumentation inst) {
            System.out.println("[MyAgent][agentmain]: " + agentArgs);
            System.out.println("========= ========= =========");
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述

    2.3 指定agent启动类

    2.3.1 使用MANIFEST.MF

    • 在resources文件夹下创建META-INF文件夹,再创建MANIFEST.MF文件,填写如下内容,最后一行要有空一行
    Manifest-Version: 1.0
    Premain-Class: org.example.agent.MyAgent
    Agent-Class: org.example.agent.MyAgent
    Can-Redefine-Classes: true
    Can-Retransform-Classes: true
    Can-Set-Native-Method-Prefix: true
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    Premain-Class:包含premain方法的类,需要配置为类的全路径
    Agent-Class:包含agent方法的类,需要配置为类的全路径
    Can-Redefine-Classes:为true时表示能够重新定义class
    Can-Retransform-Classes:为true时表示能够重新转换class,实现字节码替换
    Can-Set-Native-Method-Prefix:为true时表示能够设置native方法的前缀

    • 相应pom.xml,在manifestFile中指定MANIFEST.MF文件
    
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>org.examplegroupId>
        <artifactId>my-agentartifactId>
        <version>1.0-SNAPSHOTversion>
    
        <name>my-agentname>
        
        <url>http://www.example.comurl>
    
        <properties>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
        properties>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-assembly-pluginartifactId>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependenciesdescriptorRef>
                        descriptorRefs>
                        <archive>
                            <manifestFile>src/main/resources/META-INF/MANIFEST.MFmanifestFile>
                        archive>
                    configuration>
                plugin>
            plugins>
        build>
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    在这里插入图片描述

    2.3.2 使用 maven 插件

    • 在manifestEntries中添加配置
    
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>org.examplegroupId>
        <artifactId>my-agentartifactId>
        <version>1.0-SNAPSHOTversion>
    
        <name>my-agentname>
        
        <url>http://www.example.comurl>
    
        <properties>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
        properties>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-assembly-pluginartifactId>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependenciesdescriptorRef>
                        descriptorRefs>
                        <archive>
                            <manifestEntries>
                                <Premain-Class>org.example.agent.MyAgentPremain-Class>
                                <Agent-Class>org.example.agent.MyAgentAgent-Class>
                                <Can-Redefine-Classes>trueCan-Redefine-Classes>
                                <Can-Retransform-Classes>trueCan-Retransform-Classes>
                                <Can-Set-Native-Method-Prefix>trueCan-Set-Native-Method-Prefix>
                            manifestEntries>
                        archive>
                    configuration>
                plugin>
            plugins>
        build>
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    在这里插入图片描述

    2.4 打包

    • 运行IDEA的maven插件,点击assembly:assembly,会生成2个jar包,my-agent-1.0-SNAPSHOT.jar中不带这个项目所依赖的jar包,my-agent-1.0-SNAPSHOT-jar-with-dependencies.jar中包含这个项目所依赖的jar包,后面需要使用my-agent-1.0-SNAPSHOT-jar-with-dependencies.jar这个jar

    在这里插入图片描述

    2.5 创建一个springboot工程

    • 用来验证agent包中的premain方法
    • pom.xml,添加了maven的打包插件,用于后面使用java -jar启动项目时,做动态修改验证
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.6.6version>
            <relativePath/> 
        parent>
        <groupId>com.examplegroupId>
        <artifactId>maven-demoartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>maven-demoname>
        <description>maven-demodescription>
        <properties>
            <java.version>8java.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackagegoal>
                            goals>
                        execution>
                    executions>
                    <configuration>
                        <includeSystemScope>trueincludeSystemScope>
                        <mainClass>com.example.mavendemo.MavenDemoApplicationmainClass>
                    configuration>
                plugin>
            plugins>
        build>
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    在这里插入图片描述

    • 启动类
    package com.example.mavendemo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class MavenDemoApplication {
    
        public static void main(String[] args) {
            System.out.println("hello world!");
            SpringApplication.run(MavenDemoApplication.class, args);
            System.out.println("hahaha");
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    2.6 验证

    • 在idea的VM options中添加-javaagent:xxx.jar参数

    在这里插入图片描述

    • 运行程序,查看打印

    在这里插入图片描述

    3. agentmain方式

    • 和premain模式不同,不能通过添加启动参数的方式将agent包挂载到主程序,需要使用com.sun.tools.attach包下的VirtualMachine工具类,需要注意该类不是jvm标准规范,是由Sun公司自己实现的,使用前需要引入依赖

    3.1 新建工程

    • 创建一个maven工程
    • pom.xml,使用系统环境中的com.sun.tools包,引入maven-assembly-plugin插件,指定main方法启动类
    • ${JAVA_8_HOME}/lib/tools.jar就是jdk8下面com.sun.tools存放的位置,其他版本的jdk不一定是在这个文件夹下,${JAVA_8_HOME}是你自己设置jdk环境变量时的名字,根据自己的设置进行修改
    
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>org.examplegroupId>
        <artifactId>attach-demoartifactId>
        <version>1.0-SNAPSHOTversion>
    
        <name>attach-demoname>
        
        <url>http://www.example.comurl>
    
        <properties>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>com.sungroupId>
                <artifactId>toolsartifactId>
                <version>8version>
                <scope>systemscope>
                <systemPath>${JAVA_8_HOME}/lib/tools.jarsystemPath>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-assembly-pluginartifactId>
                    <version>2.5.5version>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>org.example.attach.AttachermainClass>
                            manifest>
                        archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependenciesdescriptorRef>
                        descriptorRefs>
                    configuration>
                plugin>
            plugins>
        build>
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    在这里插入图片描述
    在这里插入图片描述

    3.2 验证

    3.2.1 指定进程号方式

    • 启动类
    package org.example.attach;
    
    import java.io.IOException;
    
    import com.sun.tools.attach.AgentInitializationException;
    import com.sun.tools.attach.AgentLoadException;
    import com.sun.tools.attach.AttachNotSupportedException;
    import com.sun.tools.attach.VirtualMachine;
    
    public class Attacher {
    
        public static void main(String[] args)
            throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
            // 指定进程号
            VirtualMachine vm = VirtualMachine.attach(args[0]);
            // 指定agent包位置和要传入的参数
            vm.loadAgent(
                "/Users/fisher/Documents/code/gitee/myAgent/target/my-agent-1.0-SNAPSHOT-jar-with-dependencies.jar",
                "Hello JVM Attach");
            vm.detach();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

    • 使用maven插件,assembly:assembly,同样生成2个jar包,后面使用attach-demo-1.0-SNAPSHOT-jar-with-dependencies.jar

    在这里插入图片描述

    • 打包待修改的项目,使用spring-boot-maven-plugin,点击package

    在这里插入图片描述

    • 使用java -javaagent:/Users/xxx/Documents/code/gitee/myAgent/target/my-agent-1.0-SNAPSHOT-jar-with-dependencies.jar=Fisher -jar maven-demo-0.0.1-SNAPSHOT.jar启动,同样可以看到premain中的方法被执行了

    在这里插入图片描述

    • 查看进程号jps

    在这里插入图片描述

    • 运行agent包,java -jar attach-demo-1.0-SNAPSHOT-jar-with-dependencies.jar 2555,2555就是待修改的运行中的程序的进程号

    在这里插入图片描述

    • 查看待修改程序,已经执行了agentmain中的代码

    在这里插入图片描述

    3.2.2 指定程序名称方式

    • 启动类
    package org.example.attach;
    
    import java.io.IOException;
    import java.util.List;
    
    import com.sun.tools.attach.AgentInitializationException;
    import com.sun.tools.attach.AgentLoadException;
    import com.sun.tools.attach.AttachNotSupportedException;
    import com.sun.tools.attach.VirtualMachine;
    import com.sun.tools.attach.VirtualMachineDescriptor;
    
    public class Attacher {
    
        public static void main(String[] args)
            throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
            // 查询所有运行的java程序
            List<VirtualMachineDescriptor> list = VirtualMachine.list();
            for (VirtualMachineDescriptor virtualMachineDescriptor : list) {
                String displayName = virtualMachineDescriptor.displayName();
                // 这里指定运行的程序的名称,使用jps就能看出名称
                if (displayName.equals("maven-demo-0.0.1-SNAPSHOT.jar")) {
                    System.out.println(virtualMachineDescriptor.id());
                    VirtualMachine attach = VirtualMachine.attach(virtualMachineDescriptor);
                    attach.loadAgent("/Users/xxx/Documents/code/gitee/myAgent/target/my-agent-1.0-SNAPSHOT-jar-with-dependencies.jar",
                            "Hello VirtualMachineDescriptor");
                    attach.detach();
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    在这里插入图片描述

    • 重新打包,运行agent包,java -jar attach-demo-1.0-SNAPSHOT-jar-with-dependencies.jar

    在这里插入图片描述

    • 查看待修改程序

    在这里插入图片描述

  • 相关阅读:
    计算机网络学习四(网络层概述)
    C++中的多态
    Python迭代器和生成器
    Vue移动端动态表单生成组件
    【智慧零售】门店管理设备解决方案,为企业数字化运营升级赋能
    Java SpringBoot VII
    [feign]远程调用实现token信息的传递
    温故知新(十一)——IIC
    基于长短期神经网络的可上调容量PUP预测,基于长短期神经网络的可下调容量PDO预测,LSTM可调容量预测
    SpringBoot_JsonFormat日期序列化
  • 原文地址:https://blog.csdn.net/qq_40977118/article/details/126608418