Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

I am not in a debugging session, there is no 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.

Is there any way in a running session, without specific instrumentation, to get hold of that instance -- through its class, through a thread dump?

like image 725
0__ Avatar asked Oct 18 '16 07:10

0__


1 Answers

It is possible to get all instances of the given class using JVMTI IterateOverInstancesOfClass function. 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. Then invoke System.load from the interpreter console to load this library.

like image 198
apangin Avatar answered Oct 12 '22 21:10

apangin