ASM转换找到具体的类类型 [英] ASM transformation to find concrete class type

查看:126
本文介绍了ASM转换找到具体的类类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,该项目将跟踪从包中的类到任何其他类的方法调用。重要的是我可以识别具体类型,并且我更希望有最小的跟踪开销。触发探测的时间没有限制;它可以在调用方法之前或之后。

I'm working on a project that will trace method calls from a class within a package to any other class. It's important that I can identify concrete types, and I'd prefer to have a minimum tracing overhead. There is no restrictions on when the probe is triggered; it can be before or after a method has been called.

当前使用ASM,但不要求它。系统正在从AspectJ移动,以允许动态附件,因此这就是。

ASM is currently used, but there is no requirement for it. The system is moving from AspectJ in order to allow dynamic attachment, so that's out.

以下是当前的情况。 'Tracer'enum / singleton接收probe(int)并处理调用。从具体类型到引用类型的调用就足够了。

Below is the current situation. A 'Tracer' enum/singleton receives probe(int) and handles the call. It is sufficient to find calls from concrete types to reference types.

@Override
void visitMethodInsn(final int opcode, final String owner, final String name, final String desc) {
    Integer probeID = Tracer.INSTANCE.probes.createProbeIDAt(new Call(owner, name, desc))

    super.visitFieldInsn(GETSTATIC, "org/flightofstairs/honours/capture/agent/Tracer", "INSTANCE", "Lorg/flightofstairs/honours/capture/agent/Tracer;");
    super.visitLdcInsn(probeID)
    super.visitMethodInsn(INVOKEVIRTUAL, "org/flightofstairs/honours/capture/agent/Tracer", "probe", "(Ljava/lang/Integer;)V")

    super.visitMethodInsn(opcode, owner, name, desc);
}

我进一步的想法是以某种方式获取对被调用类的引用调用getClass()来检索具体类型。如果堆栈包含INVOKEINTERFACE顶部的对象ref,但是调用方法的任何参数都放在它上面,这将是微不足道的。

My idea for going further is to somehow get a reference to the invoked class and call getClass() to retrieve the concrete type. This would be trivial if the stack contained the object ref on top for INVOKEINTERFACE, but any parameters to the called method are placed above it.

这不是问题计算对象引用的堆栈位置,假设已知参数的数量,但java无法从堆栈中的较低位置复制引用。

It wouldn't be a problem to calculate the stack position of the object reference, given that the number of parameters is known, but java offers no way to duplicate a reference from lower in the stack.

任意建议?

干杯。

推荐答案

解决方案是记录INVOKEINTERFACEs在遍历期间。最后,ASMs Analyzer可用于查找将objectref压入堆栈的指令,从那里可以很容易地添加getClass()和跟踪代码。

A solution is to record INVOKEINTERFACEs during traversal. At the end, ASMs Analyser can be used to find instructions that pushed the objectref onto the stack, and from there it's simple to add getClass() and tracing code.

A解决方案如下所示。 https://gist.github.com/2795738

A solution is shown here. https://gist.github.com/2795738

这篇关于ASM转换找到具体的类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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