1

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.

2
  • 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? Commented 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. Commented Jul 6, 2015 at 19:39

5 Answers 5

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Just a note: you can print out any evaluated expression when breakpoint is hit in IntelliJ. So better solution would probably be to configure the breakpoint to not suspend the execution and print out d so you don't pollute your codebase with printlns. Also you might want to debug external library where you can't change the code.
Problem is: take() is a blocking call, the jvm would never reach the print to the console.
0

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:

http://jd.benow.ca/

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

Hm, the BlockingQueue was just an example. Could be Object.wait(), too. I just want to attach to a sleeping (or running) thread, no matter where the thread is at the moment.
@CalibeR.50, You can find code for Object.wait() too. But keep in mind that this class, 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.
I don't want to know what's happening inside the implementation, I just want to suspend the thread without a breakpoint. I answered the question below.
0

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.

  1. journal your system, and keep all processing idempotent and deterministic.
  2. 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
  3. add a repl to your server, one that you can telnet into and inspect the system with
  4. Investigate DVR solutions for Java, such as http://chrononsystems.com. They allow rollback of the system to the point of the exception.

Comments

0

Just noticed that there is a pause / suspend button in Eclipse and IntelliJ. This is doing the job.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.