不使用调试参数启动 JVM 调试 java 应用程序

通常,要将调试器附加到正在运行的 jvm,您需要使用以下参数启动 jvm:

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

现在,如果我想调试一个没有在调试模式下启动的进程,我能做什么呢?

当一个生产系统(例如,在没有调试参数的情况下启动)出现一个“随机”错误时,就会出现这种情况。因此,我无法使用适当的参数重新启动 jvm,因为没有人知道如何再次重现 bug。在这种情况下是否不可能附加到 JVM?

只是澄清一下,除非 JVM 是在调试模式下启动的,否则不可能使用 jdb 之类的工具附加到已经运行的 JVM

来自 JVM 手册页

另一种使用 jdb 的方法是将它附加到一个 JavaVM 已经在运行 用 jdb 调试的程序必须以 以下选项:

75792 次浏览

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.

You can always use jdb and debug by hand :P

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.

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

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