views:

4418

answers:

5

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 situatoin arrises when a production system (ie started without debug args) exhibits a 'random' (I use the term loosly) 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 possibel 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:

+2  A: 

You may be able to use jsadebugd (JDK) to attach a debug server to the process (note that it isn't available on Windows). It is marked as experimental, so you may want to try it out on a test machine first.

McDowell
I'm using linux so this seems to be the most promising solution
hhafez
A: 

You can always use jdb and debug by hand :P

OscarRyz
I always thought that you can't attach to jvm with jdb unless the jvm was started with to allow debug connectionsAm I wrong?
hhafez
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
I don't think that is correct according to the jdb man page-- start quoteAnother 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
That probes my understanding was wrong.
OscarRyz
+2  A: 

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.

sk
link does not work.... maybe you outa remove the http:// before the https://... I would've but dont have the enough reputation yet
Newtopian
+1 VisualVM looks really interesting. BTW: Link is fixed now.
sleske
+1  A: 

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

Vijay
A: 

Just to clarify it is not possibel 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

potted plant
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