The author | Wang Xiaoge

Arthas has become integral to many Java developers, playing an important role in our daily development and troubleshooting of problems online. As a small developer, I need to check all kinds of bugs raised by online operation classmates, diagnose all kinds of online problems, daily operation and maintenance, and optimize online problems.

When I first came to the company, I was afraid of operation and maintenance tasks. I was not familiar with the code, and there were many problems. Almost broke down. Basically, I didn’t do any work in the operation and maintenance for a week. I was totally devoted to the operation and maintenance task, and troubleshooting was inefficient.

Having experienced this crash, I have been trying to change it until Arthas’s open source release has relieved me of the burden of this crash, and has also made me the go-to person for my colleagues to consult Arthas for troubleshooting problems. Although Arthas is very convenient to use, there are also some problems. The Arthas Idea plugin was born because it was a little inconvenient to be a problem finder.

Arthas tools are not simple enough. You need to remember some commands, especially some advanced syntax that is very extensible. For example, ogNL gets the Spring Context as it wants, watch and trace are not simple enough. So all you need is a plug-in that can easily handle string information.

The Idea Arthas plugin has its place when dealing with online problems requires the fastest and most convenient commands. This is the core reason for writing this plug-in in the first place.

Arthas IDEA Plugin practice

Arthas has a wide range of functional points (see the larger image below), which I don’t want to explain here. Please refer to the usage documentation, but it has been updated recently and the names of the commands in the usage documentation may change.

Plug-in installation

Download arthas idea plug-ins: plugins.jetbrains.com/plugin/1358…

  • Mac:Preferences -> Plugins
  • Windows:Settings -> Plugins

Install Plugin form Disk… The import plug-in

After installation, restart IDEA and you can use it happily!

Get static variables

First, you need to get the hash value of the classloader, and then you need to get the command. This is an interactive process that requires consistency. Any subsequent static process that passes through the static Spring context needs to have this interactive process. Paste the execution and get the result.

The hash value of the classloader is cached here

The final merged script is as follows.

ognl  -x  3 '@com.wangji92.arthas.plugin.demo.controller.StaticTest@INVOKE_STATIC_NAME' -c 316bc132
Copy the code

Reflection set static field

To set a static field by reflection, see github.com/WangJi92/ar…

Fill in the values you want to change, default set default values by type Str->”” Long -> 0L, etc.

ognl -x 3  '#[email protected]@class.getDeclaredField("INVOKE_STATIC_FINAL"),#modifiers= #field.getClass().getDeclaredField("modifiers"),#modifiers.setAccessible(true),#modifiers.setInt(#field,#field.getModifi Ers () & ~ @ Java. Lang. Reflect the Modifier @ FINAL), # field. SetAccessible (true), # field. The set (null, "set the value of") ' -c 316bc132
Copy the code

Spring Context Invoke

The spring Context is used to invoke the bean’s methods and contents of the fields.

Static Spring Context Invoke Method Field

Set the path to static Spring Context.

Spring conetxt is a static spring context that is called by ogNL using a classloader.

Mon @ com.wangji92.arthas.plugin.demo.com. ApplicationContextProvider @ the context reference demo will need to configure the address.

ognl -x 3 '#springContext=@com.wangji92.arthas.plugin.demo.common.ApplicationContextProvider@context,#springContext.getBean("commo nController").getRandomInteger()' -c 316bc132
Copy the code

Watch Spring Context Invoke Method Field

Watch is supported in the Spring MVC scenario. To obtain the Spring context indirectly through watch, you need to invoke the interface, for example: github.com/WangJi92/ar…

watch -x 3 -n 1  org.springframework.web.servlet.DispatcherServlet doDispatch '@org.springframework.web.context.support.WebApplicationContextUtils@getWebApplicationContext(params[0].getServletContex t()).getBean("commonController").getRandomInteger()'
Copy the code

TimeTunnel Spring Context Invoke Method Field

Arthas gets spring context via TT. You can refer to this document: github.com/WangJi92/ar… What’s going on here? You do not need to remember the parameter information, and then simply encapsulate the parameters of the call by default. Complex parameter scenarios are not supported and need to be manually concatenated.

Record getting the Spring context

tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod
Copy the code

Then call the spring Context method based on that target

tt -w 'target.getApplicationContext().getBean("commonController").getRandomInteger()' -x 3 -i 1000
Copy the code

Get a spring environment variable

Static Spring Context (watch, tt) ¶ How do you know that the variables in the environment must be configured by you? In configuration center scenarios such as NACOS, where the estimated hand speed is a little bit slower and might not go up, there is a need to get the current environment variable. Select the variable, then right-click to execute the command. Since the static Spring context is used, the classLoader value is still required. This is basically an upper level arthas application.

ognl -x 3 '#springContext=@com.wangji92.arthas.plugin.demo.common.ApplicationContextProvider@context,#springContext.getEnvironment ().getProperty("custom.name")' -c 316bc132
Copy the code

Get all of the Spring environment variables

You must be confused by spring’s various priorities, so what do you do? Arthas Idea Plugin allows you to take all of the current environment variables and print them out one by one so you can know the priority, especially if you have access to remote configuration centers such as NacOS and Diamond.

Reference: blog.csdn.net/xunjiushi97…

ognl -x 3 '#springContext=@com.wangji92.arthas.plugin.demo.common.ApplicationContextProvider@context,#allProperties={},#standardSe rvletEnvironment=#propertySourceIterator=#springContext.getEnvironment(),#propertySourceIterator=#standardServletEnviron ment.getPropertySources().iterator(),#propertySourceIterator.{#key=#this.getName(),#allProperties.add(" "),#allProperties.add("------------------------- name:"+#key),#this.getSource() instanceof java.util.Map ?#this.getSource().entrySet().iterator.{#key=#this.key,#allProperties.add(#key+"="+#standardServletEnvironment.getProper ty(#key))}:#{}},#allProperties' -c 316bc132
Copy the code

TimeTunnel Tt

The method executes the time-space tunnel of the data, records the input and return information of each call of the specified method, and can observe these different times of the call (can be triggered again, triggered periodically, the only drawback is the loss of ThreadLocal information [implicit parameters], reference object data changes are invalid), which facilitates the second trigger. In particular, it is not convenient to record the debugging of the case, the second trigger, the cycle trigger, but since duan Ling god TT do whatever they want to be taken away. What does the arthas plug-in do here? Some common commands are added to trigger the second time, so that the user does not worry about memory, and the whole process is more coherent.

Stack stack

Gets the call stack from where the method executes. Java Debug and Arthas idea is an integration of the commands you need to modify, and the arthas idea plugin also handles problems in your coding process. Blog.csdn.net/u012881904/…

stack com.wangji92.arthas.plugin.demo.controller.CommonController getRandomInteger -n 5
Copy the code

Decompile Class Jad

Decompile method, class source, sometimes need to see whether the submitted code is on line? This feature is very friendly.

Reference: github.com/WangJi92/arth! as-idea-plugin/issues/2

jad --source-only com.wangji92.arthas.plugin.demo.controller.CommonController getRandomInteger
Copy the code

Watch, trace

Increased the default parameters, the default expansion of the hierarchy limit times, the user does not need to know these core parameters, simple use is good, to use more advanced their own help to know.

watch com.wangji92.arthas.plugin.demo.controller.CommonController getRandomInteger '{params,returnObj,throwExp}' -n 5 -x 3
trace com.wangji92.arthas.plugin.demo.controller.CommonController getRandomInteger -n 5
Copy the code

Trace-e (hierarchical print trace)

Trace-e is very troublesome to construct by itself, which is simplified by interface operation, requiring observation of multiple classes and multiple methods. Select the scene you want to add more.

trace -E com.wangji92.arthas.plugin.demo.controller.CommonController|com.wangji92.arthas.plugin.demo.service.ArthasTestService traceE|doTraceE -n 5
Copy the code

Heap Dump

Jmap-dump :format=b,file=/temp/dump.hprof pid

heapdump  /tmp/dump.hprof Printing stack information 
Copy the code

Special usage link

This must be said, this special use of the link on the online you can check out when at a loss, very useful.

For calling methods through the Spring Context

Calling complex methods from the Spring context is not supported, and since it is not convenient to do so, it must be handled manually.

For example, the treatment of Map Names here can be learned in a flash.

For more information, see demo: github.com/WangJi92/ar…

/ * *
     * Complex parameter call scenario
     * static spring context
     * ognl -x 3 '#user=new com.wangji92.arthas.plugin.demo.controller.User(),#user.setName("wangji"),#user.setAge(27L),#[email protected] .arthas.plugin.demo.common.ApplicationContextProvider@context,#springContext.getBean("commonController").complexParamete rCall(#{"wangji":#user})' -c e374b99
     *
     * watch get spring context note You need to call the method once
     * watch -x 3 -n 1  org.springframework.web.servlet.DispatcherServlet doDispatch '#user=new com.wangji92.arthas.plugin.demo.controller.User(),#user.setName("wangji"),#user.setAge(27L),@org.springframework.web.con text.support.WebApplicationContextUtils@getWebApplicationContext(params[0].getServletContext()).getBean("commonControlle r").complexParameterCall(#{"wangji":#user})'
     *
     * tt get spring context , only first get time index ok
     * tt -w '#user=new com.wangji92.arthas.plugin.demo.controller.User(),#user.setName("wangji"),#user.setAge(27L),target.getApplicationContext ().getBean("commonController").complexParameterCall(#{"wangji":#user})' -x 3 -i 1000
     * @return
     * /
    @RequestMapping("complexParameterCall")
    @ResponseBody
    public String complexParameterCall(@RequestBody  Map<String, User> names) {
        if (names = = null) {
            return "EMPTY";
        }
        return names.toString();
    }
Copy the code

conclusion

This article gives you a brief introduction to installing and using Arthas IDEA. Arthas IDEA eliminates some of the memorization and mechanical rework that Arthas uses.

Arthas is officially running an essay call from 26 March to 26 April if you have:

  • Problems identified using Arthas
  • Source code interpretation of Arthas
  • Advise Arthas
  • No limit, other Arthas related content

Welcome to participate in the essay contest and get prizes. Click here for more details.

“Alibaba Cloud originators pay close attention to technical fields such as microservice, Serverless, container and Service Mesh, focus on cloud native popular technology trends and large-scale implementation of cloud native, and become the technical circle that knows most about cloud native developers.”