[英]JVM - get hold of instance of a class in a running (non-instrumented) session
I need to stop a process by invoking a particular method on an instance of a given class. 我需要通过在给定类的实例上调用特定方法来停止进程。 The process has been running for a day, and if I shut it down hard, I lose a lot of work. 该过程已经运行了一天,如果我将其关闭,将会失去很多工作。 Due to a bug in the API, the GUI is wrongly wired and doesn't call the correct stop function. 由于API中的错误,GUI的接线错误,并且未调用正确的停止功能。 I have an interactive Java (Scala) interpreter attached, so if I could get hold of the instance of the process, I could easily invoke the correct method. 我附加了一个交互式Java(Scala)解释器,因此,如果我可以掌握该流程的实例,则可以轻松地调用正确的方法。
I am not in a debugging session, there is no com.sun.jdi. 我不在调试会话中,没有com.sun.jdi。 I can see the instance in a stack trace dump, but StackTraceElement
doesn't contain actual instances, just classes and line numbers. 我可以在堆栈跟踪转储中看到该实例,但是StackTraceElement
不包含实际实例,仅包含类和行号。
Is there any way in a running session, without specific instrumentation, to get hold of that instance -- through its class, through a thread dump? 在没有特定工具的情况下,正在运行的会话中是否可以通过其类,通过线程转储来获取该实例?
It is possible to get all instances of the given class using JVMTI IterateOverInstancesOfClass
function. 可以使用JVMTI IterateOverInstancesOfClass
函数获取给定类的所有实例。 See an example in this answer . 请参阅此答案中的示例。
Create a JNI library that will find the required instance using the above function and invoke a method on it from JNI_OnLoad
entry. 创建一个JNI库,该库将使用上述函数查找所需的实例,并从JNI_OnLoad
条目对其调用一个方法。 Then invoke System.load
from the interpreter console to load this library. 然后从解释器控制台调用System.load
来加载此库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.