preface

Last weekend, a friend celebrated his birthday. I overheard him talking about the recent situation. He said that there were too many projects in the company, and a lot of bugs needed to be fixed every day.

With more and more projects, especially friends in outsourcing companies, they may need to switch two or three projects every day. Should we start the project debugging locally whenever there is a problem?

This article will introduce what is remote debugging, Spring Boot how to enable remote debugging?

What is remote debugging?

So-called remote debugging is the server program running on a remote server, we can in the local server code (premise is local code must be consistent code) and the remote server is running a breakpoint, whenever there is a request to the remote server always able to locally know remote server’s internal state.

Simple meaning: The ability to debug server-side code in real time without having to start the project locally.

Why debug remotely?

As the size of the project increases, so does the startup time. Why spend ten minutes trying to start a project just to fix a BUG? Aren’t you afraid the boss will scold you?

What is JPDA?

JPDA(Java Platform Debugger Architecture) is a Java Platform debugging system, as shown in the following figure:


The main protocol for debugging is JDWP. Before Java SE 5, the IMPLEMENTATION Interface of JVM is JVMPI(Java Virtual Machine Profiler Interface), while in Java SE 5 and later versions, Use JVMTI(Java Virtual Machine Tool Interface) to replace JVMPI.

So, if you are using Java SE versions prior to Java SE 5, use the following debugging command format:

java -Xdebug -Xrunjdwp:...
Copy the code

If you are using Java SE 5 or later, the command format is as follows:

java -agentlib:jdwp=...
Copy the code

How do I enable remote debugging?

Since most versions of Java SE 5 are in use today, the previous ones are ignored.

The most common commands to enable remote debugging are as follows:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9093 -jar xxx.jar
Copy the code

Java-agentlib: JDWP = is the base command, followed by a string of optional arguments. Details below.

transport

Specifies the communication protocol between the running application being debugged and the debugger. The following are optional values:

  1. dt_socket: in this paper,socketMode connection (common)
  2. dt_shmem: Uses the shared memory mode to connect, limited support, only supports the Windows platform

server

Specifies whether the current application acts as a debug server or client. The default value is n (client).

If you want the current application to be debugged, set the value to y; If you want to use the current application as the client, as the debugger, set this value to n.

suspend

Whether to block the current application until it is connected after it is started. The default value is Y (block).

In most cases this value should be n, meaning there is no need to block waiting for the connection. One possible application scenario is y, where your program starts with a bug, and in order to debug, the program must wait for the debugger to connect.

address

Exposed port. The default value is 8000

Note: This port cannot be the same as the project port, and is not occupied and open to the public.

onthrow

This parameter means that debugging is interrupted when the program throws the specified exception.

onuncaught

Whether to interrupt debugging when a program throws an uncaught exception. The default value is n.

launch

The program to execute when debugging is interrupted.

timeout

Timeout in ms (ms)

When suspend = y, this value represents a timeout waiting for a connection; When suspend = n, this value represents a usage timeout after a connection.

Common Commands

Here are some common reference commands to make it easier to understand.

  1. In order toSocketWay to monitor8000Port, the program starts blocking (suspendThe default value ofy) until connected, the command is as follows:
-agentlib:jdwp=transport=dt_socket,server=y,address=8000
Copy the code
  1. In order toSocketWay to monitor8000Port, when the program is started5Seconds no debugger connection terminates, program starts blocking (suspendThe default value ofy) until connected.
-agentlib:jdwp=transport=dt_socket,server=y,address=localhost:8000,timeout=5000
Copy the code
  1. Select an available shared memory connection address and use itstdoutPrint, program start does not block.
-agentlib:jdwp=transport=dt_shmem,server=y,suspend=n
Copy the code
  1. In order tosocketWay to connect tomyhost:8000To initiate blocking until the connection is successful.
-agentlib:jdwp=transport=dt_socket,address=myhost:8000
Copy the code
  1. In order toSocketWay to monitor8000Port, the program starts blocking (suspendThe default value ofy) until connected. When thrownIOExceptionInterrupts debugging and executes insteadusr/local/bin/debugstubThe program.
-agentlib:jdwp=transport=dt_socket,server=y,address=8000,onthrow=java.io.IOException,launch=/usr/local/bin/debugstub
Copy the code

How do I enable remote debugging of IDEA?

First, run the packaged Spring Boot project on the server and run the following command (you can configure the parameters according to the actual situation) :

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9193 -jar debug-demo.jar
Copy the code

After the project is launched successfully, click Edit Configurations, click the + in the popup box, and then select Remote.


Then fill in the address and port of the server and click OK.


After the configuration is complete, click DEBUG to DEBUG and run.


After configuration, click Save, because I configured suspend=n, so the server program does not have to block waiting for our connection. We click the IDEA debug button, and when I visit a certain interface, I can debug normally.


conclusion

A little knowledge every day, did you learn today?