1 introduction

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

Arthas can help you when you are stuck with a problem like the following:

  • From which JAR is this class loaded? Why are all kinds of class-related exceptions reported?
  • Why didn’t the code I changed execute? Did I not commit? Got the branch wrong?
  • If you encounter a problem, you cannot debug it online. Can you only re-publish it by logging?
  • There is a problem with a user’s data processing online, but it cannot be debugged online, and it cannot be reproduced offline!
  • Is there a global view of the health of the system?
  • Is there any way to monitor the real-time health of the JVM?

Arthas supports JDK 6+, Linux/Mac/ Windows, command line interaction, and rich Tab auto-completion to further locate and diagnose problems

2 Main Functions

Arthas provides three main features:

  • (1) Information monitoring
    • Basic process running information: memory, CPU usage, thread information, thread stack, thread statistics, and environment variables
    • Object information: class object static properties, Mbean property information, loaded class information, class loader, class method information
  • (2) Method call
    • Method call input parameter, return value view
    • Method invocation path, invocation time, method invocation times, success times, failure times, etc
    • Log and redo method calls
  • (3) Class file processing
    • Dump bytecode, bytecode decompilation, class compilation, and class reloading of loaded classes

3 Installation and Use

3.1 installation

Download arthas-boot.jar and start it with java-jar:

wget https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar
Copy the code

Enter the process number and enter the Arthas command interface to use:

Print help information:

java -jar arthas-boot.jar -h
Copy the code

3.2 the use of

Here are some of the common Arthas commands and their usage and principles to see how they solve our practical problems. For details, please refer to the official Arthas documentation

(1) Overall Dashboard data

From the arthas command line interface, enter the Dashboard command to display the current tomcat multithreading status, JVM regions, GC status, and other information in real time

(2) Check thread monitoring

Run the thread command to display the status information of all threads. Run the thread-n 3 command to display the three busiest threads, which can be used to check the CPU consumption of threads. Run the thread-b command to display the threads in the BLOCKED state, which can be used to check the thread lock problem

(3) the JVM monitoring

Enter the JVM command to view detailed performance data for the JVM

(4) Observe method parameters and return values

Sometimes we need to check the parameters and return values during troubleshooting. Usually, we need to add log printing, which is cumbersome. Based on the watch command, we can easily do all these

$ watch demo.MathGame primeFactors "{params,returnObj}" -x 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in44 ms. ts=2018-12-03 19:16:51; [cost=1.280502ms] result= @arrayList [@object [][@INTEGER [535629513],], @ArrayList[@INTEGER [3], @integer [19], @Integer[191], @Integer[49199], ], ]Copy the code

(5) Observe the method invocation path and time consuming details

Sometimes, there will be service delays. If you want to check which step takes a long time, you usually add logs. Using the trace command can easily solve this problem:

$ trace demo.MathGame run
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in42 ms. `---ts=2018-12-04 00:44:17; thread_name=main; id=1; is_daemon=false; priority=5; TCCL=sun.misc.Launcher$AppClassLoader@3d4eAC69 '--[10.611029ms] Demo.mathGame: Run () +-- [0.05638ms] java.util.Random:nextInt() +-- [10.036885ms] Demo. MathGame: primeFactors () ` - [0.170316] ms demo. MathGame:print(a)Copy the code

4 Implementation Principles

The overall macro module call diagram is as follows:

For reasons of length, the following two core principles are briefly introduced:

(1) Information monitoring and class file processing

Java Management Extensions (JMX) manage a set of MBean objects Arthas uses these MBean objects to monitor memory, GC, classloading information, and JVM information

(2) Method call

Since JDK5, with the introduction of java.lang.Instrument, programmers dynamically modify class code by modifying the bytecode of a method. Among the parameters in the method of the proxy class, there are Instrumentation inst instances. From this example, we can invoke the various interfaces provided by Instrumentation. For example, call Inst.getallloadedClasses () to get all the classes that have been loaded. Call Inst.addtransformer (new SdlTransformer(), true) to add the transformer. Call Inst.reTransformClasses (Class CLS) to issue a reconversion request to the JVM

Arthas uses ASM to generate bytecodes for enhanced classes with enhanced functions such as method call entry, return value viewing, method call statistics, method call logging and redo, as well as adding and transforming methods based on the Instrumentation interface provided by the JDK

5 Actual Cases

The official Arthas documentation provides a number of user cases, but here are a few of the more interesting ones:

(1) Check strange application log sourcesCase details

The StringBuilder implementation code is modified to print the log call stack information and compile to generate StringBuilder.clss. Then change the actual bytecode usage of the StringBuilder used in the application based on the Re-define command provided by Arthas

(2) Check the 401/404 SpringBoot application problemCase details

Page access returns 401/404, which is often a headache, especially in an online environment where Arthas provides trace to print out the complete request tree for the page access and figure out which Servlet returns 404

$ trace javax.servlet.Servlet *
Press Ctrl+C to abort.
Affect(class-cnt:7 , method-cnt:185) cost in 1018 ms.
Copy the code

Using the trace command, the trace object is javax.servlet.Filter to locate which Filter intercepts the request to locate the source of the problem that returned 401

$ trace javax.servlet.Filter *
Press Ctrl+C to abort.
Affect(class-cnt:13 , method-cnt:75) cost in 278 ms.
Copy the code

(3) Online code hot updateCase details

Sometimes to quickly verify a fix for an online problem, or for quick testing, we need hot update code Arthas to provide the following solution

  • Step 1 Run the jad command to decompile the code
  • Step 2 Modify the code in the text editor
  • Step 3 Run the sc command to query the ClassLoader of the class where the code resides
  • Step 4 Run the MC command to specify the ClassLoader to compile the code
  • Step 5 Re-define the code

reference

Arthas official documentation

Arthas Quick start

Arthas, the flying Java diagnostic tool

Unlock Arthas and JVM-SandBox for ali-online problem diagnosis tools