Several tips for Eclipse debug debugging

Source: Internet
Author: User

On the Internet to see an article on the Eclipse Debug debugging, organized more comprehensive, is reproduced as follows:

Three points first.

    • Do not use SYSTEM.OUT.PRINTLN as a debugging tool
    • Enable verbose logging levels for all components
    • Use a Log Parser to read the log

1. Conditional breakpoint

Imagine how we usually add breakpoints, and it's common practice to double-click the left side of the line number. In the debug view, breakpoint View lists all breakpoints, but we can add a Boolean condition to determine whether the breakpoint is skipped. If the condition is true, the program stops at the breakpoint, otherwise the breakpoint is skipped and the program resumes execution.

2. Abnormal Breakpoint

There is one in the breakpoint view that looks like J! button, we can use it to add a breakpoint based on an exception, for example we want the program to pause when NullPointerException throws, we can do this:

3, Observation point

This feature I like very much, he allows the program execution to pause and debug when a selected property is accessed or changed. The simplest way is to double-click the line number of the statement that declares the member variable in the class, and you can add an observer point.

4. View variables

You can see the value of the variable using CTRL+SHIFT+D or Ctrl+shift+i on the selected variable, and we can also add monitoring in expressions view.

5. Change the value of the variable

We can change the value of the variable at debug time. You can do this as shown in variables view.

6. Stop in the Main methodIn the Run/debug setting, we can enable this feature as shown. The program will stop at the first line of the Main method 7. Environment VariablesWe can easily add environment variables in the Edit Conriguration dialog box 8. Drop to FrameThis feature is very cool and is my second favorite feature, Drop to frame means that it can be re-executed at the beginning of the current method, and the values of all the context variables are back to that time. Not necessarily the current method, you can click on any frame in the current call stack to jump there (except for the first frame). The main use is that all variable states are quickly restored to the beginning of the method and re-executed again, so that you can debug it over and over again in the context of your concern (combined with other functions such as changing the value of the variable), instead of having to debug it again. Of course, the side effects of the original execution process are irreversible (for example, you inserted a record into the database). 9. Step FiltrationWhen we debug the F5 will go into the inside of the method, but there is a drawback sometimes may go into some libraries inside (such as the JDK), may not be what we want, we can add a filter in the preferences, exclude the specified package.

10. Enter, Skip, return

In fact, this technique is the most basic knowledge of Debug.
    • F5-step into: Move to the next step, if the current line is a method call, will enter the first line of this method. (Can be excluded by the Nineth article)
    • F6-step over: Moves to the next line. If the current row has a method call, the method is executed and returned to the next line.
    • F7-step Return: Resumes execution of the current method, and when the current method finishes executing, the control will go to the row that the current method is called.
    • F8-moves to the next breakpoint.
Eclipse Debugging Common Tips 1, conditional breakpoints

Breakpoints everyone is familiar with, in the Eclipse Java editing area of the wardrobe double-click to get a breakpoint, the code will run here when the stop.

A conditional breakpoint, as the name implies, is a breakpoint with a certain condition, and the code will stop when it runs to the breakpoint only if the user set conditions are met.

Right-click at the breakpoint and select the last "Breakpoint Properties"

The property interface of the breakpoint and the meaning of each option,

2. Variable Breakpoint

A breakpoint can not only be hit on a statement, but a variable can also accept a breakpoint.

is a variable of the breakpoint, in the value of the variable initialization, or change the value of the variable can be stopped, of course, the variable breakpoint can also be conditional, and the above described conditional breakpoint settings are the same.

3. Method Breakpoint

The method breakpoint is to hit the breakpoint at the entrance of the method,

The special point of the method breakpoint is that it can be hit in the JDK source code, because the JDK at compile time to remove debugging information, so the normal breakpoint is not hit inside, but the method breakpoint can be, this method can be used to view the call stack.

4. Change the value of the variable

The code stops at the breakpoint, but the value passed is not correct, how to modify the value of the variable to ensure that the code to continue the correct process, or that there is an abnormal branch is always going to go, can you debug the condition, to see if the abnormal branch code is correct?

In the Variables small window of the debug view, we can see that the value of the Mdestjarname variable is "F:\Study\eclipsepro\JarDir\jarHelp.jar"

We can right-click on the variable and select "Change Value ..." Modify the value of the variable in the popup dialog box,

Or in the following values to view the changes in the window, save the Ctr+s saved, the value of the variable will be changed to a new value.

5. Re-commissioning

The fallback for this debug is not omnipotent and can only be rolled back in the current thread's stack frame, and can be returned at most to the beginning of the invocation of the current thread.

On fallback, right-click on the thread method that requires fallback and select Drop to Frame

6. Remote Debugging

For debugging programs that are not on this computer, there are two ways to

1, the machine as a client

2, the machine as a service side

The premise of using remote debugging is that the server side and client code are consistent.

This machine acts as a client

This machine is commonly used by the client, the Java program on the remote server needs to open the remote debug switch at startup,

Server-side need to add virtual machine parameters

1.5 Previous version (1.5 also available later): "-xdebug-xrunjdwp:transport=dt_socket,server=y,address=8000"

Version 1.5 and above: "-agentlib:jdwp=transport=dt_socket,server=y,address=8000"

F:\study\eclipsepro\screensnap>java-xdebug-xrunjdwp:transport=dt_socket,server=y,address=8000-jar Screensnap3.jar

When connecting to a remote server, you need to create a new remote debugger in eclipse

There is a small place to note that the connection on the time does not seem to automatically switch to the debug view, do not think that the local debug program is not connected to the server side.

This machine serves as the service side

You only need to modify the "Connection Type" as a client compared to the native machine.

At this point eclipse will enter the state of waiting for the connection

The connection program uses the following parameters to connect to the local server, IP address should be replaced by implementing IP ~ ~

"-agentlib:jdwp=transport=dt_socket,suspend=y,address=127.0.0.1:8000"

F:\study\eclipsepro\screensnap>java-agentlib:jdwp=transport=dt_socket,suspend=y,address=127.0.0.1:8000-jar Screensnap3.jar

While remote debugging, local code modifications can be synchronized to remote, but will not be written to remote files, that is, local modifications will be the next time you start the remote program, will not affect the next use of remote code.

For more detailed information on remote debugging, refer to "debugging Java applications remotely using Eclipse".

Seems to have missed a breakpoint, an abnormal breakpoint, to fill.

7. Abnormal Breakpoint

Often meet some anomalies, and then the program will exit, to find the place where the exception occurred is more difficult, fortunately, can hit an abnormal breakpoint,

We have added a Nullpointexception exception breakpoint, and when the exception occurs, the code stops at the occurrence of the exception and should be helpful when locating the problem.

Several tips for Eclipse debug debugging

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.