为什么使用StackTraceElement时GetlineNumber返回-1
我想在仪器使用Java字节码时获取当前的代码行号。仪器是通过ASM实现的。在访问码之后插入与getlineNumber相对应的字节码,返回值为-1,但在其他位置通过仪器获得的返回值是正常的。
例如,根据ASM的逻辑,源代码如下
public static int add(int a, int b){
int sum = a + b;
return sum;
}
,获取行号信息的字节码应在添加方法之后插入。 但是,当我在主方法中调用该函数时,获得的行号
同时为-1,我还分析了仪器前后的汇编代码,如下
//this is before instrumentation
public static int add(int, int);
Code:
0: iload_0
1: iload_1
2: iadd
3: istore_2
4: iload_2
5: ireturn
//this is after instrumentation
public static int add(int, int);
Code:
0: new #33 // class java/lang/StringBuilder
3: dup
4: invokespecial #34 // Method java/lang/StringBuilder."<init>":()V
7: ldc #36 // String _
9: invokevirtual #40 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
12: invokestatic #46 // Method java/lang/Thread.currentThread:()Ljava/lang/Thread;
15: invokevirtual #50 // Method java/lang/Thread.getStackTrace:()[Ljava/lang/StackTraceElement;
18: iconst_1
19: aaload
20: invokevirtual #56 // Method java/lang/StackTraceElement.getLineNumber:()I
23: invokevirtual #59 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
26: invokevirtual #63 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
29: invokestatic #69 // Method afljava/logger/Logger.writeToLogger:(Ljava/lang/String;)V
32: iload_0
33: iload_1
34: iadd
35: istore_2
36: iload_2
37: ireturn
所示,我不仅得到了行号,而且得到了行号还有类名称和方法名称。其中,班级名称和方法名称是正常获得的,并获得了行号为-1。
此外,只有在访问码位置之后插入才能使行号为-1,并且在其他位置插入相同的字节码将没有此问题。
这是我的仪器代码(例如Holger的代码)的一部分
private void instrument(){
mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
mv.visitInsn(Opcodes.DUP);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Thread", "currentThread", "()Ljava/lang/Thread;", false);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Thread", "getName", "()Ljava/lang/String;", false);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
mv.visitLdcInsn("_" + classAndMethodName + "_");
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Thread", "currentThread", "()Ljava/lang/Thread;", false);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Thread", "getStackTrace", "()[Ljava/lang/StackTraceElement;", false);
mv.visitInsn(Opcodes.ICONST_1);
mv.visitInsn(Opcodes.AALOAD);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StackTraceElement", "getLineNumber", "()I", false);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(I)Ljava/lang/StringBuilder;", false);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "afljava/logger/Logger", "writeToLogger", "(Ljava/lang/String;)V", false);
}
@Override
public void visitCode() {
super.visitCode();
instrument();
}
,而是使用Visecode插入代码。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
行号由
linenumbertable
属性哪个将字节码位置映射到源代码行。当您使用ASM库转换代码时,将需要注意调整代码位置以反映更改。这意味着,当您在任何原始代码之前注入代码时,与行号相关的第一个代码的位置也会被调整,因此您的新代码未涵盖行号。
您可以在通过 vistcode /methodvisitor.html#visitlinenumber(Int,Objectweb.asm.label)“ rel =” nofollow noreferrer“>
visitlineNumber
。在最好的情况下,这仍然是在任何可执行代码之前(如果已经通过其他方式注入合成代码)之前。这样,新代码与第一个记录的行号相关联。但是,您无需处理堆栈跟踪即可重新构造此信息,因为它在代码注入的这一点已经知道。由于类和方法名称也是已知的,因此甚至不需要生成字符串串联代码。您可以事先组装字符串。
我使用了一个简单的打印语句,而不是您的记录器,但是示例应该易于调整。
作为替代方案,如果您想尽可能多地使用原始逻辑,则只需更改第一个报告的行号关联的字节码位置,以介绍您的注入
代码位置与所有以下说明相关联,直到报告下一行号为止。虽然ASM会按照代码位置的顺序调用访问者方法,但在打电话给班级作者时,我们不需要那么严格。
因此,我们可以通过调用
visitlabel(newStart);
在instrance();
之前将标签
与方法的开头关联起来,而不知道行数字。到时,visitlineNumber
首次调用,我们替换标签start> start
,该代表该方法的原始开始,并使用我们的新标签代表新的开始。 ASM不介意我们在instrument();
之前没有调用visitlineNumber
,因为仅与label
重要的是代码位置。The line numbers are given by the
LineNumberTable
Attribute which maps bytecode locations to source code lines. When you transform code with the ASM library, it will take care to adapt code locations to reflect changes.This implies that when you inject code before any original code, the location of the first code associated with a line number gets adapted too, so your new code is not covered by the line numbers.
Instead of injecting the code on
visitCode
, you may inject it after the first line number has been reported throughvisitLineNumber
. In the best case, this still is before any executable code (it may not, if synthetic code has been injected by other means already).This way, the new code gets associated with the first recorded line number. However, you don’t need to deal with stack traces to reconstitute this information, as it is already known at this point of code injection. Since class and method name are known too, there is not even a need to generate string concatenation code. You can assemble the string beforehand.
I used a simple print statement instead of your logger, but the example should be easy to adapt.
As an alternative, if you want to stay with your original logic as much as possible, you may just alter the bytecode location of the first reported line number association, to cover your injected code:
Keep in mind that a line number reported for a code location is associated with all following instructions, until the next line number is reported. While ASM will invoke the visitor methods in the order of the code locations, we don’t need to be as strict when calling into the class writer.
So we can associate a
Label
with the beginning of the method by callingvisitLabel(newStart);
beforeinstrument();
, without knowing the line number. By the time,visitLineNumber
is called for the first time, we replace the labelstart
, which represents the original start of the method, with our new label, representing the new start. ASM doesn’t mind that we didn’t callvisitLineNumber
beforeinstrument();
, as only the code location associated with theLabel
matters.