Is it possible to debug a running java process (with Eclipse / IntelliJ) without having a breakpoint? Would be useful to get the state of an object when having a construct like this:
double d = Math.random();
BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
queue.take();
"take()" will block forever, and I'd like to get the value of d with the debugger after take() was called.
-
I am missing something in the question.. you want to know the value of d without a break point; I am not sure why you want to avoid a break point as a break point on line 2 or 3 would give you the value of d. So I cannot tell whether System.out would suffice?Chris K– Chris K2015年07月06日 13:52:36 +00:00Commented Jul 6, 2015 at 13:52
-
1@ChrisK When I run a big server application and want to see whats wrong if it fails, I can't set the breakpoint afterwards. I'd have to either do a memory dump (whicht costs time to analyze and stuff) or, if possible, simply attach a debugger to a thread.Stezy– Stezy2015年07月06日 19:39:38 +00:00Commented Jul 6, 2015 at 19:39
5 Answers 5
I'd like to get the value of d with the debugger after take() was called
A simple solution would be to print the value. Then you don't need the debugger at all. The debugger is useful when changing data due to testing something, inspecting certain objects at runtime etc.
queue.take();
System.out.println(d);
2 Comments
In your case it's not option. One option you can have is to de-compile the jar containing BlockingQueue class, convert them to source files, include it in your project, and set breakpoints inside take() to see the behavior.
One best decompiler is:
using this you can see source of take() function and copy whole class and paste it in your package with name BlockingQueue.java and paste whole source and debug as you wish.
3 Comments
Object, in imported in source by-default. You can create replica of Object class with different name and inherit your class from it and use it as you want.Viewing the state of a thread after an error would be very useful; and while it is more common in some interpreted languages it is sadly lacking in Java. There are however four approaches that come to mind, however bare in mind that the standard approach here in Java is to log important state for later diagnostics.
- journal your system, and keep all processing idempotent and deterministic.
- attach a debugger after the error; you will not be able to roll back to the point of the exception but you will be able to inspect the current state of the system
- add a repl to your server, one that you can telnet into and inspect the system with
- Investigate DVR solutions for Java, such as http://chrononsystems.com. They allow rollback of the system to the point of the exception.
Comments
Just noticed that there is a pause / suspend button in Eclipse and IntelliJ. This is doing the job.
Comments
It is possible with the Pause action in IntelliJ. You can find it in the debugger's toolbar or in Run | Debugging Actions | Pause. This action will allow you to read the variable values as you described. You can read more about it here. Also, this action has several advanced use-cases, such as debugging without sources.