JVM 远程调试会话因未捕获的异常而终止

发布于 2024-11-07 20:47:27 字数 1820 浏览 0 评论 0原文

我正在尝试远程调试 scala 项目。执行程序(从 SBT 构建和执行)和我的调试器 (Intellij) 都是本地的,但由于我不想使用 Intellij 进行构建,因此使用远程调试会话似乎是方便调试的最简单选择。

该程序使用以下选项启动:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005

它在启动时成功挂起,并且我可以成功附加我的调试器。我可以设置断点并单步调试代码,但问题是未捕获的异常会终止调试会话并断开调试器的连接,从而违背了调试的目的。这是默认行为是荒谬的,所以我必须做错什么。

Intellij 的控制台显示:

Connected to the target VM, address: 'localhost:5005', transport: 'socket'
Disconnected from the target VM, address: 'localhost:5005', transport: 'socket'

我在执行或调试方面是否缺少某些内容?

编辑:添加堆栈跟踪,我在类的主要部分抛出 new Exception("what") :

[error] java.lang.Exception: what
[error]     at travellr.application.prophet.Prophet$.main(Prophet.scala:80)
[error]     at travellr.application.prophet.Prophet.main(Prophet.scala)
[error]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[error]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[error]     at java.lang.reflect.Method.invoke(Method.java:597)
[error]     at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:81)
[error]     at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
[error]     at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:86)
[error]     at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:81)
[error]     at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:86)
[error]     at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:83)
[error]     at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

I'm attempting to debug a scala project remotely. Both the executing program (built and executed from SBT) and my debugger (Intellij) are local, but since I don't want to build using Intellij, using a remote debugging session seemed the easiest option for convenient debugging.

The program is started with the following options:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005

It's successfully suspending on launch, and I can successfully attach my debugger. I can set break points and step through the code, but the problem is that an uncaught exception terminates the debugging session and disconnects the debugger, defeating the purpose of debugging. This being the default behaviour would be asburd, so I have to be doing something wrong.

Intellij's Console reads:

Connected to the target VM, address: 'localhost:5005', transport: 'socket'
Disconnected from the target VM, address: 'localhost:5005', transport: 'socket'

Is there something I'm missing on either the execution or debugging side of things?

Edit: adding a stack trace where I'm throwing new Exception("what") in the main of my class:

[error] java.lang.Exception: what
[error]     at travellr.application.prophet.Prophet$.main(Prophet.scala:80)
[error]     at travellr.application.prophet.Prophet.main(Prophet.scala)
[error]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[error]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[error]     at java.lang.reflect.Method.invoke(Method.java:597)
[error]     at scala.tools.nsc.util.ScalaClassLoader$anonfun$run$1.apply(ScalaClassLoader.scala:81)
[error]     at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
[error]     at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:86)
[error]     at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:81)
[error]     at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:86)
[error]     at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:83)
[error]     at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面如桃花 2024-11-14 20:47:27

如果异常从未被捕获,它将导致您的应用程序关闭并且 JVM 终止,这显然会结束调试会话。

您可以添加异常断点,以便调试器在抛出异常时立即暂停执行。单击调试工具栏中的“查看断点”按钮,然后查看“异常断点”选项卡。它有助于了解您正在寻找什么类型的异常,以便您可以为该特定类型设置异常断点;您应该能够通过检查堆栈跟踪找到合适的类型。否则,您最终将因许多不相关的异常而暂停。如果您知道异常发生在哪个线程上,那么将异常断点限制在该线程上也会很有帮助。

请注意,SBT(如果您不分叉单独的虚拟机)和 Scala 的运行程序(如果您分叉)都会捕获客户端代码中抛出的所有异常。因此,不会有“未捕获的异常”,并且您需要在“捕获的异常”上暂停。

If the exception is never caught, it will cause your application to close and the JVM to terminate, which obviously ends the debugging session.

You can add an exception breakpoint so that the debugger will suspend execution as soon as the exception is thrown. Click on the "View Breakpoints" button in the debug toolbar, then see the "Exception Breakpoints" tab. It helps to know what type of exception you're looking for so that you can set the exception breakpoint for that specific type; you should be able to find the appropriate type by examining your stacktrace. Otherwise, you'll end up suspending on lots of unrelated exceptions. If you know which thread the exception occurs on, it can also be helpful to limit the exception breakpoint to that thread.

Note that both SBT (if you're not forking a separate VM) and Scala's runner (if you are forking) catch all exceptions thrown in client code. Therefore, there will be no "uncaught exceptions", and you will need to suspend on "caught exceptions".

如果没结果 2024-11-14 20:47:27

在开始之前,我还没有编写任何 scala 代码,但这可能会帮助您捕获 java 中未捕获的异常,请参阅方法 Thread.UncaughtExceptionHandler

当线程即将终止时
由于 Java 未捕获异常
虚拟机将查询线程
为其 UncaughtExceptionHandler 使用
Thread.getUncaughtExceptionHandler()
并将调用处理程序的
uncaughtException 方法,传递
线程和异常作为参数。
如果一个线程还没有它的
明确 UncaughtExceptionHandler
设置,那么它的ThreadGroup对象就会起作用
作为它的 UncaughtExceptionHandler。如果
ThreadGroup对象没有特殊的
处理要求
例外,它可以转发
调用默认未捕获
异常处理程序。

我想您可以记录异常,它可能会让您深入了解问题。

编辑:这还假设您可以重新部署一个新项目,但情况可能并非如此。

Before I begin I have not coded any scala, but this may help you can catch uncaught exceptions in java see the method Thread.UncaughtExceptionHandler

When a thread is about to terminate
due to an uncaught exception the Java
Virtual Machine will query the thread
for its UncaughtExceptionHandler using
Thread.getUncaughtExceptionHandler()
and will invoke the handler's
uncaughtException method, passing the
thread and the exception as arguments.
If a thread has not had its
UncaughtExceptionHandler explicitly
set, then its ThreadGroup object acts
as its UncaughtExceptionHandler. If
the ThreadGroup object has no special
requirements for dealing with the
exception, it can forward the
invocation to the default uncaught
exception handler.

I guess you can log the Exception and it may give you an insight to the problem.

EDIT: This also assumes you can redeploy a new project which may not be the case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文