1. What is it? What kind of problem?

Arthas is An open source Java diagnostics tool from 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?
  • How to quickly locate application hot spots, generate flame map?

Arthas uses command line interaction and rich Tab completion functionality to further facilitate problem location and diagnosis.

Arthas is so powerful that you don’t have to work overtime to check problems.

2. Why is it needed?

Many companies have relatively perfect APM monitoring system for applications, which can cover and solve problems in most scenarios;

Arthas is not intended to replace APM, and imperative interactions are not as intuitive as graphical monitoring systems.

Arthas can be integrated with existing monitoring systems and used in the right context to help us solve problems faster and more efficiently.

(Seems es APM smells better?)

3. Quick start

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

Obtain the arthas-boot application package directly by using the wget command.

Arthas is a Java application that starts with java-JAR. Arthas scans the current system for Java processes using the JPS command and prints the application ID.

Select the number of the Java application you want to diagnose and press Enter to attach to enter the Arthas console.Then, go!

4. List common commands

The command function
dashboard Real-time data panel for the current system
sc View information about classes loaded by the JVM
sm View method information for loaded classes
jad Decompile the source code that specifies the loaded class
thread View thread stack information for the current JVM
watch Method to perform data observations
trace Method internally calls the path and prints the time spent on each node on the method path
stack Prints the call path to which the current method is called
tt A space-time tunnel of method execution data, recording the input and return information of each call to the specified method, and observing these different time calls
monitor Method Execution monitoring
classloader View the classloader inheritance tree, urls, class loading information
heapdump The heap dump function is similar to the jmap command

4.1. Dashboard

Definition:

  • Real-time data panel for current system;
  • View thread, VIRTUAL machine, runtime status information;

Command: dashboard -i 5000-n 1

Usage Scenarios:Application of the overall problem troubleshooting, abnormal thread check;

4.2. Sc

Definition:

  • View the loaded classes in the JVM.

  • View class information, source, inheritance, class loader;

Command: sc -d ClassName

Usage Scenarios:

Class loading exceptions such as ClassNotFoundException, ClassCastException, ClassNoDefException…

(During the development process, you often need to rely on third-party packages, and different package versions may cause related class exceptions. You can run this command to directly locate the source of the target class.)

4.3. Sm

Definition:

  • View method information of loaded classes.

  • Specify all method names under the class, input parameter types;

Command: sm -d ClassName method

Usage scenario: : Method related exception NoSuchMethodException;

(Doesn’t feel very practical, and generics don’t support it)

4.4. Jad

Decompiler specifies the loaded class source code;

Command: jad -source-only ClassName

Usage Scenarios:

  • Specifies whether code changes are committed.

  • View the source code;

(You can directly check the metaphysical problems that have not taken effect after the code is packaged online)

4.5. The thread

Meaning:

Thread-n: queries the top N threads with the highest CPU usage

Thread ID: Views the specified thread stack

Thread-b: displays the threads that are currently blocking other threads

Application scenario: Slow application request response, lock waiting, abnormal thread locating

(Let me see which student poisoned the code.)

4.6. Watch

Meaning: methods perform data observation;

Command: watch ClassName method ‘{params[0],returnObj}’ -x n

Usage scenarios: determine online call input and output parameters, observe method call;

(Let me see what parameter you threw at me??)

4.7. The trace

The method calls the path internally and outputs the time spent on each node on the method path.

Run the trace ClassName method command

Usage Scenarios:

  • Determine the interface time consuming node;

  • View the actual invocation path of method execution;

(The time-consuming node is grasped, steady!)

4.8. The stack

Meaning: Output the call path of the current method being called; A lot of times we know that a method is being executed, but there are a lot of paths that the method is being executed on, or you don’t know where it’s being executed from, and you need stack;

Stack ClassName method -n 5 ‘1==1’

Usage Scenarios:

View the actual call path of method execution, the source code does not understand the code execution path, directly on this;

4.9. Tt

Meaning: Records the input and return information of each call to the specified method and can observe these different time calls;

Command:

tt -t ClassName method

tt -i index

Usage Scenarios:

  • View method call records;

  • View exception methods, stack;

Figure 4.10. Profilers flame

Description: Supports the generation of flame charts for application hotspots. Essentially by sampling, and then collecting the sample results to generate a flame map;

Command: start –event CPU –interval 10000000

Usage Scenarios:

Check application performance problems by flame diagram;

4.11. Ognl +Arthas (Getting any Spring Beans)

Use the tt command to get the Spring context

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

Tt - 1000 - w 'target. I getApplicationContext () getBean (" activityAdapterServiceImpl "). FindByUid (" 111 ")

ognl '#context=@com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory@contexts.iterator.next, # context) getBean (" activityServiceImpl "). FindByUid (" 111 ") '

5. Ognl expression support

Arthas integrates OGNL expression support, making it easy to combine online debugging, observation expression writing, and more.

Definition: Object-graph Navigation Language

Supports static method calls and value access of classes:

@[class name]@[method name | value name]

exp:

Ognl '@ [email protected] (" hello"

Ognl '@ com. Seewo. SeewoResource @ APP_CODE'

Ognl - 7 f9a81e8 '@ org. C springframework. Boot. SpringApplication @ logger'

Summary: OGNL has a concept of OgnlContext, a Map structure at the bottom

OGNL, and practice language developer.ibm.com/zh/articles…

6.Arthas idea plugin

Can’t remember orders? Idea plugin go!

Objective: To simplify command construction and convenient use;

Usage scenario: Output convenient debugging commands in the fastest way to handle online problems.

When using Arthas, right click where you want to debug, select command, copy, go to Arthas console, paste enter and execute.

The installation address: plugins.jetbrains.com/plugin/1358…

Instructions: www.yuque.com/docs/share/…

7. Command summary

8. The last

  • 1. The troubleshooting of problems in the development process should be well grounded;

  • 2. Make assumptions based on our experience and knowledge system, and use tools to help us verify and solve problems;

  • 3. More thinking, learning and practice to improve r&d efficiency;