Skip to content
Advertisement

Is it possible to start java jdwp after JVM startup (aka: at runtime) without command line parameters?

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

Advertisement

Answer

In modern JVM (Java 6+) the agents use the JVM TI interface.

The JVM Tool Interface (JVM TI) is a programming interface used by development and monitoring tools. It provides both a way to inspect the state and to control the execution of applications running in the Java virtual machine (VM).

Inside JVM TI, you must enable the desired capabilities

The capabilities functions allow you to change the functionality available to JVM TI–that is, which JVM TI functions can be called, what events can be generated, and what functionality these events and functions can provide.

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 OnLoad phase (in most JVM). (I.e: can_generate_breakpoint_events, can_suspend, …)

Add Capabilities: Typically this function is used in the OnLoad function. Some virtual machines may allow a limited set of capabilities to be added in the live phase.

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement