gpt4 book ai didi

java - 使用 javaagents 在 Java 中进行基本 block 日志记录

转载 作者:搜寻专家 更新时间:2023-11-01 03:40:22 25 4
gpt4 key购买 nike

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

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

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

使用 Javassist 声明变量如下:

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

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

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

最佳答案

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

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

int mseCSindex=m.getMethodInfo().getCodeAttribute().getMaxLocals()-1;

接下来,我需要表中的 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){}
}

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

        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);
}

它很乱而且有可能完全摧毁自己,但它奏效了!

关于java - 使用 javaagents 在 Java 中进行基本 block 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16248610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com