The problem

The company took on a secondary development and maintenance of Spring Cloud project, output exception log and print exception.msg(), no output E.p rintStackTrace (), for example, throws a NullPointException, it outputs a Java lang. NullPointerException, specific which line error, no output. Because the code is online, unlike the test environment, it can be updated at will. That’s where Arthas comes in.

Arthas source

Arthas is an open source Java diagnostic tool for Alibaba that developers love. Arthas is an open source, online Java diagnostics tool that uses command-line interaction to support web-based diagnostics and tab-complete functionality to further locate and diagnose problems. Thanks to Arthas’s powerful and rich functionality, Arthas can do things beyond imagination.

What problems arthas can solve

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?

How is Arthas installed

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

curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar
Copy the code

Arthas common command

dashboard

In the real-time data panel of the current system, press CTRL + C to exit.

When running ali-Tomcat, real-time information about the current Tomcat is displayed, such as QPS for HTTP requests, RT, number of errors, thread pool information, and so on.

thread

View the current thread information, view the thread stack

Support one-click display of the current top N busiest threads and print stack:

$ thread -n 3
Copy the code

Thread-b, which finds the thread currently blocking another thread

$ thread -b

Copy the code

Note that currently support only to find out the synchronized keyword blocked threads, if is a Java util. Concurrent. The Lock, it is not support.

watch

Allows you to easily observe the invocation of the specified method. The range that can be observed is: return value, throw exception, input parameter, by writing OGNL expression to view the corresponding variable.

Watch has many parameters, mainly because it can observe objects in four different scenes

The parameter name Parameters that
class-pattern Class name expression matches
method-pattern Method name expression matches
express Observation expression
condition-express Conditional expression
[b] inBefore method callTo observe the
[e] inAfter method exceptionTo observe the
[s] inAfter the method returnsTo observe the
[f] inAfter the method is done(Normal return and abnormal return) observation
[E] Enable regular expression matching. The default value is wildcard matching
[x:] Specifies the depth of property traversal for the output, which defaults to 1

Observe the method parameters and return values

$ $ watch com.slf.arthas.controller.DemoController login '{params,returnObj,throwExp}' -n 5 -x 3
Copy the code

Observe examples of abnormal information

$ watch demo.MathGame primeFactors "{params[0],throwExp}" -e -x 2
Copy the code

jad

Decompile the source code that specifies the loaded class

The JAD command decompiles the byte code of the class actually running in the JVM into Java code so you can understand the business logic.

  • On Arthas Console, decomcompiled source code is syntactically highlighted to make it easier to read
  • Of course, decompilated Java code may have syntax errors, but that doesn’t affect your reading comprehension
The parameter name Parameters that
class-pattern Class name expression matches
[c:] Hashcode of the ClassLoader to which the class belongs
[classLoaderClass:] Specifies the class name of the ClassLoader that executes the expression
[E] Enable regular expression matching. The default value is wildcard matching

Compile the Java. Lang. String

 jad java.lang.String
Copy the code

Decompile the specified function

jad demo.MathGame main
Copy the code

Arthas: online tutorial arthas.aliyun.com/doc/arthas-…

Tutorials on the left and commands on the right

Give it a try.

Problems encountered

No PID found, JPS not installed

Centos is installed with open-JDK, JPS command is not installed

X- openJDK-devel (JPS) is a package that provides the JPS tool.

The query

yum list | grep jdk-devel
Copy the code

The installation

Yum install Java -- 1.8.0 comes with its - devel. X86_64Copy the code

instrument library is missing in target VM

Github.com/alibaba/art…

reference

Arthas’s official website