Eclipse's Debug Introduction and Tips "Reprint"

Source: Internet
Author: User
Tags terminates

No programmer can one go write code without any bugs, so many programmers have a considerable amount of time spent on debug, and program debugging is the work that every programmer has to face. How to use Eclipse for effective, especially efficient debugging of code is a skill worth learning.

First, the Eclipse debugging introduction

Second, Eclipse and debug-related views

    • Debug View

    • Variables View

    • Breakpoints View

    • Expressions View

    • Display View

Third, Debug

    • Set breakpoints

    • Debug program

    • Debugging local Java language programs

    • Remote debugging

First, the Eclipse debugging introduction

Anyone who has used eclipse knows that eclipse comes with a Java debugger and can provide a lot of basic debugging capabilities. The Eclipse Platform Workbench and its tools are built around the JDT component, which provides the following features for Eclipse:

Project management tools perspective and View builder, editor, search, and build features debugger

Second, Eclipse and debug-related views

Figure 1. 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)

Debug View:

The debug view allows you to manage the programs you are debugging and running on the workbench, and he displays the stack frames for the suspended threads 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 the thread is suspended, its stack frame is displayed as a child element. Here are some common debug buttons:

Skip all Breakpoints: Set all breakpoints to be skipped, after setting the 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 return to the beginning of the first line of the current method to start execution again, you can re-execute the Java stack frame, you can select a specified stack frame, and then click on the Drop to frame, so that you can re-enter the specified stack frame. When using drop to frame

Attention:

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

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

Stepbystep_co

Step Filters: This function is relatively simple, when we want to ignore some of the classes we do not care about when debugging, you can turn on step Filters to filter, the program will continue until it encounters an unfiltered position or breakpoint. The step Filters function is comprised of the use step filters,edit step Filters,filter type,filter package four. Here's how:

Step Filtering, Debug, Java, 1:windows, Preferences.

Step 2: Select ' Use Step Filters '.

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

Step 4: Click ' Apply '.

In principle, the Edit Step Filter command is used to configure the step filter rule, while the filter type and the filter package refer to the filtered Java type and Java package respectively.

Step Return:

Jump out of the current method, and in the execution of the called method, use step return to jump out of the method after executing all the code of the current method and return to the method that called the method.

Step over:

In stepping, when you encounter a child function within a function, you do not step into the child function, but instead complete the child function in the stop, that is, the entire function as a single step.

Step into, enter and continue stepping with child function

Resume:

Resumes a paused thread and jumps directly from the current position to the next breakpoint position.

Suspend: Pauses the selected thread, this time can be browsed or modified code, check data and so on.

Eclipse supports thread suspend and resume through suspend and resume. In general, suspend is suitable for debugging multi-threaded applications, and when it is necessary to view 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 note: The first is when you modify the program code during debugging, then save, click Resume, and the program hangs at the breakpoint. The second is that when the program throws an exception, the resume is run and the program is suspended at the breakpoint.

Terminate:

Eclipse terminates debugging of the local program with the Terminate command.

Disconne

Ct:

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

Variables View:

Variables View displays variable information related to the stack frame selected in Debug view, and when you debug a Java program, the variable can optionally display more detailed information in the details pane. In addition, the Java object can display the value of the property it contains. There are many things you can do with the right mouse click of a variable in the window, and the main actions are the following:

All Instances: Opens a dialog box to display all instances of the Java class, which requires a Java virtual machine to support the retrieval of the instance.

All References: Opens a dialog box to display all Java objects that reference the variable.

Change value: Changes the values of variables that can be used in conjunction with drop to frame for program debugging. Use these two functions to replace the re-debug

Copy Variables: The value of the copied variable, especially if the variable value 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 them.

Breakpoints View:

Breakpoints View will list all breakpoints you have set in your current workspace, and double-click the breakpoint to enter the location of the breakpoint in your program. You can also enable or disable breakpoints, delete, add new, and group them according to the workgroup or point hit count. The following two tips are useful for using breakpoints:

Hit Count:

is how many times the code snippet at the specified breakpoint is run, the most typical is a loop, and if you want a loop to execute 10 times on the thread to suspend, specify hit count value of 10, then the current loop executes to the nineth time when it hangs.

Conditional: As the name implies, is conditional judgment, such as when we need to loop variable i==10, the thread hangs, the condition is set to i==10, select suspend when "true".

If the above hit count and conditional are selected, the expression and value settings are not valid. If you select suspend when value changes, it may be suspended conditional when the value of the variable is changed.

Expressions View:

To find the value of an expression in the editor of the Debug perspective, select the whole line with the breakpoint 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 a variable a+b, you can add an expression to the expression view: a+b

Display 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).

Third, Debug

Set breakpoints

In the source code file, at the front 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 in the same location to cancel the breakpoint. Sometimes we have this need, that is, I do not want a line of execution code, such as a For loop will loop more than 1000 times, I just want to let the thread hang for debugging in the No. 500 time, this time we can use conditional breakpoint. Set conditional breakpoints: We can set a trigger condition for the breakpoint, once the condition is met to start debugging, you can click on the breakpoint at the right mouse button, select Breakpoint Properties to enter the breakpoint Settings page, just when the breakpoint view we learned hit For the usage of count and conditional, here you can set the conditions and number of executions.

Debug program

1. Debugging the local Java language program

In all debugging, debugging a Java program is the simplest, with the main set breakpoints, start debugging, stepping, end debugging a few steps.

Set breakpoints: Already mentioned earlier.

Start Debugging: Eclipse provides four ways to start the program (Launch) debugging, respectively, through the menu (run–> Debug), Icons ("Green bug"), right->debug as and shortcut keys (F11), at this point, with other commands ( such as run) are similar.

Single-step: mainly debug with several views previously spoken, where several buttons in the Debug view have shortcut keys:

Step Retuen (F7)

Step Over (F6)

Step into (F5)

End debugging: Terminates debugging of the local program with the Terminate command.

2. Remote Debugging

Remote debugging is primarily used to debug non-native Java programs, where the non-local is not only called non-Local on the other person's machine. Programs that run on a Web server on this computer also need remote debugging when debugging. The approximate steps for remote debugging and debugging of native Java language programs have been basically all along, just a little different on 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, the remote debugging configuration requires that you configure some different settings in the Run > Debug window. You need to select the Remote Java application option in the left view and click New. This creates a new remote boot configuration that shows three tabs: Connect, Source, and Common.

On the Connect tab, in the Project field, select the project that you want to reference when you start the search source code. On the Connect tab, in the Host field, enter the IP address or domain name of the remote host running the Java program. On the Connect tab, in the Port field, enter the ports where the remote VM receives the connection. Typically, this port is specified when a remote VM is started. If you want the debugger to decide whether the Terminate command is available in a remote session, you can choose the Allow termination of remote VM option. Select this option if you want to be able to terminate a connected VM. Now, when you select the Debug option, the debugger tries to connect to the remote VM at the specified address or port, and the results are displayed in the Debug view.

Transferred from: http://mp.weixin.qq.com/s?plg_nld=1&plg_uin=1&mid=403424743&idx=3&plg_nld=1&scene=22 &plg_auth=1&__biz=mjm5ota1mduyma%3d%3d&plg_dev=1&srcid=1220gqzb8cmxtynqgxwenqup&plg_usr=1 &plg_vkey=1&sn=24dd809796110414bda26406ba96dbd6#rd

Eclipse's Debug Introduction and Tips "Reprint"

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.