Remote Debug Debug Java code
When the daily environment and the pre-environment encounter problems, you can use the remote debugging method to local break point, debugging locally. Production environment due to network isolation and system stability considerations, remote code debugging is not possible.
The overall process is by modifying the remote service java_opts parameter and then debugging locally through tools such as eclipse or idea.
Below is a brief introduction to the theory.
Theory
JPDA (Java Platform Debugger Architecture) is an abbreviation for the Java Platform Debug architecture. Consists of 3 specifications, namely JVMTI (JVM Tool Interface), JDWP (Java Debug Wire Protocol), JDI (Java debug Interface).
- 1.JVMTI defines the debugging services that a virtual machine should provide, including debug information (information such as stack information), debug behavior (action such as a client setting a breakpoint) and notifications (notification, for example, notifies the client when a breakpoint is reached), This interface is implemented by virtual machine implementations and is combined in a virtual machine
- 2.JDWP defines the communication between the debug service and the debugger, including the definition of the debug Information format and the debug request mechanism
- The 3.JDI defines the debug interfaces that the debugger can use at a high level of language to easily interact with remote debugging services, and the Java language implementation allows the debugger's implementation to access the virtual machine debugging services directly using that interface. The Java Debugging tool, Jdb, is the JDI implementation provided by Sun. Eclipse IDE, Its two plug-ins Org.eclipse.jdt.debug.ui and Org.eclipse.jdt.debug are closely related to their powerful debugging capabilities, the former being the implementation of the Eclipse debugging tool interface, while the latter is a complete implementation of the JDI.
Remote debugging
Remote debugging is divided into active connection debugging, and passive connection debugging. Here's an example of eclipse.
Active connection debugging: Server configuration Monitoring port, local IDE connection remote listening port for debugging, general debugging problems in this way.
Passive connection debugging: The local IDE listens on a port and waits for a remote connection to the local port. Generally used for remote services cannot start, connect to local debug analysis at startup.
Active connection debugging
The remote service configuration startup script is required first:
java_opts= "$JAVA _opts-xdebug-xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000"
If it is the boot jar package, the directive:
Java-xdebug-xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000-jar Test.jar
Here-xdebug is to inform the JVM to work in debug mode,-XRUNJDWP is to notify the JVM to use (Java Debug Wire protocol) to run the debugging environment. Transport is the way to listen for socket ports (you can also dt_shmem shared memory, but limited to Windows machines, and the service provider and debug side can only be on the same machine). Server=y indicates that it is currently the debug server, and =n indicates that it is currently the debug client. Suspend=n is not interrupted at startup (if interrupted at startup, it is typically used for debugging problems that cannot be started). The address=8000 indicates a local listening 8000 port.
After the remote service (Tomcat/jboss) starts successfully, local eclipse places a breakpoint on the place where it needs to be debugged, and then the project right-starts remote debugging: Debug As->debug configurations->remote Java Application. Host for remote host ip,port for remote monitoring debug port, Connection type: Standard (Socket Attach),
Click Debug, and then break the point, the remote service runs to a breakpoint where it is interrupted locally, and then it is debugged.
Passive connection debugging
First requires eclipse configuration monitoring, such as the Eclipse configuration image of the active connection debug, Connection type select: Standard (Socket Listen), configure the local listening port, such as default 8000. Click Debug to start waiting for remote connection debugging.
Then configure the remote service startup script:
java_opts= "$JAVA _opts-xdebug-xrunjdwp:transport=dt_socket,address=127.0.0.1:8000,suspend=y"
If it is a debug jar package, the directive:
Java-xdebug-xrunjdwp:transport=dt_socket,address=127.0.0.1:8000,suspend=y-jar Remoting-debug.jar
parameter meaning and active connection debugging, just here suspend=y means interrupt on startup, need to connect the local IDE debug boot. Address=ip:port,ip needs to be modified to be local to the external IP.
This allows remote projects to be connected to the local when they are started, which makes it easy to debug projects that cannot start.
Summarize
In addition to the startup script as above configuration, you can also be configured in this way:
At startup: Sh catalina.sh jpda start. Modify whether to break at startup or local listening port, set Variable value: Jpda_suspend=y jpda_address=9999.
Remote Debug Debug Java code