Debug---Eclipse Breakpoint Debugging Basics

Source: Internet
Author: User

1. Enter debug mode (basic knowledge list)
1. Set breakpoints
2. Start the debug mode on the servers side
3, run the program, in the background encountered a breakpoint, enter the Debug debugging state
=============================
scope function Shortcut keys
Global single-Step return F7
Global single-Step skip F6
Global single-step jump into F5
Global single-Step jump selection Ctrl+f5
Global Debug Last started F11
Global Continue F8
Global use filters to step SHIFT+F5
Global Add/Remove breakpoints Ctrl+shift+b
Global Display Ctrl+d
Global Run last boot ctrl+f11
Global Run to row ctrl+r
Global Execution Ctrl+u

=============================
1.Step into (also F5) jump
2.Step over (also F6) skip
3.Step return (also F7) executes the current method, and then return jumps out of this method
4.step filter Progressive filtering is performed until an unfiltered position or breakpoint is encountered (set Filter:window-preferences-java-debug-step Filtering)
5.resume restarts debug and runs until breakpoint is encountered.
For example: A and B two breakpoints, the debug process found a breakpoint is useless, remove a breakpoint, run resume will skip a directly to the B breakpoint.
6.hit count set execution times for a for loop in the program (set breakpoint view-right-click count)
7.inspect check operation. Executes an expression that displays the execution value
8.watch real-time monitoring of changes to objects, methods, or variables
9. The breakpoint (breakpoints) We often say is the line breakpoints, except for line breakpoints, there are other types of breakpoints: field (Watchpoint) Breakpoint,method Breakpoint, exception breakpoint.
10.field Breakpoint also known as watchpoint (monitoring point) suspend when member variable is read or modified
11. Add method Breakpoint Suspend (Run-method breakpoint) when entering/leaving this approach
12. Add exception breakpoint Catch the Execption when the suspension (to be continued ...)
Breakpoint Properties:
1.hit count execution number of times after suspend for loop
2.enable condition meet your input criteria (for ture\ change) on hold
3.suspend thread Multi-threaded when this threads are suspended
4.suspend VM suspend Virtual machine
Variables in the 13.variables view can change the value of the variable, and the variables view selects the variable right-click--change value. Once for quick commissioning.
The 14.debug process modifies some code after the--〉save&build-->resume--> is re-suspended on the breakpoint
15.resume when debug debugging runs out of exception, run resume and start debugging again from the breakpoint
16. If a row has many methods,
When the first time you press F5 key to jump into this line first method, F6 step by step debugging, F7 out of this method.
When the second press F5 key to jump into this line the second method, F6 step by step debugging, F7 jump out of this method.
And so on. To enter this line the first few methods, just a few times F5 and F7.

2. Basic knowledge (including some practices)

The Eclipse debugger itself is a standard set of plugins within Eclipse. Eclipse also has a special debug view that is used to manage debugging or running programs in the workbench. It can display the stack frame of a suspended thread in each debug target. Each thread in the program appears as a node in the tree, and the Debug view shows the process for each running target. If a thread is in a suspended state, its stack frame is displayed as a child element.

Before using the Eclipse debugger, assume that you have installed the appropriate Java SDK/JRE (I recommend using Java VM V1.4) and the Eclipse Platform SDK V3.3, and that both work fine. In general, it is better to test the debugging options with the Eclipse sample first. If you want to develop and debug a C + + project, you will also need to obtain and install the C + + development tool (C + + development TOOLS,CDT). See reference resources for Java SDK/JRE, Eclipse platforms and samples, and CDT . Figure 1 shows a general view of the Debug perspective.


Figure 1. General view of the Eclipse Debug perspective


Debugging Java Language Programs

You need to cleanly compile and run the code at the moment you can debug the item. First, you need to create a run configuration for your application to ensure that the application starts correctly. You then need to Set up the Debug configuration in a similar way through the Run > Debug menu. You also need to select a class to use as the primary Java class for debugging (see Figure 2). You can set up multiple debug configurations for a single project as you wish. When the debugger starts (from Run > Debug), it opens in a new window, where you can start debugging.


Figure 2. To set the main Java class for a project in a debug configuration


Next, you'll discuss some of the common debugging practices in Eclipse.

Set breakpoints

When you start the application for debugging, Eclipse automatically switches to the debug perspective. Undoubtedly, the most common debugging step is to set breakpoints so that you can check for variables and values within a conditional statement or loop. To set a breakpoint in the Java Perspective's Package Explorer view, double-click the selected source code file and open it in an editor. To traverse the code, place the mouse over the tag bar of the suspect code line (to the left of the editor area) and double-click to set the breakpoint.


Figure 3. Two breakpoints seen on the left side of the editor


Now start the debug session from the Run > Debug menu. It is best not to put multiple statements on one line, because you cannot step in and set a line breakpoint on multiple statements on the same line.


Figure 4. The arrow at the left margin in the view indicates the row that is currently executing


There is also a handy breakpoint view to manage all breakpoints.


Figure 5. Breakpoint View


Conditional breakpoint

Once you know where the error occurred, you may want to know what the program is doing before the program crashes. One way is to step through each line of the program's statement. Execute one line at a time until you run to the suspect line of code. Sometimes it's best to run only a piece of code, stop running at the suspect code, and check the data at this location. You can also declare conditional breakpoints, which are triggered when the value of an expression changes (see Figure 6). In addition, you can also use code help when entering conditional expressions.


Figure 6. Set conditional breakpoint triggers


Evaluate the value of an expression

To calculate the value of an expression in the editor of the Debug perspective, select the line of code that sets the breakpoint, and in the context menu, Select the Inspect option by Ctrl+shift+i or right-clicking the variable you are interested in (see Figure 7). The value of an expression is evaluated in the context of the current stack frame, and results are displayed in the Expressions view of the display window.


Figure 7. To calculate the value of an expression by using the Inspect option


Cut activity Code

The Display view allows you to manipulate the activity code in a cut-type manner (see Figure 8). To process a variable, enter the variable name in the Display view and the view will prompt you for a familiar content helper.


Figure 8. Display View


When the debugger stops at a breakpoint, you can continue the debugger session by selecting the Step over option from the Debug View toolbar (see Figure 9). The higher the line of code that is displayed, the more expensive the operation continues to execute the next line of code in the same method (or proceed to the next line of code that calls the method that called the current method). Variables that change after the previous step are highlighted in a certain color (yellow by default). Colors can be changed in the Debug Preferences page.


Figure 9. Variable to change color


To suspend the execution thread in the Debug view, select a running thread, and click Suspend in the Debug View toolbar . The current call stack for the thread is displayed, and the currently executing line of code is highlighted in the editor in the Debug perspective. When a thread is suspended, the mouse is placed on a variable in the Java editor, and the value of the variable is displayed in a small hover window. At this point, the top stack frame of the thread is also automatically selected, and the visible variables are also displayed in the Variables view. You can check the variable by clicking the appropriate variable name in the Variables view.

Hot swap bug FIX: Dynamic code correction

If you are running Java Virtual machine (Java MACHINE,JVM) V1.4 or later, Eclipse supports a feature called Hot Swap bug fix (hotswap Bug fixing), which is not supported by JVM V1.3 or a lower version. This feature allows you to change the source code in a debugger session, which is better than exiting the application, changing the code, recompiling, and then starting another debugging session. To take advantage of this feature, you can re-debug your code after you change it in the editor. This functionality is possible because the JVM V1.4 is compatible with the Java Platform Debugger architecture (Java Platform Debugger architecture,jpda). JPDA implements the ability to replace modified code in a running application. This is especially useful if the application starts longer or if it takes a long time to execute to the point where the program failed.

If the program has not been fully executed while debugging is complete, select the Terminate option in the context menu of the debug view. An easy mistake is to use Debug or Run instead of Resume in a debugger session. Doing so will start another debugger session instead of continuing the current session.

3.DEBUG Debug Mode (method Summary)

1. Conditional breakpoint
Right-click at the breakpoint-select "Breakpoint Properties"
Tick "Enabled" to start the breakpoint
Tick "hit Count" input box write run count
Tick "Enable Condition" input box write stop condition

2. Variable Breakpoint
Variables can also accept breakpoints, either when the value of a variable is initialized, or when the value of a variable changes.
You can add conditions to the same settings as the above conditional breakpoints

3. Method Breakpoint
The special point of a method breakpoint is that it can be hit in the JDK's source code.
Because the JDK is compiled with the debug information removed, so the normal breakpoint is not hit inside
However, a method breakpoint can be used to view the method's call stack in this way

4. Change the value of the variable
In the Variables small window of the debug view,
You can right-click on the variable and select "Change Value ..." Modify the value of a variable in a popup dialog box
Or in the following values in the View window to modify, Ctr+s save

5. Re-commissioning
The fallback for this debug is not omnipotent and can only be returned at the beginning of the call to 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
When connecting to a remote server, you need to create a new remote debugger in eclipse

7. Abnormal Breakpoint
It is difficult to find the place where the exception occurred, but it is OK to hit an abnormal breakpoint
Breakpoints Tool Box top right fourth use "Add Exception exception Breakpoint"
When an exception occurs, the code stops at the point where the exception occurred and should be helpful when locating the problem

4. Various breakpoint setting methods (practice)

Everyone must have used the debugging capabilities of Eclipse, and in the process of debugging it is not natural to avoid using breakpoints (breakpoint), but I do not know whether the various breakpoints in eclipse are known. This article provides an illustrated overview of all types of breakpoints in Eclipse and their settings, and we hope to help. (2011.11.20)

1. Sample Programs
Breakpointdemo is a figment application, just for the purpose of explaining the use of various breakpoints in eclipse. As shown in the Code,

Breakpointdemo consists of two main methods:
[1]setvalue, the method assigns a value of member variable value to a random integer with a range of 0-9, based on the specified number of times (count).
[2]printvalue, the method calls SetValue () to assign value and prints out the value of value, but if value is divisible by 3, a IllegalArgumentException exception is thrown.

2. Line Breakpoint
Line Breakpoin is the simplest eclipse breakpoint, as long as you double-click the left column of a line of code to set a breakpoint on that row. Here, the line breakpoint is set on the 20th row of code, as shown in

You can set a condition for line breakpoint, and when the program runs to that breakpoint, it will be interrupted only if it satisfies the set criteria. Right-click the breakpoint on line 20th and select "BreakpointProperties ... "

In the pop-up Properties dialog box, tick "Conditional" and enter "count% 2 = = 0" In the text box.

This condition indicates that the program will be interrupted only if Count is an even number when the program is running to the 20th row. Carefully, you will find that the breakpoint icon has changed, a question mark is more.


3. Watchpoint
Line breakpoint is concerned with the "process" of running a program, which is often referred to as one-step debugging using this breakpoint. Sometimes, however, we are not familiar with the process of running the program, and may not be too concerned, not sure where to set the breakpoint is appropriate, but may be more concerned about a key variable changes or use. At this point, you can set a special breakpoint--watchpoint for the variable. In this example, we are most concerned about the value of the member variable values, then you can set a watchpoint for it, double-click the 9th Line of code corresponding to the left column is OK.

Use the method mentioned in 2 to view the properties of the breakpoint,

By default, when the variable is accessed or its value is modified, the program will be interrupted. However, in this example, you only want the program to be interrupted when you modify the value, so uncheck the "Access".

At this point, we will find that the original Watchpoin icon has also changed.


4. Method Breakpoint
As with attention to the access and modification of a variable, we can also focus on the procedure's invocation of a method, that is, you can set method breakpoint. Here, you set the method breakpoint for methods SetValue. Similarly, double-click on the left column of the 11th line of code.

You still want to view the properties of the breakpoint. By default, only "Entry" is checked, and "Exit" is not selected.

This means that the program is interrupted when the method is first entered (the invocation begins), and the program is not interrupted when it leaves the method (the call ends). In this example, you need to tick "Exit" at the same time.

After clicking OK, you can see that the icon for the breakpoint has also changed.

Depending on the settings here, when the program runs to line 20th, it will be interrupted on line 12th, although there are no explicit breakpoints, but this is the SetValue () method's entry (Entry). It must be noted that the program will not be interrupted when it runs to line 16th, although it looks like the exit (exit) of the SetValue () method. In fact, the program will be interrupted on line 17th, where the SetValue () call ends.

5. Exception Breakpoint
If we expect a particular exception to occur, the program can be interrupted to make it easier to see the state of the program at that time. This can be achieved by setting the Exceptionbreakpoint. This example intentionally throws a IllegalArgumentException exception on line 23rd, and we expect the program to be interrupted when it is run here. But instead of setting line breakpoint directly for this row of code, we set the Exception  Breakpoint. The method of setting exception breakpoint is different from other types of breakpoints, and it cannot be set directly on the Code Editor by double-clicking the left column. Click the icon in the upper right corner of the breakpoints view like Ji,
 
in which you select IllegalArgumentException, and click OK, then a exception breakpoint is set up.
 

when value is a multiple of 3, the program is interrupted on line 23rd, so we can use the debugger to see if value is equal to 0, 3, or 6.


6. Class Load Breakpoint
There is also a breakpoint--class load breakpoint that you might not normally use, that is, when a class is loaded, the breakpoint can break the program.

Debug---Eclipse Breakpoint Debugging Basics

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.