在 Java 中设置断点

2022-01-16 00:00:00 debugging jvm breakpoints java

如何在 Java 中设置断点?它只是基于源文件名和行号吗?类名或方法名是否也包含在内?

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?

如果我的调试器中有旧版本的源代码并设置了断点,则当我单步执行时光标会关闭.能走多远?它会进入错误的方法(如果该文件中有多个类,甚至是错误的类)?

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

当 JVM 中有多个同名的类时会发生什么(如果您有多个类加载器可能会发生)?他们都得到断点了吗?

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?

在 Web 应用程序容器中,我可以只为一个 Web 应用程序(而不是其他 Web 应用程序)设置断点吗?

In a web application container, can I set breakpoints for just one web application (and not the others)?

其中有多少是特定于 IDE 的,又有多少是由 JVM 提供的调试接口决定的?例如:在 Eclipse 中,我可以根据变量的值设置条件断点.这只是 Eclipse 在 JVM 中的无条件断点上完成的过滤吗?

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 实际执行的操作.例如,在 Eclipse 中,如果您在方法的中间添加断点,那将是基于行的断点.如果在包含方法签名的行上添加断点,那将是方法入口断点.

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.

如果您正在查看的源代码不是正在运行的类的确切源代码,那么行断点当然不会映射到正确的行.所以 java 可能不会停在你想要的那一行,你的 IDE 确实可能会向你显示错误的方法甚至错误的类.但是方法入口断点仍然有效(在正确的时刻停止),即使定义方法的行发生了变化;但是 IDE 可能再次在调试器中显示错误的行.(还有其他类型的事件/断点,比如类加载,......你可以看看 EventRequest 如果您想了解更多关于内部的信息).

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 EventRequest if you want to know more about the internals).

回答您的另一个问题:断点适用于 JVM 中的所有类加载器.

To answer your other question: breakpoints apply to all classloaders in the JVM.

相关文章