使用java動態位元組碼技術簡單實現arthas的trace功能。

xShower發表於2020-11-03

參考資料

ASM 系列詳細教程

編譯時,找不到asm依賴


用過[Arthas]的都知道,Arthas是alibaba開源的一個非常強大的Java診斷工具。

不管是線上還是線下,我們都可以用Arthas分析程式的執行緒狀態、檢視jvm的實時執行狀態、列印方法的出入參和返回型別、收集方法中每個程式碼塊耗時,

甚至可以監控類、方法的呼叫次數、成功次數、失敗次數、平均響應時長、失敗率等。

 

前幾天學習java動態位元組碼技術時,突然想起這款java診斷工具的trace功能:列印方法中每個節點的呼叫耗時。簡簡單單的,正好拿來做動態位元組碼入門學習的demo。

程式結構

src
 ├── agent-package.bat
 ├── java
 │   ├── asm
 │   │   ├── MANIFEST.MF
 │   │   ├── TimerAgent.java
 │   │   ├── TimerAttach.java
 │   │   ├── TimerMethodVisitor.java
 │   │   ├── TimerTrace.java
 │   │   └── TimerTransformer.java
 │   └── demo
 │       ├── MANIFEST.MF
 │       ├── Operator.java
 │       └── Test.java
 ├── run-agent.bat
 ├── target-package.bat
 └── tools.jar

  

編寫目標程式

 程式碼

package com.gravel.demo.test.asm;

/**
 * @Auther: syh
 * @Date: 2020/10/12
 * @Description:
 */
public class Test {
    public static boolean runnable = true;
    public static void main(String[] args) throws Exception {
        while (runnable) {
            test();
        }
    }

    // 目標:分析這個方法中每個節點的耗時
    public static void test() throws Exception {
        Operator.handler();
        long time_wait = (long) ((Math.random() * 1000) + 2000);
        Operator.callback();
        Operator.pause(time_wait);
    }
}

  

Operator.java

/**
 * @Auther: syh
 * @Date: 2020/10/28
 * @Description: 輔助類,同樣可用於分析耗時
 */
public class Operator {

    public static void handler() throws Exception {
        long time_wait = (long) ((Math.random() * 10) + 20);
        sleep(time_wait);
    }

    public static void callback() throws Exception {
        long time_wait = (long) ((Math.random() * 10) + 20);
        sleep(time_wait);
    }

    public static void pause(long time_wait) throws Exception {
        sleep(time_wait);
    }

    public static void stop() throws Exception {
        Test.runnable = false;
        System.out.println("business stopped.");
    }

    private static void sleep(long time_wait) throws Exception {
        Thread.sleep(time_wait);
    }
}

  

MANIFEST.MF

編寫MANIFEST.MF檔案,指定main-class。注意:冒號後面加空格,結尾加兩行空白行。

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: syh
Created-By: Apache Maven
Build-Jdk: 1.8.0_202
Main-Class: com.gravel.demo.test.asm.Target

  

打包

偷懶寫了bat批命令,生成target.jar

@echo off & setlocal
attrib -s -h -r -a /s /d demo
rd /s /q demo
rd /q target.jar
javac -encoding utf-8 -d . ./java/demo/*.java
jar cvfm target.jar ./java/demo/MANIFEST.MF demo
rd /s /q demo
pause
java -jar target.jar

  

java agent探針

instrument 是 JVM 提供的一個可以修改已載入類檔案的類庫。而要實現程式碼的修改,我們需要實現一個 instrument agent。

jdk1.5時,agent有個內定方法premain。是在類載入前修改。所以無法做到修改正在執行的類。
jdk1.6後,agent新增了agentmain方法。agentmain是在虛擬機器啟動以後載入的。所以可以做攔截、熱部署等。

講JAVA探針技術,實際上我自己也是半吊子。所以這裡用的是邊分析別人例子邊摸索的思路來實現我的簡單的trace功能。
例子使用的是ASM位元組碼生成框架

MANIFEST.MF

首先一個可用的jar,關鍵之一是MAINFEST.MF檔案是吧。

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: syh
Build-Jdk: 1.8.0_202
Agent-Class: asm.TimerAgent
Can-Retransform-Classes: true
Can-Redefine-Classes: true
Class-Path: ./tools.jar
Main-Class: asm.TimerAttach

  

我們從MANIFEST.MF中提取幾個關鍵的屬性

屬性
 說明

Agent-Class

agentmain入口類

 

Premain-Class

 

premain入口類,與agent-class至少指定一個。

Can-Retransform-Classes

 

對於已經載入的類重新進行轉換處理,即會觸發重新載入類定義。

 

Can-Redefine-Classes

 

對已經載入的類不做轉換處理,而是直接把處理結果(bytecode)直接給JVM

 

Class-Path

 

asm動態位元組碼技術依賴tools.jar,如果沒有可以從jdk的lib目錄下拷貝。

 

Main-Class

 

這裡並不是agent的關鍵屬性,為了方便,我把載入虛擬機器的程式和agent合併了。

 

程式碼

然後我們來看看兩個入口類,首先分析一個可執行jar的入口類Main-Class。

public class TimerAttach {

    public static void main(String[] args) throws Exception {
        /**
         * 啟動jar時,需要指定兩個引數:1目標程式的pid。 2 要修改的類路徑及方法,格式 package.class#methodName
         */
        if (args.length < 2) {
            System.out.println("pid and class must be specify.");
            return;
        }

        if (!args[1].contains("#")) {
            System.out.println("methodName must be specify.");
            return;
        }

        VirtualMachine vm = VirtualMachine.attach(args[0]);
        // 這裡為了方便我把 vm和agent整合在一個jar裡面了, args[1]就是agentmain的入參。
        vm.loadAgent("agent.jar", args[1]);
    }
}

  

程式碼很簡單,1:args入參校驗;2:載入目標程式pid(args[0]);3:載入agent jar包(因為合併了,所以這個jar其實就是自己)。

其中vm.loadAgent(agent.jar, args[1])會呼叫agent-class中的agentmain方法,而args[1]就是agentmain的第一個入參。

public class TimerAgent {
    public static void agentmain(String agentArgs, Instrumentation inst) {
        String[] ownerAndMethod = agentArgs.split("#");
        inst.addTransformer(new TimerTransformer(ownerAndMethod[1]), true);
        try {
            inst.retransformClasses(Class.forName(ownerAndMethod[0]));
            System.out.println("agent load done.");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("agent load failed!");
        }
    }
}

  

在 agentmain 方法裡,我們呼叫retransformClassess方法載入目標類,呼叫addTransformer方法載入TimerTransformer類實現對目標類的重新定義。

類轉換器

public class TimerTransformer implements ClassFileTransformer {
    private String methodName;

    public TimerTransformer(String methodName) {
        this.methodName = methodName;
    }

    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) {
        ClassReader reader = new ClassReader(classFileBuffer);
        ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        ClassVisitor classVisitor = new TimerTrace(Opcodes.ASM5, classWriter, methodName);
        reader.accept(classVisitor, ClassReader.EXPAND_FRAMES);
        return classWriter.toByteArray();
    }
}

  

對被匹配到的類中的方法進行修改

public class TimerTrace extends ClassVisitor implements Opcodes {
    private String owner;
    private boolean isInterface;
    private String methodName;

    public TimerTrace(int i, ClassVisitor classVisitor, String methodName) {
        super(i, classVisitor);
        this.methodName = methodName;
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        super.visit(version, access, name, signature, superName, interfaces);
        owner = name;
        isInterface = (access & ACC_INTERFACE) != 0;
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature,
                                     String[] exceptions) {
        MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
        // 匹配到指定methodName時,進行位元組碼修改
        if (!isInterface && mv != null && name.equals(methodName)) {

            // System.out.println("    package.className:methodName()")
            mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");

            mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);

            mv.visitLdcInsn("    " + owner.replace("/", ".")
                    + ":" + methodName + "() ");
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                    "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);

            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);

            mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);

            // 方法程式碼塊耗時統計並列印
            TimerMethodVisitor at = new TimerMethodVisitor(owner, access, name, descriptor, mv);
            return at.getLocalVariablesSorter();
        }
        return mv;
    }

    public static void main(String[] args) throws IOException {
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        TraceClassVisitor tv = new TraceClassVisitor(cw, new PrintWriter(System.out));
        TimerTrace addFiled = new TimerTrace(Opcodes.ASM5, tv, "test");
        ClassReader classReader = new ClassReader("demo.Test");
        classReader.accept(addFiled, ClassReader.EXPAND_FRAMES);

        File file = new File("out/production/asm-demo/demo/Test.class");
        String parent = file.getParent();
        File parent1 = new File(parent);
        parent1.mkdirs();
        file.createNewFile();
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(cw.toByteArray());
    }
}

  

要統計方法中每行程式碼耗時,只需要在每一行程式碼的前後加上當前時間戳然後相減即可。

所以我們的程式碼是這麼寫的。

public class TimerMethodVisitor extends MethodVisitor implements Opcodes {
    private int start;
    private int end;
    private int maxStack;
    private String lineContent;
    public boolean instance = false;
    private LocalVariablesSorter localVariablesSorter;
    private AnalyzerAdapter analyzerAdapter;

    public TimerMethodVisitor(String owner, int access, String name, String descriptor, MethodVisitor methodVisitor) {
        super(Opcodes.ASM5, methodVisitor);
        this.analyzerAdapter = new AnalyzerAdapter(owner, access, name, descriptor, this);
        localVariablesSorter = new LocalVariablesSorter(access, descriptor, this.analyzerAdapter);
    }

    public LocalVariablesSorter getLocalVariablesSorter() {
        return localVariablesSorter;
    }

    /**
     * 進入方法後,最先執行
     * 所以我們可以在這裡定義一個最開始的時間戳, 然後建立一個區域性變數var_end
     * Long var_start = System.nanoTime();
     * Long var_end;
     */
    @Override
    public void visitCode() {
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "nanoTime", "()J", false);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
        start = localVariablesSorter.newLocal(Type.LONG_TYPE);
        mv.visitVarInsn(ASTORE, start);


        end = localVariablesSorter.newLocal(Type.LONG_TYPE);

        maxStack = 4;
    }

    /**
     * 在每行程式碼後面增加以下程式碼
     * var_end = System.nanoTime();
     * System.out.println("[" + String.valueOf((var_end.doubleValue() - var_start.doubleValue()) / 1000000.0D) + "ms] " + "package.className:methodName() #lineNumber");
     * var_start = var_end;
     * @param lineNumber
     * @param label
     */
    @Override
    public void visitLineNumber(int lineNumber, Label label) {
        super.visitLineNumber(lineNumber, label);
        if (instance) {
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "nanoTime", "()J", false);
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
            mv.visitVarInsn(ASTORE, end);

            // System.out
            mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");

            // new StringBuilder();
            mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);

            mv.visitLdcInsn("        -[");
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                    "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);

            mv.visitVarInsn(ALOAD, end);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "doubleValue", "()D", false);
            mv.visitVarInsn(ALOAD, start);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "doubleValue", "()D", false);
            mv.visitInsn(DSUB);
            mv.visitLdcInsn(new Double(1000 * 1000));
            mv.visitInsn(DDIV);
            // String.valueOf((end - start)/1000000)
            mv.visitMethodInsn(INVOKESTATIC, "java/lang/String", "valueOf", "(D)Ljava/lang/String;", false);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                    "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);

            mv.visitLdcInsn("ms] ");
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                    "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);

            // .append("owner:methodName() #line")
            mv.visitLdcInsn(this.lineContent + "#" + lineNumber);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                    "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);

            // stringBuilder.toString()
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);

            // println stringBuilder.toString()
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);

            // start = end
            mv.visitVarInsn(ALOAD, end);
            mv.visitVarInsn(ASTORE, start);

            maxStack = Math.max(analyzerAdapter.stack.size() + 4, maxStack);
        }
        instance = true;
    }

    /**
     * 拼接位元組碼內容
     * @param opcode
     * @param owner
     * @param methodName
     * @param descriptor
     * @param isInterface
     */
    @Override
    public void visitMethodInsn(int opcode, String owner, String methodName, String descriptor, boolean isInterface) {
        super.visitMethodInsn(opcode, owner, methodName, descriptor, isInterface);
        if (!isInterface && opcode == Opcodes.INVOKESTATIC) {
            this.lineContent = owner.replace("/", ".")
                    + ":" + methodName + "() ";
        }
    }

    @Override
    public void visitMaxs(int maxStack, int maxLocals) {
        super.visitMaxs(Math.max(maxStack, this.maxStack), maxLocals);
    }
}

  

如果初學者不會改位元組碼。可以利用idea自帶的asm外掛做參考。

使用java動態位元組碼技術簡單實現arthas的trace功能。

 

這樣,一個可執行的agent jar就寫完了,然後打包

@echo off
attrib -s -h -r -a /s /d asm
rd /s /q asm
rd /q agent.jar
javac -XDignore.symbol.file=true -encoding utf-8 -d . ./java/asm/*.java
jar cvfm agent.jar ./java/asm/MANIFEST.MF asm
rd /s /q asm
exit

  

測試

執行目標程式 target.jar

java -jar target.jar

  

列印Test.test中每個節點耗時

java -jar agent.jar [pid] demo.Test#test

  

結果

使用java動態位元組碼技術簡單實現arthas的trace功能。

 

列印Operator.handler方法每個節點耗時

使用java動態位元組碼技術簡單實現arthas的trace功能。

相關文章