Within a Java process, I want to access the JDI (Java Debugging Interface) of that same process.
I make the following assumptions:
- The process is being executed within the debugger (in Intelli/J)
- I use the Oracle JVM 8
- I run Linux (no portability is needed)
That is, I want to do something like:
import com.sun.jdi.*
import com.sun.jdi.connect.*
VirtualMachine vm = new SelfConnector().attach()
(Except, of course, that there is no such object.)
How can I connect to my own VM?
Why do I want that?
I wish to write a function findObj
that finds an object via its unique ID. For example, if in the Intelli/J debugger I see a variable "x = {Something@1234}
", then I want to be able to add a watch expression "findObj(1234)
" that finds and return the object. That way, I will be able to see the object #1234 even after the variable x
has been changed or I have left the current stack frame.
I believe that I can implement this function once I have access to the JDI for the current process.
[EDIT: It does not answer the question, but it solves my use case: In Intellij/J you can "mark" objects, and use these markers in watch expressions.]
What have I tried?
I have tried to connect to the process itself via a SocketAttach AttachingConnector using the hostname and port that Intelli/J supplies in the -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:39469,suspend=y,server=n
argument when starting the Java process in a debug session. (Following roughly the method described here.) I get:
Exception in thread "main" java.net.ConnectException: Connection refused
Probably because Intelli/J is already connected and one cannot connect twice to that port? [EDIT: Intellij/J starts the jwdp agent with parameter server=n
which means the process will do the connecting. Hence the connection refused. But further experiments show that even if one uses server=y
, at most one connection is accepted.]
Another approach was to use a ProcessAttach AttachingConnector. In that case, I got:
Exception in thread "main" java.io.IOException: Not a debuggee, or not listening for debugger to attach
x = {Type@123} Type@abcdef
. Here 123 is the unique ID that I mean, andabcdef
is thehashCode
you refer to.{Type@123}
is generated by the debugger, andType@abcdef
is just the output oftoString()
. – Graehl