preface

Debug Traces the running process of the code. If exceptions occur during the running of the program, enable the Debug mode to locate exceptions and parameter changes during the running. Usually we can also enable Debug mode to track the running flow of the code to learn the source of the tripartite framework.

Therefore, learn how to use Debug in Intellij IDEA, including the following contents:

  • Debug the opening
  • Basic usage & shortcut keys
  • Variable to view
  • Computed expression
  • Intelligence into
  • Breakpoint condition setting
  • Multithreaded debugging
  • The fallback breakpoint
  • Interrupt the Debug
  • Attached: JRebel activated

1. Debug

First, view the Debug interface in IDEA.

The following is the interface after the Debug mode is started in IDEA and the breakpoint is entered. Here IS Windows, which may be different from Mac ICONS. Just to summarize the 8 places highlighted below:

  1. Start the service in Debug mode, and the left button starts the service in Run mode. During development, I usually start Debug mode directly, so that I can Debug the code at any time.
  2. Breakpoint: Click on the left line or Ctrl+F8 to open/cancel breakpoint. The color of the breakpoint line can be set by yourself.
  3. Debug window: The Debug window is automatically activated when the access request reaches the first breakpoint. If not, go to Settings, as shown in Figure 1.2.
  4. Debug button: there are eight debug buttons. You can view the corresponding shortcut keys by hovering the mouse over these buttons. The same function can be found in the menu bar Run, as shown in Figure 1.4.
  5. Service button: This is where you can turn off/start services, set breakpoints, etc.
  6. Method call stack: This shows All the methods that this thread is debugging through. Check the [Show All Frames] button in the upper right corner to not Show the methods of other libraries, otherwise there would be a lot of methods here.
  7. Variables: In the Variables area, you can view Variables in the current method before the current breakpoint.
  8. Watches: View Variables. You can drag Variables from the Variables area to the Watches to view Variables

(figure 1.1)

If you select Show debug window on breakpoint, the debug window is automatically activated when the breakpoint is entered

(figure 1.2)

If there is no toolbar or status bar displayed at the bottom of your IDEA, you can open it in View. The toolbar displayed will be convenient for us to use. Try out these four options on your own.

(figure 1.3)

There are debugging corresponding functions in the menu bar Run, and you can view the corresponding shortcut keys.

(figure 1.4)

Two, basic usage & shortcut keys

The debugging function mainly corresponds to buttons 4 and 5 in Figure 1:

1. First, the first group of buttons, a total of 8 buttons, from left to right as follows:

(figure 2.1)

  • Show Execution Point (Alt + F10) : If your cursor is on another line or page, click this button to jump to the current line of Execution.
  • Step Over (F8) : Step Over, line by line, if there is a method on the line does not enter the method.
  • Step Into (F7) : if the current line has a method, you can enter the method itself. This is usually used to enter custom methods, but not official library methods, such as the put method on line 25.
  • Force Step Into (Alt + Shift + F7) : Force Step Into (Alt + Shift + F7) : Force Step Into (Alt + Shift + F7)
  • Step Out (Shift + F8) Step Out (Shift + F8) Step Out (Shift + F8) Step Out (Shift + F8) Step Out (Shift + F8) Step Out (Shift + F8) Step Out (Shift + F8)
  • Drop Frame (none by default) : rollback breakpoint, as described in the following section.
  • Run to Cursor (Alt + F9) : Run to Cursor (Alt + F9) : Run to Cursor (Alt + F9) : Run to Cursor (Alt + F9) : Run to Cursor (Alt + F9) : Run to Cursor (Alt + F9) : Run to Cursor (Alt + F9) : Run to Cursor (Alt + F9) : Run to Cursor (Alt + F9) : Run to Cursor (Alt + F9) : Run to Cursor.
  • Evaluate Expression (Alt + F8) : Evaluate Expression, as explained in the next section.

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

(figure 2.2)

  • Rerun ‘XXXX’ : Rerun the program will close the service and restart the program.
  • Update ‘tech’ Application (Ctrl + F5) : This function is usually performed after your code has changed. The corresponding operation for this function is in the service configuration, as shown in Figure 2.3.
  • Resume Program (F9) : For example, if you have two breakpoints at line 20 and line 25, you are currently running at line 20, press F9 to run to the next breakpoint (line 25), press F9 to run the entire process, because there are no breakpoints left.
  • Pause Program: Pauses a Program and enables debugging. No specific usage has been found.
  • Stop ‘XXX’ (Ctrl + F2) : Press twice to close the program. Sometimes you will find that when a service is shut down and then restarted, the port is occupied. This is because the service is not completely shut down, and you need to kill all JVM processes.
  • View Breakpoints (Ctrl + Shift + F8) : View all Breakpoints, as covered in a later section.
  • Mute Breakpoints: Mute Breakpoints. After selecting this breakpoint, all Breakpoints turn gray and become invalid. Press F9 to run the program directly. Click again and the breakpoint turns red, valid. If you want to disable only one breakpoint, you can disable Enabled by right-clicking on the breakpoint, as shown in Figure 2.4, and the breakpoint fails.

The Update program, On ‘Update’ actions, is what you do when you perform an Update operation, typically ‘Update classes and resources’, that is, Update class and resource files.

It is often better to work with hot-deployed plug-ins, such as JRebel, so you don’t have to restart the service every time you change the code. How to activate JRebel is enclosed in the final section.

The following On frame deactivation is triggered when the IDEA window loses focus, that is, when you switch from IDEA to the browser, IDEA will automatically Do things for you. Generally, you can set Do nothing. Switching frequently will consume resources.

(figure 2.3)

(figure 2.4)

Third, variable view

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

1. In IDEA, the value of the current variable is displayed after the line where the parameter is located.

(figure 3.1)

**2. Hover the cursor over the parameter to display the current variable information. ** Click to open details as shown in Figure 3.3. I usually use this method. It’s quick and easy.

(figure 3.2)

(figure 3.3)

3. Look in Variables, which shows all the Variables in the current method.

(figure 3.4)

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

(figure 3.5)

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

(figure 3.6)

(figure 3.7)

4. Calculate the expression

The Evaluate Expression mentioned earlier is the Evaluate Expression button in Figure 4.1 (Alt + F8). You can use this action to evaluate the value of an expression during debugging rather than printing information.

(figure 4.1)

Press Alt + F8 or the button. Alternatively, you can select an expression and press Alt + F8 to pop up the Evaluate expression window. Press Enter or Evaluate.

This expression can not only be a general variable or parameter, but also a method, which you can use to see the return value of a method when you call several methods in a single line of code.

(figure 4.2)

2, set the variable, in the calculation of the expression of the box, you can change the value of the variable, so that sometimes it can be very convenient for us to debug the various values of the situation is not.

(figure 4.3)

Five, intelligent into

Think of several methods in a line of code, and how to select only one method to enter. We mentioned using Step Into (Alt + F7) or Force Step Into (Alt + Shift + F7) to get inside a method, but these two operations will be entered in the order of method calls, which is a bit cumbersome.

Smart Step Into (Shift + F7) can be seen in Run, as shown in Figure 5.1

(figure 5.1)

Shift + F7 will automatically locate the current breakpoint line and list the method to enter, as shown in Figure 5.2. Click the method to enter the method.

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

(figure 5.2)

Set breakpoint conditions

By setting breakpoint conditions, the system stops at the breakpoint only when the conditions are met. Otherwise, the system runs directly.

Usually, when we iterate over a large collection or array and set a breakpoint inside the loop, do we want to look at the values of the variables one by one? That must be exhausting, and maybe you missed this and it’s worth a second try.

1. Right-click on the breakpoint to directly set the conditions for the current breakpoint, as shown in Figure 6.1.

(figure 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 conditions for the breakpoint.

Check Log Message to Console to output the current breakpoint line to the console, as shown in Figure 6.3

Check Evaluate and log to Evaluate the expression and print the result to the console before executing this line.

(figure 6.2)

(figure 6.3)

3. Let’s talk about the Filters on the right. These are not commonly used in general.

Instance filters: Enter the Instance ID(as shown in Figure 6.5), but I failed here. I don’t know why. Leave a message for those who know.

Class filters: Class filters by Class name

Pass Count: Used in loops. If the breakpoint is in a loop, this value can be set to stop at the breakpoint as many times as the breakpoint is in the loop.

(figure 6.4)

(figure 6.5)

4, abnormal breakpoint, by setting abnormal breakpoint, in the program to intercept the exception, will automatically locate the abnormal line.

Click the + sign to add Java Exception Breakpoints, as shown in Figure 6.6. Then enter the Exception class for the breakpoint, as shown in Figure 6.7, and you can see the added Exception Breakpoints in Java Exception Breakpoints.

I have added a NullPointerException exception breakpoint, as shown in Figure 6.8. When a NullPointerException occurs, it automatically locates on the NullPointerException line.

(figure 6.6)

(figure 6.7)

(figure 6.8)

Seven, multithreading debugging

Normally we debug in a thread, step by step down. But sometimes when you Debug, you can’t even make another request.

That’s because IDEA defaults to block level ALL at Debug and blocks other threads only when the current Debug thread is finished. You can select Thread in View Breakpoints, as shown in Figure 7.1, and then click Make Default to set the Default option.

(figure 7.1)

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 and switch the other one to enter another Debug thread.

(figure 7.2)

Rollback breakpoint

During debugging, want to go through the process again without making another request?

First of all, take a look at the method call stack, as shown in Figure 8.1. First, the insertDemo method is requested to enter DemoController, and then the Insert method is called.

(figure 8.1)

2. Breakpoint rollback

The IDEA test does not go back line by line or back to the previous breakpoint, but back to the previous method.

There are two ways to roll back. One is the Drop Frame button (Figure 8.2), which steps back according to the method called, including other methods of the tripartite library (undoing the Show All Frames button will display methods of the tripartite library, figure 8.3).

In the second way, select the method to fall back on the call stack method, right-click on Drop Frame(Figure 8.4), go back to the last method call of the method, then press F9(Resume Program), you can see that the Program enters the breakpoint of the method.

However, it is important to note that breakpoint rollback can only retrace the process. Some parameters/data whose state has been changed cannot be reversed to the previous state, such as objects, collections, updated database data, etc.

Figure [8.2]

Figure [8.3]

Figure [8.4]

9. Interrupt Debug

Do you want to interrupt the request at Debug without going through the rest of the process?

Sometimes, when we see the error in the parameter, we do not want to go through the later process, how to interrupt this request. Shut down the service and restart the program? Well, THAT’s what I used to do.

Specifically, I don’t see a way to interrupt the request directly (other than shutting down the service), but you can avoid the subsequent flow by forcing a Return, as shown in Figure 9.1.

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

(figure 9.1)

(figure 9.2)

Sidebar: JRebel activation

So far I have been using JRebel as a hot deployment tool with good results, as configuration files such as Java code or XML can be hot updated. Occasionally, if the service is open for a long time or there are too many files changed, the hot update does not take effect and the service needs to be restarted.

This is just a brief description of a free way to get permanence that I saw on the Internet (not cracked), not sure when this way will not work.

  • The first thing you need is a Twitter account
  • Go to 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 Settings, as shown in Figure 10.3. If Jrebel is not already installed, install the search install Jrebel in Plugins first.

(figure 10.1)

(figure 10.2)

Determine when this approach will not work.

  • The first thing you need is a Twitter account
  • Go to 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 Settings, as shown in Figure 10.3. If Jrebel is not already installed, install the search install Jrebel in Plugins first.