Debug debugging Tips for Eclipse

Source: Internet
Author: User

As a developer, it is necessary to master the debugging techniques in the development environment. We are in the process of writing Java programs, often encounter a variety of inexplicable problems, in order to detect where the program is a problem, often need to increase the log, look at the value of variables, so debugging is cumbersome. Suppose I spend 1 hours a day debugging my application, and that's a lot of time to build up. For this reason, use these times to value and understand all the features that make our debugging more convenient. It will save you some time and make your life more comfortable and relaxed.

First we need to note three points: do not use SYSTEM.OUT.PRINTLN as a debugging tool \ Use a Log Parser to read logs \ Enable verbose logging level for all components

A crazy statement about debugging as the opening. But it has to be said! Try slicing your complex logic into multiple separate units and writing unit tests to detect the correctness of your code. I imagine a process like this should happen very often--some people click through a large Web application, fill in multiple forms, switch to different pages, check the final page for the computational logic and implement most of the logic in this debug view. Always ask yourself before starting your tomcat: Is there a way to detect these behaviors using a unit test? You may not know or forget about these times in the past, but from now on, we will focus on some of the debugging techniques of Eclipse, and you will find a lot of good things about good code design.

Debug mode interface, divided into 5 regions
(1) Debug (Debug) area, show what is currently being debugged code, in the number of lines
(2) the Variables (variable) area, which displays the value of the variable.
(3) Code editing area
(4) Code structure area, showing each method in the code.
(5) Console (console) area.



First, conditional breakpoints

We usually debug the breakpoint when the general is to click on the left side of the line number, this time there will be a breakpoint to be beaten up. In the debug view, breakpoint View lists all breakpoints, but we can add a Boolean condition to determine whether the breakpoint is skipped. If the condition is true, the program stops at the breakpoint, otherwise the breakpoint is skipped and the program resumes execution. This is useful if you are interested in only one part of the app. For example, if you want to check the program during the 13th cycle, or debug some functionality in an abstract parent class, you are only interested in one of the specific implementations. You can set the conditions in the breakpoints view or the right-click menu ("Breakpoint Properties") labeled by the Blue breakpoint next to the code. You can pause the program when the conditional code fragment is true, or suspend the program when the value of the code fragment changes.


There are three options in the breakpoint attribute, enabling "enable", and hit count and conditional are selected according to our needs.
1) Hit Count is the number of times the code segment at the specified breakpoint is run, most typically a loop, and if you want a loop to execute 10 times on the thread to suspend, then specify the hits count value to 10, then the current loop executes to the nineth time when it hangs.
2) Conditional, as the name implies, is conditional judgment, for example, we need to loop variable i==10, when the thread hangs, the condition is set to i==10, select suspend when "true".
If the above hit count and conditional are selected, it will be effective if the expression and value settings are unreasonable. If you select suspend when value changes, it may be suspended conditional when the value of the variable is changed.

Second, the variable view

1, variable name view, display all the available instance variables and local variables in the current line of code 2. Displays all variable values 3. You can change the value of a variable by this window

If you need to see the values of a Map object or list object in a variable view, it is not always easy for Eclipse's default settings. Assuming that you are using HashMap, you need to click through the individual entity entries and face various HashMap implementation details. However, there is a button called "Show Logical Structure" above the variable view. It is very useful, especially if the information displayed by the ToString () method of your object is not friendly.

When you need to change the input information slightly, you do not need to restart the debugging session, just enter the new information in a table, you can directly modify the value of your variable in the debugging phase. There are times when you can save some time, you can simulate some weird situations through this function more simply.

You can see the value of the variable using CTRL+SHIFT+D or Ctrl+shift+i on the selected variable, and we can also add monitoring in expressions view.



Third, display view

Do you know the "Display view"? You can activate it through "window", "Show View", "Display" during debugging. Your eclipse should now be a new, blank view. You can use this view to enter or calculate some new code. The code is executed in the context of the current debug location, which means that you can use all variables or even content assistants. To execute your code, simply mark it and use the right-click menu or Ctrl+u (execute) or ctrl+shift+i (check).



Iv. Drop to Frame

Drop to frame means that it can be re-executed at the beginning of the current method, and the values of all the context variables are back to that time. Not necessarily the current method, you can click on any frame in the current call stack to jump there (except for the first frame). The main use is that all variable states are quickly restored to the beginning of the method and re-executed again, so that you can debug it over and over again in the context of your concern (combined with other functions such as changing the value of the variable), instead of having to debug it again. Of course, the side effects of the original execution process are irreversible (for example, you inserted a record into the database).



Five, Step filtration

We can add a filter to the preferences to exclude the specified package.


Vi. entering, skipping, returning

In fact, this technique is the most basic knowledge of Debug.
F5-step into: Move to the next step, if the current line is a method call, will enter the first line of this method. (Can be excluded by the Nineth article)
F6-step over: Moves to the next line. If the current row has a method call, the method is executed and returned to the next line.
F7-step Return: Resumes execution of the current method, and when the current method finishes executing, the control will go to the row that the current method is called.
F8-moves to the next breakpoint.



In short, now the basic debugging methods are basically shared, what, you do not understand, then please open your eclipse to do the actual work! Do not blindly read the blog and not to practice, the use of debug from life also used life, say again, now to do it!

Enter Debug mode:
1. Set breakpoints
2. Start the debug mode on the servers side
3, run the program, in the background encountered a breakpoint, enter the Debug debugging state



Debug debugging Tips for Eclipse

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.