Development Tools Lecture 26: Using IDEA for Local Debugging and Remote Debugging

Development Tools Lecture 26: Using IDEA for Local Debugging and Remote Debugging

Debug is used to track the running process of the code. Usually, an exception occurs during the running of the program. Enabling the Debug mode can analyze and locate the location of the abnormality and the change of parameters during the running process; and in the actual troubleshooting process, Remote Debug will also be used. This article is the twenty-sixth lecture on development tools, mainly introducing the techniques of Debug and Remote Debug based on IDEA.

1. The beginning of Debug

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. Here I am a MacBook, and the idea version is 2022.1.1. The icon may be slightly different from that of Windows. Just briefly talk about the 8 places marked in the picture below:

  • ① Start the service in Debug mode, and the button on the left is to start in Run mode. During development, the Debug mode is usually started directly to facilitate debugging the code at any time.
  • ② Breakpoint: Click the left button in the line number column on the left, and 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 to set it, as shown in Figure 1.2.
  • ④ Debug button: There are 8 buttons in total. The main function of debugging corresponds to these buttons. You can view the corresponding shortcut keys by hovering the mouse over the buttons. You can find the same corresponding function in the menu bar Run, as shown in Figure 1.4.
  • ⑤ Service button: You can close/start the service here, set breakpoints, etc.
  • ⑥ Method call stack: This shows all the methods that the thread debugged. Check the [Show All Frames (display all frames)] button in the upper right corner, and the methods of other class libraries will not be displayed , otherwise there will be a lot of methods here.
  • ⑦ Variables: In the variable area, you can view the variables in the current method before the current breakpoint.
  • ⑧ Watches: View variables, you can drag the variables in the Variables area to Watches to view
    insert image description here

Check "Show debug window on breakpoint" in the settings, and the Debug window will be activated automatically after the request enters the breakpoint
insert image description here

If the toolbar or status bar is not displayed at the bottom of your IDEA, you can open it in View, and displaying the toolbar will be convenient for us to use. You can try these four options yourself.

insert image description here

In the menu bar Run, there are functions corresponding to debugging, and you can view the corresponding shortcut keys at the same time.

insert image description here

For a detailed explanation of Idea, please refer to this article: Development Tools Lecture 1: Summary of Intellij Idea Skills

2. Basic usage & shortcut keys

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

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

img

  • Show Execution Point Show Execution Point(Option + F10): If your cursor is on another line or other page, click this button to jump to the line where the current code is executed.
  • Step over Step Over(F8): Go down line by line, if there is a method on this line, it will not enter the method.
  • Step into Step Into(F7): 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.
  • Forced step Force Step Into(Option + Shift + F7): can enter any method, when viewing the underlying source code, you can use this method to enter the official class library.
  • Step out Step Out(Shift + F8): 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): Fallback breakpoint, detailed in the following chapters.
  • Run to the cursor Run to Cursor(Option + F9): 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 a break point.
  • Option calculation expression Evaluate Expression(Option + F8): detailed description in the following chapters.

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

img

  • Rerun 'xxxx'(command + R): 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 has been changed. The operation corresponding to this function is in the service configuration, as shown in Figure 2.3.
  • Resume Program(Option + command + R): Resume the program. For example, you have two breakpoints on line 20 and line 25, and you are currently running to line 20. Press F9 to run to the next breakpoint (that is, line 25). Press F9 again to run the entire process, because there is no breakpoint behind.
  • Pause Program: Suspend the program and enable Debug. No specific usage has been found so far.
  • Stop 'xxx'(command + F2): Press twice in succession to close the program. Sometimes you will find that when you close the service and restart it, the port is reported to be occupied. This is because the service is not completely closed, so you need to kill all JVM processes.
  • View Breakpoints(command + Shift + F8): View all breakpoints, which will be covered in later chapters.
  • Mute Breakpoints: Dumb breakpoint, after selecting this, all breakpoints will become gray, and the breakpoints will be invalid. Press F9 to run the program directly. Click again, the breakpoint turns red, valid. If you only want to disable a certain breakpoint, you can right-click on the breakpoint to cancel Enabled, and the breakpoint of this line will become invalid.
  • update program

On 'Update' actions, what to do when an update operation is performed, generally selected 'Update classes and resources', that is, to update class and resource files.

Generally, it is better to cooperate with hot deployment plug-ins, such as JRebel , so that there is no need to restart the service every time the code is changed. How to activate JRebel, you can refer to Lecture 27 of this article Development Tools: JRebel activation and use .

The following On frame deactivationis 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, and frequent switching will consume more resources.

img

img

3. Variable view

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

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

insert image description here

  • When the cursor hovers over the parameter, the current variable information is displayed. Click to open the details as shown below. I usually use this method, which is fast and convenient.
    insert image description here

insert image description here

  • View in Variables, all variables in the current method are displayed here.
    insert image description here

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

img

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

img

img

4. Calculation expression

The calculation expression mentioned above is the button in the figure below, Evaluate Expression (Option + F8). You can use this operation to calculate the value of an expression during debugging without having to print information.

img

  • Press Option + F8 or the button, or you can select an expression and press Option + F8 to pop up the calculation expression window, as follows, 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 one line of code, you can check the return value of a method in this way.
insert image description here

  • Set the variable, in the calculation expression box, you can change the value of the variable, so sometimes it is very convenient for us to debug the situation of various values.

insert image description here

5. Smart access

Think about it, there are several methods in a line of code, how to choose only one method to enter. As mentioned before, use Step Into (Option + F7) or Force Step Into (Option + Shift + F7) to enter the method, but these two operations will be entered in sequence according to the order of method calls, which is 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 below

insert image description here

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 interior of the method.

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

img

6. Breakpoint condition setting

By setting the breakpoint condition, when the condition is met, it will stop at the breakpoint, otherwise it will run directly.

Usually, when we are traversing a relatively large collection or array, we set a breakpoint in the loop. Do we want to see the values ​​​​of the variables one by one? It must be very tiring, maybe you still missed this and it is worth doing it again.

  • Right-click on the breakpoint to directly set the condition of the current breakpoint , as shown in the figure below, the breakpoint will only take effect when the orderIdList is not empty.

insert image description here

  • Click View Breakpoints (Command + Shift + F8) to view all breakpoints

    • Java Line Breakpoints displays all breakpoints, check Condition on the right, and set the condition of the breakpoint.
    • 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.

insert image description here

img

  • 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 filtering, input the instance ID (the instance ID in the figure below), but I didn’t succeed here, I don’t know why, please leave a message if you know it.
    • Class filters: Class filtering, filtering based on class names, also unsuccessful...
    • Pass count: used in the loop, if the breakpoint is in the loop, you can set this value, stop at the breakpoint after looping many times, and the subsequent loops will stop at the breakpoint.

insert image description here

img

  • Exception breakpoint, by setting an exception breakpoint, when an exception that needs to be intercepted occurs in the program, it will automatically locate the exception line .

As shown in the figure below, click the + sign to add Java Exception Breakpoints to add an exception breakpoint. Then enter the exception class that needs a breakpoint
insert image description here

After that, you can see the added exception breakpoint in Java Exception Breakpoints.

insert image description here

A NullPointerException exception breakpoint is added here. After a null pointer exception occurs, it is automatically located at the null pointer exception line.

  • will reduce the single test speed

img

7. Multi-thread debugging

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

That's because IDEA's default blocking level is ALL when debugging, which will block other threads, and only run 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.

img

Switch threads. In the drop-down list of Frames in the figure below, you can switch the current thread. As shown below, I have two Debug threads here, and switching the other one will enter another Debug thread.
insert image description here

8. Fallback breakpoint

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

  • First of all, get to know the call stack of this method . As shown in the figure, first request to enter the insertDemo method of DemoController, and then call the insert method. Let’s ignore other invokes. The top method is the method where the current breakpoint is located.

img

  • breakpoint fallback

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, which rolls back step by step according to the method called, including other methods of the third-party class library
img

Canceling the Show All Frames button will display the methods of the third-party class library
img

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

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

9. Interrupt Debug

Do you want to interrupt the request during debugging and stop going through the rest of the process?

Sometimes, after we see that the parameters passed in are wrong, we don’t want to go through the subsequent process. How to interrupt this request (the subsequent process will delete the database data...), should we close the service and restart the program?

To be exact, I haven't found a way to directly interrupt the request (except for shutting down the service), but you can avoid the subsequent process through Force Return, that is, force return, as shown in the figure

insert image description here

Click Force Return, and the Return Value window will pop up. The return type of my method is Map, so I return the results directly here to force the return, so as not to proceed with the subsequent process. Or you can new HashMap<>().

insert image description here

10. Remote debugging (Remote Debug)

Sometimes, there is no problem when debugging locally, but a bunch of inexplicable problems pop up when it is packaged and deployed to the test environment . What should I do at this time?

10.1. Run server code with specific JVM parameters

To allow the code running on the remote server to support remote debugging, you must add specific JVM parameters when starting. The parameters for jdk9 or higher versions are :

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

If it is just temporary debugging, do not add an IP address that restricts access in front of the port number. After the debugging is completed, remove the above JVM parameters and re-release to prevent the security risks that may be caused by opening the remote debugging port.

10.2. Local connection to remote server debug port

Open Intellij IDEA, select "Edit Configurations..." at the top right, click the + sign after entering, and select "Remote", just fill in the content in the red box according to the figure below, where Name fills in the name, here is remote webserver, host is the ip/hostname of the machine where the remote code is running, and port is the debug_port specified in the previous step. Then click Apply, and finally click OK

insert image description here

Now select the remote webserver created in the previous step in the drop-down box of "Edit Configurations..." in the previous step, and then click the debug button on the right to see the console log. If something like "Connected to the target VM, address: ''10.112.100.190:5005', transmission: 'socket''' appears, it means that the connection has been successful. The actual content displayed here is as follows:
insert image description here

10.3. Set a breakpoint and start debugging

The remote debug mode has been enabled, and now you can break points in the code that needs to be debugged, such as:

img

As shown in the figure, if there is a √ in the breakpoint, it means that the selected breakpoint is correct.

Now send a request to the remote server locally, look at the bug interface of the local console, scroll to the debugger tab, you can see that the internal state (various variables) of the current remote service has been displayed, and the variable value of the line is also displayed at the place where the breakpoint was set just now.

img

Remarks: It should be noted that the code used for remote debugging must be exactly the same as the code deployed remotely, and no modification can occur, otherwise the marked breakpoint will not be hit , remember to remember.

11. Reference articles

Guess you like

Origin blog.csdn.net/qq_28959087/article/details/131796418