Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The function and usage of Arthas in Java

2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the role and use of Arthas in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1 introduction

Arthas is an open source Java diagnostic tool of Alibaba, which is deeply loved by developers.

Arthas can help you solve problems like the following when you are at a loss as to what to do:

From which jar package was this class loaded? Why are all kinds of related Exception reported?

Why didn't the code I changed be executed? Is it because I don't have commit? Is the branch wrong?

If you can't debug online if you encounter a problem, can you only republish it by adding a log?

Online encounter a user's data processing problems, but online also can not debug, offline can not be reproduced!

Is there a global perspective to view the health of the system?

Is there any way to monitor the real-time running status of JVM?

Arthas supports JDK 6 cycles and Linux/Mac/Winodws, adopts command line interaction mode, and provides rich automatic completion functions of Tab, which further facilitates problem location and diagnosis.

2 main functions

The functions provided by Arthas can be divided into the following three aspects:

(1) Information monitoring

Basic information of process operation: memory, CPU usage, thread information, thread stack, thread count, environment variable information

Object information: static properties of class objects, attribute information of Mbean, loaded class information, class loader, class method information

(2) method call

The method calls the input parameter and returns the value to view

Statistics on the call path, call time, number of method calls, number of success, number of failures, etc.

Record and redo method calls

(3) Class file processing

Dump bytecode of loaded classes, bytecode decompilation, class compilation, class reloading

3 install and use 3.1 installation

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

Wget https://alibaba.github.io/arthas/arthas-boot.jarjava-jar arthas-boot.jar

Then enter the corresponding number of the process and enter the command interface of Arthas to use:

Print help information:

Java-jar arthas-boot.jar-h3.2 use

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

(1) overall dashboard data

In the command line interface of arthas, enter the dashboard command to display the current tomcat multithreaded status, JVM regions, GC and other information in real time.

(2) View thread monitoring

Entering the thread command will display the status information of all threads. Entering thread-n 3 will show the current busiest 3 threads, which can be used to troubleshoot the thread CPU consumption input thread-b will show the thread currently in the BLOCKED state, and can troubleshoot the thread lock

(3) JVM monitoring

Enter the jvm command to view the detailed performance data of jvm

(4) observation method parameters and return value

Sometimes when troubleshooting problems, we need to check the parameters, return values, and usually need to add log printing, which is quite tedious. Based on the watch command, we can easily do all this.

$watch demo.MathGame primeFactors "{params,returnObj}"-x 2Press Ctrl+C to abort.Affect (class-cnt:1, method-cnt:1) cost in 44 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],],] (5) observe the path of the method call, details of the time spent

Sometimes you will encounter a service stutter. If you want to find out which step takes a long time, you usually add a log. You can easily solve this problem by using the trace command:

$trace demo.MathGame runPress Ctrl+C to abort.Affect (class-cnt:1, method-cnt:1) cost in 42 ms.`-ts=2018-12-04 00 purl 44vet 44witch 17 th threadbare nameplate mainstant1 lead iso daemon false pretentious priority5 TCCL=sun.misc.Launcher$AppClassLoader@3d4eac69 `- [10.611029ms] demo.MathGame:run () +-[0.05638ms] java.util.Random:nextInt () +-[10.036885ms] demo.MathGame:primeFactors ()`-[0.170316ms] demo.MathGame:print () 4 implementation principle

The overall macro module call figure is as follows:

For the reason of the space, the following is a brief introduction to the two core principles involved:

(1) Information monitoring and class file processing

JMX (Java Management Extensions Java Management extension, a framework for embedding management functions for applications) provided by JDK, JMX manages a series of MBean objects, based on which Arthas implements memory, GC, class loading information, and JVM information monitoring.

(2) method call

After JDK5, java.lang.Instrument is introduced, and programmers modify the class code dynamically by modifying the bytecode of the method. Among the parameters in the methods of the proxy class, there is an Instrumentation inst instance. With this example, we can call various interfaces provided by Instrumentation. For example, call inst.getAllLoadedClasses () to get all the loaded classes. Add a new converter by calling inst.addTransformer (new SdlTransformer (), true). Call inst.retransformClasses (Class cls) to initiate a reconversion request to JVM

Arthas uses ASM to generate the bytecode of the enhanced class, and the enhanced functions include method call input parameters, return value view, method call statistics, method call record and redo, and then add and transform methods based on the Instrumentation interface provided by JDK.

5 actual combat cases

The official documentation of Arthas provides many user cases. Here are some interesting ones:

(1) troubleshoot the source of application strange logs

Some strange logs sometimes appear in the application of case details service. It is troublesome to locate the source of these logs by modifying the implementation code of StringBuilder to print out the call stack information of the log, compile and generate StringBuilder.clss, and then modify the actual bytecode of StringBuilder used in the application based on the redefine command provided by Arthas.

(2) troubleshoot the problem of SpringBoot application 401ax 404.

The visit to the case details page returns 401 Servlet 404. When you encounter this problem, you usually have a headache, especially in the online environment, through the trace command provided by Arthas, print out the complete request tree for page access, and locate which Servlet returns 404.

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

Through the trace command, the trace object is the source of the problem that javax.servlet.Filter locates which Filter intercepts the request and returns 401.

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

Case details sometimes in order to quickly verify fixes for online problems, or for quick testing, we need the hot update code Arthas to provide the following steps to solve the problem

Step 1 the jad command decompiles the code

Step 2 text editor modifies the code

Step 3 the sc command finds the ClassLoader of the class where the code is located

Step 4 the mc command specifies the ClassLoader compiled code

Step 5 redefine command hot update code

This is the end of the content of "the function and use of Arthas in Java". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report