Tomcat Remote Debug Operations and principles

Source: Internet
Author: User

Operation Chapter

This section is mainly about how to turn on Tomcat remote debugging, with an example. This article works for Windows and Linux.

Suppose there are two machines,a is the Tomcat server machine, andB is the IDE installation machine. a and b can be the same machine, usually a is a test environment andB is the development environment.

Compact version

In the tomcat/bin/folder of the A machine , create a new file SetEnv.bat(or startup.sh, depending on your operating system), enter:

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

Save the file. Start startup.bat(or startup.sh).

on the B machine, start IDE Intellij idea.

Open Edit Configurations in Run

Click the green plus sign on the left to select remote

Enter the IP address and port.

Set breakpoints in your program and refresh your Web page to trigger breakpoints for debugging. [end]

Concise version Explanation

Enable Tomcat Remote Debug mode in a test environment

Tomcat is very simple to start remote debugging, just set the next variable in the JVM startup parameters.

What is done above is the startup parameters of the JVM under Setup .

According to Intellij idea 's tip:

For versions above JDK1.5, theJVM parameters are:

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

For JDK1.4 versions, use:

-xdebug-xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

Again the old version, will not have to list again, actually also rarely encountered.

why intellij give different versions of jdk jvm parameters, stackoverflow

Before Java 5.0, use -xdebug and -xrunjdwp arguments. These options would still work in later versions, but it would run in interpreted mode instead of JIT, which would be slo Wer.

From Java 5.0, it's better to use the -agentlib:jdwp single option:

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

Options on -xrunjdwp or agentlib:jdwp arguments is:

  • Transport=dt_socket:means the the-used to connect-to-JVM (socket is-a good choice, it can be used To debug a distant computer)
  • ADDRESS=8000:TCP/IP Port exposed, to connect from the debugger,
  • Susp End=y:if ' y ', tell the JVM to wait until debugger was attached to begin execution, otherwise (if ' n '), starts execution R Ight away.

The answer explains that the use of the -xdebug method, primarily in interactive mode, is weaker in performance than using -agentlib .

At the same time, the answer also explains the corresponding three parameters:

Transport: There are two forms, the socket and the shared memory, which need to cross the machine and only use the socket

Address: Port number, where the TCP protocol is used . We can use

Cat grep ' 8000 '

To see if the port is open

Suspend: If y, you need to wait for the debugger on the B machine to Open, the program will start to run. Otherwise, the program starts without suspending and running directly.

In the Tomcat official documentation , the script used is:

-xdebug-xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

This is the version of JDK1.4 that Intellij idea gives . However , the official Tomcat document also gives

Catalina JPDA Start

The latter way, in fact, triggers the Catalina script in tomcat:

If ["$" = "JPDA"]; Then  if [-Z ' $JPDA _transport], then    jpda_transport= "Dt_socket"  fi  If [-Z "$JPDA _address"]; then    jpda_address= "localhost:8000"  fi  If [-Z "$JPDA _suspend"]; then    jpda_suspend= "n"  fi  if [-Z ' $JPDA _opts];    Then jpda_opts= "-agentlib:jdwp=transport= $JPDA _transport,address= $JPDA _address,server=y,suspend= $JPDA _suspend"  fi  catalina_opts= "$CATALINA _opts $JPDA _opts"  Shiftfi

The script reads the custom parameter (if present) as a parameter to start JPDA . The default port number is 8000.

set the JVM parameters for Tomcat

There are several ways to set parameters, essentially modifying the java_opts value in Catalina, which is The startup parameter of the JVM.

One way is to set the value directly in the Catalina . However, this method,hardcode the Catalina file, belongs to the not recommend mode.

According to Oracle 's official documentation:

The recommended way is to create a new SetEnv.bat(or startup.sh, depending on your operating system) in the Tomcat/bin directory, and write the parameters that need to be set.

This recommended way to react to the code in Catalina is:

# Ensure that any user defined CLASSPATH variables is not used on startup,# but allow them to being specified in setenv.sh, In rare case, it is needed. classpath=if [-R "$CATALINA _base/bin/setenv.sh"]; Then  . "$CATALINA _base/bin/setenv.sh" elif [-R "$CATALINA _home/bin/setenv.sh"]; Then  . "$CATALINA _home/bin/setenv.sh" fi

The Catalina script will go first to read the environment variables from the setenv.

Finally, start startup .

Principle article

The core is the JPDA(Java Platform Debugger Architecture) framework. IBM has a very detailed introduction:

https://www.ibm.com/developerworks/cn/java/j-lo-jpda1/

https://www.ibm.com/developerworks/cn/java/j-lo-jpda2/

https://www.ibm.com/developerworks/cn/java/j-lo-jpda3/

https://www.ibm.com/developerworks/cn/java/j-lo-jpda4/

Key excerpts are as follows:

    1. JPDA is the JVM's debug standard, and any JDK must be implemented.
    2. Debugging A Java program is essentially requesting the current state from the JVM. This requires sending a certain command to the JVM and setting a callback.
    3. three levels of JPDA: Java Virtual Machine tool interface (JVMTI), Java Debug Line Protocol (JDWP), and Java Debug Interface (JDI). The role diagram between them:

  1. JVMTI (Java Virtual machine Tool Interface): The native interface to the JVM is at the bottom of the system. All debugging functions are required for this interface. is the interface between debug and profiler and is the basis for threading analysis, monitoring, and code coverage checks. There will be a tightly coupled Agent to implement the JVMTI. The Agent 's load time is either early in the JVM initialization or runtime loaded (corresponding to the suspend parameter above ).
  2. JDWP (Java Debug wire Protocol): An interactive communication protocol. Defines the commands, response data, and error codes in the interaction. Does not contain transport layer specific implementations. It is mainly divided into two stages: handshake and answer.
  3. JDI (Java Debug Interface): parse the JDWP to provide services such as queues, caches, etc. for JDWP. Divided into data modules, all the data on the virtual machine, the state map to Java data Objects, link modules, the debugger to the virtual machine to initiate links to obtain a variety of states, divided into active and passive form. The event request and processing module.
  4. Due to the existence of the JVM God, the Java operation is inherently controllable, so the development of its debug program is also far easier with C+ +.
  5. Similar tools:Apache Harmony

Tomcat Remote Debug Operations and principles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.