Java API to turn on debugging in a running JVM [duplicate]
Asked Answered
L

3

4

Is there a programmatic way to turn on debugging in a running JVM instance? I am looking for an API that would make a running JVM, a debug server. The API would do the equivalent of

-Xdebug -Xrunjdwp:transport=dt_socket,address=<host:port>,server=y,suspend=n. 
Lamblike answered 3/8, 2011 at 23:35 Comment(0)
B
2

You can attach to any running JVM using Visual VM. Be sure to download version 1.3.2 and install all the plugins.

Boz answered 4/8, 2011 at 0:39 Comment(1)
+1 Correct! This should work at least when the JVM was started under the same user as VisualVM (or if permissions are appropriate).Worshipful
N
2

The debug port that the questioner refers to is for source code debugging, such as with an Eclipse debugger. The port for VisualVM is the JMX port, which takes a different set of JVM options. There are exceptions though: Mission Control for JRockit uses the jdwp port rather than the JMX port.

And yes, you can dynamically load the JMX debugger in code. Its complicated but OSGI can load this dynamically I believe. For example, here is a reference to Jetty project doing this.

Newsdealer answered 3/3, 2013 at 19:15 Comment(0)
W
1

This is not possible. The JVM has to know from the beginning that it is in debug mode. It runs a bit slower then but provides external processes with debug information and breakpoints which is not possible when the JVM already has been startet.

Worshipful answered 3/8, 2011 at 23:52 Comment(1)
Daniel, I agree. I was hoping there was such an API. For what I am trying to achieve, I fully assume that the target JVM is not yet in debug mode, and that I would manage to get some code executed in it that would switch that VM to debug mode. After I know the exact port number the VM has started listening on, I would write that information to a well-known location that users of debuggers would consult and use for attaching their debuggers to that VM.Lamblike

© 2022 - 2024 — McMap. All rights reserved.