Written in the front: As two new middleware were added to the current project recently, I encountered a lot of problems in the process of access, most of which were solved by my colleagues and middleware developers. I learned a lot of debug skills in the process of solving them. At the same time, combined with some introduction on the web, summed up the relevant knowledge about Eclipse Debug.

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

First, Eclipse debugging introduction

Eclipse Debug related views

2.1 the Debug View

2.2 the Variables View

2.3 Breakpoints View

2.4 Expressions View

2.5 the Display View

Third, the Debug

3.1 Setting a Breakpoint

3.2 Debugging program

3.2.1 Debugging The Local Java Program

3.3.2 Remote Debugging


First, Eclipse debugging introduction

As anyone who has used Eclipse knows, it comes with a Java debugger that provides many basic debugging functions. The Eclipse Platform workbench and its tools are built around the JDT component, which provides Eclipse with the following features:

Project management tools perspective and view constructors, editors, search and build function debuggers


Eclipse Debug related views

Figure 1. A general view of the Eclipse Debug perspective (which can be accessed by clicking the Debug button in the upper right corner of the Eclipse interface)


Debug View:

The Debug view allows you to manage programs that are being debugged and running from the workbench. It displays stack frames of suspended threads in the program you are debugging, with each thread in the program appearing as a node in the tree. He shows the running process for each target. If a thread is suspended, its stack frame is represented as a child element. Here are some common debug buttons:

Skip All Breakpoints:: Sets All Breakpoints to be skipped. After Skip All Breakpoints are set, there will be a slash on All Breakpoints to indicate that Breakpoints will be skipped and threads will not be suspended at that point.

Drop to Frame: This command causes the program to restart execution by returning to the first line at the beginning of the current method. You can re-execute the Java stack Frame by selecting a specified stack Frame and clicking Drop to Frame to re-enter the specified stack Frame. Note when using Drop to Frame:

1. Cannot drop to a method in an already executed method stack.

2. Dropping into a stack frame does not change the value of the global data. For example, a vertor containing an element is not emptied.

Step Filters: When we debug and want to ignore classes that we don’t care about, we can enable Step Filters to filter and the program will continue to execute until an unfiltered position or breakpoint is reached. The Step Filters function consists of Use Step Filters, Edit Step Filters, Filter Type, and Filter Package. Specific operations are as follows:

Step 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 some code from your own codebase.

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 Java Type and Java Package to be filtered, respectively.

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

Step Over: in Step execution, when encountering a sub-function in a function, it will not Step into the sub-function, but stop the whole execution of the sub-function, that is, take the whole sub-function as a Step.

Step Into: to Step Into a subfunction as it encounters it and continue to Step

Resume: Resumes a suspended thread, jumping directly from the current position to the next breakpoint.

Suspend: Suspends a selected thread to browse or modify code, examine data, etc.

Eclipse supports Suspend and Resume of threads through Suspend and Resume. Suspend is generally used for debugging multithreaded programs. When you need to view the stack frames and variable values of a thread, Suspend the thread by using the Suspend command. Resume is used to Resume. There are two kinds of resumes to be aware of. The first is when you modify the program code during debugging, and then save. Clicking Resume will suspend the program from the breakpoint. The second is that when the program throws an exception, Resume is run and the program also suspends at the breakpoint.

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

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


The Variables View:

The Variables View displays variable information related to the selected stack frame in the Debug View, which can optionally display more detailed information in the details pane when debugging a Java program. In addition, Java objects can display the values of the properties they contain. Right-click a variable in the window to perform many 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: : Open a dialog to display All Java objects that reference this variable,

Change Value: : Changes the Value of a variable. This function can be used in conjunction with Drop to Frame for program debugging. Use these two functions instead of redebugging

Copy Variables: Copy the value of a variable, especially if the value of the variable is very long (such as JSON data).

Find: Sometimes you can Find a class that has too many variables.


Breakpoints View:

The Breakpoints View will list all Breakpoints you have set in the current working interval. Double-click the breakpoint to access the breakpoint location in the program. You can also enable or disable breakpoints, delete, add new ones, and group them based on workgroups or point-hit counts. There are two techniques that are useful when using breakpoints:

Hit Count: Specifies how many times the breakpoint is run. The most typical example is loops. If you want a loop to be executed 10 times before the thread suspends, specify a Hit Count of 10.

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

If both Hit Count and Conditional are selected, the expression and value are not set properly. If Suspend when value changes is selected, Conditional may Suspend when the value of the variable changes.


Expressions View:

To evaluate an expression in the editor of the Debug perspective, select the entire line on which breakpoints are 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 evaluate the variable A +b, I can add an expression to the expression view: A +b


Display the View:

You can use this view to input or perform some new code. This code is executed in the context of the current debug location, which means you can use all variables and even content helpers. To execute your code, simply mark it and use the right menu or CTRL+U(execute) or CTRL+SHIFT+I (check).


Third, the Debug

To set breakpoints

In the source code file, double click on the marked line preceding the line of code where you want to set the breakpoint to set it, and double click again at the same location to cancel it. Sometimes I don’t want to go through code line by line, like a for loop that will loop 1000 times, but I just want to suspend the thread on the 500th time to debug, and that’s when we can use conditional breakpoints.

Set conditional breakpoints: You can set a trigger condition for that Breakpoint, and once that condition is met, you can right click on the Breakpoint and choose Breakpoint Properties to go to the Breakpoint setting page, and we learned about Hit Count and Conditional in the Breakpoint view. Here you can set conditions and times of execution.


The debugger

1. Debug local Java language programs

Of all the debugging, debugging a Java program is the easiest, mainly set breakpoints, start debugging, single step, end debugging steps.

Set breakpoints: This has already been said.

Launch debugging: Eclipse provides four ways to Launch a program from the menu (Run ->Debug), the icon (” Green bug “), the right click ->Debug As, and the shortcut key (F11), similar to other commands (such As Run).

Single step: Mainly use the previous views for debugging, where several buttons in the Debug view have shortcut keys:

Step Retuen(F7)

Step Over (F6)

Step Into (F5)

End debugging: Terminates debugging of local programs by using Terminate.


2. Remote debugging

Remote debugging is primarily used to debug Java programs that are not local, and not just on someone else’s machine. Programs running on a Web server on the local machine also need to use remote debugging when debugging. The general steps for remote debugging are basically the same as for debugging native Java language programs, with some differences in Settings.

The Eclipse debugger enables debugging of remote applications. It can connect to a remote VM running a Java application and connect itself to that application. Using a remote debug session is much the same as using a local debug session. However, remote Debug 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, and then click New. This creates a new remote launch configuration that displays three tabs: Connect, Source, and Common.

In the Project field of the Connect TAB, select the Project to reference when you start searching for source code. In the Host field of the Connect TAB, enter the IP address or domain name of the remote Host where the Java program is running. In the Port field of the Connect TAB, enter the Port on which the remote VM receives connections. Typically, this port is specified when the remote VM is started. If you want the debugger to determine whether Terminate is available ina remote session, select the Allow Termination of Remote VM option. Select this option if you want to terminate connected VMS. Now, when you select the Debug option, the debugger attempts to connect to the remote VM at the specified address or port, and the results are displayed in the Debug view.


References:

The Eclipse Help: help.eclipse.org/luna/index….

Debugging using the Eclipse platform: www.ibm.com/developerwo…

Remotely debug Java applications using Eclipse: www.ibm.com/developerwo…

Using the Eclipse debug a Java program ten techniques: developer.51cto.com/art/201304/…

Eclipse debugger: Zero contact Combat skills: hb.qq.com/a/20111125/…