Home  >  Article  >  Java  >  Graphical introduction to how to use Debug in Intellij IDEA

Graphical introduction to how to use Debug in Intellij IDEA

黄舟
黄舟Original
2017-09-06 09:50:233228browse

Debug is used to track the running process of the code. Usually exceptions occur during the running of the program. Enabling Debug mode can analyze and locate the location of the exception and the changes in parameters during the running process. Usually we can also enable Debug mode to track the running process of the code and learn the source code of the third-party framework.

So let’s learn how to use Debug in Intellij IDEA, mainly including the following content:

1. The beginning of Debug

2. Basic usage & shortcut keys

3, variable View

4. Calculation expression

五, smart step enters

## Six, breakpoint conditions settings

## Seven, multi -threaded Debugging

8. Rollback Breakpoint

9. Interrupt Debug

10. Attachment: JRebel Activation

1. Debug Opening

First, let’s take a 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. This is Windows, and the icons may be slightly different from the Mac icons. Let’s simply talk about the 8 places marked in the picture below:

① Start the service in Debug mode, and a button on the left starts it in Run mode. During development, I usually start the Debug mode directly to facilitate debugging the code at any time.

 ② Breakpoint: Left-click in the line number column on the left, or use the shortcut key Ctrl+F8 to set/cancel a 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 automatically activated. If it is not activated automatically, you can go to settings to set it, as shown in Figure 1.2.

 ④ Debugging buttons: There are 8 buttons in total. The main functions of debugging correspond to these buttons. You can view the corresponding shortcut keys by hovering the mouse over the buttons. The same corresponding functions can be found in the menu bar Run, as shown in Figure 1.4.

 ⑤ Service button: You can close/start services, set breakpoints, etc. here.

 ⑥Method call stack: All the methods passed by the thread for debugging are displayed here. If you check the [Show All Frames] button in the upper right corner, the methods of other class libraries will not be displayed, otherwise there will be a Lots 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 them

 [Figure 1.1]

 

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

 

 [Figure 1.3]: If your IDEA There is no toolbar or status bar displayed at the bottom. It can be opened in View. Displaying the toolbar will facilitate our use. You can try these four options yourself.

 

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

 

2. Basic Usage & Shortcut Keys

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

 1. Let’s talk about the first group of buttons first. There are 8 buttons in total. They are as follows from left to right: Execution Point (Alt + F10): If your cursor is on another line or page, click this button to jump to the line where the current code is executed.

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

  > Step Into (F7): Step into. 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 official class library method, such as line 25. put method.

  > 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 method you stepped into to the method calling point. At this time, the method has been executed, but the assignment has not been completed.

  > Drop Frame (default none): rollback breakpoint, detailed 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 Breaking point.

  > Evaluate Expression (Alt + F8): Calculation expression, described in detail in the following chapters.

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

[Figure 2.2]

Rerun 'xxxx': Rerun The program will shut down the service and then restart the program.

  > Update 'tech' application (Ctrl + F5): Update program, usually this function can be executed after your code has been 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, you have two breakpoints at line 20 and line 25. You are currently running to line 20. Press F9 to run to the next breakpoint. (i.e. line 25), and then press F9 to run the entire process because there are no breakpoints later.

  > Pause Program: Pause the program and enable Debug. No specific usage has been found yet.

  > Stop 'xxx' (Ctrl + F2): Press twice in succession to close the program. Sometimes you will find that when you close the service and start it again, it will report that the port is occupied. This is because the service is not completely closed, so you need to kill all JVM processes.

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

   > Mute Breakpoints: Mute breakpoints. After selecting this, all breakpoints will turn gray and the breakpoints will become invalid. Press F9 to run the program directly. Click again and the breakpoint turns red and is 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, and the breakpoint of this line will be disabled.

[Figure 2.3]: Update program, On 'Update' actions, what is done when performing the update operation, generally select 'Update classes and resources', that is, update classes and resource files.

It is generally better to use 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.

                                                 IAL FRAMEIAL It is relatively resource consuming.

      

                 2.4]   

   

        

##                 ulation 2.4】   

## During the debugging process, it is very necessary to track and view changes in variables. Here is a brief introduction to several places where variables can be viewed in IDEA. I believe most people understand it.

 1. As follows, in IDEA, the value of the current variable will be 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 the details as shown in Figure 3.3. I usually use this method, it's quick and convenient.

 

[Figure 3.2]

 

[Figure 3.3]

 3. View in Variables, which displays all variables in the current method.

 

[Figure 3.4]

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

 

[Picture 3.5]

If you find that you don’t have Watches, it may be where the picture below is.

 

[Figure 3.6]

 

[Figure 3.7]

IV. Calculation expression

The previously mentioned calculation expression is shown in 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 the information.

 

[Figure 4.1]

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

 This expression can not only be 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.

  [Figure 4.2]

 2. Set variables. In the calculation expression box, you can change the value of the variable, which sometimes makes it convenient for us to debug various It's not worth it.

  [Figure 4.3]

5. Smart Step

Think about it, there are several methods in a line of code, how can you only choose Enter in a certain way. I mentioned before that you can use Step Into (Alt + F7) or Force Step Into (Alt + Shift + F7) to enter the method, but these two operations will be entered sequentially according to the method calling order, 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

  [ Figure 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 the method to enter the inside of the method.

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

  [Figure 5.2]

6. Breakpoint condition setting

By setting breakpoint conditions, the system will stop when the conditions are met. at breakpoint, otherwise run directly.

Usually, when we are traversing a relatively large collection or array, we set a breakpoint within the loop. Do we have to look at the values ​​of the variables one by one? It must be tiring, and maybe you missed it 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.

  [Figure 6.1]

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

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

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

If you check Evaluate and log, you can calculate the expression when executing this line of code value and output the result to the console.

 [Picture 6.2]

 

 [Picture 6.3]

 

 3. Let’s talk about the one on the right Filters filter, these are not commonly used in general, let’s briefly explain their meaning.

Instance filters: Instance filtering, enter the instance ID (instance ID in Figure 6.5), but I didn’t succeed here. I don’t know why. Friends who know it can leave a message.

Class filters: Class filtering, filtering based on class name, also failed....

Pass count: used in loops, if the breakpoint is in the loop, you can set this value, After how many times the loop stops at the breakpoint, subsequent loops will stop at the breakpoint.

 [Figure 6.4]

 

 [Figure 6.5]

 

 4. Exception breakpoint, By setting exception breakpoints, 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 requires breakpoints, as shown in Figure 6.7. You can then see the added exception breakpoints in Java Exception Breakpoints.

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

 [Picture 6.6]

 

 [Picture 6.7]

 

 [Picture 6.8]

 

7. Multi-thread debugging

Normally when we debug, we do it in one thread and go down step by step. But sometimes you will find that when debugging, you can't even initiate another request?

That’s because IDEA’s default blocking level when debugging is ALL, which will block other threads. Other threads will only run when the current debugging thread finishes. 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.

 [Figure 7.1]

 

To switch threads, in the drop-down list of Frames in Figure 7.1, you can switch the current thread, as shown below I have two here Debug thread, switch to another one and enter another Debug thread.

 [Figure 7.1]

 

8. Rollback breakpoint

When debugging, you want Go through the process again without making another request?

1. First get to know the call stack of this method, as shown in Figure 8.1. First, request to enter the insertDemo method of DemoController, and then call the insert method. We will ignore other invokes. The top method is where the current breakpoint is. Methods.

 [Figure 8.1]

 

 2. Breakpoint rollback

The so-called breakpoint rollback is actually a rollback to the previous At the beginning of a method call, the test in IDEA cannot rewind line by line or return to the previous breakpoint, but returns to the previous method.

There are two ways to roll back. One is the Drop Frame button (Figure 8.2), which gradually rolls back according to the method called, including other methods of the third-party class library (cancel the Show All Frames button will display the third-party class Library method, as shown in Figure 8.3).

The second method is to select the method to be rolled back on the call stack method, right-click and select Drop Frame (Figure 8.4), roll back to the previous method call of the method, and then press F9( Resume Program), you can see that the program has entered the breakpoint of this method.

But there is one thing to note. Breakpoint rollback can only go through the process again. If the status of some parameters/data has changed before, it cannot be rolled back to the previous state, such as objects and collections. , updated database data, etc.

 Graph[8.2]

 

 Graph[8.3]

 

  Graph[8.4]

 

9. Interrupting Debug

Want to interrupt the request when debugging and not go through the remaining process?

Sometimes, after we see that the parameters passed in are incorrect, we don’t want to go through the subsequent process. How to interrupt this request (the subsequent process will delete the database data...), is it necessary? Close the service and restart the program? Well, I've done that before.

To be precise, I have not found a way to directly interrupt the request (other than shutting down the service), but you can avoid subsequent processes through Force Return, that is, forced return, as shown in Figure 9.1.

Click Force Return, and the Return Value window pops up. The return type of my method is Map, so I directly return results here to force the return, so that no further steps are performed. Or you can new HashMap<>().

 [Picture 9.1]

 

 [Picture 9.2]

 

10. Attachment: JRebel activation

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

Here is just a brief description of a way I saw on the Internet to obtain permanent use rights for free (non-cracked). I am not sure when this method will no longer be available.

 ① First you need a Facebook or Twitter account (Twitter is best)

 ② 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 search and install the JRebel plug-in in Plugins.

 [Figure 10.1]

 

 [Picture 10.2]

 

 [Picture 10.3]

 

The above is the detailed content of Graphical introduction to how to use Debug in Intellij IDEA. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn