How to use Debug graphic tutorial in IDEA

How to use Debug graphic tutorial in IDEA

Click on "IT Ranch" above and select "Set as Star" to deliver technical dry goods daily!

Author: bojiangzhou https://www.cnblogs.com/chiangchou

Debug is used to track the running process of the code. Usually an exception occurs during the running of the program. Debug mode can be used 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.

So learn how to use Debug in Intellij IDEA, mainly including the following:

  • Debug opening
  • Basic usage & shortcut keys
  • Variable view
  • Calculation expression
  • Smart step
  • Breakpoint condition setting
  • Multi-threaded debugging
  • Fallback breakpoint
  • Interrupt Debug
  • Attachment: JRebel activation

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:

  1. 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.
  2. Breakpoint: Click the left button in the row 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.
  3. Debug window: After the access request reaches the first breakpoint, the Debug window will be automatically activated. If it is not activated automatically, you can go to the settings to set it, as shown in Figure 1.2.
  4. Debug button: There are 8 buttons in total. 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.
  5. Service button: You can turn off/start services, set breakpoints, etc. here.
  6. 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.
  7. Variables: In the variable area, you can view the variables in the current method before the current breakpoint.
  8. Watches: To view variables, you can drag the variables in the Variables area to Watches to view

[Picture 1.1]

Check Show debug window on breakpoint in the settings, then the Debug window will be automatically activated after the request enters the breakpoint

[PICTURE 1.2]

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.

[PICTURE 1.3]

There are debugging corresponding functions in the menu bar Run, and the corresponding shortcut keys can be viewed at the same time .

[PICTURE 1.4]

2. Basic usage & shortcut keys

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

1. 1. 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 a 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, the method has been executed at this time, but the assignment has not been completed.
  • Drop Frame (None by default): Rewind the breakpoint, which will be 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 turn to gray 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.

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 with hot deployment plug-ins, 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.3]

[PICTURE 2.4]

3. 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 current variable value 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]

4. calculation expression

The aforementioned calculation expression is shown in Figure 4.1, the button, 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 then Alt + F8, the calculation expression window 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]

5. 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]

6. 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 missed 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 in general, 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 then the loop will stop at the breakpoint.

 [PICTURE 6.4]

[PICTURE 6.5]

4. Abnormal breakpoints. By setting an abnormal breakpoint , when an exception that needs to be intercepted occurs in the program, the abnormal 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]

7. multi-threaded debugging

Under normal circumstances, we debug in a thread, step by step. But sometimes you will find that when you want to initiate another request during Debugging, it is impossible to proceed?

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]

8. 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), go back to the last method call of the method, and then 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, and updated database data Wait.

Figure [8.2]

Figure [8.3]

Figure [8.4]

9. 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 that before.

To be precise, I have not found 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 no subsequent processes will be performed. 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 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 (non-cracked) way of obtaining permanent usage rights I saw on the Internet. I'm not sure when this method can't be used.

  • First you need a Twitter account
  • 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!

Dry goods sharing

Recently, I organize personal study notes into a book and share them in PDF. Follow me, reply to the following code, you can get the Baidu disk address, no routine to receive! • 001: "Java Concurrency and High Concurrency Solutions" study notes; • 002: "Deep JVM Kernel-Principles, Diagnosis and Optimization" study notes; • 003: "Java Interview Collection" • 004: "Docker Open Source Book" • 005: "Kubernetes Open Source Book" • 006: "DDD Crash (Domain Driven Design Crash)" • 007: All • 008: Add technical discussion group

Wonderful in the past

spike combat system architecture and analysisoptimize the Boot your the Spring7 Ge IntelliJ IDEA plug-ins necessary to improve the coding efficiencyGoodbye micro-services, from more than 100 questions to a child superstarsystem is running slow, CPU 100%, Full Too many GC times, this trick will help you get it all! 8 common SQL misuses

Reference: https://cloud.tencent.com/developer/article/1467169 How to use Debug in IDEA Graphic and text tutorial-Cloud + Community-Tencent Cloud