Eclipse debugging skills (Summary), eclipsedebug

Source: Internet
Author: User

Eclipse debugging skills (Summary), eclipsedebug

As a developer, it is necessary to master debugging skills in the development environment. During java programming, we often encounter various inexplicable problems. In order to detect the problem of the program, we often need to add logs and check the value of variables, which makes debugging very troublesome. If I spend one hour debugging my application every day, it will be a lot of time. For this reason, use these time points to focus on and understand all the features that make debugging easier. It will save you some time and make your life easier and easier.

I. Debug View

The most common debugging window is:

Window Description
Debug window Displays the current thread method call stack and the number of lines of code (code with debugging information)
Breakpoint Breakpoints window => In the breakpoint List window, you can easily add breakpoints, Set breakpoint conditions, and delete breakpoints.
Variable Variables window => Display the local variables of the current method, non-static method, including this application, you can modify the variable value
Code editing window => I don't need to say much about this.
Output Console window => Log and other output content. During debugging, you can set the level of the component to be followed to a lower level to obtain the output information

In addition, the auxiliary Windows include:

Window Description
Expression window => Write the expression of the data you want to observe, or modify the variable value.
Display window => The code block and output content can be executed in the display.
Outline window => View methods and variables of the current class
Type level Type hierarchy window => View the inheritance level of the current class, including the Implementation interface and class inheritance level.
Method Call relationship Call hierarchy window => Check which methods are called by the current method and which classes and lines of the called method can directly open the corresponding method.
Search Result Search window => With shortcut keys, you can view variables, methods, and other code locations that are referenced or defined in a workspace, project, or work set.

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 displays the stack frame of the thread suspended in the program you are debugging, each thread in the program appears as a node of the tree. It shows the processes that are running for each target. If a thread is suspended, its stack frame is displayed as a child element. The following are some common debug buttons:


1. indicates that the current implementation continues running until the next breakpoint. The shortcut key is F8.

2. Interrupt the entire process

3. Enter the current method. The shortcut key is F5.

4. Run the next line of code. The shortcut key is F6.

5. Exit the current method and return to the call layer. The shortcut key is F7.

6. indicates the stack of the current thread. You can see which code is running, the entire call process, and the code line number.

Details:

Skip All Breakpoints: sets All Breakpoints to skipped. After Skip All Breakpoints is set, a diagonal line is generated on All Breakpoints, indicating that the breakpoint will be skipped, the thread will not be suspended at the breakpoint.

Drop to Frame: This command allows the program to return to the first line at the beginning of the current method and re-execute the java stack Frame. You can select a specified stack Frame, click Drop to Frame to re-enter the specified stack Frame. Note the following when using Drop to Frame:

1. You cannot drop the method in the method stack that has been executed.

2. When drop to stack frame, the original value of global data is not changed. For example, a vertor containing elements is not cleared.

Step Filters: this function is relatively simple, that is, when we want to ignore some classes that we do not pay attention to during debugging, we can enable Step Filters for filtering, the program continues until there is an unfiltered location or breakpoint. The Step Filters Function Consists of Use Step Filters, Edit Step Filters, Filter Type, and Filter Package. The procedure is as follows:

Step 1: Windows> Preferences> Java> Debug> Step Filtering.


Step 2: Select 'use Step Filters'

Step 3: select the required options on the screen. You can add some code in your own code library.

Step 4: Click 'application'

In principle, the Edit Step Filter command is used to configure the Step Filter rule, while the Filter Type and Filter Package respectively refer to the filtered Java Type and Java Package.

Step Return: jump out of the current method. during the execution of the called method, using Step Return will jump out of the method after all the code of the current method is executed and Return to the method that calls this method.

Step Over: During single-Step execution, when a sub-function is encountered in the function, the sub-function is not executed in a single Step in the sub-function, but the sub-function is stopped after execution, that is to say, take the sub-function as a step.

Step

Resume: Resume the paused thread and directly jump from the current position to the next breakpoint.

Suspend: Pause the selected thread. In this case, you can browse or modify the code and check the data.

Eclipse supports thread suspension and restoration through Suspend and Resume. Generally, Suspend is suitable for debugging multi-threaded programs. When you need to view the stack frames and variable values of a thread, you can run the Suspend command to temporarily Suspend the thread. Resume is used for recovery.

Note the following two types of Resume:

First, when you modify the program code during the debugging process, save the code, and click Resume, the program will be temporarily suspended at the breakpoint.

The second is when the program throws an exception and runs Resume, the program will be temporarily suspended at the breakpoint.

Terminate: Eclipse uses the Terminate command to Terminate local program debugging.

Disconnect: Eclipse uses the Disconnect command to terminate the socket connection to the remote JVM.

1. debug and execute

Mark Function Shortcut Key Description
6.4 Step Info F5 -> Step-by-step (if a method call is available, the method will be called for debugging );
6.4 Step Over F6 -> Skip one step (if you do not enter any method call of the line, you can directly execute the current code line and skip to the next line );
6.4 Step Return F7 -> Return in one step (after the current method is executed, the current method is displayed from the call stack, and the current method is called );
6.5 Resume F8 -> Resume normal execution (until the next breakpoint is encountered );
7.4 Run to Line Ctrl + R -> Execute to the current row (ignore all breakpoints in the middle and execute to the row where the current cursor is located );
6.3 Drop To Frame None -> Roll back to the starting point of the specified method for execution. This function is awesome.
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 in this method To re-Execute from the first line of this method.
Of course, operations that have side effects, such as database operations and modifying the object content of input parameters, may be re-executed as you want.
6.1 + 6.2 Copy Stack None -> Copy the current thread stack information

If some classes and packages need to be excluded during debugging and do not need to be debugged, you can use the Edit Step Filters settings.

A6 properties: Information about Java Process startup, including console startup parameters and environment parameters. If there is a problem with the parameter startup parameters, check whether the actual startup parameters here are correct. You can also view the options related to debugging supported by the virtual machine.


2. View data

Mark Function Shortcut Key Description
7.4 Inspect Ctrl + shift + I -> View the selected variables, expression values, or execution results. Press ctrl + shift + I again to add the current expression or value to the Expressions window;
7.4 Display Ctrl + shift + d -> Display the selected variable, expression value, or execution result. Press ctrl + shift + d again to add the current expression or value to the Display window;
7.4 Execute Ctrl + u -> Execute the selection expression;
7.4 Run to Line Ctrl + r -> Execute to the current row (ignore all breakpoints in the middle and execute to the row where the current cursor is located );
7.3 All Instances Ctrl + shift + n -> View all objects of the selected class. This function is superb;
7.3 Instance Count None -> View the number of all objects of the selected class;
7.4 Watch None -> Add the current variable and expression to the Expressions window;

3) Variables View)


1. For the variable name view, display all the instance variables and local variables that can be accessed in the current code line

2. display all variable values

3. You can change the variable value in this window.

Variables View displays the variable information related to the stack frame selected in the Debug View. When debugging a Java program, you can choose to display more detailed information in the details pane. In addition, Java objects can also display the values of their attributes. In this window, you can right-click a variable and perform the following operations:

All Instances: open a dialog box to display All Instances of the java class. To use this function, the java Virtual Machine must support instance retrieval.

All References: open a dialog box to display All java objects that reference the variable,

Change Value: Change the Value of a variable. This function can be used with Drop to Frame for program debugging. You can use these two functions instead of re-debug.

Copy Variables: This function is used to Copy the value of a variable, especially when the value of a variable is long (such as json data.

Find: Sometimes, when there are many variables in a class, you can search for them.

4) Breakpoints View (breakpoint View)


1. Display All Breakpoints

2. Disable the port selected in window 1 and click Enable again.

3. Exception breakpoint

Breakpoints View lists all the Breakpoints you set in the current work interval. You can double-click the breakpoint to enter the position of the breakpoint in the program. You can also enable or disable breakpoints, delete, add new ones, and group them based on the hit count of working groups or points. The following two tips are useful when using breakpoints:

Hit Count: Specifies how many times the code segment at the breakpoint runs. The most typical example is a loop. If a thread is suspended for 10 times of execution, the Hit Count value is set to 10, the current cycle will be suspended when it is executed for the ninth time.

Conditional: As the name implies, it is the condition judgment. For example, if we need to loop variable I = 10 and the thread is suspended, the condition is set to I = 10 and Suspend when "true" is selected ".

If both the above Hit Count and Conditional are selected, the expression and value setting will be invalid if they are unreasonable. If Suspend when value changes is selected, the Conditional Al may be suspended when the variable value changes.

5) Expressions View (expression View)


1. Expression

2. Click here to add an expression.

To evaluate the expression value in the Debug perspective Editor, select a whole line with breakpoints and select the Inspect option in 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 you want to calculate the value of the variable a + B, you can add an expression: a + B in the expression view.

6) Display View


You can use this view to input or calculate some new code. The code is executed in the context of the current debugging location, which means that you can use all variables or even content assistants. To execute your code, just mark it and use the right-click menu or CTRL + U (execute) or CTRL + SHIFT + I (check)

7) Code Viewing auxiliary window

1. Code view:

Code view, used to display specific code. The green part indicates the code to be executed.


Mark Function Shortcut Key Description
11.1 ~ 11.5 Quick type hierarchy Ctrl + t View the inheritance layers of the current class and interface. When it is entered by default, it displays the subclass that inherits/implements the current class/method, and the sub-interface is 11.1; ctrl + t again, the superclass/interface 11.2 inherited/Implemented by the current class and interface is displayed. This function is often used during debugging and 11.3 is used for method calls of the interface or abstract class, ctrl + t check the implementation class 11.4 and navigate directly to the corresponding implementation method 11.5.
  Quick outline Ctrl + o View the outline of the current class, including methods and attributes. It is of little use;
  Open declarations F3 View variables, attributes, and method Definitions

2. Call Hierarchy window:

Mark Function Shortcut Key Description
12.1 ~ 12.2 Open call hierarchy Ctrl + alt + h View the invocation level of the method. You can view the 12.1 of the currently called method, or the 12.2 of the methods of other classes called by the current method.


3. Type Hierarchy window:

Mark Function Shortcut Key Description
13.1 ~ 13.4 Open type hierarchy F4 View the inheritance level. You can view the inheritance level of the class, including subclass parent class 13.1, or the class implementation interface inheritance level 13.2. Based on the selected class/interface, the outline 13.3 and 13.4 of the class are displayed on the right. 13.3 you can choose whether to display the attributes and methods of the parent class/parent interface.

4. Search window:

Mark Function Shortcut Key Description
14.1 Declarations Ctrl + g The location of the same method signature defined in the workspace and third-party jar package is 14.1
14.2 References Ctrl + shif + g The currently selected variables, attributes, and methods are referenced in the workspace and third-party jar packages at 14.2.
14.3   Ctrl + shift + u View where variables, attributes, and methods appear in the current class 14.3
14.4 Implements   View the class 14.8 that implements the current interface.
14.4 ~ 14.7 Display Mode   You can select different display modes.


Ii. Debug

1. Set breakpoints

In the source code file, double-click the line marked in front of the code line to set the breakpoint, and double-click again at the same position to cancel the breakpoint. Sometimes we still need this, that is, I don't want to execute code in a row. for example, a for Loop will loop over 1000 times, I only want to suspend the thread for debugging 500th times. In this case, we can use the conditional breakpoint. Set the condition Breakpoint: we can set the trigger condition for this Breakpoint. Once a certain condition is met, Debugging starts. You can right-click the Breakpoint and select Breakpoint Properties to go To the Breakpoint settings page, when talking about the breakpoint view, we learned how to use Hit Count and Conditional. Here we can set conditions and execution times.

1.1) breakpoint type and breakpoint window

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

1. line breakpoints: a breakpoint with certain conditions. The Code stops running at the breakpoint only when the conditions set by the user are met.

2. method breakpoints, therefore, normal breakpoints cannot be hit, but the method breakpoint is acceptable. You can use this method to view the call stack of the method.

3. Observe the breakpoint (watch breakpoints-member variable access change)

4. exception breakpoint (exception breakpoints)

5. class load breakpoint)

The settings for each Breakpoint are somewhat different. You can right-click the Breakpoint and choose Breakpoint properties to set the settings. However, in the Breakpoint window, a quick setting interface is provided. The Breakpoint properties contains more filters, in fact, it is quite useful.

Breakpoint-related shortcut keys:

Shortcut Key Description
Ctrl + shift + B Place a large breakpoint at the cursor/cancel a breakpoint
Ctrl + alt + B Ignore all breakpoints
Alt + shift + q, B Activate the breakpoint window

1. Line breakpoint: Click 1.1 or 1.4 on a line in the method. For a row breakpoint, you can set a condition of 1.3 For the suspended thread/VM and 1.2 access times.

The condition in 1.3 is that when spring registers the Bean definition (registerBeanDefinition), if it is org. springframework. demo. MyBean, it suspends the thread and can start single-step debugging.

For the use of the number of hits (hit count) 1.2, it is generally in the loop, there is a problem with the processing of the nth object, set hit count = N, when re-debugging, it is convenient to stop debugging when the number of cycles to be debugged is reached.

2. Method breakpoint: Set the breakpoint 2.1 and 2.2 on the method. The advantage of a method breakpoint is that the method can be accessed or exited from the method at 2.3, stop for debugging, similar to a line breakpoint, and only the row breakpoint and method breakpoint have the setting function of conditional and number of accesses.

However, a method breakpoint has another benefit. If the code is compiled without debugging information specified, the line breakpoint does not work and can only be used as a method breakpoint.

If you are interested, you can use A1 to Add line number... Remove the check box before debugging.


3. Observe the breakpoint: the breakpoints 3.1 and 3.3 are played on the member variables. Only the object member variables have an effect, but static member variables do not.
You can set the thread/VM 3.2 to be suspended when the variable is accessed or set, that is, all access or setting methods for member variables similar to 3.4 will be monitored.

4. Exception breakpoint: You can add an exception breakpoint through 4.6 or click the exception class information output in the log information to add it.
Exception breakpoint 4.1: when a system exception occurs, the thread/VM is suspended at the position where the caught exception is thrown or when the program fails to capture the exception 4.2 or 4.4, you can also specify whether the child classes that contain exceptions are also detected at 4.3 and 4.5.

In addition to the exception suspension of the above normal settings, you can set the suspension from java-> debug, mainly including the following two:

1. Whether to suspend (suspend execution on uncaught exceptions) in the event of global uncaptured events. During debugging, there is always an exception suspension that affects debugging, but no exception breakpoint is set, you can check this option;

2. Whether to suspend during compilation errors. This usually happens when code is modified while debugging;

In addition, you need to mention an application started using the main method. You can check stop in main A3 in the debugging configuration. When the program enters, the thread will be suspended and waiting for debugging.




5. class loading breakpoint: the breakpoint 5.1 is called on the class name. The interface cannot load the breakpoint, but the abstract class is acceptable, but the breakpoint does not obviously enter the classloader during debugging, the single-step Inspector enters the constructor of the subclass. After a non-abstract class suspends a thread, the single-step Inspector enters the classloader (if the filter is not filtered out) 5.3. The thread/VM5.2 is suspended when the class is loaded for the first time or when the first subclass is loaded.


2. debug the program

1. debug local Java programs

Among all the debugging tasks, debugging a Java program is the easiest, including setting breakpoints, starting debugging, performing one step, and finishing debugging.

1) set the breakpoint:


2) start debugging: Eclipse provides four methods to start the program (Launch) debugging, namely through the menu (Run-> Debug), the icon ("green bug "), right-click-> Debug As and the shortcut key (F11). At this point, it is similar to other commands (such As Run.


The prompt is displayed. You need to switch to the Debug workspace, check "Remember my deactivating", Remember to select it, and click "Yes" next time ].


3) one-step execution: debug using the previous several views. The shortcut keys in the debug view are as follows:

Step Retuen (F7)

Step Over (F6)

Step Into (F5)


End debugging: Use the Terminate command to Terminate local program debugging.

Ii. Case studies

Scenario 1: James wrote a task executor, who executes some tasks without interruption. After running on the Internet for a period of time, he found that some faults may occur, after running the task for a period of time, the operator unexpectedly exits. Because it is a null pointer, James wants to debug it locally and does not know where the breakpoint is hit. This problem is a probability event, it may not appear, so after James debug several times, he is dizzy and can't even see the code clearly, I think how nice it would be to have a breakpoint that stops every time a null pointer exception occurs and let him discover the problem.

Exception breakpoint

Exception breakpoint, an exception breakpoint is a breakpoint that is automatically suspended after an exception is thrown.

Click the red part to add an exception breakpoint.


Enter the exception type to be located, such as NullPointerException. After any NullPointerException is thrown in the system, the current thread will be suspended and you will be given the opportunity to locate the problem.

Scenario 2: James wrote a huge loop. During the code debugging, James found that every time the cycle reaches 100,000th times, there was a problem and he did not meet his expectations, so James made a breakpoint in the loop and wanted to see what was going on, but James never imagined how difficult it was to reach 100000 cycles. At this time, James began to think about connecting them, if there is such a breakpoint:

If loop COUNT = 100000, the thread stops

Conditional breakpoint

As shown in the figure on the right, if you want to stop the loop for 1000 times, you can create a condition Breakpoint and right-click the Breakpoint to hold the Breakpoint Properties.


Select Enable Condition

Add your own condition in the blank space. If the condition returns true, the thread will be suspended. If it is false, this exception will be ignored.

Hit Count indicates the number of times that the breakpoint has been passed and the thread is formally suspended. If it is set to 500, the first 499 times will be expressed and will not be stopped after the breakpoint. When it is 500th times, the breakpoint suspends the current thread.



Expression


Expressions can be used to view some command lines that are not available in the current Code to locate problems.

Scenario 3: James has encountered a problem recently. When calling a third-party plug-in, there is always a problem. James suspected that it was a third-party plug-in bug, but James could not debug the source code, what Should James do?

Debug locates third-party plug-ins

1. Use the decompilation tool to decompile the code

2. filter the decompiled source code.

3. Fixed Source code compilation errors.

4. debug

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.