How to start source code debugging of tomcat in Idea and enter into tomcat for debugging

How to start source code debugging of tomcat in Idea and enter into tomcat for debugging

When using the idea development tool to debug code, if it is a Java web project, using Tomcat as the web container, breakpoint debug tracing, when tracing to the org.apache.catalina package, it cannot enter. This is because the Tomcat running in idea is integrated through a plug-in, and the lib package in Tomcat is no longer in the project's dependency path, so it cannot be traced in.

First, mark a breakpoint in the interface implementation class called back by Tomcat in your own project, and start the web project through idea. When the breakpoint information shown in the figure appears, because the breakpoint position marks the interface class called back by Tomcat, the call stack is Tomcat internal code. However, double-clicking the class name under the org.apache.catalina package does not respond at this time, because we have not added the dependency files corresponding to Tomcat to the classpath.

Adding Dependencies

<dependency>
 <groupId>org.apache.tomcat</groupId>
 <artifactId>tomcat-catalina</artifactId>
 <version>8.5.55</version>
 <scope>provided</scope>
</dependency>

Because the jar file in the lib directory of tomcat is used at runtime, the scope here uses the provided method

Now you can enter the tomcat source code debugging

How is the tomcat startup log printed?

03-Jun-2020 10:31:30.929 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server. Server version: Apache Tomcat/8.5.55
03-Jun-2020 10:31:30.938 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server build: May 5 2020 22:10:54 UTC
03-Jun-2020 10:31:30.938 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number (: 8.5.55.0
03-Jun-2020 10:31:30.938 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Operating system name: Windows 10
03-Jun-2020 10:31:30.938 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS.Version: 10.0
03-Jun-2020 10:31:30.938 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
03-Jun-2020 10:31:30.939 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java environment variable: C:\Program Files\Java\jdk1.8.0_212\jre
03-Jun-2020 10:31:30.939 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java virtual machine version: 1.8.0_212-b10
03-Jun-2020 10:31:30.939 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM.Vendor: Oracle Corporation
03-Jun-2020 10:31:30.939 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: C:\Users\Administrator\.IntelliJIdea2018.3\system\tomcat\Unnamed_spring-shiro-training
03-Jun-2020 10:31:30.939 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: D:\tomcat8.5.55\apache-tomcat-8.5.55

By locating the VersionLoggerListener log, you can view

private void log() {
 log.info(sm.getString("versionLoggerListener.serverInfo.server.version",
  ServerInfo.getServerInfo()));
 log.info(sm.getString("versionLoggerListener.serverInfo.server.built",
  ServerInfo.getServerBuilt()));
 log.info(sm.getString("versionLoggerListener.serverInfo.server.number",
  ServerInfo.getServerNumber()));
 log.info(sm.getString("versionLoggerListener.os.name",
  System.getProperty("os.name")));
 log.info(sm.getString("versionLoggerListener.os.version",
  System.getProperty("os.version")));
 log.info(sm.getString("versionLoggerListener.os.arch",
  System.getProperty("os.arch")));
 log.info(sm.getString("versionLoggerListener.java.home",
  System.getProperty("java.home")));
 log.info(sm.getString("versionLoggerListener.vm.version",
  System.getProperty("java.runtime.version")));
 log.info(sm.getString("versionLoggerListener.vm.vendor",
  System.getProperty("java.vm.vendor")));
 log.info(sm.getString("versionLoggerListener.catalina.base",
  System.getProperty("catalina.base")));
 log.info(sm.getString("versionLoggerListener.catalina.home",
  System.getProperty("catalina.home")));


 if (logArgs) {
 List<String> args = ManagementFactory.getRuntimeMXBean().getInputArguments();
 for (String arg : args) {
  log.info(sm.getString("versionLoggerListener.arg", arg));
 }
 }


 if (logEnv) {
 SortedMap<String, String> sortedMap = new TreeMap<>(System.getenv());
 for (Map.Entry<String, String> e : sortedMap.entrySet()) {
  log.info(sm.getString("versionLoggerListener.env", e.getKey(), e.getValue()));
 }
 }


 if (logProps) {
 SortedMap<String, String> sortedMap = new TreeMap<>();
 for (Map.Entry<Object, Object> e : System.getProperties().entrySet()) {
  sortedMap.put(String.valueOf(e.getKey()), String.valueOf(e.getValue()));
 }
 for (Map.Entry<String, String> e : sortedMap.entrySet()) {
  log.info(sm.getString("versionLoggerListener.prop", e.getKey(), e.getValue()));
 }
 }
}

The discovery is obtained through key-value pairs, and then found through global string search

insert image description here

But the match is English, so how do you type Chinese?

Finally, through debugging, I found this

insert image description here

Similar to the above, when debugging, I found that there are still quite a lot of things started by tocmat. Look at the following

insert image description here

Data after idea tomcat is started

03-Jun-2020 10:31:30.939 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line arguments: -Djava.util.logging.config.file=C:\Users\Administrator\.IntelliJIdea2018.3\system\tomcat\Unnamed_spring-shiro-training\conf\logging.properties
03-Jun-2020 10:31:30.940 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line arguments: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
03-Jun-2020 10:31:30.940 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line arguments: -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:52290,suspend=y,server=n
03-Jun-2020 10:31:30.940 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line arguments: -javaagent:C:\Users\Administrator\.IntelliJIdea2018.3\system\captureAgent\debugger-agent.jar
03-Jun-2020 10:31:30.940 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dfile.encoding=UTF-8
03-Jun-2020 10:31:30.940 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=
03-Jun-2020 10:31:30.940 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099
03-Jun-2020 10:31:30.940 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
03-Jun-2020 10:31:30.940 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line arguments: -Dcom.sun.management.jmxremote.password.file=C:\Users\Administrator\.IntelliJIdea2018.3\system\tomcat\Unnamed_spring-shiro-training\jmxremote.password
03-Jun-2020 10:31:30.940 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.access.file=C:\Users\Administrator\.IntelliJIdea2018.3\system\tomcat\Unnamed_spring-shiro-training\jmxremote.access
03-Jun-2020 10:31:30.940 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line arguments: -Djava.rmi.server.hostname=127.0.0.1
03-Jun-2020 10:31:30.940 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
03-Jun-2020 10:31:30.940 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line arguments: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
03-Jun-2020 10:31:30.941 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
03-Jun-2020 10:31:30.941 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line arguments: -Dcatalina.base=C:\Users\Administrator\.IntelliJIdea2018.3\system\tomcat\Unnamed_spring-shiro-training
03-Jun-2020 10:31:30.941 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=D:\tomcat8.5.55\apache-tomcat-8.5.55
03-Jun-2020 10:31:30.941 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=D:\tomcat8.5.55\apache-tomcat-8.5.55\temp

The above is just the basic environment configuration and then it is ready to link to the tomcat service.

03-Jun-2020 10:31:30.941 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat native library [1.2.24] using APR version [1.7.0].
03-Jun-2020 10:31:30.941 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR features: IPv6[true], sendfile[true], accept filters[false], random[true].
03-Jun-2020 10:31:30.941 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
03-Jun-2020 10:31:30.944 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1g 21 Apr 2020]
03-Jun-2020 10:31:31.032 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing protocol handler ["http-nio-8080"]
03-Jun-2020 10:31:31.046 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
03-Jun-2020 10:31:31.055 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 175489 ms
03-Jun-2020 10:31:31.080 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
03-Jun-2020 10:31:31.080 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.55
03-Jun-2020 10:31:31.089 INFO [main] org.apache.coyote.AbstractProtocol.start Starting protocol handler ["http-nio-8080"]
03-Jun-2020 10:31:31.102 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 47 ms

tomat startup is mainly in Catalina

insert image description here
insert image description here

Then

insert image description here

start up

/**
* Await and shutdown.
*/
public void await() {

 getServer().await();


}

In fact, Tomcat startup is essentially just a socket server.

@Override
public void await() {
 // Negative values ​​- don't wait on port - tomcat is embedded or we just don't like ports
 if (port == -2) {
 // undocumented yet - for embedding apps that are around, alive.
 return;
 }
 if (port==-1) {
 try {
  awaitThread = Thread.currentThread();
  while(!stopAwait) {
  try {
   Thread.sleep( 10000 );
  } catch(InterruptedException ex ) {
   // continue and check the flag
  }
  }
 finally
  awaitThread = null;
 }
 return;
 }


 // Set up a server socket to wait on
 try {
 awaitSocket = new ServerSocket(port, 1,
  InetAddress.getByName(address));
 } catch (IOException e) {
 log.error("StandardServer.await: create[" + address
    + ":" + port
    + "]: ", e);
 return;
 }


 try {
 awaitThread = Thread.currentThread();


 // Loop waiting for a connection and a valid command
 while (!stopAwait) {
  ServerSocket serverSocket = awaitSocket;
  if (serverSocket == null) {
  break;
  }


  // Wait for the next connection
  Socket socket = null;
  StringBuilder command = new StringBuilder();
  try {
  InputStream stream;
  long acceptStartTime = System.currentTimeMillis();
  try {
   socket = serverSocket.accept(); //Once accecpt, the following will start to execute socket.setSoTimeout(10 * 1000); // Ten seconds
   stream = socket.getInputStream();
  } catch (SocketTimeoutException ste) {
   // This should never happen but bug 56684 suggests that
   // it does.
   log.warn(sm.getString("standardServer.accept.timeout",
    Long.valueOf(System.currentTimeMillis() - acceptStartTime)), ste);
   continue;
  } catch (AccessControlException ace) {
   log.warn(sm.getString("standardServer.accept.security"), ace);
   continue;
  } catch (IOException e) {
   if (stopAwait) {
   // Wait was aborted with socket.close()
   break;
   }
   log.error(sm.getString("standardServer.accept.error"), e);
   break;
  }


  // Read a set of characters from the socket
  int expected = 1024; // Cut off to avoid DoS attack
  while (expected < shutdown.length()) {
   if (random == null)
   random = new Random();
   expected += (random.nextInt() % 1024);
  }
  while (expected > 0) {
   int ch = -1;
   try {
   ch = stream.read();
   } catch (IOException e) {
   log.warn(sm.getString("standardServer.accept.readError"), e);
   ch = -1;
   }
   // Control character or EOF (-1) terminates loop
   if (ch < 32 || ch == 127) {
   break;
   }
   command.append((char) ch);
   expected--;
  }
  finally
  // Close the socket now that we are done with it
  try {
   if (socket != null) {
   socket.close();
   }
  } catch (IOException e) {
   // Ignore
  }
  }


  // Match against our command string
  boolean match = command.toString().equals(shutdown);
  if (match) {
  log.info(sm.getString("standardServer.shutdownViaPort"));
  break;
  } else
  log.warn(sm.getString("standardServer.invalidShutdownCommand", command.toString()));
 }
 finally
 ServerSocket serverSocket = awaitSocket;
 awaitThread = null;
 awaitSocket = null;


 // Close the server socket and return
 if (serverSocket != null) {
  try {
  serverSocket.close();
  } catch (IOException e) {
  // Ignore
  }
 }
 }
} 

insert image description here

After the tomcat container is started, the following is the content of the Springmvc module

insert image description here
insert image description here

This is the end of this article about how to start source code debugging of tomcat in Idea and enter tomcat for debugging. For more content about starting source code debugging of tomcat in Idea, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Example of quick interface generation method for implementing idea class
  • Tips for debugging source code in IDEA: Identify the correct path for multiple implementation classes of abstract classes or interfaces

<<:  What does the legendary VUE syntax sugar do?

>>:  Summary of Common Problems with Mysql Indexes

Recommend

About the implementation of JavaScript carousel

Today is another very practical case. Just hearin...

Example of removing json backslash in php

1. Remove backslashes through the "stripslas...

How to install Docker on Windows Server 2016

Recently Microsoft released Windows Server 2016, ...

jQuery realizes the full function of shopping cart

This article shares the specific code of jQuery t...

Linux general java program startup script code example

Although the frequency of starting the shell is v...

VUE implements a Flappy Bird game sample code

Flappy Bird is a very simple little game that eve...

In-depth understanding of javascript prototype and prototype chain

Table of contents 1. What is a prototype? 2. Prot...

Example code for implementing complex table headers in html table

Use HTML to create complex tables. Complex tables...

Detailed explanation of CSS3 rotating cube problem

3D coordinate concept When an element rotates, it...

In-depth explanation of InnoDB locks in MySQL technology

Table of contents Preface 1. What is a lock? 2. L...

Sliding menu implemented with CSS3

Result:Implementation code: <!DOCTYPE html>...

Steps to install superset under win10 system

Superset is a lightweight self-service BI framewo...

How to modify the MySQL character set

1. Check the character set of MySQL show variable...

Detailed explanation of Grid layout and Flex layout of display in CSS3

Gird layout has some similarities with Flex layou...