简体   繁体   English

是否可以在Eclipse中调试已运行的Java程序?

[英]Is it possible to debug an already running Java program in Eclipse?

What I'm asking is if it's possible to do the following steps in order: 我要问的是,是否可以按顺序执行以下步骤:

  1. Run as... Java application 运行为... Java应用程序
  2. Let the program run until it reaches the point I want to debug 让程序运行直到达到我想要调试的程度
  3. Debug that program when it gets to that point. 当程序到达那一点时调试该程序。

The reason why I launch the program using "Run as..." and not "Debug as..." is because the program hangs (I suspect an infinite loop) at some point, but I'm not able to replicate it consistently. 我之所以使用“Run as ...”启动程序而不是“Debug as ...”是因为程序在某个时刻挂起(我怀疑是一个无限循环),但我无法一致地复制它。 What I mean is that I can make it hang consistently, but it happens during random occasions. 我的意思是我可以让它一直挂起,但它会在随机场合发生。

For what it's worth, my program is a game and it hangs at different points in the game, which is why I can't be sure where to put a breakpoint, so it would be great if i could let it run first and then switch to debug at the point where it hangs/loops. 对于它的价值,我的程序是一个游戏,它挂在游戏的不同点,这就是为什么我不能确定在哪里放置一个断点,所以如果我可以让它先运行然后切换会很好在挂起/循环的位置进行调试。

Why don't you just execute your program with "Debug as..." without setting any breakpoint. 为什么不用“Debug as ...”执行程序而不设置任何断点。 Then, when it hangs, switch to Eclipse's debug perspective and suspend your program with the double vertical bars button? 然后,当它挂起时,切换到Eclipse的调试透视图并使用双垂直条按钮暂停程序?

If you always run it with "Debug as..." you can pause execution in the event that the program hangs. 如果您始终使用“Debug as ...”运行它,则可以在程序挂起时暂停执行。 This will open the debugger and allow you to inspect the threads that are running. 这将打开调试器并允许您检查正在运行的线程。

Apart from the solutions suggesting not to set any breakpoints, you could also look into attaching the java debugger to a running program. 除了建议不设置任何断点的解决方案之外,您还可以考虑将java调试器附加到正在运行的程序。

There is basic explanation of how to it at InformIT . InformIT中有如何解释它的基本解释。

A fast google search yielded this tutorial on how to do it with eclipse. 一个快速的谷歌搜索产生了关于如何使用eclipse做这个教程

If I remember correctly you have to start your program with additional parameters to allow attaching - this might prove a problem if your program is completely frozen, as this might prevent the attaching from working: check this stackoverflow question for even more information. 如果我没记错的话,你必须使用其他参数来启动程序以允许附加 - 如果程序完全冻结,这可能会成为一个问题,因为这可能会阻止附加工作:查看此stackoverflow问题以获取更多信息。

This will actually start your application in debug mode, but you can postpone attaching a debugger until you really need it. 这实际上将在调试模式下启动您的应用程序,但您可以推迟附加调试器,直到您真正需要它。

You can use 'Remote Java Application' functionality in eclipse to attach to a Java virtual machine. 您可以使用eclipse中的“远程Java应用程序”功能连接到Java虚拟机。 You need to add additional parameters when starting your application: 您需要在启动应用程序时添加其他参数:

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

You can reed more here: http://blog.javachap.com/index.php/debugging-in-java/ 你可以在这里更多地了解: http//blog.javachap.com/index.php/debugging-in-java/

Maybe you can add some debug logging to pinpoint the place where it hangs. 也许你可以添加一些调试日志来精确定位它挂起的地方。

Best way to log it into file to have it in hisory. 将其记录到文件中以将其保存在组织中的最佳方法。 System.out.println("before some code"); System.out.println(“在某些代码之前”); some code that may be problematic System.out.println("after some code"); 一些可能有问题的代码System.out.println(“在一些代码之后”);

When you will se that "after some code" is not reached you know it is hanging before this part. 如果你没有达到“在某些代码之后”,你知道它在这部分之前就已经悬空了。 Or if you use log4j do it with log4j some debug info with time stamps, then you can go closer and put more debug information closer to place where it hangs, and eventualy you will know where to put breakpoint and step trough the code. 或者如果你使用log4j用log4j做一些带有时间戳的调试信息,那么你可以更接近并将更多的调试信息放在它挂起的地方附近,并且最终你会知道在哪里放置断点并逐步通过代码。

'Run As' never picks up breakpoints and hence it will not stop at the place where you want to debug and so the answer to your question is a No. But the point no 2 in your question contradicts your statement in the paragraph below in which you explain why you chose not to use 'debug as'. “Run As”从不接收断点,因此它不会停留在您要调试的地方,因此您的问题的答案是否定的。但是您的问题中的第2点与您在下面的段落中的陈述相矛盾你解释为什么你选择不使用'debug as'。 If you do not know where the program hangs how can you let the program run until it reaches the point you want to debug? 如果您不知道程序挂起的位置,那么如何让程序运行直到达到您想要调试的程度? (point no 2). (第2点)。

I would debug it using 'debug as' as @Nicola Musatti has pointed 我会使用'debug as'来调试它,正如@Nicola Musatti指出的那样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM