Today’s sharing started, please give us more advice ~

This article mainly describes some basic knowledge and common skills of Idea debugging and how to perform remote debugging. Debugging code is something every programmer does every day. It is a very effective way to eliminate bugs and check your code logic.

The environment

The software environment involved in this paper is shown in the following table:

The little Secrets of Debugging

If you want to do a good job, you must first sharpen your tools. To properly use the Debug function of Idea, you need to have a good understanding of it.

Call out the Debug window

If your Idea does not display the Debug window, you can do the following in Settings: Select Show Debug Window on Breakpoint

Learn about the Debug window of the IDEA

This shows a window for a normal-looking Debug. Eight sections are marked, and the text of these eight sections is explained as follows:

Part 1: Start: Start the program in Debug mode. Debug occurs only when the program is run in Debug mode

Part 2: Breakpoints: Click the left button in the line number bar, or the shortcut key Ctrl+F8 to open/cancel breakpoints, breakpoint line color can be set by yourself

Part 3: Debug Window: The Debug window is automatically activated when the access request reaches the first breakpoint. If it is not automatically activated, you can set it in Settings.

Part four: This part has seven buttons,

The function is to restart the program, view all breakpoints, and disable breakpoints. The functions of each button are described as follows:

Part five: There are eight buttons in total. The functions of each button are as follows:

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

  • Step Over (F6) : However, go down line by line. If there is a method on this line, it will not enter the method.

  • Step Into(F7) : if the current line has a method, can enter the method inside, generally used to enter a custom method, not Into the official class library method

  • Force Step Into (Alt + Shift + F7) : Force Step Into (Alt + Shift + F7) : Force Step Into (Alt + Shift + F7)

  • Drop Frame (default none) : rollback breakpoint

  • 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, more on this later.

Part 6: Method call stack, click Show All Frames to Show only custom methods for the business.

Part 7: Variables (Variables area), in the Variables area you can see the value of the variable before the breakpoint.

Part EIGHT: Watches Viewing Variables. You can drag Variables from the Variables area to Watches to view them.

Computed expression

Press Alt+F8 or click to invoke the evaluated expression box. As shown below:

As shown above, in the expression box we can enter the statement we want to run. For example, we can write user.getUsername () to get useName.

Of course, you can also modify the existing value, for example, the current userName from Zhang SAN to Li Si, make it effective and continue to run. This function is used to change the value passed in to the expected value to continue the test.

Intelligence into

If multiple methods are called in a line of code and we don’t want to run them individually, we can select the current line of code and press Shift+F7 to invoke the smart walk in function

Such as: If (stringutils.isnotBlank (user.getUsername ()))) calls two methods. After invoking the intelligent step in, we can choose which method to execute. We can select isNotBlank, You can also choose the getUserName method. Is it very easy to use?

Breakpoint condition setting

Breakpoint conditional setting is also a very useful feature. Imagine a scenario like this: I have a large collection and I want to find an object in that collection. How do I break it? Of course not! Select user.getid ()>0 from userList where user.getid ()>0

Multithreaded debugging

When we Debug a method, other requests cannot come in. This is because other requests are blocked by default in Debug mode.

Of course, this can be adjusted by simply selecting Thread on the breakpoint. If Thread is set to Default, other requests are not blocked during debugging.

Remote debugging

For some problems that cannot be reproduced locally, we can reproduce them in the test environment or online environment by means of remote debugging.

steps

In fact, the operation procedure is very simple. You only need to set up remote connection in Idea and start the service in Debug mode in the Debug server.

Set up remote connections in Idea

I added a Remote named testng-spring-boot to my Idea. Once added, you can use most of the default Settings, but note that you need to set the address of the Debug server in Host. Also, if the default listening port 5005 conflicts with your project’s port, you may need to change it.

After setting up the Idea, start the service in Debug mode on the Debug server using the following command:

Among them: testng – spring – the boot – demo – 0.0.1 – the SNAPSHOT. Jar jar package name of the project, you need to replace your jar package name.

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

This section is a direct copy of the default command generated in IDEA. This command is used in jdk5-8. If your JDK version is higher than 8, you can switch the command as follows

Where JDWP: tells the JVM to run the debug environment using Java Debug Wire Protocol. Parameters also have a series of debugging options: The startup result of the project is shown below:

Listening for transport dt_socket at address:5005

The Settings can now be debugged normally by selecting Remote from the previous Settings and clicking as follows

The debugging result is as follows: When the /v1, user, or GET interface is accessed, logs can be generated on remote Debug.

At the same time, breakpoints can be entered normally.

How remote debugging works

Said how to carry out remote debugging, is not still not satisfied, it does not matter, the following is to introduce the principle of remote debugging.

We all know that Java applications run on the JVM (virtual machine) and run in the following steps: Java files —–> compiled generated class files (class files) ——>JVM loaded class files —->JVM run bytecode files ->JVM translators translate into different machine code that each machine knows.

Because Java programs run in the JVM as bytecode, virtual machines on different platforms use the same program storage format. So Java program has a good cross-platform, also provides a theoretical basis for our remote debugging. Because of the uniform bytecode file,

So, you only need the local class files to be consistent with the remote server class files, and the two JVMS communicate through debugging protocols, the default being over sockets.

conclusion

This paper first gives a detailed description of the Debug of Idea, which is more scattered and abundant. However, if you can master the words of our debugging efficiency will be greatly improved. Finally, remote debugging is introduced. Hope to help you.

Today’s share has ended, please forgive and give advice!