在 Java 中设置断点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/856388/
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
Setting breakpoints in Java
提问by Thilo
How does setting breakpoints in Java work? Is it just based on the source file name and line number? Does the class or method name also figure in?
在 Java 中设置断点是如何工作的?是否仅基于源文件名和行号?类或方法名称是否也包含在内?
If I have an old version of the source in my debugger and set a breakpoint, the cursor is off when I step through. How far off can it be? Can it go into the wrong method (or even the wrong class if there are more than one class in that file)?
如果我的调试器中有一个旧版本的源代码并设置了断点,则当我单步执行时光标会关闭。还能差多远?它是否会进入错误的方法(如果该文件中有多个类,甚至会进入错误的类)?
What happens when there are multiple classes of the same name in the JVM (could happen if you have more than one classloader)? Do they all get the breakpoint?
当 JVM 中有多个同名类时会发生什么(如果您有多个类加载器,可能会发生这种情况)?他们都得到断点了吗?
In a web application container, can I set breakpoints for just one web application (and not the others)?
在 Web 应用程序容器中,我可以只为一个 Web 应用程序(而不是其他应用程序)设置断点吗?
How much of this is IDE specific and how much is determined by the debugging interface that the JVM provides? For example: In Eclipse I can set conditional breakpoints based on the values of variables. Is that just filtering done by Eclipse on an unconditional breakpoint in the JVM?
其中有多少是 IDE 特定的,有多少是由 JVM 提供的调试接口决定的?例如:在 Eclipse 中,我可以根据变量的值设置条件断点。这只是由 Eclipse 在 JVM 中的无条件断点上完成的过滤吗?
采纳答案by Wouter Coekaerts
There are different kind of breakpoints. Some breakpoints are line-based, some are not. How this affects your actual debugging depends on what your IDE actually does. For example, in Eclipse, if you add a breakpoint in the middle of a method, that will be a line-based breakpoint. If you add a breakpoint on a line containing the signature of a method, that will be a method entry breakpoint.
有不同类型的断点。有些断点是基于行的,有些则不是。这如何影响您的实际调试取决于您的 IDE 实际执行的操作。例如,在 Eclipse 中,如果在方法的中间添加断点,那将是基于行的断点。如果在包含方法签名的行上添加断点,那将是方法入口断点。
If the source code you're looking at is not the exact source of the class that is running, a line breakpoint won't be mapped onto the right line of course. So java might not stop at the line you intended, and your IDE indeed could be showing you the wrong method or even wrong class. But a method entry breakpoint will still work (stop at the right moment), even if the line on which the method was defined has changed; but again an IDE might show the wrong line in the debugger. (And there are other kind of events/breakpoints too, like class loading,... You could take a look at the subinterfaces of EventRequestif you want to know more about the internals).
如果您正在查看的源代码不是正在运行的类的确切源代码,则行断点当然不会映射到正确的行上。因此,java 可能不会停在您想要的行上,您的 IDE 确实可能会向您显示错误的方法甚至错误的类。但是方法入口断点仍然有效(在正确的时刻停止),即使定义方法的行已经改变;但同样,IDE 可能会在调试器中显示错误的行。(还有其他类型的事件/断点,例如类加载,...如果您想了解更多有关内部结构的信息,可以查看EventRequest的子接口)。
To answer your other question: breakpoints apply to all classloaders in the JVM.
回答您的另一个问题:断点适用于 JVM 中的所有类加载器。
回答by Jim Ferrans
The JVM supports a standard API for debugging (see Java Platform Debugger Architecture), and all IDEs use the JPDA to do all the heavy lifting of setting breakpoints, computing expressions, and so on. The IDEs "just" wrap it up in nice user interfaces.
JVM 支持用于调试的标准 API(请参阅Java 平台调试器体系结构),并且所有 IDE 都使用 JPDA 来完成设置断点、计算表达式等的所有繁重工作。IDE“只是”将其包装在漂亮的用户界面中。
When you set a breakpoint, the IDE talks to the JVM's Tool Interface (part of JDPA) and gives it the source file and line number of the breakpoint. The JVM has the information it needs to map the physical location of the breakpoint onto the actual location of the Java statement in the compiled code of the class. When the JVM reaches a breakpoint, it stops executing that thread and tells the IDE the breakpoint's source file and line number. The IDE then displays the location to you.
当您设置断点时,IDE 会与 JVM 的工具接口(JDPA 的一部分)对话,并为其提供断点的源文件和行号。JVM 拥有将断点的物理位置映射到类的编译代码中 Java 语句的实际位置所需的信息。当 JVM 到达断点时,它会停止执行该线程并告诉 IDE 断点的源文件和行号。然后,IDE 会向您显示该位置。
When I debug I sometimes add or delete lines to fix a problem, and then continue on to the next problem. But things get wierd because breakpoints now appear to be sooner or later than I expected, and if I set new breakpoints they're not actually at the source line I set them at. When this happens, I restart the program and then both the IDE and the JVM are consistent again.
当我调试时,我有时会添加或删除行来解决问题,然后继续解决下一个问题。但是事情变得很奇怪,因为现在断点似乎比我预期的要早或晚,而且如果我设置新断点,它们实际上并不在我设置它们的源代码行中。发生这种情况时,我重新启动程序,然后 IDE 和 JVM 再次保持一致。