55

Can I move the instruction pointer directly to a line of my choice (within the current method) while debugging a Java program in Eclipse (Galileo)?

It's straightforward to drag the instruction pointer to the desired line within a method in Visual Studio, but I don't see a way to do that in Eclipse (and don't find anything about it in the docs or on google).

10
  • 1
    Sounds like a very bad thing to do. Why would you need it? And in VS, you can do this with C# or just C++? Commented Oct 30, 2009 at 19:15
  • 14
    @Pilgrim: Why would that be a bad thing? It's a great tool for debugging. You can explore the behavior of various branches in a method and get much more done in one debugging pass than if you had to restart execution with a new set of input to run other branches. Combined with the ability to edit code and continue debugging (with the recompiled code), it makes fixing while debugging very efficient. I don't know about C++, I have not done C++ development on that platform. Commented Oct 30, 2009 at 19:43
  • 1
    I also want to jump over some statements. Not backtrack or anything. This should be basic function of a debugger/IDE. Commented May 13, 2013 at 21:55
  • 1
    I'm stunned, it's hard to believe this, it seems to be true. Commented May 13, 2013 at 21:57
  • 2
    @DenisTulskiy Bad thing? Are you kidding? We are developers, we know what we are doing. I just want to try out certain variations of code execution without recompiling many times. In Visual Studio, you just drag the pointer and drop it. Of course, you cannot drop it to anywhere, scope matters, variables, etc. When I said this is a basic functionality I expect from an IDE, I didn't mean it's easy to implement. And it's not bad at all, because we know what we are doing. It would enhance your productivity (thinking of recompiling something several times for no reason vs. just drag&drop). Commented May 13, 2013 at 22:03

10 Answers 10

35

This is possible...

Eclipse Tips and Tricks (JDT)

Drop to frame - When stepping through your code, you might occasionally step too far, or step over a line you meant to step into. Rather than restarting your debug session, you can use the Drop to Frame action to quickly go back to the beginning of a method. Select the stack frame corresponding to the Java method you wish to restart, and select Drop to Frame from Debug view toolbar or the stack frame's context menu. The current instruction pointer will be reset to the first executable statement in the method. This works for non-top stack frames as well.

Note that Drop to frame is only available when debugging with a 1.4 or higher VM, or the J9 VM. There are some situations where a JVM may be unable to pop the desired frames from the stack. For example, it is generally impossible to drop to the bottom frame of the stack or to any frame below a native method.

Sign up to request clarification or add additional context in comments.

7 Comments

As Eric J. told in another comment this functionality allows you to get back to the beginning of a method and re-execute it but doesn't allow you to move the instruction pointer to a given line unless the execution passes from there.
In Eclipse Luna and this still cannot be done - except for Drop to Frame - which partially does it. (This is sure time saver in other VS for C/C++/C#). Here's a resolved bug report on Eclipse that seems to have implemented it back in 2008, so probably this is a limitation of jvm: bugs.eclipse.org/bugs/show_bug.cgi?id=118147 Pl post updates on this thread whenever this gets available.
This is really nice to know, but not what I was trying to achieve when posting my question.
It also seems you cannot drop further than the next native frame on the callstack.
@JohanGunnarsson: the bug seems to be for CDT (C++) only, not for Java
|
15

This is not possible.

If you simply want to execute some code at the current place you can use the Expressions view and enter your code as an expression. The methods called by the expression evaluation will run in the current debugging context.

5 Comments

The only thing 'sort of' what he is looking for is step filtering help.eclipse.org/help32/index.jsp?topic=/…
Looks like there's enough agreement (based on the upvotes) that this isn't possible. Thanks for the info.
Kind of sad that we had the technology to do this in debuggers in the early 1980s but can't do it now...
Also, in case you didn't know, you can MODIFY variables in order to force various code paths. In the "Expressions" you can put "x=23" etc. For instance, setting an object to null will likely exercise your catch block.
This is actually the "Immediate window" version of Eclipse
3

Moving the pointer like in Visual Studio is not possible, however workarounds are:

Going backwards to the beginning of the currently executed method: Select the method from the debug call stack, right click -> "Drop to frame" et voila you're back at the beginning of the method.

Now to reach your desired line select the line by clicking in it and hit ctrl+r or right click the line and select "Run to line".

These techniques are hugely helpful and reduce debugging efforts massively, enjoy!

Comments

1

A trick I use is to type a space in your class, somewhere safe such as in the comment line; immediately delete it and save the class. This forces the execution point to jump to the beginning of your current method. Not ideal, I admit, but it can sometimes be used as a workaround to achieve what you want.

Although in the default installation of eclipse it is not possible to do directly move the execution point like in Visual Studio, there may exist an eclipse plugin which provides that functionality somewhere. Have a search around.

Comments

0

I like ankon's answer best, but another option (that will only work for your specific instance -- if that) is to stop at a breakpoint on your if and modify the variable(s) evaluated in the conditional such that it returns false (from the "Variables" view, right click on a variable and click "Change Value...")

1 Comment

Thanks for the suggestion. Generally that would be a work-around. In this instance the if statement evaluates the result of a function call, so I would have to step in there and alter the return value.
0

I thought that this was totally possible in older versions of eclipse, I thought I had the memory of doing it, but I guess I just implanted that memory when I worked in Visual Studio. From what I'm reading it might come to the jvm and not eclipse itself, there are pages where it's suggested that the jvm cannot handle that.

In my opinion Eclipse is many many times better than VS, I worked extensively in both and since I discovered Eclipse I was always in pain when I had to work in VS. But not having this feature is definitely hurting right now hehe.

Comments

0

You can jump directly to any other method call inside of the currently debugged method. Select some method call below your current instruction pointer and use "Step into selection" from the context menu.

Comments

0

unfortunately not possible to step forward with instruction pointer (program counter), so what you need to do instead is to introduce your own "debugging" variables that you can test on - lets say you want to step around a loop that takes too long, then add a variable and test on its increased value and then encapsulate the loop in an if with that variable. I know this is ugly, but it gets it done - or you could just develop in C++ :-)

Comments

-2

Just right click on desired line and choose run to line.That's it...

5 Comments

Not in 2009 when I asked this question :-) I'm not using Eclipse anymore so I cannot confirm whether this works or not.
If you click "Run to line", it won't move your instruction pointer. It is going to resume the execution and break when the selected line is hit. This won't allow you to go back or jump into blocks with conditions.
This seems to only work well when running forward, not when trying to run backward.
and won't skip code lines between the current pointer position and the target line, and exactly that is the most common use of this feature in Visual Studio.
The combination of Ctrl+R (Run to Line) and right-click on the method in the stack -> Drop to frame is hugely helpful and reduces debugging efforts massively!
-3

Put the cursor on the line of your choice and either hit ctrl-R ("Run to line") or right-click and select "Run to line" from the context menu.

1 Comment

That doesn't move the instruction pointer to a line of my choosing, it sets a temporary breakpoint at that line and just starts running again. It will execute intermediate instructions, and it will never reach the line I selected unless the natural program flow was headed there anyhow. In other words, it does not let me set the instruction pointer inside a branch that would not otherwise be reached.

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.