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中

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("========= ========= =========");
    }

}

在这里插入图片描述

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

在这里插入图片描述

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文件
<?xml version="1.0" encoding="UTF-8"?>

<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.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>my-agent</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>my-agent</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

在这里插入图片描述

2.3.2 使用 maven 插件

  • 在manifestEntries中添加配置
<?xml version="1.0" encoding="UTF-8"?>

<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.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>my-agent</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>my-agent</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifestEntries>
                            <Premain-Class>org.example.agent.MyAgent</Premain-Class>
                            <Agent-Class>org.example.agent.MyAgent</Agent-Class>
                            <Can-Redefine-Classes>true</Can-Redefine-Classes>
                            <Can-Retransform-Classes>true</Can-Retransform-Classes>
                            <Can-Set-Native-Method-Prefix>true</Can-Set-Native-Method-Prefix>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

在这里插入图片描述

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启动项目时,做动态修改验证
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>maven-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>maven-demo</name>
    <description>maven-demo</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                    <mainClass>com.example.mavendemo.MavenDemoApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

在这里插入图片描述

  • 启动类
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");
    }

}

在这里插入图片描述

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环境变量时的名字,根据自己的设置进行修改
<?xml version="1.0" encoding="UTF-8"?>

<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.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>attach-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>attach-demo</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>8</version>
            <scope>system</scope>
            <systemPath>${JAVA_8_HOME}/lib/tools.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.example.attach.Attacher</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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

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();
    }
}

在这里插入图片描述

  • 使用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();
            }
        }
    }
}

在这里插入图片描述

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

在这里插入图片描述

  • 查看待修改程序

在这里插入图片描述

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值