Alibaba Arthas--A Sharp Tool for Online Problem Analysis

Posted by arundathi on Thu, 05 Sep 2019 10:34:43 +0200

Catalog

This blog is reprinted from Ali Open Source Java Diagnostic Tool

1. What is Arthas

Arthas is an Alibaba Open Source Java online diagnostic tool, which is very powerful and can solve many online problems that are not easy to solve.

Arthas diagnosis uses command-line interaction mode, supports JDK6+, Linux, Mac, Windows operating systems, commands also support the use of tab keys to automatically complete all kinds of information, diagnosis is very convenient.

This is its official website:

https://alibaba.github.io/arthas/index.html

Github address:

https://github.com/alibaba/arthas

2. What can Arthas solve?

Look at Arthas's official note

When you are confronted with the following similar problems and you are helpless, Arthas can help you solve them:
1. From which jar package does this class load? Why do you report various kinds of Exception s?
2. Why hasn't my code been executed? Am I not commit? Is the branch wrong?
3. You can't debug online when you encounter problems. Can you only redistribute it by adding logs?
4. There is a problem with data processing of a user online, but it can not debug online and can not be reproduced offline.
5. Is there a global perspective to see how the system works?
6. What can we do to monitor the real-time running state of JVM?

Do you think it's too compelling after reading it? Especially, it can decompile classes online and debug and track problem codes online without adding logs.

3. Fast Installation

Officially, arthas-boot is recommended for installation, which is very convenient. The following is a demonstration based on the Linux system environment. The general solution to online problems is also based on the Linux environment.

Step 1: Download

Download the arthas-boot package in any directory.

wget https://alibaba.github.io/arthas/arthas-boot.jar

[root@VM_0_7_centos ~]# wget https://alibaba.github.io/arthas/arthas-boot.jar
--2019-07-30 14:48:31--  https://alibaba.github.io/arthas/arthas-boot.jar
Resolving alibaba.github.io (alibaba.github.io)... 185.199.108.153, 185.199.109.153, 185.199.110.153, ...
Connecting to alibaba.github.io (alibaba.github.io)|185.199.108.153|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 98637 (96K) [application/java-archive]
Saving to: 'arthas-boot.jar'

100%[==========================================================================================================>] 98,637      32.8KB/s   in 2.9s   

2019-07-30 14:48:36 (32.8 KB/s) - 'arthas-boot.jar' saved [98637/98637]

Step 2: Operation

Use the java-jar command to execute the arthas-boot package.

java -jar arthas-boot.jar

[INFO] arthas-boot version: 3.1.1
[INFO] Found existing java process, please choose one and hit RETURN.
* [1]: 13062 spring-boot-best-practice-0.0.1-SNAPSHOT.jar

Step 3: Select the process

After running arthas-boot, the console displays all Java processes and selects one that you need to diagnose.

As shown in the second step, there is only one Java process, input serial number 1, return, Arthas will attach to the target process, and output the log:

[INFO] Start download arthas from remote server: https://maven.aliyun.com/repository/public/com/taobao/arthas/arthas-packaging/3.1.1/arthas-packaging-3.1.1-bin.zip
[INFO] Download arthas success.
[INFO] arthas home: /root/.arthas/lib/3.1.1/arthas
[INFO] Try to attach process 13062
[INFO] Attach process 13062 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.                           
 /  O   |  .--. ''--.  .--'|  '--'  | /  O   '   .-'                          
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.                          
|  | |  ||  |      |  |   |  |  |  ||  | |  |.-'    |                         
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'                          


wiki      https://alibaba.github.io/arthas                                      
tutorials https://alibaba.github.io/arthas/arthas-tutorials                     
version   3.1.1                                                                 
pid       13062                                                                 
time      2019-07-30 14:49:34

At this point, the installation and start-up are completed.

For more installation options, see https://alibaba.github.io/arthas/install-detail.html

4. Practical use

After starting, the current cursor will enter the arthas console and accept various operation commands.

Next, the head of the stack will do a few demonstrations of common commands, so that you have a basic understanding of it and the ability to quickly start.

1,dashboard
Display the real-time data panel of the current system, press ctrl+c to exit.

$ dashboard

2. thread
View the thread stack information for the current JVM.

thread id, which shows the running stack of the specified thread:

$ thread 20

Display the current busiest first N threads and print the stack:

$ thread -n 3

3. sc
View the class details loaded by the JVM.

$ sc -d *Test

4. sm
View method information for loaded classes.

$ sm -d cn.javastack.springbootbestpractice.SpringBootBestPracticeApplication main

5. jad
Decompilation specifies the source code of the loaded class.

$ jad cn.javastack.springbootbestpractice.SpringBootBestPracticeApplication

6. trace
Display method internal call paths, non-real-time returned commands and output total time-consuming on method paths, and detailed time-consuming on each node.

$ trace -j cn.javastack.springbootbestpractice.web.JsonTest getUserInfo

7. monitor
Timely monitoring of a method call.

$ monitor cn.javastack.springbootbestpractice.web.JsonTest getUserInfo -c 5

- c 5: Represents statistics every 5 seconds, the statistical period, the default value is 120 seconds.

Monitoring Dimension Description:

8. watch
The observation method executes the data, and can easily observe the call situation of the specified method, such as return value, throwing exception, entering parameter, etc.

$ watch cn.javastack.springbootbestpractice.web.JsonTest getUserInfo '{params, returnObj}' -x 2 -b

The above monitoring is a method's entry status, which is monitored before the method is executed: - b, traversal depth: - x 2.

9. quit/exit
Exit the current Arthas.

This command only exits the client that is currently connected. Arthas attached to the target process will continue to run. The port will not be closed. It can be used directly next time the connection is made.

10. shutdown
Close the Arthas server and exit all Arthas clients.

The above demonstrates the basic use of 10 commands. Details of the use of various commands can be consulted in the command band - help.

For more commands, see:

https://alibaba.github.io/arthas/commands.html

5. Summary

In conclusion, using Arthas can easily diagnose a Java application, such as system data panel, JVM real-time running status, class loading, monitoring method implementation, displaying method execution path, etc.

These practical functions of Arthas can help us solve some common online problems and are also independent of application code, but they are limited to a JVM process. If it is a distributed system, Arthas is a bit difficult.

Topics: Java github jvm Linux