调试java应用程序而无需使用调试参数启动JVM

时间:2023-01-19 17:18:29

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

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

> 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?

当生产系统(即没有调试args启动)表现出“随机”(我使用松散术语)错误时,就会出现这种情况。所以我不能用适当的参数重启jvm,因为没有人知道如何再次重现bug。在这种情况下是否无法连接到JVM?

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

只是为了澄清它是不可能使用像jdb这样的工具连接到已经运行的JVM,除非它们是在调试模式下启动的

from the JVM man page

来自JVM手册页

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:

使用jdb的另一种方法是将其附加到已经运行的Java VM。必须使用以下选项启动要使用jdb调试的VM:

5 个解决方案

#1


37  

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.

您可以使用jsadebugd(JDK)将调试服务器附加到进程(在Windows上使用Windows调试工具提供)。它被标记为实验性的,因此您可能希望首先在测试机器上进行尝试。

Usage:

用法:

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

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

可以使用jdb -listconnectors找到arg的连接器名称。

#2


25  

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

只是为了澄清它是不可能使用像jdb这样的工具连接到已经运行的JVM >>,除非它们是在调试模式下启动的

in soviet russia source reads you

在苏联俄罗斯来源读你

jdb -connect sun.jvm.hotspot.jdi.SAPIDAttachingConnector:pid=9426

#3


5  

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

使用jstack(在死锁的情况下很有用)或者btrace VisualVM插件也可以做到这一点

#4


5  

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.

VisualVM不是调试器,但您可以从中获取线程转储和堆转储,这对于诊断某些问题非常有用。最有用的功能需要JVM 5或6。

#5


-4  

You can always use jdb and debug by hand :P

您可以随时使用jdb和debug:P

#1


37  

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.

您可以使用jsadebugd(JDK)将调试服务器附加到进程(在Windows上使用Windows调试工具提供)。它被标记为实验性的,因此您可能希望首先在测试机器上进行尝试。

Usage:

用法:

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

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

可以使用jdb -listconnectors找到arg的连接器名称。

#2


25  

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

只是为了澄清它是不可能使用像jdb这样的工具连接到已经运行的JVM >>,除非它们是在调试模式下启动的

in soviet russia source reads you

在苏联俄罗斯来源读你

jdb -connect sun.jvm.hotspot.jdi.SAPIDAttachingConnector:pid=9426

#3


5  

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

使用jstack(在死锁的情况下很有用)或者btrace VisualVM插件也可以做到这一点

#4


5  

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.

VisualVM不是调试器,但您可以从中获取线程转储和堆转储,这对于诊断某些问题非常有用。最有用的功能需要JVM 5或6。

#5


-4  

You can always use jdb and debug by hand :P

您可以随时使用jdb和debug:P