在 Eclipse 中调试 Java 时移动指令指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1651379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moving the instruction pointer while debugging Java in Eclipse
提问by Eric J.
Can I move the instruction pointer directlyto a line of my choice (within the current method) while debugging a Java program in Eclipse (Galileo)?
在 Eclipse (Galileo) 中调试 Java 程序时,我可以将指令指针直接移动到我选择的一行(在当前方法中)吗?
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).
在 Visual Studio 的方法中将指令指针拖动到所需的行很简单,但我在 Eclipse 中没有看到这样做的方法(并且在文档或谷歌上没有找到任何关于它的信息)。
采纳答案by ankon
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.
如果您只是想在当前位置执行一些代码,您可以使用该Expressions
视图并将您的代码作为表达式输入。表达式求值调用的方法将在当前调试上下文中运行。
回答by John S.
This is possible...
这个有可能...
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.user/tips/jdt_tips.html
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.
Drop to frame - 在单步执行代码时,您可能偶尔会走得太远,或者跨过您打算进入的行。您可以使用 Drop to Frame 操作快速返回到方法的开头,而不是重新启动调试会话。选择与您希望重新启动的 Java 方法对应的堆栈框架,然后从调试视图工具栏或堆栈框架的上下文菜单中选择 Drop to Frame。当前指令指针将被重置为方法中的第一个可执行语句。这也适用于非顶部堆栈帧。
请注意,仅在使用 1.4 或更高版本的 VM 或 J9 VM 进行调试时才可使用降帧。在某些情况下,JVM 可能无法从堆栈中弹出所需的帧。例如,通常不可能下降到堆栈的底部框架或本机方法下方的任何框架。
回答by fl0w
Moving the pointer like in Visual Studio is not possible, however workarounds are:
像在 Visual Studio 中那样移动指针是不可能的,但解决方法是:
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.
返回到当前执行的方法的开头:从调试调用堆栈中选择方法,右键单击 ->“Drop to frame”等等,你又回到了方法的开头。
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".
现在要到达所需的行,请通过单击该行并按 ctrl+r 或右键单击该行并选择“运行到行”来选择该行。
These techniques are hugely helpful and reduce debugging efforts massively, enjoy!
这些技术非常有用,可以大大减少调试工作,享受吧!
回答by IqbalHamid
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.
尽管在 eclipse 的默认安装中不可能像在 Visual Studio 中那样直接移动执行点,但可能存在一个 eclipse 插件在某处提供该功能。四处找找。
回答by violoncello
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...")
我最喜欢 ankon 的答案,但另一个选项(仅适用于您的特定实例——如果那样的话)是在您的断点处停止if
并修改条件中评估的变量,使其返回 false(来自“变量”视图,右键单击变量并单击“更改值...”)
回答by user1294431
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.
我认为这在旧版本的 eclipse 中是完全可能的,我认为我有这样做的记忆,但我想我只是在我在 Visual Studio 中工作时植入了那个记忆。从我正在阅读的内容来看,它可能会出现在 jvm 而不是 eclipse 本身,有些页面建议 jvm 无法处理它。
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.
在我看来,Eclipse 比 VS 好很多倍,我在这两个方面都做了大量工作,自从我发现 Eclipse 后,当我不得不在 VS 中工作时,我总是很痛苦。但是现在没有这个功能肯定会受到伤害,呵呵。
回答by Bananeweizen
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.
您可以直接跳转到当前调试方法内的任何其他方法调用。在当前指令指针下方选择一些方法调用,然后使用上下文菜单中的“步入选择”。
回答by serup
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++ :-)
不幸的是,无法使用指令指针(程序计数器)向前推进,因此您需要做的是引入您自己的“调试”变量,您可以对其进行测试 - 假设您想要绕过一个耗时太长的循环,然后添加一个变量并测试其增加的值,然后将循环封装在带有该变量的 if 中。我知道这很丑,但它完成了 - 或者你可以用 C++ 开发 :-)
回答by Ajay Takur
Just right click on desired line and choose run to line.That's it...
只需右键单击所需的行并选择运行到行。就是这样...
回答by Jonathan Feinberg
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.
将光标放在您选择的行上,然后按 ctrl-R(“运行到行”)或右键单击并从上下文菜单中选择“运行到行”。