Debug a java application without starting the JVM with debug arguments

JavaDebuggingJvmJvm Arguments

Java Problem Overview


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:

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

> 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

Solution 3 - Java

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.

Solution 4 - Java

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

Solution 5 - Java

You can always use jdb and debug by hand :P

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhhafezView Question on Stackoverflow
Solution 1 - JavaMcDowellView Answer on Stackoverflow
Solution 2 - Javapotted plantView Answer on Stackoverflow
Solution 3 - Javask.View Answer on Stackoverflow
Solution 4 - JavaVijayView Answer on Stackoverflow
Solution 5 - JavaOscarRyzView Answer on Stackoverflow