1

I am trying to create a tool that can capture all the read and writes made by a java program. Also, I would like to know what fields of what object is access/modified.

I currently looked at:-

1) java.lang.instrument I could not do much with that. I could not understand how to write an agent that can get access to the a running program and create a watch on different objects/fields and anything related. I would appreciated if you have any idea or information on that.

2) jvmti I looked at jvmti and tried to create a jvmti tool, but I figured out that to get the objects, I would need the JVMTI_EVENT_OBJECT_ALLOC be a potential capability. But, I figured that, it is not. Moreover, I read that this event is not called for new command. Hence, at the moment, even this does not seem applicable.

So, I would like to know if you guys know any way to do what I want to do, either using the above mentioned methods or any other technique/tool that you may be aware of?

NOTE: I do not have access to the source code of the application. All, I have are the class files.

4

2 回答 2

1

使用ASM lib很容易做到。创建一个新的类加载器,在加载所有类之前对其进行检测,并将其用于加载目标类。创建一个新的 MethodAdapter 并覆盖该visitFieldInsn方法。然后查找PUTFIELDPUTSTATIC和操作码。虽然这看起来很可怕(因为我的解释很可能是胡言乱语),但实际上很容易。只需下载 ASM 手册,您就会立即知道如何操作。GETFIELDGETSTATIC

编辑:我忘记告诉你,为了能够拦截 JDK 代码完成的读写操作,你必须检测这些类,将它们保存到文件并使用修改后的引导类路径运行 JVM,通过命令行参数-Xbootclasspath(java.* 和其他一些包;我相信至少 sun.* 和 javax.* 也需要这个)。

这对于 AspectJ 也可能是可行的……但我不确定。

于 2011-07-25T22:15:18.870 回答