In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you what are the Debug debugging skills in Eclipse. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
I. Debug view
The most commonly used windows in debugging are:
Window description Debug window mainly displays the current thread method call stack, and the number of lines of code (code with debugging information) breakpoint Breakpoints window = > breakpoint list window, it is convenient to add breakpoints, set breakpoint conditions, delete breakpoints and other variables Variables window = > displays local variables of the current method, non-static methods, including this applications You can modify the variable value code editing window = > this needless to say output Console window = > log and other output content. When debugging, you can set the concerned component level a little lower to get more output information.
In addition, auxiliary windows are:
Window description expression expression window = > write the expression of the data you need to observe, or modify variable value Display window = > you can execute code blocks in display, output content, etc. Outline Outline window = > View the method of the current class, variable and other type level Type hierarchy window = > View the inheritance hierarchy of the current class, including implementation interface, class inheritance hierarchy method call relationship Call hierarchy window = > see which methods are called by the current method. In which class and line of the method is called, you can directly open the corresponding method search result Search window = > combine the shortcut keys to view the code location where variables, methods, etc. are referenced or defined in the workspace, project, workset, etc.
1) window overview:
2) Debug View (thread stack view):
The debug view allows you to manage the program you are debugging and running on the workbench. It shows the stack frame of the suspended thread in the program you are debugging, and each thread in the program appears as a node of the tree. He shows the process of each target that is running. If a thread is suspended, its stack frame is displayed in the form of child elements. Here are some common debug buttons:
1. Indicates that the current implementation continues until the next breakpoint, and the shortcut key is F8.
two。 Means to interrupt the whole process.
3. Indicates to enter the current method, and the shortcut key is F5.
4. Means to run the next line of code, and the shortcut key is F6.
5. Means to exit the current method and return to the call layer. The shortcut key is F7.
6. Represents the stack of the current thread, from which you can see what code is running, and the entire calling process, as well as the line number of the code
Detailed introduction:
Skip All Breakpoints: set all breakpoints to be skipped. After setting Skip All Breakpoints, there will be a slash on all breakpoints, indicating that the breakpoint will be skipped and the thread will not be suspended at that breakpoint.
Drop to Frame: this command allows the program to restart execution back to the first line of the current method, re-execute the java stack frame, select a specified stack frame, and then click Drop to Frame, so that you can re-enter the specified stack frame. Note when using Drop to Frame:
1. Cannot drop into a method in a method stack that has already been executed.
When 2.drop is added to stack frame, the original value of the global data is not changed, for example, a vertor containing elements is not emptied.
Step Filters: this function is relatively simple, that is, when we want to ignore some classes that we don't care about when we are in debug, we can turn on Step Filters to filter, and the program will execute until we encounter an unfiltered location or breakpoint. The Step Filters function consists of four items of Use Step Filters,Edit Step Filters,Filter Type,Filter Package. The specific operations are as follows:
Step 1: Windows-> Preferences-> Java-> Debug-> Step Filtering.
Step 2: select 'Use Step Filters'
Step 3: select the desired options on the screen. You can add some code from your own code library.
Step 4: click 'Apply''
In principle, the Edit Step Filter command is used to configure Step Filter rules, while Filter Type and Filter Package refer to the filtered Java type and Java Package, respectively.
Step Return: jump out of the current method, and during the execution of the called method, using Step Return will jump out of the method and return to the method that called it after all the code of the current method has been executed.
Step Over: in single-step execution, when you encounter a sub-function within a function, you will not step into the sub-function, but stop the entire execution of the sub-function, that is, the whole sub-function as a step.
Step Into: step by step, enter and continue step by step when you encounter a subfunction
Resume: resumes the paused thread and jumps directly from the current position to the next breakpoint.
Suspend: pause the selected thread so that you can browse or modify the code, check the data, etc.
Eclipse supports thread suspension and recovery through Suspend and Resume. Generally speaking, Suspend is suitable for debugging multithreaded programs. When we need to check the stack frame and variable values of a thread, we can suspend the thread through the Suspend command. Resume is used for recovery.
There are two types of Resume to be aware of:
The first is that when you modify the program code during debugging, then save it, and click Resume, the program will temporarily hang at the breakpoint.
The second is that when the program throws an exception, it runs Resume, and the program also hangs at the breakpoint.
Terminate: Eclipse terminates debugging of the local program with the Terminate command.
Disconnect:Eclipse uses the Disconnect command to terminate the socket connection to the remote JVM.
1. Debug and execute
Marking function shortcuts describe 6.4Step InfoF5- > step into (if there is a method call, enter the calling method for debugging); 6.4Step OverF6- > step skip (do not enter any method call into the line, directly execute the current line of code and skip to the next line); 6.4Step ReturnF7- > single step return (after executing the current method and popping it up from the call stack to return to the place where the current method is called) 6.5ResumeF8-> resumes normal execution (until the next breakpoint is encountered); 7.4Run to LineCtrl+R- > executes to the current line (all intermediate breakpoints will be ignored and executes to the current cursor line); 6.3Drop To Frame none-> rollback to the beginning of the specified method, which is a great function.
Right-click a method on the method call stack and select Drop To Frame to execute from the beginning of the method
For example, to re-execute this method, you can use Drop To Frame on this method, which will be re-executed from the first line of this method.
Of course, for methods that have side effects, such as database operations, operations such as changing the object contents of the passed parameters may be re-executed and what you want again. 6.1+6.2Copy Stack none-> copy current thread stack information
If you need to exclude some classes, packages, etc. that do not need to be debugged when debugging, you can use the Edit Step Filters setting.
Properties: Java process startup information of A6, including console startup parameters, environment parameters and so on. If there is a problem with the parameter startup parameters, you can first see if the actual startup parameters here are wrong. You can also view options for virtual machines that support debugging.
2. Data viewing
Mark function shortcut description 7.4 inspect the value of the selected variable, expression or execution result, press ctrl+shift+i again to add the current expression or value to the Expressions window for view; 7.4 Displayctrl shift shortcut-> display the selected variable, value or execution result of the expression, press ctrl+shift+d again to add the current expression or value to the Display window to display; 7.4Executectrl select expression 7.4Run to Linectrl+r- > execute to the current line (all intermediate breakpoints will be ignored and executed to the current cursor line); 7.3All Instancesctrl+shift+n- > view all objects of the selected class, which is awesome 7.3Instance Count none-> check the number of all objects of the selected class; 7.4Watch none-> add the current variable and expression to the Expressions window
3) Variables View (variable view)
1. For the variable name view, showing all accessible instance variables and local variables in the current line of code
two。 Show the values of all variables
3. You can change the value of a variable through this window
Variables View displays variable information related to the stack frame selected in Debug View, and when debugging a Java program, variables can choose to display more detailed information in the details pane. In addition, the Java object can display the values of the properties it contains. Select the variable in this window and click the right mouse button to do a lot of operations, the main operations are as follows:
All Instances: opens a dialog box to display all instances of the java class. Using this feature requires the java virtual machine to support instance retrieval.
All References:: opens a dialog box to display all java objects that reference the variable
Change Value:: changes the value of the variable, which can be used in conjunction with Drop to Frame for program debugging. These two functions can be used instead of re-debug.
Copy Variables: copy the value of a variable, especially if the value of the variable is very long (such as json data), this feature comes in handy.
Find: sometimes when there are a lot of variables in a class, you can look for it.
4) Breakpoints View (breakpoint view)
1. Show all breakpoints
two。 Invalidate the port selected in the current window 1 and click enable again.
3. Abnormal breakpoint
Breakpoints View will list all the breakpoints you have set in the current work area. Double-clicking the breakpoint will take you to the location of the breakpoint in the program. You can also enable or disable breakpoints, delete, add new ones, and group them according to workgroups or hit counts. The following two techniques are very useful when using breakpoints:
Hit Count: specifies how many times the code snippet at the breakpoint is run. The most typical example is a loop. If you want to suspend a thread after 10 execution times, specify a Hit count value of 10, and the current loop will fail when it executes for the ninth time.
Conditional: as the name implies, it is a condition judgment. For example, if we need the loop variable iDiversion10 and the thread hangs, then the condition is set to iDetermine 10, and select Suspend when "true".
If both Hit Count and Conditional are selected above, it will be invalidated if the expression and value are not set properly. If you choose Suspend when value changes, it is possible that Conditional hangs when the value of the variable changes.
5) Expressions View (expression View)
1. Expression.
two。 Click here to add an expression
To find the value of the expression in the editor of the Debug perspective, select the entire line with a breakpoint and select the Inspect option from the context menu. The expression is evaluated in the context of the current stack frame, and the result is displayed in the Expressions view of the Display window. For example, if I want to calculate the value of the variable aaccounb, I can add an expression to the expression view: aaccounb
6) Display View
You can use this view to enter or calculate some new code. This code is executed in the context of the current debugging location, which means that you can use all variables or even content helpers. To execute your code, simply mark it and use the right-click menu or CTRL+U or CTRL+SHIFT+I
7) Code View Secondary window
1. Code view:
Code view, which is used to display specific code. Where the green part refers to the code that will be executed currently.
The marking function shortcut key describes 11.1~11.5quick type hierarchyctrl+t to view the inheritance hierarchy of the current class and interface. When entering by default, the subclass inheriting / implementing the current class / method is displayed, subinterface 11.1; again ctrl+t, the superclass / interface 11.2 of the current class and interface inheritance / implementation will be displayed. When debugging, we often use this function to view the implementation class 11.4 in the interface or abstract class method invocation 11.3 ctrlinitit, and navigate directly to the corresponding implementation method 11.5. Quick outlinectrl+o looks at the outline of the current class, including methods, properties, etc.; it is of little use; where open declarationsF3 looks at variables, properties, and method definitions
2. Call Hierarchy window:
Markup function shortcuts describe 12.1~12.2open call hierarchyctrl+alt+h to view the hierarchy of method calls. You can see where the current method is called, or where the current method calls method 12.2 of other classes.
3. Type Hierarchy window:
Tag function shortcut key description 13.1~13.4open type hierarchyF4 view inheritance hierarchy, you can view the inheritance hierarchy of a class, including subclass parent class 13.1, or class implementation interface inheritance level 13.2. According to the selected class / interface, the outline of the class 13.3,13.4 is displayed on the right. 13.3 you can choose whether to display the properties and methods of the parent class / parent interface.
4. Search window:
Marking function shortcuts describe the location where 14.1declarationsctrl+g 's same method signature is defined in the workspace and in the third-party jar package 14.114.2referencesctrl+shif+g currently selected variables, attributes, methods are referenced in the workspace and third-party jar packages 14.214.3ctrl+shift+u view variables, attributes, Where the method appears in the current class 14.314.4implements view the display mode of class 14.814.4 / 14.7 that implements the current interface. You can choose different display modes. Look at the picture in detail
II. Debug
1. Set breakpoint
In the source code file, at the preceding marker line of the line of code where you want to set the breakpoint, double-click the left mouse button to set the breakpoint, and double-click again at the same location to cancel the breakpoint. Sometimes we also need to do this, that is, I do not want to execute code one line at a time. For example, a for loop will loop more than 1000 times. I just want to suspend the thread for debugging for the 500th time. At this time, we can use conditional breakpoints. Set conditional breakpoint: we can set trigger conditions for the breakpoint. Once a certain condition is met, you can right-click on the breakpoint and select Breakpoint Properties to enter the breakpoint settings page. We just learned the usage of Hit Count and Conditional in the breakpoint view. Here you can set the conditions and the number of execution times.
1.1) breakpoint types and breakpoint window
There are five types of breakpoints that can be set during debugging:
1. Line breakpoint (line breakpoints): a conditional breakpoint, as its name implies, is a conditional breakpoint. Only if the conditions set by the user are met will the code stop when it runs to the breakpoint.
two。 Method breakpoint (method breakpoints): the special feature of method breakpoint is that it can be typed in the source code of JDK. Because JDK removes debug information during compilation, ordinary breakpoints cannot be hit inside, but method breakpoints can be done. You can view the call stack of the method in this way.
3. Observe breakpoints (watch breakpoints- member variable access changes)
4. Exception breakpoint (exception breakpoints)
5. Class load breakpoint (class load breakpoints)
The setting of each breakpoint is slightly different. You can right-click on the breakpoint-> Breakpoint properties to set it, but generally there is a quick setting interface in the breakpoint window. There is more filter in Breakpoint properties, which is of little use.
Breakpoint-related keyboard shortcuts:
Shortcut key description ctrl+shift+b at the cursor big breakpoint / cancel breakpoint ctrl+alt+b ignores all breakpoint Alt+shift+q, b activates breakpoint window
1. Line breakpoints: break points 1.1, 1.4 on a line in the method. The line breakpoint can set the condition of suspended thread / VM 1.3.The number of visits is 1.2.
The condition in 1. 3 is that when spring registers the Bean definition (registerBeanDefinition), if it is org.springframework.demo.MyBean, the thread is suspended and you can start stepping into debugging.
For the use of hit count 1.2, generally in the loop, there is a problem with the handling of the Nth object, set hit count = N, when re-debugging, it is convenient to reach the number of cycles that need to be debugged, stop and debug.
2. Method breakpoints: break points 2.1 and 2.2 in the method. The advantage of method breakpoints is that they can be debugged when they enter or exit from the method 2.3, similar to line breakpoints, and only line breakpoints and method breakpoints have the function of setting conditional and access times.
But the method breakpoint has another advantage, if the code is compiled without debugging information, the line breakpoint does not work and can only hit the method breakpoint.
If you are interested, you can Add line number through A1. Remove the front tick and have a look at it.
3. Observation breakpoints: breakpoints 3.1 and 3.3 made on member variables. Only object member variables have an effect, and static member variables do not.
Thread / VM 3.2 can be suspended when variables are accessed or set, that is, all access or setting methods to member variables like 3.4 are monitored to
4. Exception breakpoints: exception breakpoints can be added by 4.6 or by clicking the exception class information output in the log information.
When an exception occurs in the system, the thread / VM is suspended at the thrown position of the caught exception or the uncaught exception thrown by the program, and it can also be specified whether the subclass of the exception is detected 4.3,4.5.
In addition, in addition to the above normal setting exception suspension, you can set the suspension of execution from java- > debug. There are mainly the following two:
1. Whether to suspend (suspend execution on uncaught exceptions) when global uncaptured occurs. When debugging, there are always exceptions that affect debugging, but if no exception breakpoint is set, you can check this option.
2. whether it hangs during a compilation error, which usually occurs when you change the code while debugging
Another thing to mention is the application started by the main method. You can check stop in main A3 in the debug configuration. When the program enters, it will suspend the thread and wait for debugging.
5. Class load breakpoint: breakpoint 5.1 on the class name. The class load breakpoint cannot be hit on the interface, but the abstract class is OK, but when debugging, the breakpoint will not obviously enter the classloader, step into the subclass constructor, and the non-abstract class will step into the classloader after suspending the thread (if it is not filtered by filter) 5.3. Whether the class load breakpoint is on an abstract or non-abstract class, the thread / VM5.2 is suspended the first time the class loads or the first subclass is loaded.
two。 Debug program
1. Debug local Java language programs
In all debugging, debugging a Java program is the easiest, mainly setting breakpoints, starting debugging, single-step execution, the end of debugging a few steps.
1) set the breakpoint:
2) start debugging: Eclipse provides four ways to start debugging a program (Launch) through menus (Run-> Debug), icons ("green bugs"), right-> Debug As, and keyboard shortcuts (F11), which is similar to other commands (such as Run).
Pop-up prompt, you need to switch to the Debug workspace, check "Remember my decision", remember to select, then do not prompt next time, and then click [Yes].
3) step through: debug mainly using the views mentioned earlier, of which several buttons in the debug view have shortcut keys:
Step Retuen (F7)
Step Over (F6)
Step Into (F5)
End debugging: terminate debugging of the local program with the Terminate command.
II. Cases
Scene 1: Xiao Ming writes about a task executor, who executes some tasks uninterruptedly. After running on the Internet for a period of time, he finds that there is a probability of some failures. After running for a period of time, the task person exits abnormally. Because the exit is a null pointer, Xiao Ming wants to debug locally and does not know where the breakpoint is hit. The problem is a probabilistic event and may not necessarily occur. So after several times of debug, Xiao Ming was so dizzy that he could not even see the code clearly. Xiao Ming thought it would be nice if he could have a breakpoint and stop to let him find the problem whenever the null pointer was abnormal.
Abnormal breakpoint
Abnormal breakpoint: in the process of locating a problem, it is often encountered that the breakpoint cannot be hit in the right position, so it is very different from the trigger point of the problem, so you might as well try the abnormal breakpoint at this time. As the name implies, an abnormal breakpoint is a breakpoint that is automatically suspended after an exception is thrown.
Click on the red area to add an abnormal breakpoint
Enter the type of exception you want to locate, such as NullPointerException, so that after any NullPointerException exception is thrown in the system, the current thread is suspended, giving you a chance to locate the problem.
Scene 2: Xiaoming wrote a huge loop. When debugging the code, Xiaoming found that every time the loop reached 100000th time, there was a problem and did not meet his expectations, so Xiaoming made a breakpoint in the loop to see what was going on. But Xiaoming never imagined how difficult it would be to reach the 100000 loop. Xiaoming had begun to think about it at this time, if there could be such a breakpoint:
Number of If loops = = 100000, thread stops
Conditional breakpoint
As shown in the figure, loop 1000 times. If you want to stop at 500 times, you can create a conditional breakpoint, and right-click the breakpoint to hold the Breakpoint Properties.
Check Enable Condition
Add your own condition in the blank. If the condition returns true, the thread will be suspended. If it is false, the exception will be ignored.
Hit Count: after how many times the breakpoint has passed, the thread is officially suspended. If set to 500, the first 499 times will be expressed, and the breakpoint will not stop. When the 500th time, the breakpoint will suspend the current thread.
Expression.
Expressions can view some command lines that are not in the current code to easily locate the problem.
Scenario 3: Xiaoming has recently encountered a problem. There is always a problem when calling a third-party plug-in. Xiaoming suspects that it is the bug of a third-party plug-in, but Xiaoming cannot debug without finding the source code. What should Xiaoming do?
Debug locates problems with third-party plug-ins
1. Decompile code using decompilation tools
two。 Filter the decompiled source code
3. Fix source code compilation error
4. Conduct debug
Thank you for reading! This is the end of this article on "what are the Debug debugging skills in Eclipse?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.