Table of contents
2. Remote debug ordinary JAVA program
3. Remote debug JAVA Web program
4. Remote debug spring boot program
1. What is remote debugging?
Remote debug, that is, a program that can be deployed locally and remotely, is very useful for locating problems in the remote environment. The reason why we say it is the remote environment is not useful for locating bugs in the production environment. This is because remote debugging is usually used during the development and testing phases and is not recommended for use in production environments due to possible performance impact. Of course, using remote debug when there is no other way is one of the most efficient means of locating production problems.
Remote debug can be understood as a JVM specification. Of course, it is not a separate specification. It is part of the JDWP (Java Debug Wire Protocol) protocol, which defines the interaction between the Java virtual machine (JVM) and the debugger. Way. Through JDWP, developers can set breakpoints, view variables, execute codes and other operations in the development tools to debug running Java programs. To put it bluntly, the JVM that follows the protocol supports debugging and remote debugging.
2. Remote debug ordinary JAVA program
2.1. Environment
JDK:1.8
Compiler: IDEA
2.2. Test procedure
Here we wrote a very simple test program, every 1 second, i increments by 1, and then packs it into a jar package
public static void main(String[] args) throws InterruptedException {
int i=0;
while (true){
Thread.sleep(1000);
i++;
}
}
I guess many students will forget how to package ordinary java se programs into jars in IDEA. Here is a little bit more intimate, and the packaging method is given. Use the maven plug-in for packaging jar packages, specify the main entry, and then install:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.eryi.Test</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
2.3. Program start command
To remotely debug, firstly, use parameters to enable remote debug when the program starts, and complete the parameter instructions as follows:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=<host>:<port>
transport
: Specify the way of debugging information transmission, generally useddt_socket
.server
: Specify whether it is a debugging server, use toy
indicate yes.suspend
: Specify whether to pause at startup, usen
means not to pause.address
: Specify the host and port for debugging and monitoring, that is, remote debugging is performed through this port.
Taking our above program as an example, the completed startup command is:
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=127.0.0.1:5005 -jar test-1.0-SNAPSHOT.jar
If the startup is successful, the port that is listening to debug will be displayed:
2.4. Compiler configuration
We want to debug remotely in IDEA, we need to configure:
add configurations
add new configuration—>remote
Configuration:
Start debugging:
You can see that the program has entered a breakpoint.
3. Remote debug JAVA Web program
As a commonly used web server, tomcat naturally supports the remote debug function for ease of use. Ordinary java programs are started with java -jar, and tomcat is started with a startup script. The startup script records all the commands to be executed when starting tomcat, and the remote debug is also written in it.
Modify start.sh in the Windows environment, and add the following command to the first line:
export CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=<host>:<port>,server=y,suspend=n"
Modify start.bat in the Linux environment, and add the following command to the first line:
set "CATALINA_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=<host>:<port>,server=y,suspend=n"
4. Remote debug spring boot program
As a jar, the spring boot project can be debugged by using java -jar and then starting the debug of ordinary Java programs with parameters. In addition, spring boot also provides a more convenient way of remote debugging, which is Configure the parameters in the spring boot packaging plugin.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=<host>:<port></jvmArguments>
</configuration>
</plugin>
</plugins>
</build>