5

What I'm asking is if it's possible to do the following steps in order:

  1. Run as... Java application
  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. 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.

heisenbergman
  • 1,459
  • 4
  • 16
  • 33
  • Is it multi-threaded? Have you tried logging debug messages to a file? The last messages are often useful, in my experience. – David Jun 12 '13 at 11:14
  • If you know the point where it hangs , can;t you put a debug point just before that point and execute it as "Debug-As" ? – AllTooSir Jun 12 '13 at 11:15
  • Seems it is a multithreaded program. Then it may need some record-replay tools to debug it if it is difficult to debug directly. – StarPinkER Jun 12 '13 at 11:17
  • @David: It's not multi-threaded. I haven't tried logging debug messages to a file... but take note that the game doesn't terminate or anything. It just freezes, but it doesn't freeze at the same point/time everytime. – heisenbergman Jun 12 '13 at 11:19
  • @TheNewIdiot: I have an idea where it hangs, but even if I knew exactly where it hangs and put a breakpoint there, I wouldn't be able to know/predict when exactly it would hang. It could stop at the breakpoint and I'd have to resume it hundreds of times before realizing it's already at a hanged/frozen/infinitely looping state. – heisenbergman Jun 12 '13 at 11:21
  • @JermaineXu: Nope, it's not multithreaded. – heisenbergman Jun 12 '13 at 11:21
  • This question might help you: http://stackoverflow.com/questions/6855520/attach-to-application-using-eclipse – nicopico Jun 12 '13 at 11:22

6 Answers6

9

Why don't you just execute your program with "Debug as..." without setting any breakpoint. Then, when it hangs, switch to Eclipse's debug perspective and suspend your program with the double vertical bars button?

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
5

If you always run it with "Debug as..." you can pause execution in the event that the program hangs. This will open the debugger and allow you to inspect the threads that are running.

seanhodges
  • 17,426
  • 15
  • 71
  • 93
2

Apart from the solutions suggesting not to set any breakpoints, you could also look into attaching the java debugger to a running program.

There is basic explanation of how to it at InformIT.

A fast google search yielded this tutorial on how to do it with 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.

This will actually start your application in debug mode, but you can postpone attaching a debugger until you really need it.

Community
  • 1
  • 1
BergmannF
  • 9,727
  • 3
  • 37
  • 37
1

You can use 'Remote Java Application' functionality in eclipse to attach to a Java virtual machine. You need to add additional parameters when starting your application:

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

You can reed more here: http://blog.javachap.com/index.php/debugging-in-java/

kaos
  • 1,598
  • 11
  • 15
0

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"); some code that may be problematic System.out.println("after some code");

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.

Mateusz
  • 2,287
  • 20
  • 28
0

'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'. 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).

I would debug it using 'debug as' as @Nicola Musatti has pointed

veebee
  • 61
  • 10