My goal is to implement a small library in Java that would allow to programmatically take some actions and then suspend the current JVM until a debugger is connected to it.
I imagine parts of it at least are possible, for ex. when starting a JVM in debug mode with suspend=y
it will wait for a debugger to connect. Does anyone have a pointer to the code that implements this functionality?
It seems that JVMTI might enable me to do something like this programmatically, but I'm wondering if there is a higher level API or if someone has code examples/explanations on how to interact with JVMTI from Java (my assumption is that the agent in JVMTI will be able to suspend the JVM but I need to call it from the Java code).
Update: Here is what I found so far.
1) It is relatively simple to invoke JVMTI code from Java by having a JNI function that would then use the jvmti env. The trick is to keep a reference to the jvmtienv during the loading of the agent. This blog post has been very helpful https://www.javacodegeeks.com/2014/12/own-your-heap-iterate-class-instances-with-jvmti.html.
2) The functionality of the suspend option that can be passed to the JVM on startup and wait for a debugger to connect before resuming execution doesn't seem to be something that can be reused. It seems that it might just be the debugger agent that listens on this port and once a connection is established it will just proceed with execution/resume threads.
3) For the higher level API to achieve what I want, it seems that the plain thread.suspend() might do what I want. The question then is how does it differ from SuspendThread. I opened this other question for that: What is the difference between JVMTI SuspendThread and Javas thread.suspend?.