Debug debugging Tips for Eclipse

Source: Internet
Author: User
Tags ming

1. Example

Scene One: Xiao Ming wrote a task performer, the performer of the execution of a number of tasks, in the network after a period of time, found that there is a probability of some failures, found that after a period of time, the task of abnormal exit, exit because it is a null pointer, can xiaoming want to debug in the local, Do not know where to hit the breakpoint, the problem is the probability of events, not necessarily appear, so xiaoming debug several times down, dizzy, even code are not clear, Xiao Ming want to have a breakpoint whenever there is a null pointer when the time to stop to let him find the problem, that much good ah.

Exception Breakpoint


Exception breakpoint: In the process of locating the problem, often encounter breakpoint can not hit the right place, so that and the issue of the trigger point is very different, so this time may wish to try an exception breakpoint, as the name implies, the exception breakpoint refers to throw an exception after the automatic suspend breakpoint.

Click on the red area to add an exception breakpoint

Enter the type of exception you want to locate, such as nullpointerexception, so that when any NullPointerException exception is thrown in the system, the current thread will be suspended, giving you the opportunity to locate the problem.

Scene Two: Xiao Ming wrote a huge cycle, in the debugging code, Xiao Ming found that whenever the cycle to the 100,000th time, is the problem, did not reach their expectations, so Xiao Ming in the loop hit a breakpoint, want to see what is going on, but Xiao Ming never thought, How difficult it is to get to 100,000 cycles, Xiaoming has begun to think at this time if he can have such a breakpoint:

If loop Number = = 100000, thread stops

Conditional breakpoint


As shown on the right, loop 1000 times, if you want to stop in the loop to 500 times, you can create a conditional breakpoint, right click on the breakpoint suspended breakpoint Properties.

Check Enable Condition

In the space, add your own condition, and if the condition returns True, the thread is suspended and if False, the exception is ignored

Hit count is the number of times that the breakpoint has passed, formally suspending the thread, and if set to 500, then the first 499 times, after which the breakpoint will not stop, and when the No. 500 time, the breakpoint suspends the current thread.

An expression


The expression can look at some command lines that are not in the current code, which makes it easy to locate the problem.

Scene Three: Xiao Ming recently encountered a problem, in calling a third-party plug-in will always have problems, xiaoming suspect is a third-party plug-in bug, but Xiaoming did not find the source code can not be debug, Xiao Ming how to do?


Debug location of third-party plug-in issues


1. Using the Anti-compilation tool to decompile the code

2. Filter the anti-compiled source code

3. Fix source code compilation error

4. Perform the Debug

Second, debug view

The most commonly used windows in debugging are:

window Description
Debug window The main display is the current thread method call stack, and the number of lines of code (code with debugging information)
Breakpoint Breakpoints Window = = Breakpoint List window, you can easily add breakpoints, set breakpoint conditions, delete breakpoints, etc.
Variable Variables window = = Displays the local variables of the current method, non-static methods, including the This application, you can modify the value of the variable
Code editing window =-this doesn't have to be said.
Output Console window = = Log output, when debugging, you can set the focus on the component level lower, in order to get more output information

In addition the auxiliary windows are:

window Description
Expressions Expression Window = = Write an expression of the data you want to observe, or modify the value of the variable
Display window = = can execute code blocks in display, output content, etc.
Outline Outline Window = = View methods, variables, etc. of the current class
Type-level Hierarchy window = = View the inheritance hierarchy of the current class, including the implementation interface, class inheritance hierarchy
Method invocation relationship Call Hierarchy window = = See which method calls are being called by the current method, which classes are called, and the number of rows, the corresponding method can be opened directly
Search Results Searches window + = combines shortcut keys to see where variables, methods, etc. are referenced or defined in a workspace, project, work set

1) Full window:

2) Debug view (thread stack 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:

1. Indicates that the current implementation continues to run until the next breakpoint, and the shortcut key is F8.

2. Interrupt the entire process

3. The current method is entered, and the shortcut key is F5.

4. Indicates that the next line of code is running, and the shortcut key is F6.

5. Means to exit the current method, return to the call layer, and the shortcut key to F7.

6. Represents the stack of the current thread, from which you can see what code is running, and the entire calling procedure, along with the code line number

Detailed Description:

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. Note When using drop to frame:

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.

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 on ' 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: When stepping, when a child function is encountered inside a function, it does not step into the sub-function, but instead executes the child function throughout the stop, that is, the entire function as a single step.

Step into: Stepping, encountering sub-functions and continuing stepping

Resume: Resumes the suspended 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 the program code is modified during debugging, then saved, click Resume, and the program is temporarily suspended 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 the debug of the local program is terminated by the Terminate command.

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

1. Debugging and execution

Mark function shortcut Keys Description
6.4 Step Info F5 Step Into (if there is a method call, it will go into the calling method to debug);
6.4 Step over F6 -Step Skip (any method call that does not enter the row, executes the current line of code directly, and jumps to the next line);
6.4 Step Return F7 Step back (Executes the current method and pops the current method from the call stack, returning the current method being 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 line (ignores all intermediate breakpoints and executes to the current cursor line);
6.3 Drop to Frame No ---fallback to execution at the beginning of the specified method, which is quite good.
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 on this method, which will be re-executed from the first line of this method.
Of course, for methods that have side effects, such as database operations, changing the object contents of incoming parameters, and so on, can be re-executed and then the content you want.
6.1+6.2 Copy Stack No Copy current thread stack information

If you need to exclude classes, packages, and so on while debugging, you can use the edit Step filters setting.


A6 Properties:java Process Start-up information, including console startup parameters, environmental parameters and so on. If there is a problem with the parameter startup parameters, you can first see if the actual startup parameters here are incorrect. You can also view options for virtual machine support debugging.

2. Data View

7.4 /tr>
tags features shortcuts description
7.4 Inspect ctrl+shift+i View the selected variable, the value of an expression, or the result of execution, press Ctrl+shift+i again to add the current expression or value to the EXP Ressions the window;
7.4 display ctrl+shift+d --Displays the selected variable, the value of the expression, or the execution result, press Ctrl+shift+d again You can add the current expression or value to the display window;
7.4 execute ctrl+u --execute selection expression;
7.4 run to line ctrl+r --to the current line (ignores all intermediate breakpoints, executes to the current cursor row);
7.3 all Instances ctrl+shift+n --View all objects of the selected class, this feature is awesome;
7.3 Instance Count None View the number of objects for the selected class;
Watch no Add current variable, expression to expressions window;

3) Variables view (variable view)

1. For the variable name view, displays all accessible instance variables and local variables in the current line of code

2. Show all the variable values

3. Variable values can be changed by this window

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.

4) Breakpoints View (breakpoint view)

1. Show All Breakpoints

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

3. Exception Breakpoint

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 the code snippet at the specified breakpoint run how many times, the most typical is the loop, if you want a loop to execute 10 times on the thread hang, then specify the hits count value is 10, then the current loop execution to the nineth time will be hung off.

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.

5) Expressions view (expression view)

1. Expressions

2. Click this to add an expression

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

6) 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)

7) Code View Secondary window

1. Code View:


Code view, which is used to display specific code. Where the green part refers to the code that is currently going to be executed

Mark function shortcut Keys Description
11.1~11.5 Quick Type Hierarchy Ctrl+t View the inheritance hierarchy of the current class, interface, the default entry, display the inheritance/implementation of the current class/method subclass, sub-interface 11.1, again ctrl+t, will display the current class, interface inheritance/implementation of the superclass/ interface 11.2; When debugging, often with this function, in the interface or abstract class method call 11.3,ctrl+t See implementation class 11.4, directly navigate to the corresponding implementation method 11.5.
Quick Outline Ctrl+o View the outline of the current class, including methods, attributes, etc.;
Open declarations F3 See where variables, properties, methods are defined

2. Call Hierarchy window:

Mark function shortcut Keys Description
12.1~12.2 Open Call Hierarchy Ctrl+alt+h View the method is called the hierarchy, you can see the current method is called place 12.1, or the current method calls the other class method 12.2

3. Type Hierarchy window:

Mark function shortcut Keys Description
13.1~13.4 Open type Hierarchy F4 Viewing the inheritance hierarchy, you can view the class's inheritance hierarchy, including the subclass parent class 13.1, or the class implementation of the interface inheritance level 13.2, and the class/interface selected, on the right to display the class's outline 13.3, 13.4, 13.3 can choose whether to display the parent class/Parent interface properties, methods, etc.

4. Search window:

Mark function shortcut Keys Description
14.1 Declarations Ctrl+g The same method signatures are defined in the workspace and in third-party jar packages 14.1
14.2 References Ctrl+shif+g The currently selected variable, property, method in the workspace, and the location referenced in the third-party jar package 14.2
14.3 Ctrl+shift+u See where variables, properties, methods appear in the current class 14.3
14.4 Implements To view the class that implements the current interface 14.8
14.4~14.7 Display mode You can choose a different display mode, the specific view

Second, Debug

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

1.1) Breakpoint Type and Breakpoint window

There are five types of breakpoints that you can set in debugging:

1. Line Breakpoints : Conditional breakpoint, as the name implies a certain condition of the breakpoint, only satisfies the user set conditions, the code will be run to the breakpoint at the stop.

2. method Breakpoints: 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, You can view the call stack for a method in this way.

3. observe Breakpoints (watch breakpoints-member variable access change)

4. Exception Breakpoint (Exception breakpoints)

5. Class Load breakpoint (class load breakpoints)

Each breakpoint has a slightly different settings, you can right-click on the breakpoint->breakpoint properties to set, but generally in the breakpoint window has a quick Setup interface, breakpoint properties more filter, in fact, relatively chicken, the use is not very good.

Shortcut keys related to breakpoints:

shortcut Keys Description
Ctrl+shift+b Large breakpoint/Cancel breakpoint at cursor
Ctrl+alt+b Ignore All breakpoints
Alt+shift+q, b Activate Breakpoint window

1, line breakpoint: Break point 1.1, 1.4 on a line in the method. The line breakpoint can be set to suspend thread/VM condition 1.3, number of accesses 1.2.
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 begin stepping through.
For the use of Hit count 1.2, it is generally in the loop that the nth object is handled in a problematic situation, setting hit Count = N, when re-debugging, can easily reach the number of cycles that need to be debugged, stop to debug.


2. Method Breakpoint: Break point 2.1, 2.2 on method. The benefit of a method breakpoint is that you can enter or exit from a method method 2.3, stop to debug, similar to the line breakpoint, and only the line breakpoint and method breakpoint have the conditional and access times setting function.
However, the method breakpoint has another benefit, if the code compiles, specifies that the debugging information is not carried, the line breakpoint is not working, can only hit the method breakpoint.
Interested can be add line number via A1 ... Before the tick is removed, debug under the look.

3. Observe the breakpoint: Break Breakpoint 3.1, 3.3 on the member variable. Static member variables do not work unless the object member variable has an effect.
You can set the variable to be accessed or set to suspend thread/VM 3.2, that is, all access to member variables, such as 3.4, or the method of setting will be monitored


4, the Exception breakpoint: The exception breakpoint can be added by 4.6, or click the output of the log information exception class information added.
Exception Breakpoint 4.1, when the system exception occurs, at the throw location of the caught exception or the program uncaught exception throws at 4.2, 4.4, suspended thread/vm, you can also specify whether to include the exception of the subclass is also detected 4.3, 4.5.


In addition to the above normal set exception hangs, from the Java->debug can be set to suspend execution, mainly the following two:

1, whether in the event of a global not captured when the hang (suspend execution on uncaught exceptions), debugging, always have an abnormal suspension effect debugging, but not set the exception breakpoint, you can tick off this option;

2, whether in the compilation error hangs, generally in the side debugging side to change the code when this happens;

Another thing to mention is that there is a main method to start the application, you can check in the Debug configuration of Stop in main A3, when the program enters, will suspend the thread, waiting for debugging.





5. Class Load Breakpoint: Breakpoint 5.1 hit on the class name. The interface is not able to hit the class loading breakpoint, but the abstract class is possible, but in the debugging, the breakpoint will not be obvious into the classloader, stepping into the knowledge of the sub-class to enter the method of construction, A non-abstract class is entered into the ClassLoader (if no filter is removed) 5.3 When the thread is suspended. Class loading breakpoints Suspend thread/vm5.2, whether on an abstract or non-abstract class, when the class first loads or the first child class is loaded for the first time.

2. 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.

1) Set Breakpoints:



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




Pop-up prompts, you need to switch to the debug workspace, tick "Remember my decision", remember the selection, then next time no longer prompt, then click "Yes".

3) Stepping: mainly using several previous views to debug, where the Debug view of several buttons have shortcut keys:

Step Retuen (F7)

Step Over (F6)

Step into (F5)


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

Reference:

http://blog.csdn.net/u010075335/article/details/51222493

Debug debugging Tips for Eclipse

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.