每当分配对象但遇到问题时,我都试图调用静态方法。我已将代码简化为较小的工作示例。

MemorySnifferAgent.java

public class MemorySnifferAgent {
    public static void premain(String agentArguments, Instrumentation instrumentation) {
        Callback.main(); //Make sure the class is loaded ?
        instrumentation.addTransformer(new MemoryAllocationTransformer());
    }
}


MemoryAllocationTransformer.java

public class MemoryAllocationTransformer implements ClassFileTransformer {
    public byte[] instrumentByteCode(byte[] bytecode) {
        ClassReader reader = new ClassReader(bytecode);
        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        CustomClassReader customVisitor = new CustomClassReader(Opcodes.ASM4, writer);
        reader.accept(customVisitor, 0);
        return writer.toByteArray();
    }

    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
        byte[] bytes = instrumentByteCode(classfileBuffer);
        return  bytes;
    }
}


CustomClassReader.java

public class CustomClassReader extends ClassVisitor {
    public CustomClassReader(int api, ClassWriter classWriter) {
        super(api, classWriter);
    }

    public MethodVisitor visitMethod(final int access, final String name,
                                     final String desc, final String signature, final String[] exceptions) {
        MethodVisitor methodWritter = super.visitMethod(access, name, desc, signature, exceptions);
        return new CustomMethodWritter(api, name, methodWritter);
    }
}


CustomMethodWritter.java

class CustomMethodWritter extends MethodVisitor {
    String name;

    public CustomMethodWritter(int i, String name, MethodVisitor methodWriter) {
        super(i, methodWriter);
        this.name = name;
    }

    @Override
    public void visitTypeInsn(int opcode, String type) {
        super.visitTypeInsn(opcode, type);
        if (opcode == Opcodes.NEW) {
            super.visitMethodInsn(Opcodes.INVOKESTATIC, "com/chasingnanos/Callback", "onAllocation", "()V", false);
        }
    }
}


注意:


我意识到,要利用所有分配,就必须要利用所有NEW *操作码和反射API
super.visitMethodInsn(Opcodes.INVOKESTATIC,“ com / chasingnanos / Callback”,“ onAllocation”,“()V”,false);似乎是问题所在。虽然我找不到问题所在。


道歉,如果这是一个非常基本的问题。我是字节码操作的新手。

我得到的错误:

java.lang.NoClassDefFoundError
 -klass:“ java / lang / NoClassDefFoundError”

最佳答案

我认为问题与不同的ClassLoader提供的隔离性有关。特别是代理的代码和“主”代码是由不同的Classloader加载的,因此您不能从“主”代码中调用Callback。解决此问题的方法是将Callback类移动到另一个jar,然后使用-Xbootclasspath/a选项将其添加到引导类路径中。

另见类似问题
Java NoClassDefFoundError when calling own class from instrumented method

更新(过滤)

如果不是要使用引导程序,而是想过滤掉无法访问Callback的类,则可能应该执行以下操作,而不是仅检查null

public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    // check if the class inside the loader can access Callback
    // by analyzing its hierarchy of parent class loaders
    for (ClassLoader curLoader = loader; ; curLoader = curLoader.getParent()) {
        if (curLoader == null) {
            System.out.println("Skip '" + className + "' for " + loader);
            return null;
        } else if (curLoader == Callback.class.getClassLoader())
            break;
    }
    byte[] bytes = instrumentByteCode(classfileBuffer);
    return bytes;
}

关于java - 通过检测字节码调用类的静态方法时,出现NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42660451/

10-14 11:18