IDEA remote Debug debugging, you don't need it, but you must know it

foreword

Remote debugging is really useful. My personal real experience is that once, my newly developed function did not take effect in production. I checked the logs and found no problems with the data. Finally, I used remote debugging to find that my newly developed code did not exist.
So it doesn't matter if you don't need it, but you have to do it as an emergency measure.

Source: This article is reprinted, revised, and supplemented based on the article " Remote Debug Debugging of IDEA, Direct Online Show Operation"

configuration

IDEA settings

The settings of the IDEA of the higher and lower versions may have a slightly different interface. I use 2020.1.1. Roughly the same, explore by yourself.

  1. Select Edit Configuration
    picture

  2. Click the plus sign and select Remote
    insert image description here

  3. See figure for detailed configuration
    insert image description here

Note: Be careful that the port is not occupied. The subsequent port is used to communicate with the remote java process.

It can be noticed that the command to start the project generates different scripts according to switching different jdk versions

Select jdk1.4, then it is

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=50055

This is why you search other blogs, there will be such a configuration, in fact, this configuration is also feasible. But more accurately, it should follow the configuration of jdk5-8 below

Select jdk 5-8, then it is

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

Select jdk9 or above, then it is

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

It is said that because jdk9 has become safe, remote debugging is only allowed locally, if you want to remote, you need to configure * before the port

Startup script retrofit

Use the Command line arguments for remote JVM obtained by idea configuration, that is, -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=50055

The modified startup script is as follows

nohup java \
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=50055 \
-jar remote-debug-0.0.1-SNAPSHOT.jar &

Note that ^ is used for line breaks in windows, for example

java ^
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=50055 ^
-jar remote-debug-0.0.1-SNAPSHOT.jar

illustrate:

  1. The port can be set at will, even if it is not occupied, but it must be consistent with the port set in remote in IDEA! Copy other parameters. For detailed parameter explanations, please refer to the appendix or search by yourself

  2. remote-debug-0.0.1-SNAPSHOT.jar is changed to your own jar package name

  3. The script I gave runs in the background. If you don’t need to run in the background, remove nohup and & by yourself

  4. Select the one just configured in IDEA to start
    picture

detail

Detail 1: Stop at a local breakpoint, will it continue to execute after closing the program?

If the remote debugging stops at its own breakpoint, and the project in IDEA is closed to stop running, will it continue to run and execute the rest of the logic? Yes, it's not easy to remember

Taking the following code as an example, it stops at the first line. Then it stopped in IDEA, and found that the console still printed the remaining logs after the stop.

picture

Detail 2: What happens if the jar package code is inconsistent with the local one?

What happens if the code in IDEA is not consistent with the jar package.

Conclusion: Ensure that the code is consistent with the remote start.

Otherwise, the number of lines will not match when you debug. Reporting an error and throwing an exception will not. It can still match the number of lines like this

For example, if you debug the test1 method, the test2 method is under test1, and adding code in test2 will not affect the line number in test1, which can accurately reflect the line number during debugging. (I also found that the production code in the details is inconsistent with my local code)

Detail 3: Where is the log printed?

The log will not be printed on IDEA's console. That is, System.out and log.info are still printed remotely.

Detail 4: Will others get stuck when debugging?

During remote debugging, if a breakpoint is hit, will the page request be stuck after stopping?

For example, if you use remote debugging, other QAs are testing this page, what is the result they see? Will it get stuck?Yes, this has actually happened to me.

Detail 5: The local code fixes the bug, will the remote call be executed?

If you find a bug in the remote debugging process, restart the project in IDEA after the local modification, and call it again on the page, can it be fixed?No, the code in the remotely deployed jar is still running

Detail 6: The problem of local Drop Frame (Reset Frame)

Regarding the problem of drop frame, if the drop frame is re-debugged, will 2 records be inserted?
Let me talk about it firstWhat is Drop Frame, view the application's call stack in the debugger of idea, use the Drop Frame function, you can roll back to the previous stack frame, in a sense you can go back in time. If you miss the key point that you want to look at again , which helps to retype the function.Simply put, it returns to the previous calling method.

insert image description here

Some friends may ask: Why can’t I find Drop Frame when I’m debugging?
In fact, this is related to the version. Before the 2022.1 version, it was called Drop Frame, and then it was changed to Reset Frame.

Ok, let’s continue with our question, as shown in the figure below userMapper.insert(eo). This method is not modified with @Transactional. After the mapper method is executed, the transaction will be submitted immediately, and there will be an extra row of records in the library table. If you drop the frame, do it again Debug, execute the code again, and insert another record.
insert image description here

If you add @Transational, there will be no two records, and the transaction is not submitted when Drop Frame is executed, and the insertion code will not be inserted again.

Detail 7: Remote Drop Frame problem

If the above logic of inserting into the database is replaced by a remote interface call, and the same code is executed again after the Drop Frame, will the remote interface be executed twice? Will do.

Guess you like

Origin blog.csdn.net/qq_16607641/article/details/131122053