Within Alibaba, there are a lot of home-grown tools for developers to use, and one of the tools that almost every Java developer has used is Arthas, a Java diagnostic tool that is incredibly lightening. The tool became open source in September 2018.
Making address: https://github.com/alibaba/arthas user documentation: https://alibaba.github.io/arthas/
Copy the code
In your daily development, do you encounter any of the following problems:
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 can diagnose all of these problems!! Isn’t it nice and powerful?
Arthas supports JDK 6+, command line interaction, and rich Tab completion functionality to further locate and diagnose problems.
1. Install arthas-boot
Download arthas-boot.jar and start it with java-jar:
wget https://alibaba.github.io/arthas/arthas-boot.jarjava -jar arthas-boot.jar
Copy the code
Print help information:
java -jar arthas-boot.jar -h
Copy the code
If the download speed is slow, you can use aliyun’s image:
java -jar arthas-boot.jar --repo-mirror aliyun --use-http
Copy the code
2. Use as.sh to install
Arthas supports one-click installation on Linux/Unix/Mac platforms. Please copy and paste the following contents into the command line and press Enter to execute:
curl -L https://alibaba.github.io/arthas/install.sh | sh
Copy the code
The above command will download the startup script file as.sh to the current directory, which you can place anywhere or add to $PATH.
Run./as.sh directly under the shell to enter the interface.
You can also run the./as.sh -h command to obtain more parameter information.
1. Start the Demo
wget https://alibaba.github.io/arthas/arthas-demo.jarjava -jar arthas-demo.jar
Copy the code
Arthas-demo is a simple program that generates a random number every second, performs a factorization, and prints out the factorization.
2. Start arthas
Execute from the command line:
wget https://alibaba.github.io/arthas/arthas-boot.jarjava -jar arthas-boot.jar
Copy the code
-
The user executing the program needs to have the same permissions as the target process. For example, run the sudo su admin && Java -jar arthas-boot.jar command or sudo -u admin -eh Java -jar arthas-boot.jar command as the admin user.
-
If attatch is not on the target process, look at the logs under ~/logs/arthas/.
-
If the download is slow, you can use aliyun’s image: Java -jar arthas-boot.jar –repo-mirror aliyun –use-http
-
Java -jar arthas-boot.jar -h Displays more parameters.
Select apply Java process:
$ $ java -jar arthas-boot.jar* [1]: 35542 [2]: 71560 arthas-demo.jar
Copy the code
If the Demo process is the second one, enter 2 and enter/Enter. Arthas attaches to the target process and prints a log:
[INFO] Try to attach process 71560[INFO] attach process 71560 success.[INFO] arthas-client connect 127.0.0.1 3658,--. ,------. ,--------.,--. ,--. ,---. ,---. / O \ | .--. ''--. .--'| '--' | / O \ ' .-'| .-. || '--'.' | | | .--. || .-. |`. `-.| | | || |\ \ | | | | | || | | |.-' |`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'wiki: https://alibaba.github.io/arthasversion: 3.0.5.20181127201536 pid: 71560 time: 19:16:24 $2018-11-28
Copy the code
3. Check the dashboard
Input Dashboard and press Enter/Enter to display the current process information. Press CTRL + C to interrupt the execution.
$ dashboardID NAME GROUP PRIORI STATE %CPU TIME INTERRU DAEMON17 pool-2-thread-1 system 5 WAITIN 67 0:0 false false27 Timer-for-arthas-dashb system 10 RUNNAB 32 0:0 false true11 AsyncAppender-Worker-a system 9 WAITIN 0 0:0 false true9 Attach Listener system 9 RUNNAB 0 0:0 false true3 Finalizer system 8 WAITIN 0 0:0 false true2 Reference Handler system 10 WAITIN 0 0:0 false true4 Signal Dispatcher system 9 RUNNAB 0 0:0 false true26 as-command-execute-dae system 10 TIMED_ 0 0:0 false true13 job-timeout system 9 TIMED_ 0 0:0 false true1 main main 5 TIMED_ 0 0:0 false false14 nioEventLoopGroup-2-1 system 10 RUNNAB 0 0:0 false false18 nioEventLoopGroup-2-2 system 10 RUNNAB 0 0:0 false false23 nioEventLoopGroup-2-3 system 10 RUNNAB 0 0:0 false false15 nioEventLoopGroup-3-1 system 10 RUNNAB 0 0:0 false FalseMemory used total Max usage GCheap 32M 155M 1820M 1.77% gc.ps_scavenge. Count 4ps_eden_space 14M 65M 672M 2.21% Gc.ps_scavenge. Time (M 166PS_survivor_space 4M 5M 5M s)ps_old_gen 12M 85M 1365M 0.91% GC.ps_marksweep. Count 0nonheap 20M 23M-1 GC.ps_marksweep. Time (0code_cache 3M 5M 240M 1.32% ms)Runtimeos.name Mac OS xos. version 10.13.4 Java.version Home /Library/Java/JavaVir tualMachines/ jdk1.8.0_162. JDK /Contents/Hom e/jre
Copy the code
4. Run the sysenv command to obtain the Main Class of the process
$ sysenv | grep MAIN JAVA_MAIN_CLASS_71560 demo.MathGame
Copy the code
5. Use jad to reverse edit Main Class
$ jad demo.MathGameClassLoader:+-sun.misc.Launcher$AppClassLoader@3d4eac69 +-sun.misc.Launcher$ExtClassLoader@66350f69Location:/tmp/arthas-demo.jar/* * Decompiled with CFR 0_132. */package demo; import java.io.PrintStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Random; import java.util.concurrent.TimeUnit; public class MathGame { private static Random random = new Random(); private int illegalArgumentCount = 0; public static void main(String[] args) throws InterruptedException { MathGame game = new MathGame(); do { game.run(); TimeUnit.SECONDS.sleep(1L); } while (true); } public void run() throws InterruptedException { try { int number = random.nextInt(); List<Integer> primeFactors = this.primeFactors(number); MathGame.print(number, primeFactors); } catch (Exception e) { System.out.println(String.format("illegalArgumentCount:%3d, ", this.illegalArgumentCount) + e.getMessage()); } } public static void print(int number, List<Integer> primeFactors) { StringBuffer sb = new StringBuffer("" + number + "="); Iterator<Integer> iterator = primeFactors.iterator(); while (iterator.hasNext()) { int factor = iterator.next(); sb.append(factor).append('*'); } if (sb.charAt(sb.length() - 1) == '*') { sb.deleteCharAt(sb.length() - 1); } System.out.println(sb); } public List<Integer> primeFactors(int number) { if (number < 2) { ++this.illegalArgumentCount; throw new IllegalArgumentException("number is: " + number + ", need >= 2"); } ArrayList<Integer> result = new ArrayList<Integer>(); int i = 2; while (i <= number) { if (number % i == 0) { result.add(i); number /= i; i = 2; continue; } ++i; } return result; }}Affect(row-cnt:1) cost in 970 ms.
Copy the code
6. watch
Use the watch command to view the return value of demo.MathGame#primeFactors:
$ watch demo.MathGame primeFactors returnObjPress Ctrl+C to abort.Affect(class-cnt:1 , method-cnt:1) cost in 107 ms.ts=2018-11-28 19:22:30; [cost] = 1.715367 ms result = 19:22:31 nullts = 2018-11-28; [cost] = 0.185203 ms result = 19:22:32 nullts = 2018-11-28; [cost=19.012416ms] result= @arrayList [@INTEGER [5], @INTEGER [47], @integer [2675531],]ts=2018-11-28 19:22:33; [cost=0.311395ms] result= @arrayList [@INTEGER [2], @INTEGER [5], @integer [317], @integer [503], @Integer[887],]ts=2018-11-28 19:22:34; [cost=10.136007ms] result= @arrayList [@INTEGER [2], @INTEGER [2], @integer [3], @integer [3], @integer [31], @Integer[717593],]ts=2018-11-28 19:22:35; [cost=29.969732ms] result= @arrayList [@INTEGER [5], @INTEGER [29], @integer [7651739],]
Copy the code
5. Quit arthas
To exit the current connection, use the quit or exit command. Arthas attached to the target process will continue to run and the port will remain open for direct connection next time.
If you want to exit Arthas completely, you can execute the shutdown command.
Basic commands
-
Help — View the command help information
-
CLS — Clears the current screen area
-
Session — View information about the current session
-
Reset — Resets enhanced classes. This will restore all Arthas enhanced classes and reset all enhanced classes when Arthas server is closed
-
Version – Prints the Arthas version number loaded by the current target Java process
-
Quit — Exit the current Arthas client, leaving other Arthas clients unaffected
-
Shutdown — shutdown the Arthas server and exit all Arthas clients
-
Keymap — Arthas list of shortcuts and custom shortcuts
The JVM related
-
Dashboard – Real-time data panel for the current system
-
Thread – Views the thread stack information for the current JVM
-
JVM – View information about the current JVM
-
Sysprop – View and modify system properties of the JVM
-
New! Getstatic — View the static properties of the class
The class/this correlation
-
Sc – View information about classes loaded by the JVM
-
Sm — View method information for loaded classes
-
Dump — Dumps loaded class byte code to a specific directory
-
Re-define — Load an external. Class file, re-define it into the JVM
-
Jad – Decompiles the source code of a loaded class
-
Classloader – view the classloader inheritance tree, urls, class loading information, and use classloader to getResource
Monitor/watch/trace
-
Monitor — method performs monitoring
-
Watch – method performs data observations
-
Trace — calls to the method internally and prints the time spent on each node on the method path
-
Stack – Outputs the call path where the current method is called
-
Tt – time tunnel of method execution data, recording input and return information for each call to the specified method, and observing these different time calls
Please note that these commands, through the bytecode enhancement technology to realize, will insert some aspects in the specified class methods to implement the data statistics and observation, so online, use pretest, please try to clear need classes, methods and conditions for observation, diagnosis end to perform shutdown or class that will enhance the reset command.
options
-
Options — View or set the Arthas global switch
The pipe
Arthas supports the use of pipes for further processing, the results of the above command such as sm org.. Apache log4j. Logger | grep
-
Grep — Searches for results that meet the criteria
-
Plaintext — Removes color from the result of a command
-
Wc — Count the output by row
So that’s Arthas in a nutshell, but to really understand Arthas as a problem solving tool, you need to get your hands dirty. Practice is the mother of knowledge.
Hollis public account has authorized “Knight for Rights protection” to carry out original rights protection, in order to avoid unnecessary copyright liability issues, please indicate the source of reprint!
In the last month of 2018, Hollis knowledge Planet is on sale for a limited time. In-depth understanding of Concurrent programming in Java: What is Thread-safe, anyway? Welcome to join us.
What are LocalDate and localTime in Java8?
Learn more about enumerations in Java.
– MORE | – MORE excellent articles
-
List of Java troubleshooting tools for Ali employees
-
Contribution to Dubbo Ecology, Ali Open Source Nacos project
-
The layman can understand SpringCloud, missed the blood loss!
-
Why does Ali forbid engineers to directly use the logging system API
If you enjoyed this article.
Please long press the QR code to follow Hollis
Forwarding moments is the biggest support for me.