I would like to enable debugging at some point on the current JVM without adding the command line parameters -agentlib:jdwp.
Is it possible to do so programmatically from within the current running JVM ? Without any third party libraries ?
Other command line parameters (such as -Djdk.attach.allowAttachSelf=true) can be considered.
VirtualMachine vm = VirtualMachine.attach(Long.toString(ProcessHandle.current().pid()));
vm.loadAgentLibrary("jdwp", "transport=dt_socket,server=y,address=8000,suspend=n");
causes :
com.sun.tools.attach.AgentLoadException: Failed to load agent library: _Agent_OnAttach@12 is not available in jdwp
In modern JVM (Java 6+) the agents use the JVM TI interface.
Inside JVM TI, you must enable the desired capabilities
Which capabilities can be added when (which state of the JVM) is vendor-dependent. JDWP is just the protocol for debugging between the JVM and the debugger. It simply leverages the capabilities of the JVM TI just like any other agent. Meanwhile, most-probably, the capabilities for debugging can only be added in the
OnLoadphase (in most JVM). (I.e:can_generate_breakpoint_events,can_suspend, ...)This explains why the jdwp agent must be declared on JVM startup in order to add the proper capabilities to JVM TI.
Doc: https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#capability
Credits to @Holger for pointing the direction.