Debugging skills of Eclipse

1. Debug view


The most commonly used windows in debugging are:


window illustrate
Debug window Mainly displays the current thread method call stack, and the number of lines of code (code with debugging information)
Breakpoints Breakpoints window => Breakpoint list window, you can easily add breakpoints, set breakpoint conditions, delete breakpoints, etc.
Variables Variables window => Display the local variables of the current method, non-static methods, including this application, you can modify the variable value
code editing window Needless to say about this
Output Console window => log and other output content. When debugging, you can set the level of the component concerned to a lower level in order to obtain more output information


Additional auxiliary windows are:

window illustrate
expression expression window => Write the expression of the data you need to observe, or modify the variable value
Display window => You can execute code blocks in display, output content, etc.
Outline window => View methods, variables, etc. of the current class
Type hierarchy window => View the inheritance hierarchy of the current class, including the implementation interface, class inheritance hierarchy
Method call relationship Call hierarchy window => View which methods the current method is called by, which classes and lines the calling method is in, and you can directly open the corresponding method
Search resultsSearch window => Combined with shortcut keys, you can view the code locations where variables, methods, etc. are referenced or defined in workspaces, projects, and working sets

1) Window overview:





2) Debug View (thread stack view):


The debug view allows you to manage the program being debugged and running on the workbench. It shows the stack frames of the threads suspended in the program you are debugging. Each thread in the program appears as a tree node. He shows the running process of each target. If the thread is suspended, its stack frame is displayed as a child element. Here are some commonly used debug buttons:




1. Indicates that the current implementation continues to run until the next breakpoint, and the shortcut key is F8.

2. It means to interrupt the whole process

3. It means to enter the current method, and the shortcut key is F5.

4. Indicates to run the next line of code, the shortcut key is F6.

5. It means to exit the current method and return to the calling layer. The shortcut key is F7.

6. Represents the stack of the current thread, from which you can see which code is running, and the entire calling process, as well as the code line number



二、Debug


1. Set a breakpoint

In the source code file, at the marked line in front of the code line where you want to set a breakpoint, double-click the left mouse button to set a breakpoint, and double-click again at the same position to cancel the breakpoint. Sometimes we still have such a need, that is, I don't want to execute the code line by line. For example, a for loop will loop more than 1000 times. I just want to suspend the thread for debugging at the 500th time. At this time, we can Use conditional breakpoints. Set a conditional breakpoint: We can set a triggering condition for the breakpoint. Once a certain condition is met, debugging will begin. You can right-click on the breakpoint and select Breakpoint Properties to enter the breakpoint setting page . Just when I was talking about the breakpoint view We have learned the usage of Hit Count and Conditional, where conditions and execution times can be set.


1.1) Breakpoint types and breakpoint window

There are five types of breakpoints that can be set during debugging:

 

1. Line breakpoints  : Conditional breakpoints, as the name suggests, are breakpoints with certain conditions. Only if the conditions set by the user are met, the code will stop when running to the breakpoint.


2. Method breakpoints : The special feature of method breakpoints is that they can be placed in the source code of the JDK. Since the JDK removes the debugging information when compiling, ordinary breakpoints cannot be hit inside, but the method Breakpoints do, and you can view the call stack of a method in this way.


3.观察断点(watch breakpoints-成员变量访问变更)

4.异常断点(exception breakpoints)

5.类加载断点(class load breakpoints)


每种断点的设置有些许不一样,可以在断点上右键->Breakpoint properties进行设置,但一般在断点窗口有快速设置的界面,Breakpoint properties中多了filter, 其实比较鸡肋,用处不大。


断点相关的快捷键:


快捷键 说明
ctrl+shift+b 在光标处大断点/取消断点
ctrl+alt+b 忽略所有断点
Alt+shift+q, b 激活断点窗口

1、行断点: 在方法中的某一行上打断点1.11.4。行断点可以设置挂起线程/VM的条件1.3,访问次数1.2。 
1.3中的条件是,spring在注册Bean定义(registerBeanDefinition)时,如果是org.springframework.demo.MyBean,就挂起线程,可以开始单步调试了。 
对于命中次数(hit count)1.2的使用,一般是在循环中,第N个对象的处理有问题,设置hit count = N, 重调试时,可以方便到达需要调试的循环次数时,停下来调试。


2、方法断点:在方法上打断点2.12.2。方法断点的好处是可以从方法方法进入或者退出时2.3,停下来调试,类似行断点,而且只有行断点和方法断点有条件和访问次数的设置功能。 
但是方法断点还有另外一个好处,如果代码编译时,指定不携带调试信息,行断点是不起作用的,只能打方法断点。 
有兴趣的可以通过A1将Add line number… 前的勾去掉, 调试下看看。




3、观察断点: 在成员变量上打的断点3.13.3。只有对象成员变量有效果,静态成员变量不起作用。 
可以设置变量被访问或者设置的时候挂起线程/VM 3.2,也就是类似3.4的所有对成员变量的访问或者设置的方法都会被监控到 


4、异常断点: 异常断点可以通过4.6添加,或者点击日志信息中输出的异常类信息添加。 
异常断点4.1,系统发生异常时,在被捕获异常的抛出位置处或者程序未捕获的异常抛出处4.24.4, 挂起线程/VM, 也可以指定是否包括异常的子类也被检测4.34.5。 


另外除了以上正常设置的异常挂起,从java->debug中可以设置挂起执行,主要有下面两个:

 

1、是否在发生全局未捕获时挂起(suspend execution on uncaught exceptions),调试时,老是有异常挂起影响调试,但是没有设置异常断点的情况,就可以勾选掉这个选项;

2、是否在编译错误时挂起,一般在边调试边改代码时会发生这种情况;


另外要提一个的是有main方法启动的应用,可以在调试配置中勾选stop in main A3, 程序进入时,会挂起线程,等待调试。




5、类加载断点: 在类名上打的断点5.1。接口上是打不了类加载断点的,但是抽象类是可以的,只是在调试的时候,断点不会明显进入classloader中,单步进入知会进入到子类的构造方法中,非抽象类在挂起线程后单步进入就会到classloader中(如果没有filter过滤掉的话)5.3。类加载断点不管是打在抽象或者非抽象类上,都会在类第一次加载或者第一个子类第一次被加载时,挂起线程/VM5.2





2.调试程序


1、调试本地 Java 语言程序


在所有调试中,调试一个Java程序是最简单的,主要有设置断点、启动调试、单步执行、结束调试几步。

1)设置断点:




2)启动调试:Eclipse提供四种方式来启动程序(Launch)的调试,分别是通过菜单(Run –> Debug)、图标(“绿色臭虫”)、右键->Debug As以及快捷键(F11),在这一点上,与其他命令(例如Run)类似。




弹出提示,需要切换到调试(Debug)工作区,勾选“Remember my decision”,记住选择,则下次不再提示,然后点击【Yes】。



3)单步执行:主要使用前面讲过的几个视图进行调试,其中debug视图中的几个按钮有快捷键:

Step Retuen(F7)

Step Over (F6)

Step Into (F5)





结束调试:通过Terminate命令终止对本地程序的调试。



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325434574&siteId=291194637