使用 javaagents 在 Java 中进行基本块日志记录

Sue*_*ode 5 java instrumentation javassist

我的目标是在一些 Java 字节码的每个基本块的开头插入少量代码。目的是通过基本块记录执行路径。目前,我正在使用 Javassist 在方法的开头和结尾处检测一些代码,但我在使用 Javassist API 检测更精细的字节码位置时遇到了困难。

Javassist 为我提供了一个 Block[],它表示方法中的所有基本块。基本块可以报告它们的字节码位置,因此我知道我的仪器需要去哪里。我想使用的 Javassist 工具是 CtMethod.insertAt(int sourceCodeLineNumber,String newSourceCode),但是这使用源代码行号而不是字节码行号,这会导致这个问题中说明的问题(我相当确定没有至此解决该问题)。

问题:当变量本身来自 Javassist 检测时,如何使用检测为方法内的变量赋值。我最好不必使用其他工具,但目前我正在寻求任何可以获得的帮助。

使用 Javassist 声明变量如下:

//m is a CtMethod           
try { m.addLocalVariable("someVar", ClassPool.getDefault().get("SomePackage.SomeClass")); } 
catch (NotFoundException e) { e.printStackTrace(); }
Run Code Online (Sandbox Code Playgroud)

我最坏的情况是以某种方式推断 javassist 检测的堆栈变量,并使用遍历整个方法/类的迭代器插入字节码,但这真的很糟糕。我的方法只有一个整数输入(块 ID)和 void 输出,因此 Java 字节码在每个基本块的开头看起来像这样:

ALOAD 6 //6 is my new Javassist variable ID, however I don't think I can get Javassist to actually tell it to me
ICONST_1 //My parameters, which is an int.  I'd have to switch statement between ICONST, SIPUSH, and ALOAD depending on basic block's index size
INVOKEVIRTUAL SomePackage/SomeClass.SomeMethod(I)V
Run Code Online (Sandbox Code Playgroud)

Sue*_*ode 1

我发现从 ConstPool 表获取变量 ID 的最佳方法是在插入变量后测试最大大小

    try { m.addLocalVariable(mse, ClassPool.getDefault().get("org.javadynamicanalyzer.MethodStackEntry")); } 
    catch (NotFoundException e) { e.printStackTrace(); }

    int mseCSindex=m.getMethodInfo().getCodeAttribute().getMaxLocals()-1;
Run Code Online (Sandbox Code Playgroud)

接下来,我需要表中的 invokevirtual 索引。找到这个有点麻烦。下面的代码在 ConstPool 表中搜索我要查找的函数。我正在寻找的函数位于org.javadyanmicanalyzer.MethodStackEntry.setBlockID(int).

        int virtFunIndex=-1;
        boolean found=false;
        while(found==false){
            ++virtFunIndex;
            try{ 
                int id=cp.isMember("org.javadynamicanalyzer.MethodStackEntry", "setBlockIndex", virtFunIndex);
                if(id!=0){
                    found=true;
                }
            }
            catch(NullPointerException | ClassCastException e){}
        }
Run Code Online (Sandbox Code Playgroud)

最后,我需要检测每个块的开头:

        int len=new ControlFlow(m).basicBlocks().length;
        for(int i=0; i<len; ++i){
            Block thisbb=new ControlFlow(m).basicBlocks()[i]; //we have to re-evaluate the control flow every time we add new code
            CodeIterator itr=m.getMethodInfo().getCodeAttribute().iterator();

            int pos=thisbb.position();
            byte[] newCode=new byte[]{Bytecode.ALOAD, //loads the mse class
                                      mseCSindex, //my mse local variable
                                      Bytecode.ICONST_0, //the input to the virtual function
                                      Bytecode.INVOKEVIRTUAL, //execute the virtual function
                                      (byte) virtFunIndex>>8, //virtual function's address
                                      (byte) virtFunIndex && 0xFF};

            int n = itr.insertAt(pos, newCode);
        }
Run Code Online (Sandbox Code Playgroud)

它很混乱,并且有可能完全摧毁自己,但它成功了!