Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in! | End of article benefits

Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in! | End of article benefits

△Hollis, a person with a unique pursuit of Coding △
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits
This is Hollis’s 264th original sharing
author l Wang
Xiaoge source l Hollis (ID: hollischuang)

Arthas is inseparable for many Java developers and plays a very important role in our daily development and online troubleshooting. As a small developer, I need to check various bugs raised by online operation students, various online problem diagnosis, daily operation and maintenance, online problem optimization, etc.

When I first came to the company, I was more afraid of operation and maintenance tasks. The code was unfamiliar and there were many problems... almost collapsed. During the week of operation and maintenance, I basically did not work. I was completely devoted to operation and maintenance. In the task, troubleshooting is inefficient.

Because I deeply experienced this collapse, I always wanted to change this state until the open source of Arthas, which relieved me of a lot of burden in this collapsed state, and also made me a colleague who consulted Arthas for troubleshooting. Little helper~~

Although it is particularly convenient to use Arthas, I also encountered some problems in the process, and I felt a little inconvenient as a helper for consultation. Therefore, the Arthas idea plug-in was born.
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

At present, Arthas's official tools are not simple enough. You need to remember some commands, especially some advanced syntax with strong extensibility, such as ognl to get spring context and do whatever you want, watch and trace are not simple enough, you need to construct some command tool information, so only need A plug-in that can simply handle string information can be used.

When dealing with online problems, the fastest and most convenient commands are needed, so the Idea Arthas plugin still has meaning and value. ---This is the core reason why this plugin was originally written.

Arthas IDEA plugin practice

Arthas has a lot of function points (see the big picture below for details). I won't explain them one by one here. You can refer to the user documentation. However, it has been updated recently, and the command names in the user documentation may change.
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

Plug-in installation

Download arthas idea plugin:

https://plugins.jetbrains.com/plugin/13581-arthas-idea

  • Mac: Preferences -> Plugins
  • Windows: Settings -> Plugins
    Install Plugin form Disk... Import plug-ins
    Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

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

To obtain static variables, you
must first obtain the hash value of the classloader, and then obtain the command. This is an interactive process that requires continuity. Any subsequent static that passes through the static spring context needs to have this interactive process. The continuity is all in the same interface. Operate. Paste and execute, and then get the result.
The hash value of the classloader here is cached
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

The final merged script is as follows.


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

Reflection setting static field Set static field
through reflection, refer to: https://github.com/WangJi92/arthas-idea-plugin/issues/1
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

Fill in the value you want to modify, set the default value Str->"" Long -> 0L and so on by default according to the type.
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits


ognl -x 3  '#[email protected]@class.getDeclaredField("INVOKE_STATIC_FINAL"),#modifiers=#field.getClass().getDeclaredField("modifiers"),#modifiers.setAccessible(true),#modifiers.setInt(#field,#field.getModifiers() & [email protected]@FINAL),#field.setAccessible(true),#field.set(null,"设置的值")' -c 316bc132

Spring Context Invoke
invokes bean methods and field contents through spring context.
Static Spring Context Invoke Method Field

To set the path of static spring context on the home page
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

image.png

Since all calls to static spring context through ognl require classloader, this is the address information of the configured spring conetxt:
@com.wangji92.arthas.plugin.demo.common.ApplicationContextProvider@context Refer to the demo to configure this address.

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

Watch Spring Context Invoke Method Field

watch This is to support indirect access to spring context through watch in spring mvc scene, and you need to start an interface call, you can refer to: https://github.com/WangJi92/arthas-idea-plugin/issues/5
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

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

TimeTunnel Spring Context Invoke Method Field

This is a reference to arthas of Hengyun Duanling to get spring context through tt to do whatever you want, you can refer to this document: https://github.com/WangJi92/arthas-idea-plugin/issues/4 What did you do here? The whole is connected without remembering the parameter information, and then simple default encapsulation is performed on the called parameters. Complex parameter scenarios are not supported, and manual splicing is required.
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

Record acquisition of spring context

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

Then get the spring context call method according to this target

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

Obtaining a certain spring environment variable
Obtaining the spring environment variable depends on static spring context. Of course, the scenario where watch and tt get spring context are also supported. How complicated are the online environment and test environment programs, how do you know that the variables in the environment must be configured by you? In the scenario of nacos and other configuration centers, it is estimated that the hand speed is a little bit slower and may not go up. At this time, there is such a need to obtain the current environment variables. Select the variable, then right-click to execute the command. Since the static spring context is used, the value of classloader is still needed. This is basically the upper application of arthas.

Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

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

Get all the spring environment variables and
compare the priority. The first one must have the highest priority. You must be confused by the various priority orders of spring, so what should you do? The arthas idea plugin supports to get all the current environment variables and print them out in turn, so that you can understand the priority, especially when you connect to remote configuration centers such as nacos and diamond. Reference document: https://blog.csdn.net/xunjiushi9717/article/details/94050139
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

ognl -x 3 '#springContext=@com.wangji92.arthas.plugin.demo.common.ApplicationContextProvider@context,#allProperties={},#standardServletEnvironment=#propertySourceIterator=#springContext.getEnvironment(),#propertySourceIterator=#standardServletEnvironment.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.getProperty(#key))}:#{}},#allProperties' -c 316bc132

The TimeTunnel Tt
method executes the space-time tunnel of the data, records the input parameters and return information of each call of the specified method, and can observe the calls at these different times (re-trigger, periodic trigger, the only drawback is the loss of ThreadLocal information [hidden With parameters], the reference object data change is invalid), this is convenient for secondary triggering, especially when it is inconvenient for self-debugging to record, secondary triggering and periodic triggering, but since Duanling Dashen tt has done whatever he wants, it has been biased. What does the arthas plugin do here? Some commonly used commands for secondary triggering have been added to prevent users from worrying about memory, and the whole process is more consistent.
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits
stack
The call stack where the method is executed from (purpose: learn the call stack from the source code, understand the call flow) This is a very useful function, and it is a boon for friends who like to troubleshoot problems. The arthas idea plugin is just an integration of modified commands , I also dealt with the problems in my own coding process, source code, troubleshooting skills-Java Debug and Arthas: https://blog.csdn.net/u012881904/article/details/104591529

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

Decompile Class Jad
decompile method, class source code, sometimes need to check whether the submitted code is online? This feature is very friendly. Reference document: https://github.com/WangJi92/arthas-idea-plugin/issues/2
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

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

Watch and trace
increase the number of default parameters and the level limit of the default expansion. Users don't need to know these core parameters, just simply use them. If you want to use more advanced self help, you will 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

trace -E (hierarchical print trace)
Trace -E is very troublesome to construct by itself. It simplifies the operation through the interface. It is necessary to observe the scene of multiple classes and multiple methods. Select the scene you need and continue to add it.
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

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

Heap Dump
prints the stack, a bit similar to jump -dump:format=b,file=/temp/dump.hprof pid, download it and use MAT analysis.

heapdump  /tmp/dump.hprof 打印堆栈信息 

Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

The special usage link
must be mentioned. This special usage link can be checked online when you are at a loss. It is very useful.
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits
Calling methods
through spring context shows that calling complex methods through spring context is actually not supported. Because this operation is inconvenient, it must be handled manually.
For example, the handling of Map names here can be used for reference. More can refer to demo https://github.com/WangJi92/arthas-plugin-demo

/**
     * 复杂参数调用 场景
     * static spring context
     * ognl -x 3 '#user=new com.wangji92.arthas.plugin.demo.controller.User(),#user.setName("wangji"),#user.setAge(27L),#springContext=@com.wangji92.arthas.plugin.demo.common.ApplicationContextProvider@context,#springContext.getBean("commonController").complexParameterCall(#{"wangji":#user})' -c e374b99
     *
     * watch get spring context 备注 需要调用一次方法
     * 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.context.support.WebApplicationContextUtils@getWebApplicationContext(params[0].getServletContext()).getBean("commonController").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();
    }

Conclusion
This article briefly introduces the installation and usage skills of Arthas IDEA plug-in. The plug-in liberates some memory and mechanical repetitive work for Arthas. Welcome to try it out!

Arthas officially held an essay essay event, held from March 26th to April 26th. If you have

  • Problems checked with Arthas
  • Source code interpretation of Arthas
  • Suggestions to Arthas
  • Unlimited, other content related to Arthas

Welcome to participate in the essay activity, and there are prizes to get~
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits

Click to read the original text to access the address of the essay call.

Welfare time

GIFT TIME

Arthas technical community specially sponsored part of the gifts of this essay event (1 Tmall Genie, 2 Tao dolls, 3 CNCF fingertip tops) to readers of Hollis

Everyone just needs to comment on what you think of Arthas, do you help you solve any problems, or have any suggestions?

Like the first place to win the Tmall Elf, the 2-3th place to win the Tao doll, and the 4-6th place to win the fingertip top.

Dynamic black notes

The series of articles
on the road to becoming a god of Java engineers are in the GitHub update, welcome to follow, welcome to star.
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits
Facing Java Issue 309: How does the HotSpot virtual machine implement in-
depth concurrency in the method area after JDK1.8 Issue 013: Expand synchronized-lock optimization

If you like this article,
please press
Alibaba's open source troubleshooting tool for Niu X-Arthas, has launched the IDEA plug-in!  | End of article benefits
and hold the QR code and follow Hollis. Forward it to the circle of friends. This is my greatest support.
Good article, I am reading ❤️Read the
original text

Guess you like

Origin blog.51cto.com/13626762/2544172