101

Normally to attach a debuger to a running jvm you would need start the jvm with arguments such as the following:

> java -Xdebug -Xrunjdwp:transport=dt_socket,address=1000,server=y,suspend=n

Now if I want to debug a process that wasn't started in debug mode, what can I do?

This situation arrises when a production system (i.e. started without debug args) exhibits a 'random' (I use the term loosely) bug. So I can't restart the jvm with the appropriate arguments, because nobody knows how to reproduce the bug again. Is it impossible to attach to the JVM in this situation?

Just to clarify it is not possible to use tools like jdb to attach to already running JVMs unless they were started in debug mode

from the JVM man page

Another way to use jdb is by attaching it to a Java VM that is already running. A VM that is to be debugged with jdb must be started with the following options:

shmosel
  • 49,289
  • 6
  • 73
  • 138
hhafez
  • 38,949
  • 39
  • 113
  • 143
  • See also [Java API to turn on debugging in a running JVM](https://stackoverflow.com/questions/6934571/java-api-to-turn-on-debugging-in-a-running-jvm) – Vadzim Feb 28 '20 at 23:01

5 Answers5

51

You may be able to use jsadebugd (JDK) to attach a debug server to the process (available on Windows with the Debugging Tools for Windows). It is marked as experimental, so you may want to try it out on a test machine first.

Usage:

jsadebugd <pid>
jdb -connect sun.jvm.hotspot.jdi.SADebugServerAttachingConnector:debugServerName=localhost

The connector name withe arg can be found using jdb -listconnectors.

Zitrax
  • 19,036
  • 20
  • 88
  • 110
McDowell
  • 107,573
  • 31
  • 204
  • 267
  • 1
    I'm using linux so this seems to be the most promising solution – hhafez Dec 17 '08 at 23:05
  • Any experiences to share with this? – Thorbjørn Ravn Andersen Jul 15 '12 at 16:52
  • 1
    My experience is that it worked well the times which I needed it, in the mean time all in factory instances of the software are no configured to launch with jvm debug options by default so we can use the supported method. – hhafez Aug 24 '12 at 21:16
  • On Java 11 `jsadebugd` was replaced by `jhsdb debugd`. So that becomes `jhsdb debugd --pid `. See [slides of a talk presenting jhsdb](https://static.rainfocus.com/oracle/oow16/sess/14627958356770011JJj/ppt/JavaOne2016_CON3733.pdf) and [the docs for jhsdb](https://docs.oracle.com/en/java/javase/11/tools/jhsdb.html) – Delthas May 01 '20 at 04:07
  • It seems `SADebugServerAttachingConnector` was removed from `jdb` as well, and I think the replacement is supposed to be `jhsdb hsdb` / `jhsdb clhsdb`. I can't find any docs on what arguments to give to `jhsdb clhsdb`. – Delthas May 01 '20 at 04:20
33

Just to clarify it is not possible to use tools like jdb to attach to already running JVMs > > unless they were started in debug mode

in soviet russia source reads you

jdb -connect sun.jvm.hotspot.jdi.SAPIDAttachingConnector:pid=9426
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
potted plant
  • 339
  • 3
  • 2
  • 1
    well that is the "supported" answer but the experimental answer is the one I accepted, so it can be done in an unsupported fasion – hhafez Jun 11 '10 at 23:06
7

VisualVM isn't a debugger, but you can get thread dumps and heap dumps from it that can be useful in diagnosing some problems. The most useful features require JVM 5 or 6.

Octavia Togami
  • 4,186
  • 4
  • 31
  • 49
sk.
  • 6,336
  • 5
  • 38
  • 46
6

using jstack (useful in case of deadlocks) or the btrace VisualVM plugin could also do the trick

Vijay
  • 314
  • 1
  • 4
  • 8
-5

You can always use jdb and debug by hand :P

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 3
    I always thought that you can't attach to jvm with jdb unless the jvm was started with to allow debug connections Am I wrong? – hhafez Dec 17 '08 at 22:41
  • As far as I understand, the option you mentioned is to "enable" remote debug of your "java" command (the VM) But you can also use the jdb command instead. So instead of java MyApp you would go like jdb MyApp ( and debug interactively , set breakpoints, run, stop, watch, etc etc. ) – OscarRyz Dec 17 '08 at 22:50
  • 1
    I don't think that is correct according to the jdb man page -- start quote Another way to use jdb is by attaching it to a Java VM that is already running. A VM that is to be debugged with jdb must be started with the following options: -- end quote – hhafez Dec 17 '08 at 22:55
  • With the correct connector(unsupported for now) jdb can connect to a running process. – Thorbjørn Ravn Andersen Jun 25 '15 at 13:00