Arthas is an open source Java diagnostic tool for Alibaba that developers love.

  • Making: github.com/alibaba/art…
  • Documents: arthas.aliyun.com/doc/

In the past, with commands like watch, we used to know which class and which function was called, and then trigger the call. There are limitations:

  1. Triggering calls online is difficult
  2. You may need to select multiple times to watch the correct function
  3. Conditional/result expressions may require multiple tests

In addition, if you want to find objects in memory, you need heapdump reanalysis.

Arthas, in its latest release 3.5.1, has brought a divine feature: PassvmtoolCommand to search for objects in JVM memory.

Vmtool online tutorial

The following uses vmtool online tutorial as an example to demonstrate the function of the vmtool command.

  • Arthas.aliyun.com/doc/arthas-…

Start any Spring Boot application, such as:

wget https://github.com/hengyunabc/spring-boot-inside/raw/master/demo-arthas-spring-boot/demo-arthas-spring-boot.jar
java -jar demo-arthas-spring-boot.jar
Copy the code

Then use arthas attach target process and after that you can use vmtool:

wget https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar
Copy the code

Find string objects in the JVM

First, the vmtool command searches the JVM for strings using the getInstances action:

$ vmtool --action getInstances --className java.lang.String
@String[][
    @String[Sorry, deque too big],
    @String[head=%d tail=%d capacity=%d%n],
    @String[elements=%s%n],
    @String[sun/nio/ch/IOVecWrapper],
    @String[40252e37-8a73-4960-807e-3495addd5b08:1620922383791],
    @String[40252e37-8a73-4960-807e-3495addd5b08:1620922383791],
    @String[sun/nio/ch/AllocatedNativeObject],
    @String[sun/nio/ch/NativeObject],
    @String[sun/nio/ch/IOVecWrapper$Deallocator],
    @String[Java_sun_nio_ch_FileDispatcherImpl_writev0],
]
Copy the code

Limit parameter

With the –limit parameter, you can limit the number of returned values to avoid stressing the JVM when retrieving large amounts of data. The default value is 10.

So the above command is actually equivalent to:

vmtool --action getInstances --className java.lang.String --limit 10
Copy the code

If –limit is set to negative, all objects are traversed.

To find the spring context

In the previous online tutorial, we needed to block the spring call with the TT command and get the Spring context.

Using the vmtool command, we can directly obtain the srping context:

$ vmtool --action getInstances \ --className org.springframework.context.ApplicationContext @ApplicationContext[][ @AnnotationConfigEmbeddedWebApplicationContext[org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebAppl icationContext@12028586: startup date [Thu May 13 16:08:38 UTC 2021]; root of context hierarchy], ]Copy the code

Specifies the number of layers to expand as a result of the return

GetInstances Action returns the result bound to the Instances variable, which is an array.

The -x/–expand parameter specifies the expansion level of the result. The default is 1.

vmtool --action getInstances --className org.springframework.context.ApplicationContext -x 2

Get the Srping bean and execute the expression

GetInstances Action returns the result bound to the Instances variable, which is an array. You can execute the specified expression with the –express argument.

For example, find all spring Beans names:

vmtool --action getInstances \
--className org.springframework.context.ApplicationContext \
--express 'instances[0].getBeanDefinitionNames()'
Copy the code

For example, invoke the userController. FindUserById (1) function:

$ vmtool --action getInstances \
--className org.springframework.context.ApplicationContext \
--express 'instances[0].getBean("userController").findUserById(1)'
@User[
    id=@Integer[1],
    name=@String[name1],
]
Copy the code

Find all spring Mapping objects

vmtool --action getInstances --className org.springframework.web.servlet.HandlerMapping

$ vmtool --action getInstances --className org.springframework.web.servlet.HandlerMapping
@HandlerMapping[][
    @SimpleUrlHandlerMapping[org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@5d3819c8],
    @EmptyHandlerMapping[org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping@11d509ba], @RequestMappingHandlerMapping[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@56a5f2e 3], @WelcomePageHandlerMapping[org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WelcomePageHandlerMapping@4c0a4ed3],
    @EmptyHandlerMapping[org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping@51e1f8c3],
    @BeanNameUrlHandlerMapping[org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@68c0a39c],
    @SimpleUrlHandlerMapping[org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@110b768d],
]
Copy the code

Find all javax.servlet.filter

In Arthas’s online tutorial, we explained how to troubleshoot HTTP requests 404/401. The trace javax.servlet.filter * command is used.

Now using vmtool command, we can directly find all Filter objects, speed up the locating process.

$ vmtool --action getInstances --className javax.servlet.Filter
@Filter[][
    @OrderedCharacterEncodingFilter[org.springframework.boot.web.filter.OrderedCharacterEncodingFilter@49b69493],
    @OrderedHiddenHttpMethodFilter[org.springframework.boot.web.filter.OrderedHiddenHttpMethodFilter@5477cb9e],
    @AdminFilter[com.example.demo.arthas.AdminFilterConfig$AdminFilter@3b625385],
    @WsFilter[org.apache.tomcat.websocket.server.WsFilter@14875f22],
    @OrderedRequestContextFilter[org.springframework.boot.web.filter.OrderedRequestContextFilter@6bed550e],
    @OrderedHttpPutFormContentFilter[org.springframework.boot.web.filter.OrderedHttpPutFormContentFilter@3e538cba],
]
Copy the code

To specify this name

In the case of multiple classLoaders, you can also specify classLoaders to find objects:

vmtool --action getInstances \
 --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader \
 --className org.springframework.context.ApplicationContext
Copy the code

To specify this hash

You can run the sc command to find the classloader that loads the class.

$ sc -d org.springframework.context.ApplicationContext class-info org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext code-source file:/private/tmp/demo-arthas-spring-boot.jar! The/BOOT - INF/lib/spring - the BOOT - 1.5.13. The jar! / name org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext ... class-loader +-org.springframework.boot.loader.LaunchedURLClassLoader@19469ea2 +-sun.misc.Launcher$AppClassLoader@75b84c92
                       +-sun.misc.Launcher$ExtClassLoader@4f023edb
 classLoaderHash   19469ea2
Copy the code

Then specify with the -c/–classloader argument:

vmtool --action getInstances \
-c 19469ea2 \
--className org.springframework.context.ApplicationContext
Copy the code

Mandatory GC

Calling System.gc () may not trigger Gc behavior when the JVM argument of -xx :+DisableExplicitGC is enabled.

Vmtool provides the ability to force GC:

vmtool --action forceGc
Copy the code

If the application is configured with the -verbose: GC parameter, you can see similar logs in the application’s standard output:

[GC (JvmtiEnv ForceGarbageCollection) 25760K->17039K(349696K), [Full GC (JvmtiEnv ForceGarbageCollection) 17039K->16840K(353792K), 0.0154049 secs]Copy the code

Thank you

  • vmtoolFeatures are developed in the communityDragon-zhang zichengOn the initial PR of the company, we have discussed and revised it for many times. Thank you for his work, and welcome everyone to propose PR and participate in the development 😄.

conclusion

  • vmtool wiki: arthas.aliyun.com/doc/vmtool
  • Release log: github.com/alibaba/art…

The above shows only examples of manipulating the Spring context using the vmtool command. In fact, it can be used in many ways, such as:

  • Find the Provider/Consumer instance of RPC
  • Find subscription object information for MQ
  • Find the Mybatis mapping object

Welcome to share your experience in the Issue: github.com/alibaba/art…

recruitment

Finally, we are looking for friends, especially shenzhen students, welcome to join us.

  • Ali Cloud – Cloud native – Middleware Recruitment (Shenzhen/Hangzhou)