转换类无效

时间:2019-09-29 15:24:44

标签: java agent javassist javaagents java-attach-api

根据本教程,我尝试使Java代理正常工作。 https://www.baeldung.com/java-instrumentation#loading-a-java-agent

我确实得到了[Agent] Transforming class TestApplication 我没有错误,但是看不到转换类的任何效果。

最终,我想同时使静态负载和动态负载都能工作,但现在我只关注静态方式。


public class Static_Agent {

    public static void premain(String agentArgs, Instrumentation inst) {
        String[] tokens = agentArgs.split(";");
        String className = tokens[0];
        String methodName = tokens[1];

        System.out.println(">> "+className);
        System.out.println(">> "+methodName);
        transformClass(className, methodName, inst);
    }



    public static void transformClass(String className, String methodName, Instrumentation instrumentation) {
        Class<?> targetCls = null;
        ClassLoader targetClassLoader = null;
        // see if we can get the class using forName
        try {
            targetCls = Class.forName(className);
            targetClassLoader = targetCls.getClassLoader();
            transform(targetCls, methodName, targetClassLoader, instrumentation);
            return;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        // otherwise iterate all loaded classes and find what we want
        for(Class<?> clazz: instrumentation.getAllLoadedClasses()) {
            if(clazz.getName().equals(className)) {
                targetCls = clazz;
                targetClassLoader = targetCls.getClassLoader();
                transform(targetCls, methodName, targetClassLoader, instrumentation);
                return;
            }
        }
        throw new RuntimeException("Failed to find class [" + className + "]");
    }


    public static void transform(Class<?> clazz, String methodName, ClassLoader classLoader, Instrumentation instrumentation) {
        Transformer dt = new Transformer(clazz.getName(), methodName, classLoader);
        instrumentation.addTransformer(dt, true);
        try {
            instrumentation.retransformClasses(clazz);
        } catch (Exception ex) {
            throw new RuntimeException("Transform failed for class: [" + clazz.getName() + "]", ex);
        }
    }



}
public class Transformer implements ClassFileTransformer {


    /** The internal form class name of the class to transform */
    private String targetClassName;
    /** The class loader of the class we want to transform */
    private ClassLoader targetClassLoader;

    private String targetMethodName;

    public Transformer(String targetClassName, String targetMethodName, ClassLoader targetClassLoader) {
        this.targetClassName = targetClassName;
        this.targetClassLoader = targetClassLoader;
        this.targetMethodName = targetMethodName;
    }

    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
            ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
        byte[] byteCode = classfileBuffer;

        String finalTargetClassName = this.targetClassName.replaceAll("\\.", "/");
        if (!className.equals(finalTargetClassName)) {
            return byteCode;
        }

        if (className.equals(finalTargetClassName) && loader.equals(targetClassLoader)) {
            System.out.println("[Agent] Transforming class TestApplication");
            try {

                ClassPool cp = ClassPool.getDefault();
                CtClass cc = cp.get(targetClassName);
                CtMethod m = cc.getDeclaredMethod(targetMethodName);
                m.addLocalVariable("startTime", CtClass.longType);
                m.insertBefore("startTime = System.currentTimeMillis();");

                StringBuilder endBlock = new StringBuilder();

                m.addLocalVariable("endTime", CtClass.longType);
                m.addLocalVariable("opTime", CtClass.longType);
                endBlock.append("endTime = System.currentTimeMillis();");
                endBlock.append("opTime = (endTime-startTime)/1000;");

                endBlock.append("System.out.println(\"[Application] Withdrawal operation completed in:\" + opTime + \" seconds!\");");

                m.insertAfter(endBlock.toString());

                byteCode = cc.toBytecode();
                cc.detach();
            } catch (Exception e) {
                System.out.println("Exception"+e);
            }
        }
        return byteCode;
    }
}
public class TestApplication {

    public static void main(String[] args) {

        try {
            TestApplication.run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void run() throws Exception {
        System.out.println("--- start ---");

        while (true) {
            test();
            Thread.sleep(4_000);
        }


    }


    static int count = 0;

    public static void test() {
        System.out.println(count++);
    }

}

我以以下方式启动

java -javaagent:static_agent.jar="doeke.application.TestApplication;test" -jar application.jar

在有帮助的情况下,该项目位于此处: https://github.com/clankill3r/java_agent

编辑:

在文件末尾附近的Transformer.java中,我现在使用e.printStackTrace();

我收到以下错误:

  

[Agent]转换类TestApplication   javassist.NotFoundException:doeke.application.TestApplication在   javassist.ClassPool.get(ClassPool.java:436)在   doeke.transformer.Transformer.transform(Transformer.java:48)在   java.instrument / java.lang.instrument.ClassFileTransformer.transform(ClassFileTransformer.java:246)     在   java.instrument / sun.instrument.TransformerManager.transform(TransformerManager.java:188)     在   java.instrument / sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:563)     在   java.instrument / sun.instrument.InstrumentationImpl.retransformClasses0(本机   方法)   java.instrument / sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:167)     在doeke.static_agent.Static_Agent.transform(Static_Agent.java:56)     在   doeke.static_agent.Static_Agent.transformClass(Static_Agent.java:34)     在doeke.static_agent.Static_Agent.premain(Static_Agent.java:22)在   java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机   方法)   java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.base / java.lang.reflect.Method.invoke(Method.java:566)在   java.instrument / sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:513)     在   java.instrument / sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:525)

     

---开始---

     

0

     

1

1 个答案:

答案 0 :(得分:6)

感谢提出这个问题,让我有机会对Java Instrumentation有所了解。

花了一些时间来交叉检查示例代码和提供的教程。问题不在于编程代码,而在于如何启动程序。

如果在Transformer.java的transform()方法中添加一些记录器,则会发现在运行后代码路径已损坏:

ClassPool cp = ClassPool.getDefault();

然后,用相同的方法从以下位置替换异常捕获代码:

} catch (Exception e) {

收件人:

} catch (NotFoundException | CannotCompileException | IOException e) {

它将为您提供更多提示,如下所示:

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)
        at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: javassist/NotFoundException
        at doeke.static_agent.Static_Agent.transform(Static_Agent.java:60)
        at doeke.static_agent.Static_Agent.transformClass(Static_Agent.java:40)
        at doeke.static_agent.Static_Agent.premain(Static_Agent.java:28)
        ... 6 more
Caused by: java.lang.ClassNotFoundException: javassist.NotFoundException
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 9 more
FATAL ERROR in native method: processing of -javaagent failed

到目前为止,根本原因更加明显。这是因为在启动程序时,那些与Javassist相关的类(例如ClassPool,CtClass,CtMethod等)在运行时无法引用其相应的库。

因此,解决方案是

  1. 假定您已将static_agent.jar导出到与application.jar相同的“ build”文件夹中

  2. 所有其他文件夹结构与您提供的github中显示的相同

  3. 将“ cd”放入命令控制台中的内部版本文件夹

  4. 修改原始程序启动脚本,如下所示

Windows操作系统:

java -javaagent:static_agent.jar="doeke.application.TestApplication;test" -cp ../libs/javassist-3.12.1.GA.jar;application.jar doeke.application.TestApplication

Unix / Linux操作系统:

java -javaagent:static_agent.jar="doeke.application.TestApplication;test" -cp ../libs/javassist-3.12.1.GA.jar:application.jar doeke.application.TestApplication

您最终将获得预期的结果:

[Agent] In premain method.
>> doeke.application.TestApplication
>> test
[Agent] Transforming class
--- start ---
0
[Application] Withdrawal operation completed in:0 seconds!
1
[Application] Withdrawal operation completed in:0 seconds!

编辑

此外,让我粘贴一些有关如何通过javassist在方法中间插入代码的代码。

如果将TestApplication.java中的test()方法更改为:

line 30    public static void test() {
line 31        System.out.println(count++);
line 32        
line 33        System.out.println("Last line of test() method");
line 34    }

假设我们要在 count ========== 之间添加一条线,假设“这是行分隔符”,结果如下:

1 
-- This is line separator -- 
Last line of test() method

然后,在Transformer.java的transform(...)方法中,可以添加如下代码行:

m.insertAt(32,"System.out.println(\"-- This is line separator --\");");

这将变成:

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    byte[] byteCode = classfileBuffer;

    String finalTargetClassName = this.targetClassName.replaceAll("\\.", "/");
    if (!className.equals(finalTargetClassName)) {
        return byteCode;
    }

    if (className.equals(finalTargetClassName) && loader.equals(targetClassLoader)) {
        System.out.println("[Agent] Transforming class TestApplication");
        try {
            // Step 1 Preparation
            ClassPool cp = ClassPool.getDefault();
            CtClass cc = cp.get(targetClassName);
            CtMethod m = cc.getDeclaredMethod(targetMethodName);

            // Step 2 Declare variables
            m.addLocalVariable("startTime", CtClass.longType);
            m.addLocalVariable("endTime", CtClass.longType);
            m.addLocalVariable("opTime", CtClass.longType);

            // Step 3 Insertion of extra logics/implementation
            m.insertBefore("startTime = System.currentTimeMillis();");

            m.insertAt(32,"System.out.println(\"-- This is line separator --\");");

            StringBuilder endBlock = new StringBuilder();

            endBlock.append("endTime = System.currentTimeMillis();");
            endBlock.append("opTime = (endTime-startTime)/1000;");
            endBlock.append("System.out.println(\"[Application] Withdrawal operation completed in:\" + opTime + \" seconds!\");");

            m.insertAfter(endBlock.toString());

            // Step 4 Detach from ClassPool and clean up stuff
            byteCode = cc.toBytecode();
            cc.detach();
        } catch (NotFoundException | CannotCompileException | IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    return byteCode;
}

最后,将得到如下结果,将结果打印在方法中间:

[Agent] In premain method.
className=doeke.application.TestApplication
methodName=test
>> doeke.application.TestApplication
>> test
[Agent] Transforming class TestApplication
--- start ---
0
-- This is line separator --
=========
[Application] Withdrawal operation completed in:0 seconds!
1
-- This is line separator --
=========
[Application] Withdrawal operation completed in:0 seconds!
2
-- This is line separator --
=========
[Application] Withdrawal operation completed in:0 seconds!