1. Introduction

Today, a Bug appeared online, and the pit is that things related to wechat cannot be debugged offline. The traditional approach is to bury various log points in code and redeploy them for debugging, and then analyze them based on the information in the log. If your log is buried improperly, keep changing your code and repackaging it. Is there any SAO operation to avoid the above problem?

2. Remote debugging

There is, of course, a solution: Remote debugging. Remote debugging enables developers to directly diagnose problems on servers or other online processes. It provides a way to trace online runtime errors and determine performance bottlenecks and root causes of problems, allowing you to Debug remote servers as if you were debugging locally. Next we will use the popular Java IDE, IntelliJ IDEA from JetBrains, for remote debugging. To enable remote debugging of the code running on the remote server, specific JVM parameters must be added to boot. These parameters are:

-Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,server=y,address=${debug_port}
Copy the code

The debug_port is a debugging port opened by the server and used for subsequent local configuration.

3. Use IDEA to perform remote debugging

Remote debugging of IntelliJ IDEA is not complicated and can be easily configured through the following steps.

3.1 Setting Local Parameters

Open the configuration panel as shown above and create a new Remote debug panel as follows:

Configure your own server and local environment in the order shown above, then click OK. Ports 2 and 4 are the debug_port numbers that we specify remotely.

3.2 the JDWP protocol

One little tidbit here is the JDWP in parameters. So what is JDWP?

JDWP is short for Java Debug Wire Protocol, which defines the communication Protocol between the Debugger and the target VM. The Target VM runs the Java program we are debugging, just like a normal running JVM, except that the JDWP Agent is loaded at startup to enable debugging. The Debugger is our local debugger that sends instructions to the running Target VM to get the state of the target VM runtime and control the execution of remote Java programs. The Debugger and target VMS run in their own processes and communicate with each other through the JDWP communication protocol.

3.3 Enabling remote Debugging

Click the green beetle button shown in the arrow (Shift + F9) to start debugging, then set the breakpoint of the local code and let the remote logic trigger the breakpoint logic to debug the breakpoint.

Make sure that the local debug code is exactly the same as the remote deployment code and cannot be changed! Otherwise the breakpoint will not hit!

4. Some key points

In addition to the need to ensure code consistency, there are a few other things we need to be aware of. The remote JDWP Agent should be disabled after debugging, that is, the remote parameters should be removed. In addition, remote logs are not mapped locally during debugging, although there are tools available to map remote logs locally to provide more powerful debugging capabilities.

Also remember that while remote debugging is a very powerful tool, it is not a silver bullet! The production environment is not a good target for debugging, so don’t abuse it!

5. To summarize

As I’ve shown in this article, remote debugging using IntelliJ IDEA is simple and can be used in just a few steps. In some cases it solves our problem very conveniently. But it should not be abused, it should be used wisely.

Follow our public id: Felordcn for more information

Personal blog: https://felord.cn