简体   繁体   English

使用Java中的ASM监视对象创建

[英]Monitor Object Creation using ASM in Java

I am using ASM to monitor Object creation in Java. 我正在使用ASM来监视Java中的对象创建。 Currently, I take the call to init as the indicator of the creation of a new object and instrument a program from 目前,我将调用init作为创建新对象的指示器并从中调整程序

invoke XXX.init

to

dup;  
invoke XXX.init;  
call_my_method(Object)

My idea is to duplicate a copy of newObjectReference and, after the init of this object, I call my method to keep this object. 我的想法是复制newObjectReference的副本,并且在此对象的init之后,我调用我的方法来保留此对象。

However, during runtime, there is an exception: 但是,在运行时期间,有一个例外:

java.lang.VerifyError, Expecting to find unitialized object on stack.

When I used "-noverify" option, during runtime, if there is a thread instance, a second exception thrown: 当我使用“-noverify”选项时,在运行时,如果有一个线程实例,则抛出第二个异常:

Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at test.ThreadTest.test

For the second case, I am sure there is no call to start () of a thread except that in the original program. 对于第二种情况,我确信除了原始程序之外,没有调用线程的start ()。

Is there a better way to monitor the New Object Creation? 有没有更好的方法来监控新对象的创建?

Thanks a lot. 非常感谢。

Try, converting invoke XXX.init to 尝试,将调用XXX.init转换为

invoke XXX.init;
dup;
call_my_method(Object)

Basically call the duplicate after the init method returns. 在init方法返回后,基本上调用副本。

Explanation:: So given that you want to track new object creations, i am guessing you are looking at statements such as, new XXX() . 说明::所以假设您想要跟踪新的对象创建,我猜您正在查看诸如新XXX()之类的语句。 Now, the way this translates to bytecode is as follows:- 现在,这转换为字节码的方式如下: -

NEW XXX
DUP
INVOKESPECIAL <init>

In other words, the NEW bytecode instruction is used to create the object itself. 换句话说, NEW字节码指令用于创建对象本身。 It is duplicated on top of the stack, so you have an additional copy of the object. 它在堆栈顶部重复,因此您有一个对象的附加副本。 At this point mind you, the 2 copies of the object are uninitialized. 此时请注意,对象的2个副本未初始化。 And then the init method is invoked on the first uninitialized object on top of the stack. 然后在堆栈顶部的第一个未初始化对象上调用init方法。 By the time the constructor returns, the object is initialized, and thus the object sitting on top of the stack, is also initialized. 到构造函数返回时,对象被初始化,因此位于堆栈顶部的对象也被初始化。 (that is because the "object" sitting on top of the stack, is really an object reference pointing to the actual object which is sitting somewhere on the heap. I use the word object instead of object reference as it is simpler to explain things. sorry if this caused any confusion.) (这是因为位于堆栈顶部的“对象”实际上是一个对象引用,指向位于堆上某处的实际对象。我使用单词object而不是对象引用,因为它更容易解释事物。对不起,如果这引起任何混淆。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM