Eclipse Debug introduction and skills

Eclipse Debug introduction and skills

Click "Hollis" above to follow me, and the exciting content will be presented immediately.
Number of characters: 3000
Reading time: 6 minutes

I posted an article about the necessary plug-ins for intellij idea, and many small partners reported that the main IDE is still using Eclipse. I wrote an article about debugging techniques in Eclipse before in my blog. Push to friends who are still using Eclipse.

No programmer can write code without any BUG in one go, so many programmers spend a considerable part of their time on Debug. Program debugging is a job that every programmer must face. How to use Eclipse for effective, Especially efficient code debugging is a skill worth learning.

Anyone who has used Eclipse knows that Eclipse comes with its own Java debugger, which can provide many basic debugging functions. The Eclipse platform workbench and its tools are built around JDT components, which provide Eclipse with the following features:

Project management tools
perspectives and views
builder, editor, search and build functional
debugger

2. Debug related views in Eclipse

Figure 1. The general view of the Eclipse Debug perspective (this view can be accessed by clicking the Debug button in the upper right corner of the Eclipse interface)
Eclipse Debug introduction and skills

Debug View:
Eclipse Debug introduction and skills

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

Skip All Breakpoints:: Set all breakpoints to be skipped. After Skip All Breakpoints is set, there will be a slash on all breakpoints, indicating that the breakpoint will be skipped and the thread will not be skipped at the breakpoint Hang.

Drop to Frame: This command allows the program to return to the first line of the current method and restart execution. This java stack frame can be re-executed, and a specified stack frame can be selected, 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 to the method in the method stack that has been executed.

2. When dropped into the stack frame, the original value of the global data will not be changed. For example, a vertor containing an element will not be cleared.

Step Filters: This function is relatively simple, that is, when we want to ignore some classes we don't care about when debugging, we can turn on Step Filters to filter, and the program will continue to execute until it encounters an unfiltered location or breakpoint. Step Filters function is used by Use Step Filters, Edit Step Filters

, Filter Type, Filter Package consists of four items. The specific operations are as follows:

步骤 1: Windows -> Preferences -> Java -> Debug -> Step Filtering.

Step 2: Select'Use Step Filters'.

Step 3: Select the desired option on the screen. You can add part of the code from your own code base.

Step 4: Click'Apply'

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

Step Return: Jump out of the current method. During the execution of the called method, using Step Return will jump out of the method and return to the method that called the method after executing all the codes of the current method.

Step Over: During single-step execution, when a sub-function is encountered in the function, it will not enter the sub-function for single-step execution. Instead, the sub-function will be executed at a stop, that is, the sub-function will be taken as a step.

Step Into: Single-step execution, enter and continue single-step execution when encountering a sub-function

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

Suspend: Suspend the selected thread, at this time you can browse or modify the code, check data, etc.

Eclipse supports the suspension and resume of threads through Suspend and Resume. Generally speaking, Suspend is suitable for the debugging of multithreaded programs. When we need to view the stack frame and variable value of a certain thread, we can suspend the thread through the Suspend command. Resume is used for recovery. There are two kinds of Resume to pay attention to: The first is when you modify the program code during debugging, and then save it, click Resume, and the program will be suspended at the breakpoint. The second is when the program throws an exception, run Resume, the program will also be suspended at the breakpoint.

Terminate: Eclipse terminates the debugging of the local program through the Terminate command.

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

Variables View :
Eclipse Debug introduction and skills

Variables View displays variable information related to the selected stack frame in Debug View. When debugging a Java program, variables can choose to display more detailed information in the detailed information pane. In addition, the Java object can also display the value of the property it contains. Many operations can be performed by selecting the variable in this window and clicking the right mouse button. The main operations are as follows:

All Instances: Open a dialog box to display all instances of the java class. To use this function, the java virtual machine needs to support instance retrieval.

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

Change Value: Change the value of the variable. This function can be used in conjunction with Drop to Frame to debug the program. Use these two functions to replace re-debug

Copy Variables: Copy the value of a variable, especially when the variable value is very long (such as json data), this function comes in handy.

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

Breakpoints View :
Eclipse Debug introduction and skills

Breakpoints View will list all the breakpoints you have set in the current work interval. 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 according to workgroup or point hit count. The following two techniques are very useful in using breakpoints:

Hit Count: It is how many times the code segment at the specified breakpoint runs. The most typical is loop. If you want a loop to execute 10 times and the thread hangs, specify the Hit Count value to 10, then the current loop executes to the ninth It will hang up the next time.

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

If both Hit Count and Conditional are selected above, it will be effective if the expression and value settings are unreasonable. If you select Suspend when value changes, then Conditional may hang when the variable value changes.

Expressions View :
Eclipse Debug introduction and skills

To evaluate the value of the expression in the editor of the Debug perspective, select the entire line where the breakpoint is set, 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 I want to calculate the value of variable a+b, then I can add an expression in the expression view: a+b

Display View :
Eclipse Debug introduction and skills

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

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

Debugging program
1, debugging local Java language program

Among all debugging, debugging a Java program is the easiest, mainly including setting breakpoints, starting debugging, single-stepping, and ending debugging.

Set breakpoints: I have already said that.

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

Single step execution: mainly use the several views mentioned above for debugging, among which several buttons in the debug view have shortcut keys:

Step Retuen(F7)

Step Over (F6)

Step Into (F5)

End debugging: Terminate the debugging of the local program through the Terminate command.

2. Remote debugging

Remote debugging is mainly used to debug non-local Java programs. Non-local here is not called non-local only on other people's machines. Programs running on the Web server on this machine also need to use remote debugging when debugging. The general steps of remote debugging are basically the same as those of debugging local Java language programs, but there are some differences in the settings.

The Eclipse debugger can debug remote applications. It can connect to a remote VM running a Java application and connect itself to the application. Using a remote debugging session is roughly the same as using a local debugging session. However, remote debugging configuration requires a few different settings to be configured in the Run> Debug window. You need to select the Remote Java Application option in the left view first, and then click New. This creates a new remote startup configuration that will display three tabs: Connect, Source, and Common.
Eclipse Debug introduction and skills

In the Project field of the Connect tab, select the project to be referenced when starting the search for source code. In the Host field of the Connect tab, enter the IP address or domain name of the remote host running the Java program. In the Port field of the Connect tab, enter the port for the remote VM to receive connections. Usually, this port is specified when the remote VM is started. If you want the debugger to determine whether the Terminate command is available in the remote session, you can select the Allow termination of remote VM option. If you want to be able to terminate the connected VM, select this option. Now, when the Debug option is selected, the debugger will try to connect to the remote VM at the specified address or port, and the result will be displayed in the Debug view.

It is recommended to read
the IntelliJ IDEA plug-in for Java development.
What the hell is DDoS*** suffered by GitHub?
Java 10 will be released this month. It will change the way you write code.
Spring Boot 2.0 is released. New features at a glance

If you see this, you like this article.

Then please press and hold the QR code to follow Hollis

Eclipse Debug introduction and skills

Guess you like

Origin blog.51cto.com/13626762/2545127