如何捕获java程序中的读取和写入?

发布于 2024-11-25 22:28:59 字数 492 浏览 6 评论 0原文

我正在尝试创建一个可以捕获java程序进行的所有读写操作的工具。另外,我想知道访问/修改哪个对象的哪些字段。

我目前查看的是:-

1) java.lang.instrument 我对此无能为力。我无法理解如何编写一个可以访问正在运行的程序并在不同对象/字段和任何相关内容上创建监视的代理。如果您对此有任何想法或信息,我将不胜感激。

2)jvmti 我查看了 jvmti 并尝试创建一个 jvmti 工具,但我发现要获取对象,我需要 JVMTI_EVENT_OBJECT_ALLOC 作为潜在功能。但我想,事实并非如此。此外,我读到此事件不是为 new 命令调用的。因此,目前看来,这似乎也不适用。

所以,我想知道你们是否知道有什么方法可以做我想做的事情,无论是使用上述方法还是您可能知道的任何其他技术/工具?

注意:我无权访问该应用程序的源代码。我所有的都是类文件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼痛 2024-12-02 22:28:59

使用 ASM 库 可以非常轻松地做到这一点。创建一个新的类加载器,在加载所有类之前对其进行检测,并使用它来加载目标类。创建一个新的 MethodAdapter 并重写 visitFieldInsn 方法。然后查找 PUTFIELDPUTSTATICGETFIELDGETSTATIC 操作码。虽然这可能看起来很可怕(因为我的解释很可能是胡言乱语),但实际上非常简单。只需下载 ASM 手册,您很快就会知道如何操作。

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

这也可能适用于 AspectJ...但我不确定。

It's very easy to do with the ASM lib. Create a new Class Loader that instruments all classes before loading them and use it for loading the target classes. Create a new MethodAdapter and override the visitFieldInsn method. Then look for the PUTFIELD, PUTSTATIC, GETFIELD and GETSTATIC opcodes. Although this might look scary (as my explation is most likely gibberish), it's in fact pretty easy. Just download the ASM manual and you'll know how to do it in no time.

Edit: I was forgetting to tell that in order to be able to intercept the reads and writes of done by the JDK code you have to instrument those classes, save them to files and run the JVM with a modified bootstrap classpath, through command line argument -Xbootclasspath (java.* and some other packages; I believe that at least sun.* and javax.* also need this).

This may also be doable with AspectJ... but I'm not sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文