52

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

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • AFAIK you can't backtrack to code already executed. – Abdul Oct 30 '09 at 18:15
  • In my specific instance I want to jump over an **if** statement, but I find it generally very helpful in Visual Studio to be able to arbitrarily move the IP within a method. When googling I came across this blog (http://blogs.zdnet.com/Burnette/?p=52) claiming Eclipse is 21st century while VS 2005 (!) is 20th century, while highlighting that even old versions of VS allow the IP to be moved. – Eric J. Oct 30 '09 at 18:27
  • 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++? – Denis Tulskiy Oct 30 '09 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. – Eric J. Oct 30 '09 at 19:43
  • @Abdul: Eclipse will let you can backtrack to a previous frame. – JRL Oct 30 '09 at 19:55
  • @JRL: I know about frame backtracking. It's a good feature as far as it goes, but just allows me to replay execution of a method, not alter flow within the method. – Eric J. Oct 30 '09 at 23:34
  • 1
    I also want to jump over some statements. Not backtrack or anything. This should be basic function of a debugger/IDE. – Csaba Toth May 13 '13 at 21:55
  • 1
    I'm stunned, it's hard to believe this, it seems to be true. – Csaba Toth May 13 '13 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). – Csaba Toth May 13 '13 at 22:03
  • @CsabaToth: I have no idea why I wrote that comment 4 years ago, I was probably referring to backtracking and I probably had wrong impression of what this functionality was doing in VS. Yes, it sounds definitely useful, and yes, you can get yourself really confused by overlooking some state change. And getting yourself confused while debugging means headbanging your keyboard after several hours. If you want to jump over some piece of code, use "jump to cursor", eclipse should have it. For everything else, strive for unit tests and unit-testable code. – Denis Tulskiy May 14 '13 at 16:22

10 Answers10

33

This is possible...

http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.user/tips/jdt_tips.html

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.

John S.
  • 1,937
  • 2
  • 18
  • 28
  • 1
    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. – user1187008 May 31 '13 at 12:19
  • 1
    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: https://bugs.eclipse.org/bugs/show_bug.cgi?id=118147 Pl post updates on this thread whenever this gets available. – Mosca Pt May 06 '15 at 18:08
  • This is really nice to know, but not what I was trying to achieve when posting my question. – Eric J. Sep 15 '15 at 15:28
  • It also seems you cannot drop further than the next native frame on the callstack. – Thomas Weller Sep 21 '15 at 07:14
  • @JohanGunnarsson: the bug seems to be for CDT (C++) only, not for Java – Thomas Weller Sep 21 '15 at 07:16
  • I might be wrong - but haven't used c++ for a quite a while now - I remember seeing it confirmed for Java elsewhere around this time/May too. Did you have a JVM/Eclipse version where you verified it as working? – Mosca Pt Sep 25 '15 at 16:04
  • Fascinating. I asked the question in 2009 and responses from 2018 indicate this still isn't possible. – Eric J. May 22 '18 at 22:45
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.

ankon
  • 4,128
  • 2
  • 26
  • 26
  • The only thing 'sort of' what he is looking for is step filtering http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.jdt.doc.user/reference/views/debug/ref-usestepfilters.htm – amischiefr Oct 30 '09 at 18:57
  • Looks like there's enough agreement (based on the upvotes) that this isn't possible. Thanks for the info. – Eric J. Oct 30 '09 at 19:45
  • 7
    Kind of sad that we had the technology to do this in debuggers in the early 1980s but can't do it now... – Brian Tarbox Jun 27 '13 at 14:34
  • 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. – John Henckel Aug 01 '13 at 15:46
  • This is actually the "Immediate window" version of Eclipse – Juan Carlos Puerto Sep 24 '15 at 14:53
2

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!

fl0w
  • 3,593
  • 30
  • 34
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.

IqbalHamid
  • 2,324
  • 1
  • 18
  • 24
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.

user1294431
  • 635
  • 1
  • 6
  • 18
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.

Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
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...")

  • 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. – Eric J. Oct 30 '09 at 19:44
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++ :-)

serup
  • 3,676
  • 2
  • 30
  • 34
-2

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

Ajay Takur
  • 6,079
  • 5
  • 39
  • 55
  • Not in 2009 when I asked this question :-) I'm not using Eclipse anymore so I cannot confirm whether this works or not. – Eric J. Sep 15 '15 at 15:25
  • 7
    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. – Attila Sep 17 '15 at 09:33
  • This seems to only work well when running forward, not when trying to run backward. – Thomas Weller Sep 21 '15 at 06:59
  • 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. – jonayreyes Aug 31 '17 at 12:25
  • 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! – fl0w Jan 03 '18 at 09:54
-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.

Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
  • 7
    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. – Eric J. Oct 30 '09 at 18:16