Javassist的CtMethod.insertAt(line,src)仪器代码在错误的字节码位置 [英] Javassist's CtMethod.insertAt(line,src) instruments code at the wrong bytecode position

查看:69
本文介绍了Javassist的CtMethod.insertAt(line,src)仪器代码在错误的字节码位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在每个基本代码块的开头插入一些检测代码.使用Javaassist的ControlFlow.Block和CtMethod.insertAt()似乎是一个相当简单的任务.到目前为止,这是相关的代码块(位于转换函数中):

My goal is to insert a little bit of instrumentation code at the beginning of each basic block of code. It seems like a fairly simple task with Javaassist's ControlFlow.Block and CtMethod.insertAt(). Here's the relevant chunk of code so far (it's located in the transform function):

ControlFlow flow=new ControlFlow(m); //m is the CtMethod currently being instrumented
Block[] blockArray=flow.basicBlocks();
for(Block thisbb : blockArray){

    //Dynamically Update Method Statistics
    String blockUpdate=new String();
    String thisbbIndex=Integer.toString(thisbb.index());

    blockUpdate+=mse+".setBlockIndex("+thisbbIndex+"); ";
    blockUpdate="{ " + blockUpdate + "} ";

    //Insert
    int pos=m.getMethodInfo().getLineNumber(thisbb.position()); //Source code line position from binary line position
    System.out.print("At "+pos+": "+blockUpdate);
    int n=m.insertAt(pos, blockUpdate);
    System.out.println(" -> "+n);
}

请注意, CtMethod.insertAt(line,srcCode)中的"line"参数是源代码行的位置,而不是 bytecode 行位置.在源代码中,一些基本块报告了相同的行号!输出如下:

Note that the "line" parameter in CtMethod.insertAt(line,srcCode) is the source code line position, not the bytecode line position. In the source code, some basic blocks are reporting the same line number! Here is the output:

At 6: { _JDA_mse.setBlockIndex(0); }  -> 6
At 8: { _JDA_mse.setBlockIndex(1); }  -> 8
At 8: { _JDA_mse.setBlockIndex(2); }  -> 8
At 8: { _JDA_mse.setBlockIndex(3); }  -> 8
At 8: { _JDA_mse.setBlockIndex(4); }  -> 8
At 8: { _JDA_mse.setBlockIndex(5); }  -> 8
At 8: { _JDA_mse.setBlockIndex(6); }  -> 8

在#处代表我请求将代码放置在的位置,而->#表示源代码中实际插入的位置(如果一切正常,则它们应该相同). {...} 内的所有内容都是我要放置的代码( _JDA_mse 是我使用Javassist方法添加到函数中的局部变量,因此在使用它.)

At # represents the location that I requested the code to be placed at, and the -> # represents the location in the source code that it was actually inserted (if everything works out, they should be the same). Everything inside the { ... } is the code that I want placed (_JDA_mse is a local variable that I added to the function with Javassist methods, so there's no problem in using it).

问题在于, for(int i = 0; i< size; ++ i)包含多个基本块,这些基本块在源代码中不可分离,但在字节码中却明显不同.这就是为什么将多个基本块映射到同一条源代码行的原因,它仅表明源代码行不能提供足够的检测精度来记录基本块.是否可以模拟CtMethod.insertAt(bytecodePosition,srcString)而不是使用提供的CtMethod.insertAt(sourceLine,srcString)?

The issue is that for(int i=0; i<size; ++i) contains multiple basic blocks that are inseparable in the source code but clearly distinct in the bytecode. This is why multiple basic blocks are being mapped to the same source line, and it just shows that source code lines do not provide sufficient instrumentation accuracy to log basic blocks. Is there a way to emulate a CtMethod.insertAt(bytecodePosition,srcString) instead of using the provided CtMethod.insertAt(sourceLine,srcString)?

推荐答案

请参见

See this work around if you need to instrument at the bytecode level using variables that were inserted by Javaassist.

这篇关于Javassist的CtMethod.insertAt(line,src)仪器代码在错误的字节码位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆