当前位置: 首页 > news >正文

Java Agent入门教程

目录

  • 1. Java Agent简介
  • 2. premain方式
    • 2.1 新建工程
    • 2.2 创建agent类
    • 2.3 指定agent启动类
      • 2.3.1 使用MANIFEST.MF
      • 2.3.2 使用 maven 插件
    • 2.4 打包
    • 2.5 创建一个springboot工程
    • 2.6 验证
  • 3. agentmain方式
    • 3.1 新建工程
    • 3.2 验证
      • 3.2.1 指定进程号方式
      • 3.2.2 指定程序名称方式

1. Java Agent简介

  • java agent是一个专门制作的jar文件,可以动态修改字节码文件。它利用JVM提供的Instrumentation API可以在 JVM 执行字节码之前或在JVM 加载字节码之后对字节码文件进行修改。
  • 如果要使用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

在这里插入图片描述

  • 查看待修改程序

在这里插入图片描述

相关文章:

  • 航班信息查询 易语言代码
  • C++ 小游戏 视频及资料集(四)
  • 利用HFSS-API设计指数渐变传输线
  • js的es6
  • 开源交流丨任务or实例 详解大数据DAG调度系统Taier任务调度
  • 配置Tomcat时系统环境变量已经配置好,但是启动Tomcat时还是闪退的解决办法
  • app,小程序打包
  • CVPR2022|比VinVL快一万倍!人大提出交互协同的双流视觉语言预训练模型COTS,又快又好!
  • 2023年考武汉安全员证有什么作用?安全员岗位职责是什么?甘建二
  • 开户许可证识别 易语言代码
  • ESP8266-Arduino编程实例-MLX90615红外测温仪驱动
  • 【算法leetcode】1837. K 进制表示下的各位数字总和(rust和go是真的好用)
  • Java中的实体类为什么要 implements Serializable?
  • 第十七天计算机视觉之光学字符识别OCR
  • Transformer架构
  • echarts的各种常用效果展示
  • Java多线程(4):使用线程池执行定时任务
  • leetcode讲解--894. All Possible Full Binary Trees
  • opencv python Meanshift 和 Camshift
  • python大佬养成计划----difflib模块
  • React as a UI Runtime(五、列表)
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • SQLServer之创建数据库快照
  • TCP拥塞控制
  • vagrant 添加本地 box 安装 laravel homestead
  • Web Storage相关
  • Windows Containers 大冒险: 容器网络
  • 仿天猫超市收藏抛物线动画工具库
  • 后端_ThinkPHP5
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 如何利用MongoDB打造TOP榜小程序
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 温故知新之javascript面向对象
  • 源码安装memcached和php memcache扩展
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • ​linux启动进程的方式
  • ​如何在iOS手机上查看应用日志
  • #传输# #传输数据判断#
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • $forceUpdate()函数
  • (42)STM32——LCD显示屏实验笔记
  • (C++17) std算法之执行策略 execution
  • (Python第六天)文件处理
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (独孤九剑)--文件系统
  • (附源码)python房屋租赁管理系统 毕业设计 745613
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (一)kafka实战——kafka源码编译启动
  • (一)u-boot-nand.bin的下载
  • (转)关于pipe()的详细解析
  • (转)清华学霸演讲稿:永远不要说你已经尽力了