java字节码增强javaagent+javassist使用

  • Post author:
  • Post category:java


引言

作为一个运行时平台,平台的监控是保证平台稳定运行的重要一环。我们可以根据监控的日间交易总量、交易时间分布、服务响应时间、服务链路、服务异常率、sql执行时间、缓存命中率及服务器性能等数据进行相应调整,保证系统高可用

监控的主体思路为进行埋点,实现方式有以下几点:

1.硬编码,该种方式代码侵入性大,复杂度高,不可复用

2.AOP,该种方式是在运行是进行的,性能损耗较大,可以复用

3.javassist,该种方式是在虚拟机启动时改变目标对象的字节码,性能损耗小,可以复用

下面我们来使用javaagent实现拦截,使用javassist来实现字节码增强。

一、增加依赖

<?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>com.jiaobao</groupId>
  <artifactId>javassistDemo</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>javassistDemo</name>

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

  <dependencies>
    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.24.0-GA</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
          <configuration>
            <archive>
              <manifestEntries>
                <Project-name>${project.name}</Project-name>
                <Project-version>${project.version}</Project-version>
                <Premain-Class>com.jiaobao.PerfMonAgent</Premain-Class>
                <Boot-Class-Path>javassist-3.24.0-GA.jar</Boot-Class-Path>
                <Can-Redefine-Classes>false</Can-Redefine-Classes>
              </manifestEntries>
            </archive> 
            <skip>true</skip>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

二、创建agent的jar

作为agent的jar需要具备两个条件

1.实现premain方法(步骤二)

2.在MANIFEST.MF文件中有Premain-Class(maven可在pom文件中指定,普通java工程可以自己创建该文件)

步骤一:创建ClassFileTransformer实现类

package com.jiaobao;

import javassist.*;

import java.io.ByteArrayInputStream;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;

    public class PerfMonXformer implements ClassFileTransformer {
    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {

        byte[] transformed = null;
        System.out.println("transforming   "+className);
        ClassPool pool = ClassPool.getDefault();
        CtClass cl = null;
        
        try {
            cl = pool.makeClass(new ByteArrayInputStream(classfileBuffer));
            if(cl.isInterface() == false){//如果不是接口
                //获取方法声明的集合并做相应处理
                CtBehavior[] methods = cl.getDeclaredBehaviors();
                for(int i = 0; i< methods.length; i++){
                    /**
                     * Modifier.isNative(methods[i].getModifiers())过滤本地方法,否则会报
                     * javassist.CannotCompileException: no method body  at javassist.CtBehavior.addLocalVariable()
                     * 报错原因如下
                     * 来自Stack Overflow网友解答
                     * Native methods cannot be instrumented because they have no bytecodes.
                     * However if native method prefix is supported ( Transformer.isNativeMethodPrefixSupported() )
                     * then you can use Transformer.setNativeMethodPrefix() to wrap a native method call inside a non-native call
                     * which can then be instrumented
                     */
                    if (methods[i].isEmpty() == false && !Modifier.isNative(methods[i].getModifiers())){
                        doMethod(methods[i]);
                    }
                }
                //将修改后的CtClass对象转化为字节码
                transformed = cl.toBytecode();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(cl != null){
                cl.detach();
            }
        }
        //返回修改后的字节码
        return transformed;
    }

    /**
     * 对方法做处理
     * @param method
     */
    private void doMethod(CtBehavior method) throws CannotCompileException {
        //增加本地变量
        method.addLocalVariable("startTime", CtClass.longType);
        method.addLocalVariable("endTime", CtClass.longType);
        //在方法前加入
        method.insertBefore("startTime = System.nanoTime(); System.out.println(\"enter " + method.getName() + " time \" + startTime);");
        //在方法后加入
        method.insertAfter("endTime = System.nanoTime(); System.out.println(\"leave " + method.getName() + " time \" + endTime);");
        method.insertAfter(" System.out.println(\"time difference " + method.getName() + " \" +(endTime - startTime));");
    }
}

步骤二:编写agent类,需要实现premain方法

package com.jiaobao;

import java.lang.instrument.Instrumentation;

public class PerfMonAgent {
    private static Instrumentation inst = null;

    public static void premain(String agentArgs, Instrumentation _inst){
        System.out.println("PerfMonAgent.premain() was called...");
        inst = _inst;
        System.out.println("Adding a PerfMonXformer instance to the JVM...");
        inst.addTransformer(new PerfMonXformer());
    }

}

步骤三:打包agent,此处以idea中maven工程为例

三、测试

步骤一:测试代码

package com.jiaobao;

public class HelloWorld {
    public String sayHello(String name){
        String sayHello = "hello " + name;
        return sayHello;
    }

    public static void main(String[] args){
        HelloWorld hl = new HelloWorld();
        System.out.println(hl.sayHello("zhang"));
    }
}

步骤二:增加javaagent参数

步骤三:运行HelloWorld中main方法,结果如图

文章为本人原创,如有不正确的地方请大家指正,欢迎大家在下方留言讨论



版权声明:本文为zh2508原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。