5
头图

Debug is used to track the running process of the code. Usually an exception occurs during the running of the program. Enable the Debug mode to analyze the location of the exception and the change of the parameters during the running. Usually we can also enable the Debug mode to track the running process of the code to learn the source code of the tripartite framework.

1. Debug opening

First look at the interface in Debug mode in IDEA.

The following is the interface after starting the Debug mode in IDEA and entering the breakpoint. My case is Windows, which may be different from the Mac icons. Simply talk about the 8 places marked in the figure below:

  • ① Start the service in Debug mode, and the button on the left starts in Run mode. During development, I usually start the Debug mode directly to facilitate debugging of the code at any time.
  • ② Breakpoint: Click the left button in the line number column on the left, or the shortcut key Ctrl+F8 to open/cancel the breakpoint, the color of the breakpoint line can be set by yourself.
  • ③ Debug window: After the access request reaches the first breakpoint, the Debug window will be activated automatically. If it is not activated automatically, you can go to the settings, as shown in Figure 1.2.
  • ④ Debug button: There are 8 buttons in total, and the main functions of debugging correspond to these buttons. Hover the mouse over the buttons to view the corresponding shortcut keys. The same corresponding function can be found in the menu bar Run, as shown in Figure 1.4.
  • ⑤ Service button: You can turn off/start the service, set breakpoints, etc. here.
  • ⑥ Method call stack: This shows all the methods that the thread has passed through. Check the [Show All Frames] button in the upper right corner, and the methods of other libraries will not be displayed, otherwise there will be a lot of methods.
  • ⑦ Variables: In the variable area, you can view the variables in the current method before the current breakpoint.
  • ⑧ Watches: To view variables, you can drag the variables in the Variables area to Watches to view

[Picture 1.1]

图片

[Figure 1.2]: Check Show debug window on breakpoint in the settings, then the Debug window will be activated automatically after the request enters the breakpoint

图片

[Picture 1.3]: If there is no toolbar or status bar displayed at the bottom of your IDEA, you can open it in View, and it will be convenient for us to display the toolbar. You can try these four options yourself.

图片

[Figure 1.4]: There is a debugging function in the menu bar Run, and the corresponding shortcut keys can be viewed at the same time.

图片

2. Basic usage & shortcut keys

The Debug function mainly corresponds to the two groups of buttons 4 and 5 in Figure 1:

1. First, let's talk about the first group of buttons, there are 8 buttons in total, from left to right as follows:

[PICTURE 2.1]

图片

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

Step Over (F8): Step over, go down line by line, if there is a method on this line, it will not enter the method.

Step Into (F7): Step In, if there is a method in the current line, you can enter the method. It is generally used to enter the custom method, and will not enter the method of the official class library, such as the put method on line 25.

Force Step Into (Alt + Shift + F7): Force Step Into, you can enter any method, you can use this method to enter the official class library when viewing the underlying source code.

Step Out (Shift + F8): Step out, exit from the step-in method to the method call. At this time, the method has been executed, but the assignment has not been completed.

Drop Frame (None by default): Rewind the breakpoint, described in detail in the following chapters.

Run to Cursor (Alt + F9): Run to the cursor, you can position the cursor to the line you need to view, and then use this function, the code will run to the cursor line without interruption.

Evaluate Expression (Alt + F8): Evaluate expression, which will be explained in detail in the following chapters.

2. The second group of buttons, a total of 7 buttons, from top to bottom are as follows:

[PICTURE 2.2]

图片

Rerun'xxxx': Re-run the program, it will restart the program after closing the service.

Update'tech' application (Ctrl + F5): Update the program, generally you can execute this function after your code is changed. The corresponding operation of this function is in the service configuration, as shown in Figure 2.3.

Resume Program (F9): Resume the program. For example, if you have two breakpoints on line 20 and line 25, you are currently running to line 20. Press F9 to run to the next breakpoint (line 25), and then Press F9 to run the entire process, because there is no breakpoint later.

Pause Program: Pause the program and enable Debug. No specific usage has been found so far.

Stop'xxx' (Ctrl + F2): Press twice to close the program. Sometimes you will find that the port is occupied when the service is shut down and restarted. This is because the service is not completely shut down, and you need to check and kill all JVM processes.

View Breakpoints (Ctrl + Shift + F8): View all breakpoints, which will be covered in later chapters.

Mute Breakpoints: Dumb breakpoints. After selecting this, all breakpoints will be grayed out and the breakpoints will be invalid. Press F9 to run the program directly. Click again, the breakpoint will turn red and become effective. If you only want to disable a certain breakpoint, you can right-click on the breakpoint to cancel Enabled, as shown in Figure 2.4, the line breakpoint is disabled.

[PICTURE 2.3]

图片

Update the program, On'Update' actions, what you do when you perform the update operation, generally select'Update classes and resources', that is, update classes and resource files.

Generally, it is better to use a hot deployment plug-in, such as JRebel, so that you don't have to restart the service every time you change the code. How to activate JRebel is attached in the last chapter.

The following On frame deactivation is triggered when the IDEA window loses focus, that is, when you switch from idea to browser, idea will automatically do things for you. Generally, you can set Do nothing. Frequent switching will consume more resources.
[PICTURE 2.4]

图片

Three, variable view

In the Debug process, it is very necessary to track and view the changes of variables. Here are a few places where you can view variables in IDEA. I believe most people understand.

1. As follows, in IDEA, the value of the current variable will be displayed after the line where the parameter is located.

[PICTURE 3.1]

图片

2. Hover the cursor on the parameter to display the current variable information. Click to open the details as shown in Figure 3.3. I usually use this method, which is quick and convenient.

[PICTURE 3.2]

图片

[PICTURE 3.3]

图片

3. View in Variables. All variables in the current method are displayed here.

[PICTURE 3.4]

图片

4. In Watches, click New Watch and enter the variables to be viewed. Or you can drag it from Variables to Watche to view.

[Picture 3.5]

图片

If you find that you don't have Watches, it may be in the place below.

[PICTURE 3.6]

图片

[PICTURE 3.7]

图片

Four, calculation expression

The calculation expression mentioned earlier is the button in Figure 4.1, Evaluate Expression (Alt + F8). You can use this operation to calculate the value of an expression during debugging without printing information.

[PICTURE 4.1]

图片

1. Press Alt + F8 or the button, or you can select an expression and Alt + F8, the window for calculating the expression will pop up, as shown below, press Enter or click Evaluate to calculate the value of the expression.

This expression can be not only a general variable or parameter, but also a method. When you call several methods in a line of code, you can view the return value of a method in this way.

[PICTURE 4.2]

图片

2. Set the variable. In the calculation expression box, you can change the value of the variable, so that sometimes it is convenient for us to debug various values.

[PICTURE 4.3]

图片

Five, smart step into

Think about it, there are several methods in a line of code, how to choose only one method to enter. As mentioned before, using Step Into (Alt + F7) or Force Step Into (Alt + Shift + F7) to enter the method, but these two operations will be entered in sequence according to the method call sequence, which is more troublesome.

Then smart step into is very convenient, smart step into, this function can be seen in Run, Smart Step Into (Shift + F7), as shown in Figure 5.1

[PICTURE 5.1]

图片

Press Shift + F7, it will automatically locate the current breakpoint line, and list the methods that need to be entered, as shown in Figure 5.2, click on the method to enter the method.

If there is only one method, enter directly, similar to Force Step Into.

[PICTURE 5.2]

图片

Six, breakpoint condition setting

By setting the breakpoint condition, it stops at the breakpoint when the condition is met, otherwise it runs directly.

Usually, when we are traversing a relatively large collection or array, a breakpoint is set in the loop, should we look at the values of the variables one by one? It must be very tiring, maybe you still miss this and it's worth doing it again.

1. Right-click on the breakpoint to directly set the conditions of the current breakpoint, as shown in Figure 6.1. The breakpoint will only take effect when I set exist to true.

[PICTURE 6.1]

图片

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

Java Line Breakpoints shows all breakpoints. Check Condition on the right to set the breakpoint conditions.

Check Log message to console, the current breakpoint line will be output to the console, as shown in Figure 6.3

Check Evaluate and log, you can execute this line of code to calculate the value of the expression and output the result to the console.

[PICTURE 6.2]

图片

[PICTURE 6.3]

图片

3. Let's talk about the Filters on the right. These are not commonly used under normal circumstances, so let's briefly talk about the meaning.

Instance filters: Instance filter, enter the instance ID (the instance ID in Figure 6.5), but I have not succeeded here. I don’t know the reason. Anyone who knows will leave a comment.

Class filters: class filter, filter according to class name, also unsuccessful...

Pass count: Used in loops, if the breakpoint is in the loop, you can set this value, and stop at the breakpoint after how many loops, and the subsequent loops will stop at the breakpoint.

[PICTURE 6.4]

图片

[PICTURE 6.5]

图片

4. Abnormal breakpoint

By setting an exception breakpoint, when an exception that needs to be intercepted occurs in the program, the exception line will be automatically located.

As shown in Figure 6.6, click the + sign to add Java Exception Breakpoints and add exception breakpoints. Then enter the exception class that needs a breakpoint, as shown in Figure 6.7, and then you can see the added exception breakpoint in Java Exception Breakpoints.

I added a NullPointerException breakpoint here, as shown in Figure 6.8. After a null pointer exception occurs, it is automatically located in the null pointer exception line.

[PICTURE 6.6]

图片

[PICTURE 6.7]

图片

[PICTURE 6.8]

图片

Seven, multi-threaded debugging

Under normal circumstances, we debug in a thread, step by step down. But sometimes you will find that you can't make another request when you want to make another request during Debug?

That's because IDEA's default blocking level during Debug is ALL, which will block other threads, and will only go to other threads when the current debugging thread is finished. You can select Thread in View Breakpoints, as shown in Figure 7.1, and then click Make Default to set it as the default option.

[PICTURE 7.1]

图片

Switch thread. In the drop-down list of Frames in Figure 7.2, you can switch the current thread. As shown below, I have two Debug threads. Switching the other one will enter another Debug thread.

[PICTURE 7.2]

图片

Eight, rollback breakpoint

When debugging, do you want to go through the process again without initiating a request again?

1. First understand the method call stack, as shown in Figure 8.1, first request to enter the insertDemo method of DemoController, and then call the insert method. We don't care about other invokes. The top method is the method where the current breakpoint is located.

[PICTURE 8.1]

图片

2. Breakpoint rollback

The so-called breakpoint rollback is actually to roll back to the beginning of the previous method call. In IDEA, the test cannot roll back line by line or return to the previous breakpoint, but return to the previous method.

There are two ways to roll back, one is the Drop Frame button (Figure 8.2), which is gradually rolled back according to the called method, including other methods of the tripartite class library (cancel the Show All Frames button, and the tripartite class library methods will be displayed, as shown in the figure. 8.3).

In the second way, select the method to be rolled back on the call stack method, right-click and select Drop Frame (Figure 8.4), and go back to the last method call of the method. At this time, press F9 (Resume Program) again, you can see The program enters the breakpoint of the method.

But one thing to note is that the breakpoint rollback can only go through the process again. Some parameters/data states that have changed before cannot be rolled back to the previous state, such as objects, collections, updated database data, etc. Wait.

Figure [8.2]

图片

Figure [8.3]

图片

Figure [8.4]

图片

Nine, interrupt Debug

Want to interrupt the request during Debug, don't go through the rest of the process?

Sometimes, after seeing that the input parameters are wrong, we don't want to go through the following process. How can we interrupt this request (the following process will delete the database data...)? Should we close the service and restart the program? Well, I did it before.

To be precise, I did not find a way to directly interrupt the request (except to close the service), but Force Return, that is, forced return, can be used to avoid the subsequent process, as shown in Figure 9.1.

Click Force Return, and the Return Value window pops up. The return type of my method is Map. Therefore, I return results directly to force the return, so that the subsequent process will not be carried out. Or you can new HashMap<>().

[PICTURE 9.1]

图片

[PICTURE 9.2]

图片

X. Attachment: JRebel activation

At present, I have been using JRebel as a hot deployment tool, and the effect is fairly satisfactory. Modifying Java code or xml and other configuration files can be hot updated. Occasionally, the hot update does not take effect when the service has been opened for a long time or there are many changed files, and the service needs to be restarted.

Here is just a brief description of a free method (non-crack) that I saw on the Internet to obtain permanent usage rights. I'm not sure when this method can't be used.

  • ① First you need a Facebook or Twitter account (preferably Twitter)
  • ② Enter this website: https://my.jrebel.com/ , and log in, as shown in Figure 10.1
  • ③ Then you can get your permanent activation code in Install and Acticate.
  • ④ Set the activation code in Jrebel in the settings, as shown in Figure 10.3. If the JRebel plug-in is not installed, first install and search for the JRebel plug-in in Plugins.

[Figure 10.1]

图片

[Picture 10.2]

图片

[Picture 10.3]

图片

If there is anything wrong or a better way, please leave a message!

Author: bojiangzhou
https://www.cnblogs.com/chiangchou/p/idea-debug.html


民工哥
26.4k 声望56.7k 粉丝

10多年IT职场老司机的经验分享,坚持自学一路从技术小白成长为互联网企业信息技术部门的负责人。2019/2020/2021年度 思否Top Writer