Intellij IDEA Tutorial (xv) IDEA Debug use

Debug code used to track the operation of the process, typically an exception occurs during program execution, Debug mode may enable analysis of abnormal position location, and parameter changes during operation. Usually we can also enable the Debug mode to track running processes to learn the source code tripartite framework.

Opening a .Debug

First, look at the interface of IDEA in Debug mode.

The following is a start in Debug mode IDEA, the interface after entering the break, I have here is Windows, and Mac icons may be somewhat different. Simply say eight places in the figure marked:

① start the service in Debug mode, a button to the left is based on the Run mode is activated. In development, I usually started directly Debug mode, at any time convenient debugging code.

② breakpoints: line numbers in the left column, click the left button or shortcut keys Ctrl + F8 marked / cancel a breakpoint, the breakpoint line color can set themselves.

Debug window ③: After the access request reaches the first breakpoint, Debug window is automatically activated. If not activated, can be provided to set in, as shown in FIG 1.2.

④ Debugging button: a total of eight buttons, commissioning of the main functions of these buttons will correspond, hovering over the button to view the corresponding shortcut keys. The same can be found in the corresponding function in the menu bar Run in Figure 1.4.

⑤ service button: Here you can shut down / start the service, set breakpoints and so on.

⑥ method call stack: It shows all the methods through which the thread debugging, check [Show All Frames] button in the upper right corner, methods other libraries will not be displayed, otherwise there will be a lot of methods.

⑦ Variables: variable in the variable region can be viewed within the current method before the current breakpoint.

⑧ Watches: watch variables, you can Variables Variables in drag Watches View

[FIG. 1.1]
Figure 1.1

Debug window automatically activated after checking Show debug window in the settings on breakpoint, the request proceeds to break: [Figure 1.2]
Here Insert Picture Description

[Figure 1.3]: If your bottom IDEA does not display the toolbar or status bar, you can open the View, the toolbar will show us convenient to use. You can go try under these four options.
Here Insert Picture Description

[Figure 1.4]: There are debugging functions corresponding to Run in the menu bar, and you can view the corresponding shortcut keys.

Here Insert Picture Description

II. Basic Usage & Shortcuts

Debug debugging a main function corresponds to FIG. 4 and 5 two buttons:

1, a first set of said first button, a total of 8 buttons, from left to right as follows:
  
[Figure 2.1]
Here Insert Picture Description

  • Show Execution Point (Alt + F10): If your cursor in the other rows or other page, click on this button to jump to the current line of code is executed.

  • Step Over (F8): Step through, line by line to go down, if the method will not enter this line method.

  • Step Into (F7): into the current line if there is a method, access to the interior methods, typically into a custom methods, the library does not enter the official method, methods such as PUT line 25.

  • Force Step Into (Alt + Shift + F7): forcibly entered and can enter in any way the underlying source code to see when you can use this method to enter the official library.

  • Step Out (Shift + F8): step out of the exit from the method call to step into the method at this time the method has finished, but not yet completed the assignment.

  • Drop Frame (no default): fallback breakpoints, later chapters detail.

  • Run to Cursor (Alt + F9): Run to Cursor, you can position the cursor to the line that you need to see, and then use this function, the code will run to the cursor line, without the need to interrupt point.

  • Evaluate Expression (Alt + F8): calculation expression, described in detail in later chapters.

2, a second group of buttons, a total of seven buttons, from top to bottom as follows:

[2.2]
Here Insert Picture Description

  • Rerun 'xxxx': re-run the program, the program will restart after shutting down services.

  • Update 'tech' application (Ctrl + F5): updates, usually after you change the executable code has this feature. And this is the operation corresponding to the function in the service configuration, FIG. 2.3.

  • Resume Program (F9): recovery program, for example, you have two breakpoint at line 20 and line 25, line 20 to the current run, press F9, then run to the next breakpoint (ie, line 25), then press F9, then run a complete process, since the latter has no breakpoint.

  • Pause Program: program pause, enable Debug. Currently did not find the specific usage.

  • Stop 'xxx' (Ctrl + F2): double-continuous, close the program. Sometimes you will find when you close and then start the service, reported that the port is occupied, it is not completely closed because of service reasons, you need to killing all the JVM process.

  • View Breakpoints (Ctrl + Shift + F8): View all breakpoints, later chapters will be involved.

  • Mute Breakpoints: Breakpoints dumb, select this, all breakpoints turn gray, the breakpoint fails, you can press F9 complete run the program directly. Click again to break into the red, and effective. If you want to make a break for a failure, right Enabled canceled at the breakpoint, as shown in FIG 2.4, the line break failure.

[2.3]: update program, On 'Update' actions, performed when the update operation doing things, generally choose 'Update classes and resources', is updated class and resource files.

Generally with hot deployment with plug-ins will be better after such JRebel, so you do not have to go to every code change to restart the service. How to activate JRebel, accompanied in the last chapter.

The following On frame deactivation, when the window loses focus IDEA trigger that generally you switch from the idea to the browser when, idea will automatically help you to do things, generally you can set Do nothing, frequent switching would be more consumption of resources.

Here Insert Picture Description

[2.4]
Here Insert Picture Description

III. Variables View

In the Debug process, track changes in view of variables is necessary, here simply under the IDEA, you can view several local variables, I believe most people understand.

1, as follows, in the IDEA, the parameter value is displayed in the row behind the current variable.

[3.1]
Here Insert Picture Description

2, the cursor to hover over the parameter, to display the current variable information. Click to open the details shown in Figure 3.3. I usually use this method, convenient and quick.
[3.2]
Here Insert Picture Description

[3.3]
Here Insert Picture Description

3, in the Variables view, this shows the current method where all variables.

[Figure 3.4]
Here Insert Picture Description

4, in Watches, click on New Watch, input variables need to see. You can view or drag Watche in the Variables inside.

[Figure 3.5]
Here Insert Picture Description
If you find you do not Watches, places may be located next chart.

[FIG. 3.6]
Here Insert Picture Description
[3.7 FIG]
Here Insert Picture Description

IV. Calculation expression

4.1 buttons in the aforementioned calculation expression shown in FIG, Evaluate Expression (Alt + F8). This operation can be used to calculate the value of an expression in the debugging process, without having to go to print information.

[Figure 4.1]
 Here Insert Picture Description

1. Press Alt + F8 or button, or you can select an expression and then Alt + F8, the pop-up window evaluate the expression, as follows, press Enter or click Evaluate the value calculated expression.

This expression can be not only a general variables or parameters can also be a way, when you call the line of code in several ways when you can view to see the return value of a method in this way.

[4.2]
Here Insert Picture Description

2, set the variable in the calculation of the expression box, you can change the value of a variable, so sometimes we can be very easy to debug various values ​​of not.

[FIG. 4.3]
Here Insert Picture Description

V. Intelligent entered

Think about it, a line of code, there are several methods, how to choose one particular method to enter. Using the previously mentioned Step Into (Alt + F7) or Force Step Into (Alt + Shift + F7) into the interior of the method, but the operation will turn into two call sequence according to the method, it is too much trouble.

So smart step into very convenient, intelligent entered, this function can be seen in Run Lane, Smart Step Into (Shift + F7), shown in Figure 5.1

[FIG. 5.1]
Here Insert Picture Description

Press Shift + F7, is automatically positioned to break the current line, and lists methods need to enter, as 5.2, click method to enter the internal method.

If there is only one way, then go directly, similar to the Force Step Into.

[Figure 5.2]
Here Insert Picture Description

VI. Break conditions set

By setting a breakpoint condition, when the condition is satisfied, we stopped at a breakpoint, or run directly.

Usually, when we traverse a large collection or array when, within the loop set a breakpoint, are we going to see a value in a variable? That must be very tired, maybe you missed this worth so again.

1, on the right directly breakpoint condition current breakpoint, shown in Figure 6.1, I set a breakpoint exist to true to take effect.

[Figure 6.1]
Here Insert Picture Description

2. Click View Breakpoints (Ctrl + Shift + F8), to view all breakpoints.

Java Line Breakpoints shows all breakpoints, check Condition on the right, set conditional breakpoints.

Check Log message to console, it will break the current row to the console, Figure 6.3

Check Evaluate and log, can be performed in this line is the value calculated expression, and outputs the result to the console.

[6.2]

Here Insert Picture Description

[FIG. 6.3]
Here Insert Picture Description

3, say that the right filter Filters are not used under these general circumstances, simply meaning the next.

Instance filters: filter example, enter the instance ID (in Figure 6.5 instance ID), but I do not have success, I do not know why, know a friend leave a message.

Class filters: category filter, filter by class name, also without success ...

Pass count: for the cycle, if a breakpoint in the loop, you can set this value, how many times after the cycle stops at the breakpoint, after the cycle will stop at the breakpoint.

[FIG. 6.4]

Here Insert Picture Description

[FIG. 6.5]
Here Insert Picture Description

4, the breakpoint exception by exception breakpoint setting, when an abnormality occurs in the need to intercept the program, is automatically positioned to the line abnormality.

Figure 6.6, click the + Add Java Exception Breakpoints, add an exception breakpoint. Then enter the required breakpoint exception classes, as 6.7, then you can see the exception breakpoint added in Java Exception Breakpoints years.

I add here a NullPointerException breakpoint, Figure 6.8, null pointer exception occurs automatically positioned at null pointer exception line.

[FIG. 6.6]
Here Insert Picture Description

[FIG. 6.7]
Here Insert Picture Description

[Figure 6.8]

Here Insert Picture Description

VII. Multithreaded Debugging

Commissioning of us is in a thread, step by step to go down under normal circumstances. But sometimes you will find that when Debug, would like to initiate another request can not be carried out?

That's because IDEA blocked by default when the level is Debug ALL, can block other threads, other threads will only go if the current thread debugging finish. View Breakpoints can be selected in Thread, shown in Figure 7.1, then click Make Default to set the default option.

[FIG. 7.1]

Here Insert Picture Description

Switch threads, in the drop-down list Figure 7.2 Frames's, you can switch the current thread, as I have here two Debug thread, the other is switched into another Debug thread.

[7.2]

Here Insert Picture Description

VIII. Fallback break

When debugging, you want to re-take about the process rather than initiate a request again?

1, the first to recognize this method call stack, shown in Figure 8.1, the first request to enter insertDemo DemoController method, and then call the insert method, we invoke other and the first matter, the top of the current method is a method where the breakpoint.

[FIG. 8.1]
Here Insert Picture Description

2, break rollback

The so-called breakpoint fallback, in fact, at the beginning of fall back on a method call, the test can not line by line in the IDEA in the back or back to a breakpoint, but return to the previous method.

There are two fallback methods for a Drop Frame button (Figure 8.2), according to the gradual rollback method calls, including other methods tripartite libraries (Cancel Show All Frames button tripartite method of display libraries, as 8.3).

The second way, choose to roll back on the call stack methods, right click and choose Drop Frame (Figure 8.4), fall back on a method call at this method, then press F9 (Resume Program), you can see the program proceeds to the process at the breakpoint.

But one thing to note, breakpoints can only re-take about the rollback process, the status of certain parameters before / data has changed is unable to fall back to the state before, such as objects, collections, update the database data Wait.

FIG [8.2]

Here Insert Picture Description

FIG [8.3]
Here Insert Picture Description

FIG [8.4]

Here Insert Picture Description

IX. Debug Interrupt

When you want to Debug, the interrupt request, do not walk the rest of the process?

After some time, we saw the incoming parameter is incorrect, want to leave behind the process, and how to interrupt this request it (behind the database you want to delete the data it processes ...), you do not want to shut down the service to restart the program? Ah, I used to be so dry.

Rather, I did not find a way to directly interrupt request (except shut down service), but by Force Return, that is forced back to avoid the follow-up process, as shown in 9.1.

Click Force Return, Return Value pop-up window, my method's return type for the Map, so, here I direct return results, to force the return, so that no subsequent processes. Or you can new HashMap <> ().

[FIG. 9.1]
Here Insert Picture Description

[Figure 9.2]

Here Insert Picture Description
This article is reproduced from -https: //www.cnblogs.com/chiangchou/p/idea-debug.html

Published 81 original articles · won praise 46 · views 510 000 +

Guess you like

Origin blog.csdn.net/u013254183/article/details/105365354