使用ASM监视java.lang.ProcessBuilder

时间:2015-01-16 17:55:42

标签: java bytecode instrumentation java-bytecode-asm

我希望使用ASM工具创建一个监视器来记录对java.lang.ProcessBuilder的调用。但是,我的尝试似乎失败了。下面是一些示例代码。基本上,这段代码只是为java.lang.ProcessBuilder的构造函数添加了一个nop,然后将新的(和原始的)类文件写入修改后的类目录。这完全是为了测试,因为我想确保一切正常,我可以在运行代理之外检查更改的字节码。如果我将此示例代码指向另一个(非java.lang)类,则一切正常。是否可以使用javaagent修改java.lang.ProcessBuilder?

提前致谢,

杰里米

public class Instrumentor {

public static void agentmain(String agentArgs, Instrumentation instrumentation) {
    instrument(instrumentation);
}

public static void premain(String agentArgument, Instrumentation instrumentation) {
    instrument(instrumentation);
}

private static void instrument(Instrumentation instrumentation) {
    try {
        instrumentation.addTransformer(new SampleTransformer());
    } catch (TransformerException ex) {
        Logger.getLogger(Instrumentor.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

//--------------------------------
public class SampleTransformer implements ClassFileTransformer {

public SampleTransformer() throws TransformerException {
}

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    if ("java/lang/ProcessBuilder".equals(className)) {
        System.out.println("attempting to process " + className);
        ClassReader cr = new ClassReader(classfileBuffer);
        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
        SampleClassAdapter adapter = new SampleClassAdapter(cw);
        cr.accept(adapter, 0);
        byte[] clz = cw.toByteArray();
        try {
            System.out.println("write " + className);
            File p = new File("target\\modified-classes\\");
            p.mkdirs();
            File f = new File(p, className.replace('/', '.') + ".class");
            File o = new File(p, className.replace('/', '.') + ".original.class");
            Files.write(Paths.get(f.getAbsolutePath()), clz);
            Files.write(Paths.get(o.getAbsolutePath()), classfileBuffer);
        } catch (IOException ex) {
            Logger.getLogger(SampleTransformer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return clz;
    }
    return classfileBuffer;
}
}
//------------------------
public class SampleClassAdapter extends ClassVisitor {

public SampleClassAdapter(ClassVisitor cv) {
    super(Opcodes.ASM5, cv);
}

@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
    super.visit(version, access, name, signature, superName, interfaces);
}

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    if (mv != null && "<init>".equals(name)) {
        mv = new SampleMethodAdapter(mv, access, name, desc);
    }
    return mv;
}
}
//----------------------------------------
public class SampleMethodAdapter extends AdviceAdapter {

public SampleMethodAdapter(final MethodVisitor methodVisitor,
        final int access, final String name, final String descriptor) {
    super(Opcodes.ASM5, methodVisitor, access, name, descriptor);
}

@Override
protected void onMethodEnter() {
    mv.visitInsn(Opcodes.NOP);
}

}

2 个答案:

答案 0 :(得分:0)

您可以在运行时修改该类,但首先我建议您尝试编译您在引导类路径上覆盖的自己的版本。一旦完成这项工作,您就可以使用ASM来使代码注入在运行时工作。

答案 1 :(得分:0)

我弄清楚问题是什么。要接受的ClassReader调用需要包含EXPAND_FRAMES标志。

cr.accept(adapter, ClassReader.EXPAND_FRAMES);

在更正此调用后,我能够成功修改java.lang.ProcessBuilder的字节代码。